Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #261 from msafi/allow-some-regex-in-sourcemap-pattern
Browse files Browse the repository at this point in the history
Ignore some regex characters in sourcemap overrides pattern
  • Loading branch information
roblourens authored Dec 25, 2017
2 parents 039ec97 + 397eef2 commit 2e6b525
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/sourceMaps/sourceMapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getComputedSourceRoot(sourceRoot: string, generatedPath: string,
} else {
// generatedPath is a URL so runtime script is not on disk, resolve the sourceRoot location on disk
const genDirname = path.dirname(url.parse(generatedPath).pathname);
absSourceRoot = path.join(webRoot, genDirname, sourceRoot);
absSourceRoot = path.join(webRoot, genDirname, sourceRoot);
}
}

Expand All @@ -48,7 +48,7 @@ export function getComputedSourceRoot(sourceRoot: string, generatedPath: string,
// runtime script is not on disk, resolve the sourceRoot location on disk
const urlPath = url.parse(generatedPath).pathname;
const scriptPathDirname = urlPath ? path.dirname(urlPath) : ''; // could be debugadapter://123, no other info.
absSourceRoot = path.join(webRoot, scriptPathDirname);
absSourceRoot = path.join(webRoot, scriptPathDirname);
logger.log(`SourceMap: no sourceRoot specified, using webRoot + script path dirname: ${absSourceRoot}`);
}

Expand Down Expand Up @@ -87,7 +87,7 @@ export function applySourceMapPathOverrides(sourcePath: string, sourceMapPathOve
const patternSegment = pattern
.replace(/\*/g, '(.*)')
.replace(/\\/g, '/');
const patternRegex = new RegExp(`^${patternSegment}$`, 'i');
const patternRegex = new RegExp(`^${utils.escapeSomeRegExpCharacters(patternSegment)}$`, 'i');
const overridePatternMatches = forwardSlashSourcePath.match(patternRegex);
if (!overridePatternMatches)
continue;
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export function multiGlob(patterns: string[], opts?: any): Promise<string[]> {
}
});
});
})).then(results => {
})).then(results => {
const set = new Set<string>();
for (let paths of results) {
for (let p of paths) {
Expand Down Expand Up @@ -543,6 +543,10 @@ export function escapeRegExpCharacters(value: string): string {
return value.replace(/[\-\\\{\}\*\+\?\|\^\$\.\[\]\(\)\#]/g, '\\$&');
}

export function escapeSomeRegExpCharacters(value: string): string {
return value.replace(/[-[\]{}+?,^$|#\s]/g, '\\$&');
}

export function toVoidP(p: Promise<any>): Promise<void> {
return p.then(() => { });
}
8 changes: 7 additions & 1 deletion test/sourceMaps/sourceMapUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ suite('SourceMapUtils', () => {

test('works using the laptop emoji', () => {
assert.deepEqual(
applySourceMapPathOverrides('meteor:///💻app/src/main.js', { 'meteor:///💻app/*': testUtils.pathResolve('/project/*')}),
applySourceMapPathOverrides('meteor:///💻app/src/main.js', { 'meteor:///💻app/*': testUtils.pathResolve('/project/*') }),
testUtils.pathResolve('/project/src/main.js'));
});

Expand Down Expand Up @@ -128,6 +128,12 @@ suite('SourceMapUtils', () => {
testUtils.pathResolve('/project/src/app.js'));
});

test('allows some regex characters in the pattern', () => {
assert.deepEqual(
applySourceMapPathOverrides('webpack+foo:///src/app.js', { 'webpack+foo:///*/app.js': testUtils.pathResolve('/project/*/app.js') }),
testUtils.pathResolve('/project/src/app.js'));
});

test('replaces correctly when asterisk on left but not right', () => {
assert.deepEqual(
applySourceMapPathOverrides('/src/app.js', { '*/app.js': testUtils.pathResolve('/project/app.js') }),
Expand Down

0 comments on commit 2e6b525

Please sign in to comment.