mirror of https://github.com/webpack/webpack.git
Merge pull request #6288 from ooflorent/fix_variable_hook
Fix VariableDeclaration visitors
This commit is contained in:
commit
416247102a
100
lib/Parser.js
100
lib/Parser.js
|
|
@ -1064,13 +1064,54 @@ class Parser extends Tapable {
|
|||
}
|
||||
|
||||
prewalkVariableDeclaration(statement) {
|
||||
if(statement.declarations)
|
||||
this.prewalkVariableDeclarators(statement.declarations);
|
||||
const hookMap = statement.kind === "const" ? this.hooks.varDeclarationConst :
|
||||
statement.kind === "let" ? this.hooks.varDeclarationLet :
|
||||
this.hooks.varDeclarationVar;
|
||||
for(const declarator of statement.declarations) {
|
||||
switch(declarator.type) {
|
||||
case "VariableDeclarator":
|
||||
{
|
||||
this.enterPattern(declarator.id, (name, decl) => {
|
||||
let hook = hookMap.get(name);
|
||||
if(hook === undefined || !hook.call(decl)) {
|
||||
hook = this.hooks.varDeclaration.get(name);
|
||||
if(hook === undefined || !hook.call(decl)) {
|
||||
this.scope.renames.set(name, null);
|
||||
this.scope.definitions.add(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walkVariableDeclaration(statement) {
|
||||
if(statement.declarations)
|
||||
this.walkVariableDeclarators(statement.declarations);
|
||||
for(const declarator of statement.declarations) {
|
||||
switch(declarator.type) {
|
||||
case "VariableDeclarator":
|
||||
{
|
||||
const renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
|
||||
if(renameIdentifier && declarator.id.type === "Identifier") {
|
||||
const hook = this.hooks.canRename.get(renameIdentifier);
|
||||
if(hook !== undefined && hook.call(declarator.init)) {
|
||||
// renaming with "var a = b;"
|
||||
const hook = this.hooks.rename.get(renameIdentifier);
|
||||
if(hook === undefined || !hook.call(declarator.init)) {
|
||||
this.scope.renames.set(declarator.id.name, this.scope.renames.get(renameIdentifier) || renameIdentifier);
|
||||
this.scope.definitions.delete(declarator.id.name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.walkPattern(declarator.id);
|
||||
if(declarator.init)
|
||||
this.walkExpression(declarator.init);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
prewalkClassDeclaration(statement) {
|
||||
|
|
@ -1109,57 +1150,6 @@ class Parser extends Tapable {
|
|||
});
|
||||
}
|
||||
|
||||
prewalkVariableDeclarators(declarators) {
|
||||
for(const declarator of declarators) {
|
||||
switch(declarator.type) {
|
||||
case "VariableDeclarator":
|
||||
{
|
||||
this.enterPattern(declarator.id, (name, decl) => {
|
||||
const hookMap = declarator.kind === "const" ? this.hooks.varDeclarationConst :
|
||||
declarator.kind === "let" ? this.hooks.varDeclarationLet :
|
||||
this.hooks.varDeclarationVar;
|
||||
const hook = hookMap.get(name);
|
||||
if(hook === undefined || !hook.call(decl)) {
|
||||
const hook = this.hooks.varDeclaration.get(name);
|
||||
if(hook === undefined || !hook.call(decl)) {
|
||||
this.scope.renames.set(name, null);
|
||||
this.scope.definitions.add(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walkVariableDeclarators(declarators) {
|
||||
for(const declarator of declarators) {
|
||||
switch(declarator.type) {
|
||||
case "VariableDeclarator":
|
||||
{
|
||||
const renameIdentifier = declarator.init && this.getRenameIdentifier(declarator.init);
|
||||
if(renameIdentifier && declarator.id.type === "Identifier") {
|
||||
const hook = this.hooks.canRename.get(renameIdentifier);
|
||||
if(hook !== undefined && hook.call(declarator.init)) {
|
||||
// renaming with "var a = b;"
|
||||
const hook = this.hooks.rename.get(renameIdentifier);
|
||||
if(hook === undefined || !hook.call(declarator.init)) {
|
||||
this.scope.renames.set(declarator.id.name, this.scope.renames.get(renameIdentifier) || renameIdentifier);
|
||||
this.scope.definitions.delete(declarator.id.name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.walkPattern(declarator.id);
|
||||
if(declarator.init)
|
||||
this.walkExpression(declarator.init);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walkPattern(pattern) {
|
||||
switch(pattern.type) {
|
||||
case "ArrayPattern":
|
||||
|
|
|
|||
Loading…
Reference in New Issue