-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_brackets.js
More file actions
56 lines (54 loc) · 1.76 KB
/
Copy pathcheck_brackets.js
File metadata and controls
56 lines (54 loc) · 1.76 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
const fs = require('fs');
const path = require('path');
function walk(dir, done) {
let results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
let pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
}
walk('./components', (err, files1) => {
walk('./app', (err, files2) => {
const files = [...files1, ...files2].filter(f => f.endsWith('.tsx'));
let hasError = false;
for (const file of files) {
const content = fs.readFileSync(file, 'utf8');
const lines = content.split('\n');
lines.forEach((line, i) => {
const tokens = line.split(/[\s'"`]+/);
for (const token of tokens) {
if (token.includes('-[') || token.startsWith('[')) {
let openCount = 0;
let closedCount = 0;
for (const char of token) {
if (char === '[') openCount++;
if (char === ']') closedCount++;
}
if (openCount !== closedCount) {
if (!token.includes('{') || !token.includes('}')) { // skip string interpolation inside bracket
console.error(`Unbalanced bracket in ${file}:${i+1} -> ${token}`);
hasError = true;
}
}
}
}
});
}
if (!hasError) console.log("All [ ] matched successfully in tokens.");
})
})