Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more complex sourcemap renames #1481

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/common/sourceMaps/renameProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*--------------------------------------------------------*/

import { inject, injectable } from 'inversify';
import { MappingItem } from 'source-map';
import { ISourceWithMap, Source, SourceFromMap } from '../../adapter/sources';
import { StackFrame } from '../../adapter/stackTrace';
import { AnyLaunchConfiguration } from '../../configuration';
Expand All @@ -17,9 +18,6 @@ interface IRename {
position: Base01Position;
}

/** Very approximate regex for JS identifiers */
const identifierRe = /[$a-z_][$0-9A-Z_$]*/iy;

export interface IRenameProvider {
/**
* Provides renames at the given stackframe.
Expand Down Expand Up @@ -104,23 +102,36 @@ export class RenameProvider implements IRenameProvider {
const renames: IRename[] = [];

// todo: may eventually want to be away
let pendingName: MappingItem | undefined;
sourceMap.eachMapping(mapping => {
if (!mapping.name) {
return;
if (pendingName) {
const startPos = new Base01Position(pendingName.generatedLine, pendingName.generatedColumn);
const start = toOffset.convert(startPos);
const end = toOffset.convert(
new Base01Position(mapping.generatedLine, mapping.generatedColumn),
);
renames.push({
compiled: content.slice(start, end),
original: pendingName.name,
position: startPos,
});
pendingName = undefined;
}

// convert to base 0 columns
const position = new Base01Position(mapping.generatedLine, mapping.generatedColumn);
const start = toOffset.convert(position);
identifierRe.lastIndex = start;
const match = identifierRe.exec(content);
if (!match) {
return;
if (mapping.name) {
pendingName = mapping;
}

renames.push({ compiled: match[0], original: mapping.name, position });
});

if (pendingName) {
const position = new Base01Position(pendingName.generatedLine, pendingName.generatedColumn);
renames.push({
compiled: content.slice(toOffset.convert(position)),
original: pendingName.name,
position,
});
}

renames.sort((a, b) => a.position.compare(b.position));

return new RenameMapping(renames);
Expand Down