optimise es6.constants transformer to use the same traversal path instead of spawning another one
This commit is contained in:
parent
080b26769c
commit
a75af0a5d2
@ -1,44 +1,31 @@
|
||||
import * as messages from "../../../messages";
|
||||
import * as t from "../../../types";
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (this.isAssignmentExpression() || this.isUpdateExpression()) {
|
||||
var ids = this.getBindingIdentifiers();
|
||||
export function AssignmentExpression(node, parent, scope, file) {
|
||||
var ids = this.getBindingIdentifiers();
|
||||
|
||||
for (var name in ids) {
|
||||
var id = ids[name];
|
||||
for (var name in ids) {
|
||||
var id = ids[name];
|
||||
|
||||
var constant = state.constants[name];
|
||||
var binding = scope.getBinding(name);
|
||||
|
||||
// no constant exists
|
||||
if (!constant) continue;
|
||||
// no binding exists
|
||||
if (!binding) continue;
|
||||
|
||||
var constantIdentifier = constant.identifier;
|
||||
// not a constant
|
||||
if (binding.kind !== "const" && binding.kind !== "module") continue;
|
||||
|
||||
// check if the assignment id matches the constant declaration id
|
||||
// if it does then it was the id used to initially declare the
|
||||
// constant so we can just ignore it
|
||||
if (id === constantIdentifier) continue;
|
||||
// check if the assignment id matches the constant declaration id
|
||||
// if it does then it was the id used to initially declare the
|
||||
// constant so we can just ignore it
|
||||
if (binding.identifier === id) continue;
|
||||
|
||||
// check if there's been a local binding that shadows this constant
|
||||
if (!scope.bindingIdentifierEquals(name, constantIdentifier)) continue;
|
||||
|
||||
throw state.file.errorWithNode(id, messages.get("readOnly", name));
|
||||
}
|
||||
} else if (this.isScope()) {
|
||||
this.skip();
|
||||
}
|
||||
throw file.errorWithNode(id, messages.get("readOnly", name));
|
||||
}
|
||||
};
|
||||
|
||||
export function Scopable(node, parent, scope, file) {
|
||||
this.traverse(visitor, {
|
||||
constants: scope.getAllBindingsOfKind("const", "module"),
|
||||
file: file
|
||||
});
|
||||
}
|
||||
|
||||
export { AssignmentExpression as UpdateExpression };
|
||||
|
||||
export function VariableDeclaration(node) {
|
||||
if (node.kind === "const") node.kind = "let";
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user