Skip to content

Commit

Permalink
[Fix] avoid using reserved param name in babel 7 templates
Browse files Browse the repository at this point in the history
 - Test both Babel 6 and Babel 7
 - Add an OVERWRITE=true flag to automatically regenerate outputs
 - Use external-helpers instead of manually removing the helper with a regex
  • Loading branch information
nicolo-ribaudo authored and ljharb committed Apr 20, 2020
1 parent 391bb07 commit d3b0ba2
Show file tree
Hide file tree
Showing 59 changed files with 311 additions and 185 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Unreleased
- [Fix] avoid using reserved param name in babel 7 templates

## v2.3.1
- [Fix] Evaluate `import()` argument synchronously (#85)
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@
},
"homepage": "https://github.com/airbnb/babel-plugin-dynamic-import-node#readme",
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/plugin-external-helpers": "^7.8.3",
"@babel/plugin-transform-template-literals": "^7.8.3",
"@babel/preset-env": "^7.9.5",
"airbnb-js-shims": "^2.2.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^9.0.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-es2015-template-literals": "^6.22.0",
"babel-plugin-transform-replace-object-assign": "^1.0.0",
"babel-preset-airbnb": "^2.6.0",
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function getImportSource(t, callNode) {
}

export function createDynamicImportTransform({ template, types: t }) {
const buildImport = template('Promise.resolve(SOURCE).then(_ => INTEROP(require(_)))');
const buildImportNoInterop = template('Promise.resolve(SOURCE).then(_ => require(_))');
const buildImport = template('Promise.resolve(SOURCE).then(s => INTEROP(require(s)))');
const buildImportNoInterop = template('Promise.resolve(SOURCE).then(s => require(s))');

return (context, path) => {
const SOURCE = getImportSource(t, path.parent);
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic-import/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var testModule = Promise.resolve('test-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
1 change: 1 addition & 0 deletions test/fixtures/basic-import/expected.6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const testModule = Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
1 change: 1 addition & 0 deletions test/fixtures/basic-import/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const testModule = Promise.resolve('test-module').then(s => require(s));
3 changes: 3 additions & 0 deletions test/fixtures/basic-import/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var testModule = Promise.resolve('test-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
1 change: 1 addition & 0 deletions test/fixtures/basic-import/expected.7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const testModule = Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
1 change: 1 addition & 0 deletions test/fixtures/basic-import/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const testModule = Promise.resolve('test-module').then(s => require(s));
7 changes: 0 additions & 7 deletions test/fixtures/basic-import/expected.es2015.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/basic-import/expected.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/basic-import/expected.noInterop.js

This file was deleted.

15 changes: 15 additions & 0 deletions test/fixtures/chained-import/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Promise.resolve('test-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
}).then(function () {
return Promise.resolve('test-module-2').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
});

Promise.all([Promise.resolve('test-1').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
}), Promise.resolve('test-2').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
}), Promise.resolve('test-3').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
})]).then(function () {});
3 changes: 3 additions & 0 deletions test/fixtures/chained-import/expected.6.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/chained-import/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Promise.resolve('test-module').then(s => require(s)).then(() => Promise.resolve('test-module-2').then(s => require(s)));

Promise.all([Promise.resolve('test-1').then(s => require(s)), Promise.resolve('test-2').then(s => require(s)), Promise.resolve('test-3').then(s => require(s))]).then(() => {});
14 changes: 14 additions & 0 deletions test/fixtures/chained-import/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Promise.resolve('test-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
}).then(function () {
return Promise.resolve('test-module-2').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
});
Promise.all([Promise.resolve('test-1').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
}), Promise.resolve('test-2').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
}), Promise.resolve('test-3').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
})]).then(function () {});
2 changes: 2 additions & 0 deletions test/fixtures/chained-import/expected.7.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/fixtures/chained-import/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Promise.resolve('test-module').then(s => require(s)).then(() => Promise.resolve('test-module-2').then(s => require(s)));
Promise.all([Promise.resolve('test-1').then(s => require(s)), Promise.resolve('test-2').then(s => require(s)), Promise.resolve('test-3').then(s => require(s))]).then(() => {});
19 changes: 0 additions & 19 deletions test/fixtures/chained-import/expected.es2015.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/fixtures/chained-import/expected.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/chained-import/expected.noInterop.js

This file was deleted.

8 changes: 8 additions & 0 deletions test/fixtures/dynamic-argument/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var MODULE = Object('test-module');

Promise.resolve('' + String(MODULE)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('test-' + String(MODULE)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
4 changes: 4 additions & 0 deletions test/fixtures/dynamic-argument/expected.6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const MODULE = Object('test-module');

Promise.resolve(`${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`test-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
4 changes: 4 additions & 0 deletions test/fixtures/dynamic-argument/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const MODULE = Object('test-module');

Promise.resolve(`${MODULE}`).then(s => require(s));
Promise.resolve(`test-${MODULE}`).then(s => require(s));
7 changes: 7 additions & 0 deletions test/fixtures/dynamic-argument/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var MODULE = Object('test-module');
Promise.resolve("".concat(MODULE)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("test-".concat(MODULE)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
3 changes: 3 additions & 0 deletions test/fixtures/dynamic-argument/expected.7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const MODULE = Object('test-module');
Promise.resolve(`${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`test-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
3 changes: 3 additions & 0 deletions test/fixtures/dynamic-argument/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const MODULE = Object('test-module');
Promise.resolve(`${MODULE}`).then(s => require(s));
Promise.resolve(`test-${MODULE}`).then(s => require(s));
12 changes: 0 additions & 12 deletions test/fixtures/dynamic-argument/expected.es2015.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/dynamic-argument/expected.js

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixtures/dynamic-argument/expected.noInterop.js

This file was deleted.

6 changes: 6 additions & 0 deletions test/fixtures/import-with-comment/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Promise.resolve('my-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('my-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
2 changes: 2 additions & 0 deletions test/fixtures/import-with-comment/expected.6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
2 changes: 2 additions & 0 deletions test/fixtures/import-with-comment/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Promise.resolve('my-module').then(s => require(s));
Promise.resolve('my-module').then(s => require(s));
6 changes: 6 additions & 0 deletions test/fixtures/import-with-comment/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Promise.resolve('my-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('my-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
2 changes: 2 additions & 0 deletions test/fixtures/import-with-comment/expected.7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
2 changes: 2 additions & 0 deletions test/fixtures/import-with-comment/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Promise.resolve('my-module').then(s => require(s));
Promise.resolve('my-module').then(s => require(s));
10 changes: 0 additions & 10 deletions test/fixtures/import-with-comment/expected.es2015.js

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixtures/import-with-comment/expected.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/import-with-comment/expected.noInterop.js

This file was deleted.

7 changes: 7 additions & 0 deletions test/fixtures/nested-import/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function getModule(path) {
return Promise.resolve('test-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
}

getModule().then(function () {});
5 changes: 5 additions & 0 deletions test/fixtures/nested-import/expected.6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getModule(path) {
return Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
}

getModule().then(() => {});
5 changes: 5 additions & 0 deletions test/fixtures/nested-import/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getModule(path) {
return Promise.resolve('test-module').then(s => require(s));
}

getModule().then(() => {});
7 changes: 7 additions & 0 deletions test/fixtures/nested-import/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function getModule(path) {
return Promise.resolve('test-module').then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
}

getModule().then(function () {});
5 changes: 5 additions & 0 deletions test/fixtures/nested-import/expected.7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getModule(path) {
return Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
}

getModule().then(() => {});
5 changes: 5 additions & 0 deletions test/fixtures/nested-import/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getModule(path) {
return Promise.resolve('test-module').then(s => require(s));
}

getModule().then(() => {});
11 changes: 0 additions & 11 deletions test/fixtures/nested-import/expected.es2015.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixtures/nested-import/expected.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/fixtures/nested-import/expected.noInterop.js

This file was deleted.

24 changes: 24 additions & 0 deletions test/fixtures/non-string-argument/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Promise.resolve('' + String({ 'answer': 42 })).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + String(['foo', 'bar'])).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + 42).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + String(void 0)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + String(undefined)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + String(null)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + String(true)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve('' + String(Symbol())).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
8 changes: 8 additions & 0 deletions test/fixtures/non-string-argument/expected.6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Promise.resolve(`${{ 'answer': 42 }}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${['foo', 'bar']}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${42}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${void 0}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${undefined}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${null}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${true}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${Symbol()}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
8 changes: 8 additions & 0 deletions test/fixtures/non-string-argument/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Promise.resolve(`${{ 'answer': 42 }}`).then(s => require(s));
Promise.resolve(`${['foo', 'bar']}`).then(s => require(s));
Promise.resolve(`${42}`).then(s => require(s));
Promise.resolve(`${void 0}`).then(s => require(s));
Promise.resolve(`${undefined}`).then(s => require(s));
Promise.resolve(`${null}`).then(s => require(s));
Promise.resolve(`${true}`).then(s => require(s));
Promise.resolve(`${Symbol()}`).then(s => require(s));
26 changes: 26 additions & 0 deletions test/fixtures/non-string-argument/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Promise.resolve("".concat({
'answer': 42
})).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(['foo', 'bar'])).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(42)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(void 0)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(undefined)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(null)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(true)).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
Promise.resolve("".concat(Symbol())).then(function (s) {
return babelHelpers.interopRequireWildcard(require(s));
});
10 changes: 10 additions & 0 deletions test/fixtures/non-string-argument/expected.7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Promise.resolve(`${{
'answer': 42
}}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${['foo', 'bar']}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${42}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${void 0}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${undefined}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${null}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${true}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${Symbol()}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Loading

0 comments on commit d3b0ba2

Please sign in to comment.