Skip to content

Commit

Permalink
fix: Escape key of alias to support using $ prefix (#313)
Browse files Browse the repository at this point in the history
Closes #312 

(See #312 for detailed explanation)
  • Loading branch information
Telokis authored and fatfisz committed Aug 23, 2018
1 parent 4076423 commit 4c56684
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/normalizeOptions.js
Expand Up @@ -6,6 +6,7 @@ import findBabelConfig from 'find-babel-config';
import glob from 'glob';
import pkgUp from 'pkg-up';

import { escapeRegExp } from './utils';
import defaultResolvePath from './resolvePath';


Expand Down Expand Up @@ -79,7 +80,7 @@ function normalizeRoot(optsRoot, cwd) {
}

function getAliasTarget(key, isKeyRegExp) {
const regExpPattern = isKeyRegExp ? key : `^${key}(/.*|)$`;
const regExpPattern = isKeyRegExp ? key : `^${escapeRegExp(key)}(/.*|)$`;
return new RegExp(regExpPattern);
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils.js
Expand Up @@ -80,3 +80,7 @@ export function mapPathString(nodePath, state) {
export function isImportCall(types, calleePath) {
return types.isImport(calleePath.node.callee);
}

export function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
10 changes: 10 additions & 0 deletions test/index.test.js
Expand Up @@ -342,6 +342,7 @@ describe('module-resolver', () => {
'^regexp-priority': './hit',
'regexp-priority$': './miss',
'regexp-priority': './miss',
'$src': './test/testproject/src/'
},
}],
],
Expand Down Expand Up @@ -466,6 +467,15 @@ describe('module-resolver', () => {
);
});

it('should escape regexp', () => {
// See https://github.com/tleunen/babel-plugin-module-resolver/issues/312
testWithImport(
'$src/app',
'./test/testproject/src/app',
aliasTransformerOpts,
);
})

describe('with a regular expression', () => {
it('should support replacing parts of a path', () => {
testWithImport(
Expand Down

0 comments on commit 4c56684

Please sign in to comment.