-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentifier.ts
29 lines (27 loc) · 1.44 KB
/
identifier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { AST } from "../ast";
import { Rule } from "./rule";
import { Identifier, Node } from "estree"
import { DiagnosticSeverity } from "vscode-languageserver";
import { DECLARATIONS } from "../types";
import { isBuiltinConst, isBuiltinFunction, sourceLocEquals, rangeToSourceLoc } from "../utils";
import { Context } from "../types";
export const identifierRule = new class extends Rule<Identifier> {
public process(child: Identifier, parent: Node, context: Context, ast: AST): void {
if (parent.type !== DECLARATIONS.IMPORT) {
// Ensure that all declarations have been processed
ast.addDiagnosticCallback((node: Node) => {
const identifier = node as Identifier;
if (identifier.loc) {
const loc = identifier.loc;
const declaration = ast.findDeclarationByName(identifier.name, loc)
if (declaration === undefined && !(isBuiltinConst(identifier.name, context) || isBuiltinFunction(identifier.name, context) || ast.isDummy(identifier)))
ast.addDiagnostic(`Name '${identifier.name}' not declared`, DiagnosticSeverity.Error, loc);
}
}, child)
}
const declaration = ast.findDeclarationByName(child.name, child.loc!);
if (declaration && !sourceLocEquals(rangeToSourceLoc(declaration.selectionRange), child.loc!)) {
declaration.unused = false;
}
}
}();