Skip to content

Commit c36cce5

Browse files
committed
add sameNameCheck (works for function,keywords and globals)
1 parent 28ecf90 commit c36cce5

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/diagnostic.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport, Range } from 'lsp-types';
2-
import { GlobalStatement, ImportStatement, NodeType, OperatorStatement, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js';
2+
import { FunctionStatement, GlobalStatement, ImportStatement, KeywordStatement, NodeType, OperatorStatement, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js';
33
import { existsSync, readFileSync, statSync } from 'fs';
44
import { sysparser, syxparser } from './ast.js';
55
import { tokenizeSys, tokenizeSyx } from './lexer.js';
@@ -37,6 +37,7 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent
3737
items.push(...sameRuleCheck(ast, filePath));
3838
items.push(...importedExistentCheck(ast, filePath));
3939
items.push(...sameRegexCheck(ast,filePath));
40+
items.push(...sameNameCheck(ast.body,filePath));
4041
} catch (error) {
4142
if (isCompilerError(error)) {
4243
items.push({
@@ -294,6 +295,41 @@ function exportableCheck(statements: Statement[], filePath: string): Diagnostic[
294295
return items;
295296
}
296297

298+
// Check if everything has a unique name
299+
function sameNameCheck(statements: Statement[], filePath: string): Diagnostic[] {
300+
const items:Diagnostic[] = [];
301+
302+
function c(s:Statement[]){
303+
const encounteredNames = [];
304+
305+
s
306+
.filter(r=>statementIsA(r,NodeType.Function)||statementIsA(r,NodeType.Global)||statementIsA(r,NodeType.Keyword))
307+
.map(r=>{
308+
if(statementIsA(r,NodeType.Function))return r as FunctionStatement;
309+
if(statementIsA(r,NodeType.Global))return r as GlobalStatement;
310+
if(statementIsA(r,NodeType.Keyword))return r as KeywordStatement;
311+
}).forEach(stmt=>{
312+
313+
const n = stmt[statementIsA(stmt,NodeType.Keyword)?'word':'name'];
314+
315+
if(encounteredNames.includes(n)) items.push({
316+
message:`Name '${n}' is already seen before.`,
317+
range:subRange(stmt.range),
318+
source:'syntax-script',
319+
severity:DiagnosticSeverity.Error
320+
});
321+
else encounteredNames.push(n);
322+
323+
if(statementIsA(stmt,NodeType.Global)) c(stmt.body);
324+
});
325+
326+
}
327+
328+
c(statements);
329+
330+
return items;
331+
}
332+
297333
/**
298334
* Modifies the given range to be zero-based.
299335
* @param {Range} r Any range.

0 commit comments

Comments
 (0)