fix remaining AssignmentExpression LHS ReferencedIdentifier bugs
This commit is contained in:
parent
d5e7b5616c
commit
5aef7afade
@ -2,18 +2,29 @@ import getFunctionArity from "./get-function-arity";
|
|||||||
import * as util from "../../util";
|
import * as util from "../../util";
|
||||||
import * as t from "../../types";
|
import * as t from "../../types";
|
||||||
|
|
||||||
|
function visitIdentifier(context, node, scope, state) {
|
||||||
|
// check if this node matches our function id
|
||||||
|
if (node.name !== state.name) return;
|
||||||
|
|
||||||
|
// check that we don't have a local variable declared as that removes the need
|
||||||
|
// for the wrapper
|
||||||
|
var localDeclar = scope.getBindingIdentifier(state.name);
|
||||||
|
if (localDeclar !== state.outerDeclar) return;
|
||||||
|
|
||||||
|
state.selfReference = true;
|
||||||
|
context.stop();
|
||||||
|
}
|
||||||
|
|
||||||
var visitor = {
|
var visitor = {
|
||||||
ReferencedIdentifier(node, parent, scope, state) {
|
ReferencedIdentifier(node, parent, scope, state) {
|
||||||
// check if this node matches our function id
|
visitIdentifier(this, node, scope, state);
|
||||||
if (node.name !== state.name) return;
|
},
|
||||||
|
|
||||||
// check that we don't have a local variable declared as that removes the need
|
AssignmentExpression(node, parent, scope, state) {
|
||||||
// for the wrapper
|
var ids = this.getBindingIdentifiers();
|
||||||
var localDeclar = scope.getBindingIdentifier(state.name);
|
for (var name in ids) {
|
||||||
if (localDeclar !== state.outerDeclar) return;
|
visitIdentifier(this, ids[name], scope, state);
|
||||||
|
}
|
||||||
state.selfReference = true;
|
|
||||||
this.stop();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,28 +1,59 @@
|
|||||||
import * as t from "../../../types";
|
import * as t from "../../../types";
|
||||||
|
|
||||||
|
function buildAssert(node, file) {
|
||||||
|
return t.callExpression(
|
||||||
|
file.addHelper("temporal-assert-defined"),
|
||||||
|
[node, t.literal(node.name), file.addHelper("temporal-undefined")]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function references(node, scope, state) {
|
||||||
|
var declared = state.letRefs[node.name];
|
||||||
|
if (!declared) return false;
|
||||||
|
|
||||||
|
// declared node is different in this scope
|
||||||
|
return scope.getBindingIdentifier(node.name) === declared;
|
||||||
|
}
|
||||||
|
|
||||||
var visitor = {
|
var visitor = {
|
||||||
ReferencedIdentifier(node, parent, scope, state) {
|
ReferencedIdentifier(node, parent, scope, state) {
|
||||||
if (t.isFor(parent) && parent.left === node) return;
|
if (t.isFor(parent) && parent.left === node) return;
|
||||||
|
|
||||||
var declared = state.letRefs[node.name];
|
if (!references(node, scope, state)) return;
|
||||||
if (!declared) return;
|
|
||||||
|
|
||||||
// declared node is different in this scope
|
var assert = buildAssert(node, state.file);
|
||||||
if (scope.getBindingIdentifier(node.name) !== declared) return;
|
|
||||||
|
|
||||||
var assert = t.callExpression(
|
|
||||||
state.file.addHelper("temporal-assert-defined"),
|
|
||||||
[node, t.literal(node.name), state.file.addHelper("temporal-undefined")]
|
|
||||||
);
|
|
||||||
|
|
||||||
this.skip();
|
this.skip();
|
||||||
|
|
||||||
if (t.isAssignmentExpression(parent) || t.isUpdateExpression(parent)) {
|
if (t.isUpdateExpression(parent)) {
|
||||||
if (parent._ignoreBlockScopingTDZ) return;
|
if (parent._ignoreBlockScopingTDZ) return;
|
||||||
this.parentPath.replaceWith(t.sequenceExpression([assert, parent]));
|
this.parentPath.replaceWith(t.sequenceExpression([assert, parent]));
|
||||||
} else {
|
} else {
|
||||||
return t.logicalExpression("&&", assert, node);
|
return t.logicalExpression("&&", assert, node);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
AssignmentExpression: {
|
||||||
|
exit(node, parent, scope, state) {
|
||||||
|
if (node._ignoreBlockScopingTDZ) return;
|
||||||
|
|
||||||
|
var nodes = [];
|
||||||
|
var ids = this.getBindingIdentifiers();
|
||||||
|
|
||||||
|
for (var name in ids) {
|
||||||
|
var id = ids[name];
|
||||||
|
|
||||||
|
if (references(id, scope, state)) {
|
||||||
|
nodes.push(buildAssert(id, state.file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nodes.length) {
|
||||||
|
node._ignoreBlockScopingTDZ = true;
|
||||||
|
nodes.push(node);
|
||||||
|
return nodes.map(t.expressionStatement);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -119,6 +119,10 @@ export function updateSiblingKeys(fromIndex, incrementBy) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description
|
* Description
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -30,27 +30,33 @@ export function getBindingIdentifiers(node: Object): Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getBindingIdentifiers.keys = {
|
getBindingIdentifiers.keys = {
|
||||||
|
ComprehensionBlock: "left",
|
||||||
CatchClause: "param",
|
CatchClause: "param",
|
||||||
UnaryExpression: "argument",
|
UnaryExpression: "argument",
|
||||||
AssignmentExpression: "left",
|
AssignmentExpression: "left",
|
||||||
|
|
||||||
ImportSpecifier: "local",
|
ImportSpecifier: "local",
|
||||||
ImportNamespaceSpecifier: "local",
|
ImportNamespaceSpecifier: "local",
|
||||||
ImportDefaultSpecifier: "local",
|
ImportDefaultSpecifier: "local",
|
||||||
VariableDeclarator: "id",
|
ImportDeclaration: "specifiers",
|
||||||
|
|
||||||
FunctionDeclaration: "id",
|
FunctionDeclaration: "id",
|
||||||
FunctionExpression: "id",
|
FunctionExpression: "id",
|
||||||
|
|
||||||
ClassDeclaration: "id",
|
ClassDeclaration: "id",
|
||||||
ClassExpression: "id",
|
ClassExpression: "id",
|
||||||
|
|
||||||
SpreadElement: "argument",
|
SpreadElement: "argument",
|
||||||
RestElement: "argument",
|
RestElement: "argument",
|
||||||
UpdateExpression: "argument",
|
UpdateExpression: "argument",
|
||||||
|
|
||||||
SpreadProperty: "argument",
|
SpreadProperty: "argument",
|
||||||
Property: "value",
|
Property: "value",
|
||||||
ComprehensionBlock: "left",
|
|
||||||
AssignmentPattern: "left",
|
AssignmentPattern: "left",
|
||||||
ComprehensionExpression: "blocks",
|
|
||||||
ImportDeclaration: "specifiers",
|
|
||||||
VariableDeclaration: "declarations",
|
|
||||||
ArrayPattern: "elements",
|
ArrayPattern: "elements",
|
||||||
ObjectPattern: "properties"
|
ObjectPattern: "properties",
|
||||||
|
|
||||||
|
VariableDeclaration: "declarations",
|
||||||
|
VariableDeclarator: "id"
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user