Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register binding for newly created vars for commonjs transforms (#14097)
- Loading branch information
1 parent
bee3e35
commit ed3036d
Showing
5 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...babel-plugin-transform-modules-commonjs/test/fixtures/misc/binding-for-new-vars/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {x} from './foo.js'; | ||
var y = true; | ||
function f() { | ||
return [x, y, console]; | ||
} |
4 changes: 4 additions & 0 deletions
4
...el-plugin-transform-modules-commonjs/test/fixtures/misc/binding-for-new-vars/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"sourceType": "module", | ||
"plugins": ["transform-modules-commonjs", "./plugin"] | ||
} |
15 changes: 15 additions & 0 deletions
15
...babel-plugin-transform-modules-commonjs/test/fixtures/misc/binding-for-new-vars/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
|
||
var _foo = require("./foo.js"); | ||
|
||
var y = true; | ||
|
||
function f() { | ||
return [ | ||
/* _foo hasBinding, getBinding */ | ||
_foo.x, | ||
/* y hasBinding, getBinding */ | ||
y, | ||
/* console no hasBinding, no getBinding */ | ||
console]; | ||
} |
29 changes: 29 additions & 0 deletions
29
...babel-plugin-transform-modules-commonjs/test/fixtures/misc/binding-for-new-vars/plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = function () { | ||
return { | ||
visitor: { | ||
Program: { | ||
exit(programPath) { | ||
// Sub-traversal to ensure runs after ESM->CJS transform | ||
programPath.traverse({ | ||
ReferencedIdentifier(path) { | ||
if (!path.findParent(p => p.isFunction())) return; | ||
const varName = path.node.name; | ||
path.addComment( | ||
"leading", | ||
` ${varName} ${ | ||
path.scope.hasBinding(varName) | ||
? "hasBinding" | ||
: "no hasBinding" | ||
}, ${ | ||
path.scope.getBinding(varName) | ||
? "getBinding" | ||
: "no getBinding" | ||
} ` | ||
); | ||
}, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |