-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcheck_braces.js
More file actions
58 lines (53 loc) · 1.92 KB
/
Copy pathcheck_braces.js
File metadata and controls
58 lines (53 loc) · 1.92 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const ts = require('typescript');
const fs = require('fs');
const src = fs.readFileSync('tests/integration/auth-jwt-validation.test.ts', 'utf8');
const sourceFile = ts.createSourceFile('test.ts', src, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);
const scanner = ts.createScanner(ts.ScriptTarget.ES2022, false, ts.LanguageVariant.Standard, src);
let token;
let inTemplateExpr = false;
let realOpen = 0;
let realClose = 0;
let fakeClose = 0;
let openStack = [];
let closeStack = [];
while ((token = scanner.scan()) !== ts.SyntaxKind.EndOfFileToken) {
const pos = scanner.getStartPos();
const lineInfo = sourceFile.getLineAndCharacterOfPosition(pos);
const line = lineInfo.line + 1;
const col = lineInfo.character + 1;
const lines = src.split('\n');
const ctx = lines[line - 1]?.substring(col - 1, col + 20).replace(/\n/g, '');
if (!inTemplateExpr) {
if (token === ts.SyntaxKind.OpenBraceToken) {
realOpen++;
openStack.push({ line, col, ctx });
}
if (token === ts.SyntaxKind.CloseBraceToken) {
realClose++;
if (openStack.length > 0) {
closeStack.push({ line, col, matched: openStack.pop(), ctx });
} else {
console.log('EXTRA } at line ' + line + ' col ' + col + ' context: ' + ctx);
}
}
if (token === ts.SyntaxKind.TemplateHead || token === ts.SyntaxKind.TemplateMiddle) {
inTemplateExpr = true;
}
} else {
if (token === ts.SyntaxKind.CloseBraceToken) {
fakeClose++;
inTemplateExpr = false;
}
}
}
console.log('Real OpenBraceToken:', realOpen);
console.log('Real CloseBraceToken:', realClose);
console.log('Template CloseBrace (fake):', fakeClose);
console.log('Net unclosed (real):', realOpen - realClose);
console.log('Unclosed braces:', openStack.length);
if (openStack.length > 0) {
console.log('Unclosed brace locations:');
openStack.forEach(u => {
console.log(' line ' + u.line + ' col ' + u.col + ' context: ' + u.ctx);
});
}