check most local scopes first

pull/4960/head
Billy Levin 6 years ago committed by tanhauhau
parent d0619dcd84
commit b70b5dfdde

@ -789,7 +789,7 @@ export default class Component {
} }
let deep = false; let deep = false;
let names: string[] | null; let names: string[] | undefined;
if (node.type === 'AssignmentExpression') { if (node.type === 'AssignmentExpression') {
deep = node.left.type === 'MemberExpression'; deep = node.left.type === 'MemberExpression';
@ -804,8 +804,18 @@ export default class Component {
if (names) { if (names) {
names.forEach(name => { names.forEach(name => {
const variable = component.var_lookup.get(name); let current_scope = scope;
if (variable && variable.writable === false && !deep) { let declaration;
while (current_scope) {
if (current_scope.declarations.has(name)) {
declaration = current_scope.declarations.get(name);
break;
}
current_scope = current_scope.parent;
}
if (declaration && declaration.kind === 'const' && !deep) {
component.error(node as any, { component.error(node as any, {
code: 'assignment-to-const', code: 'assignment-to-const',
message: 'You are assigning to a const' message: 'You are assigning to a const'

@ -151,13 +151,29 @@ export default class Expression {
if (variable) { if (variable) {
variable[deep ? 'mutated' : 'reassigned'] = true; variable[deep ? 'mutated' : 'reassigned'] = true;
}
let current_scope = scope;
let declaration;
if (!deep && variable.writable === false) { while (current_scope) {
component.error(node as any, { if (current_scope.declarations.has(name)) {
code: 'assignment-to-const', declaration = current_scope.declarations.get(name);
message: 'You are assigning to a const' break;
});
} }
current_scope = current_scope.parent;
}
if (declaration && declaration.kind === 'const' && !deep) {
component.error(node as any, {
code: 'assignment-to-const',
message: 'You are assigning to a const'
});
} else if (variable && variable.writable === false && !deep) {
component.error(node as any, {
code: 'assignment-to-const',
message: 'You are assigning to a const'
});
} }
} }
}); });

Loading…
Cancel
Save