-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_brackets.js
More file actions
59 lines (51 loc) · 1.88 KB
/
check_brackets.js
File metadata and controls
59 lines (51 loc) · 1.88 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
59
const fs = require('fs');
const content = fs.readFileSync('c:\\Users\\SIMBY\\Desktop\\colabwize\\src\\components\\admin\\email\\AdminEmailCenter.tsx', 'utf8');
function checkMismatches(text) {
const stack = [];
const openers = ['{', '(', '[', '<'];
const closers = ['}', ')', ']', '>'];
const pairs = { '}': '{', ')': '(', ']': '[', '>': '<' };
let lineNo = 1;
let charNo = 0;
for (let i = 0; i < text.length; i++) {
const char = text[i];
charNo++;
if (char === '\n') {
lineNo++;
charNo = 0;
}
// Skip comments
if (char === '/' && text[i+1] === '/') {
while (i < text.length && text[i] !== '\n') i++;
lineNo++; charNo = 0;
continue;
}
if (char === '/' && text[i+1] === '*') {
while (i < text.length && !(text[i] === '*' && text[i+1] === '/')) {
i++;
if (text[i] === '\n') { lineNo++; charNo = 0; }
}
i += 2;
continue;
}
if (openers.includes(char)) {
// Heuristic to skip < if it's not a JSX tag start (e.g. math)
// But in TSX, most < are tags.
stack.push({ char, l: lineNo, c: charNo });
} else if (closers.includes(char)) {
if (stack.length === 0) {
console.log(`Extra closer ${char} at line ${lineNo}, col ${charNo}`);
continue;
}
const last = stack.pop();
if (last.char !== pairs[char]) {
console.log(`Mismatch: ${last.char} at ${last.l}:${last.c} closed by ${char} at ${lineNo}:${charNo}`);
}
}
}
while (stack.length > 0) {
const last = stack.pop();
console.log(`Unclosed ${last.char} at line ${last.l}, col ${last.c}`);
}
}
checkMismatches(content);