Skip to content

Commit 376f049

Browse files
committed
fixed more bugs
1 parent ae87fc7 commit 376f049

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

server/src/rules/binaryExpression.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const binaryExpressionRule = new class extends Rule<BinaryExpression> {
2020
'>=',
2121
'&&',
2222
'||'
23-
]
23+
]
2424

2525
public process(child: BinaryExpression, parent: Node, context: Context, ast: AST): void {
2626
ast.checkIncompleteLeftRightStatement(child.left, child.right, child.loc!, "Incomplete binary expression");
@@ -39,19 +39,30 @@ export const binaryExpressionRule = new class extends Rule<BinaryExpression> {
3939
// undefined is an identifier so we have to check this first
4040
if (child.left.type === NODES.IDENTIFIER && child.left.name === "undefined")
4141
ast.addDiagnostic(`Expected string or number on left hand side of operation, got undefined`, DiagnosticSeverity.Error, child.left.loc!);
42-
else if (child.left.type === NODES.LITERAL) {
42+
43+
44+
let left_type_correct = false;
45+
if (child.left.type === NODES.LITERAL) {
4346
const left_type = typeof child.left.value;
44-
if (!(left_type === "string" || left_type === "number"))
47+
left_type_correct = true;
48+
if (!(left_type === "string" || left_type === "number")) {
4549
ast.addDiagnostic(`Expected string or number on left hand side of operation, got ${child.left.value === null ? "null" : left_type}`, DiagnosticSeverity.Error, child.left.loc!);
50+
left_type_correct = false;
51+
}
4652

47-
else if (child.right.type === NODES.IDENTIFIER && child.right.name === "undefined")
53+
if (child.right.type === NODES.IDENTIFIER && child.right.name === "undefined" && left_type_correct)
4854
ast.addDiagnostic(`Expected ${left_type} on left hand side of operation, got undefined`, DiagnosticSeverity.Error, child.right.loc!);
49-
else if (child.right.type === NODES.LITERAL) {
55+
if (child.right.type === NODES.LITERAL) {
5056
const right_type = typeof child.right.value;
51-
if (left_type !== right_type)
57+
if (left_type !== right_type && left_type_correct)
5258
ast.addDiagnostic(`Expected ${left_type} on right hand side of operation, got ${child.right.value === null ? "null" : right_type}`, DiagnosticSeverity.Error, child.right.loc!);
5359
}
5460
}
61+
if (!left_type_correct && (child.right.type === NODES.LITERAL || (child.right.type === NODES.IDENTIFIER && child.right.name === "undefined"))) {
62+
const right_type = child.right.type === NODES.LITERAL ? typeof child.right.value : "undefined";
63+
if (!(right_type === "string" || right_type === "number"))
64+
ast.addDiagnostic(`Expected string or number on right hand side of operation, got ${right_type === "object" ? "null" : right_type}`, DiagnosticSeverity.Error, child.right.loc!);
65+
}
5566
}
5667
}
5768
};

0 commit comments

Comments
 (0)