-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignmentExpression.ts
22 lines (20 loc) · 1.05 KB
/
assignmentExpression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { AST } from "../ast";
import { Context } from "../types";
import { Rule } from "./rule";
import { AssignmentExpression, Identifier, Node } from "estree"
import { DiagnosticSeverity } from "vscode-languageserver";
export const assignmentExpressionRule = new class extends Rule<AssignmentExpression> {
public process(child: AssignmentExpression, parent: Node, context: Context, ast: AST): void {
if (child.left.type === "Identifier" && !ast.isDummy(child.left)) {
// Declaration might not have been processed yet
ast.addDiagnosticCallback((node: Node) => {
const identifier = node as Identifier;
if (identifier.loc) {
const declaration = ast.findDeclarationByName(identifier.name, identifier.loc);
if (declaration !== undefined && (declaration.meta === "const" || declaration.meta === "func"))
ast.addDiagnostic(`Cannot assign new value to constant ${identifier.name}`, DiagnosticSeverity.Error, identifier.loc);
}
}, child.left)
}
}
}();