Register binding for newly created vars for commonjs transforms (#14097)
This commit is contained in:
parent
bee3e35851
commit
ed3036d11c
@ -235,6 +235,12 @@ export default declare((api, options) => {
|
||||
|
||||
ensureStatementsHoisted(headers);
|
||||
path.unshiftContainer("body", headers);
|
||||
path.get("body").forEach(path => {
|
||||
if (headers.indexOf(path.node) === -1) return;
|
||||
if (path.isVariableDeclaration()) {
|
||||
path.scope.registerDeclaration(path);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
import {x} from './foo.js';
|
||||
var y = true;
|
||||
function f() {
|
||||
return [x, y, console];
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": ["transform-modules-commonjs", "./plugin"]
|
||||
}
|
||||
@ -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];
|
||||
}
|
||||
@ -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"
|
||||
} `
|
||||
);
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user