Repository navigation
Expand file tree
/
Copy pathcheck-angular-overlap.mjs
More file actions
104 lines (89 loc) · 2.72 KB
/
Copy pathcheck-angular-overlap.mjs
File metadata and controls
104 lines (89 loc) · 2.72 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { catalogCategories } from "./component-policy.ts";
const indexSource = readFileSync("src/index.ts", "utf8");
const angularTypesSource = readFileSync(
"node_modules/@angular-wave/angular.ts/@types/ng.d.ts",
"utf8",
);
const registeredDirectives = [...indexSource.matchAll(/\["([^"]+)"\s*,/g)].map(
(match) => match[1],
);
const angularBuiltInDirectives = new Set([
...[...angularTypesSource.matchAll(/\n\s{4}(ng[A-Za-z0-9_]+):/g)].map(
(match) => match[1],
),
...[
...angularTypesSource.matchAll(
/Record<([^>]+),\s*import\([^)]*\)\.DirectiveFactory>/g,
),
].flatMap((match) =>
[...match[1].matchAll(/"(ng[A-Za-z0-9_]+)"/g)].map(
(directive) => directive[1],
),
),
]);
const collisions = registeredDirectives.filter((directive) =>
angularBuiltInDirectives.has(directive),
);
const listTypeScriptFiles = (directory) =>
readdirSync(directory)
.flatMap((entry) => {
const path = join(directory, entry);
return statSync(path).isDirectory() ? listTypeScriptFiles(path) : path;
})
.filter((path) => path.endsWith(".ts") && !path.endsWith(".test.ts"));
const overlapFailures = [];
if (collisions.length > 0) {
overlapFailures.push(
"AngularCSS must not register directives already provided by AngularTS.",
...collisions.map((directive) => `- ${directive}`),
);
}
for (const file of catalogCategories.flatMap((category) =>
listTypeScriptFiles(join("src", category)),
)) {
const source = readFileSync(file, "utf8");
const checks = [
{
message: "must not write component controller state onto AngularTS scope",
pattern: /\bscope\.(?!\$on\b)/,
},
{
message: "must not watch AngularTS scope state",
pattern: /\$watch\b/,
},
{
message: "must not trigger AngularTS digest/apply directly",
pattern: /\$(?:apply|digest|eval)\b/,
},
{
message: "must not require ngModel directly",
pattern: /require:\s*["']ngModel["']/,
},
{
message: "must not own native checked state",
pattern: /\.checked\s*=/,
},
];
for (const { message, pattern } of checks) {
if (pattern.test(source)) {
overlapFailures.push(`${file}: ${message}`);
}
}
if (/\.value\s*=/.test(source)) {
overlapFailures.push(`${file}: must not own native value state`);
}
if (
/dispatchEvent\(new Event\(["'](?:input|change)["']/.test(source)
) {
overlapFailures.push(
`${file}: must not synthesize native input/change events`,
);
}
}
if (overlapFailures.length > 0) {
console.error(overlapFailures.join("\n"));
process.exit(1);
}
console.log("AngularTS overlap check passed.");