Skip to content

Commit f0e7f17

Browse files
committed
more bug fixes, tests for every rule
1 parent 779fcbd commit f0e7f17

15 files changed

+532
-76
lines changed

esbuild.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
const esbuild = require("esbuild");
2+
const glob = require("glob");
23

3-
esbuild.build({
4-
entryPoints: ["server/src/server.ts"], // Adjust if your main entry file is different
5-
bundle: true,
6-
platform: "node",
7-
format: "cjs", // Ensure CommonJS format
8-
outfile: "./dist/source-lsp.js",
9-
external: ["vscode"], // Avoid bundling VSCode module
10-
sourcemap: true, // Optional: Generates a source map for debugging
11-
}).catch(() => process.exit(1));
12-
13-
console.log("Built successfully")
4+
const testFiles = glob.sync("server/src/test/*.ts");
5+
6+
async function build() {
7+
esbuild.build({
8+
entryPoints: ["server/src/server.ts"],
9+
bundle: true,
10+
platform: "node",
11+
format: "cjs",
12+
outfile: "./dist/source-lsp.js",
13+
external: ["vscode"], // Avoid bundling VSCode module
14+
sourcemap: true,
15+
});
16+
17+
if (testFiles.length > 0) {
18+
await esbuild.build({
19+
entryPoints: testFiles,
20+
bundle: true,
21+
platform: "node",
22+
format: "cjs",
23+
outdir: "./dist/tests",
24+
external: ["vscode"],
25+
sourcemap: true,
26+
});
27+
} else {
28+
console.log("No test files found.");
29+
}
30+
31+
console.log("Build complete.");
32+
}
33+
34+
build().catch(() => process.exit(1))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"watch": "tsc -b -w",
6565
"lint": "eslint",
6666
"postinstall": "cd client && npm install && cd ../server && npm install && cd ..",
67-
"test": "node node_modules/mocha/bin/mocha.js server/out/test/*.js --ui tdd"
67+
"test": "node node_modules/mocha/bin/mocha.js dist/tests/*.js --ui tdd"
6868
},
6969
"devDependencies": {
7070
"@eslint/js": "^9.13.0",

server/src/ast.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class AST {
5454
catch (e) {
5555
this.ast = looseParse(text, acornOptions) as Node
5656
}
57-
console.debug(JSON.stringify(this.ast, null, 2));
57+
// console.debug(JSON.stringify(this.ast, null, 2));
5858

5959
this.context = context;
6060
this.uri = uri;
@@ -77,8 +77,8 @@ export class AST {
7777

7878
this.declarations.forEach(declarationList => {
7979
for (let i = 0; i < declarationList.length; i++) {
80-
const declaration = declarationList[i]
81-
if (declaration.unused)
80+
const declaration = declarationList[i];
81+
if (declaration.unused && declaration.name !== "✖")
8282
this.addDiagnostic("Unused name", DiagnosticSeverity.Warning, rangeToSourceLoc(declaration.selectionRange), [DiagnosticTag.Unnecessary]);
8383
}
8484
})
@@ -259,6 +259,10 @@ export class AST {
259259
name = param.name
260260
else if (param.type === NODES.REST && param.argument.type === NODES.IDENTIFIER)
261261
name = param.argument.name
262+
else {
263+
this.addDiagnostic("Unexpected token", DiagnosticSeverity.Error, param.loc!);
264+
return;
265+
}
262266
const param_declaration: DeclarationSymbol = {
263267
name: name,
264268
scope: child.body.loc!,

server/src/rules/breakStatement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export const breakStatementRule = new class extends Rule<BreakStatement> {
88
public process(child: BreakStatement, parent: Node, context: Context, ast: AST): void {
99
if (context.chapter < Chapter.SOURCE_3)
1010
ast.addDiagnostic("Break statements are not allowed", DiagnosticSeverity.Error, child.loc!)
11+
// TODO: detect unsyntatic breaks (breaks outside of loops)
1112
}
1213
}();

server/src/rules/continueStatement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export const continueStatementRule = new class extends Rule<ContinueStatement> {
88
public process(child: ContinueStatement, parent: Node, context: Context, ast: AST): void {
99
if (context.chapter < Chapter.SOURCE_3)
1010
ast.addDiagnostic("Continue statements are not allowed", DiagnosticSeverity.Error, child.loc!)
11+
// TODO: detect unsyntatic continues (continues outside of loops)
1112
}
1213
}();

server/src/rules/forStatement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const forStatementRule = new class extends Rule<ForStatement> {
1010
if (context.chapter < Chapter.SOURCE_3)
1111
ast.addDiagnostic("For statements are not allowed", DiagnosticSeverity.Error, { start: child.loc!.start, end: child.body.loc!.start })
1212
if (child.body.type !== STATEMENTS.BLOCK)
13-
ast.addDiagnostic("Missing curly braces around for", DiagnosticSeverity.Error, child.loc!);
13+
ast.addDiagnostic("Missing curly braces around for", DiagnosticSeverity.Error, { start: child.loc!.start, end: child.body.loc!.start });
1414
if (!(child.init && child.test && child.update))
1515
ast.addDiagnostic("Incomplete for loop", DiagnosticSeverity.Error, { start: child.loc!.start, end: child.body.loc!.start })
1616
}

server/src/rules/ifStatement.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { Chapter, Context } from "../types";
88
export const ifStatementRule = new class extends Rule<IfStatement> {
99
public process(child: IfStatement, parent: Node, context: Context, ast: AST): void {
1010
if (context.chapter < Chapter.SOURCE_3 && !child.alternate)
11-
ast.addDiagnostic(`Missing "else" in "if-else" statement`, DiagnosticSeverity.Error, child.consequent.loc!);
11+
ast.addDiagnostic(`Missing "else" in "if-else" statement`, DiagnosticSeverity.Error, { start: child.loc!.start, end: child.consequent.loc!.start });
1212
if (child.consequent.type !== STATEMENTS.BLOCK)
13-
ast.addDiagnostic("Missing curly braces around if", DiagnosticSeverity.Error, child.loc!);
13+
ast.addDiagnostic("Missing curly braces around if", DiagnosticSeverity.Error, child.consequent.loc!);
1414
if (child.alternate && child.alternate.type !== STATEMENTS.BLOCK)
15-
ast.addDiagnostic("Missing curly braces around else", DiagnosticSeverity.Error, child.loc!);
15+
ast.addDiagnostic("Missing curly braces around else", DiagnosticSeverity.Error, child.alternate.loc!);
1616
}
1717
}();

server/src/rules/memberExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { Chapter, Context } from "../types";
77

88
export const memberExpressionRule = new class extends Rule<MemberExpression> {
99
public process(child: MemberExpression, parent: Node, context: Context, ast: AST): void {
10-
if (context.chapter < Chapter.SOURCE_3)
11-
ast.addDiagnostic("Member access expressions are not allowed", DiagnosticSeverity.Error, child.loc!)
1210
// If computed is false, is dot abbreviation, else is object property / array access
1311
if (!child.computed)
1412
ast.addDiagnostic("No dot abbreviations", DiagnosticSeverity.Error, child.loc!);
13+
else if (context.chapter < Chapter.SOURCE_3)
14+
ast.addDiagnostic("Member access expressions are not allowed", DiagnosticSeverity.Error, child.loc!)
1515
else if (child.property.type === NODES.IDENTIFIER && child.property.name === "undefined")
1616
ast.addDiagnostic("Expected non negative integer as array index, got undefined.", DiagnosticSeverity.Error, child.property.loc!);
1717
else if (child.property.type === NODES.LITERAL) {

server/src/rules/returnStatement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export const returnStatementRule = new class extends Rule<ReturnStatement> {
88
public process(child: ReturnStatement, parent: Node, context: Context, ast: AST): void {
99
if (!child.argument)
1010
ast.addDiagnostic("Missing value in return statement", DiagnosticSeverity.Error, child.loc!);
11+
// TODO: detect unsyntatic returns (returns outside of functions)
1112
}
1213
}();

server/src/rules/whileStatement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export const whileStatementRule = new class extends Rule<WhileStatement> {
1010
if (context.chapter < Chapter.SOURCE_3)
1111
ast.addDiagnostic("While statements are not allowed", DiagnosticSeverity.Error, { start: child.loc!.start, end: child.body.loc!.start })
1212
if (child.body.type !== STATEMENTS.BLOCK)
13-
ast.addDiagnostic("Missing curly braces around while", DiagnosticSeverity.Error, child.loc!);
13+
ast.addDiagnostic("Missing curly braces around while", DiagnosticSeverity.Error, { start: child.loc!.start, end: child.body.loc!.start });
1414
}
1515
}();

0 commit comments

Comments
 (0)