-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifStatement.ts
17 lines (16 loc) · 1014 Bytes
/
ifStatement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { AST } from "../ast";
import { Rule } from "./rule";
import { IfStatement, Node } from "estree"
import { STATEMENTS } from "../types";
import { DiagnosticSeverity } from "vscode-languageserver";
import { Chapter, Context } from "../types";
export const ifStatementRule = new class extends Rule<IfStatement> {
public process(child: IfStatement, parent: Node, context: Context, ast: AST): void {
if (context.chapter < Chapter.SOURCE_3 && !child.alternate)
ast.addDiagnostic(`Missing "else" in "if-else" statement`, DiagnosticSeverity.Error, { start: child.loc!.start, end: child.consequent.loc!.start });
if (child.consequent.type !== STATEMENTS.BLOCK)
ast.addDiagnostic("Missing curly braces around if", DiagnosticSeverity.Error, child.consequent.loc!);
if (child.alternate && child.alternate.type !== STATEMENTS.BLOCK && child.alternate.type !== STATEMENTS.IF)
ast.addDiagnostic("Missing curly braces around else", DiagnosticSeverity.Error, child.alternate.loc!);
}
}();