From feefe9e05d85246cb18cbcf7c9956fbba563fb00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ruci=C5=84ski?= Date: Mon, 12 Jun 2017 02:09:25 +0200 Subject: [PATCH] fix: Prevent double application of the transform (#176) --- .gitignore | 2 +- src/utils.js | 5 ++ test/index.test.js | 56 +++++++++++++++++++ test/testproject/node_modules/first/index.js | 0 test/testproject/node_modules/second/index.js | 0 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/testproject/node_modules/first/index.js create mode 100644 test/testproject/node_modules/second/index.js diff --git a/.gitignore b/.gitignore index 3fec2da..79d76b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .DS_Store -node_modules/ +/node_modules/ npm-debug.log lib/ coverage/ diff --git a/src/utils.js b/src/utils.js index 8d5ff34..c039cf0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -50,7 +50,12 @@ export function mapPathString(nodePath, state) { const modulePath = getRealPath(nodePath.node.value, state); if (modulePath) { + if (nodePath.node.pathResolved) { + return; + } + nodePath.replaceWith(state.types.stringLiteral(modulePath)); + nodePath.node.pathResolved = true; } } diff --git a/test/index.test.js b/test/index.test.js index a0dc88f..9807b4e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -509,6 +509,62 @@ describe('module-resolver', () => { }); }); }); + + describe('multiple alias application', () => { + it('should resolve the cyclic alias only once', () => { + const fileName = path.resolve('test/testproject/src/app.js'); + const cycleAliasTransformerOpts = { + babelrc: false, + plugins: [ + [plugin, { + alias: { + first: 'second', + second: 'first', + }, + }], + [plugin, { + alias: { + first: 'second', + second: 'first', + }, + }], + ], + filename: fileName, + }; + + testWithImport( + 'first', + 'second', + cycleAliasTransformerOpts, + ); + }); + + it('should resolve the prefix alias only once', () => { + const fileName = path.resolve('test/testproject/src/app.js'); + const cycleAliasTransformerOpts = { + babelrc: false, + plugins: [ + [plugin, { + alias: { + prefix: 'prefix/lib', + }, + }], + [plugin, { + alias: { + prefix: 'prefix/lib', + }, + }], + ], + filename: fileName, + }; + + testWithImport( + 'prefix/test', + 'prefix/lib/test', + cycleAliasTransformerOpts, + ); + }); + }); }); describe('with custom cwd', () => { diff --git a/test/testproject/node_modules/first/index.js b/test/testproject/node_modules/first/index.js new file mode 100644 index 0000000..e69de29 diff --git a/test/testproject/node_modules/second/index.js b/test/testproject/node_modules/second/index.js new file mode 100644 index 0000000..e69de29