Repository navigation
Expand file tree
/
Copy pathcheck-private-methods.mjs
More file actions
162 lines (136 loc) · 3.99 KB
/
Copy pathcheck-private-methods.mjs
File metadata and controls
162 lines (136 loc) · 3.99 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import fs from "node:fs";
import path from "node:path";
import ts from "typescript";
const roots = ["src", "integrations", "tools"];
const skippedDirectories = new Set([
".build",
".cache",
".vscode-test",
"bin",
"build",
"coverage",
"dist",
"node_modules",
"obj",
"out",
"target",
"vendor",
]);
const failures = [];
for (const root of roots) {
if (!fs.existsSync(root)) continue;
for (const file of sourceFiles(root)) checkFile(file);
}
if (failures.length > 0) {
console.error(
"Private methods must use an underscore prefix and @internal; legacy TypeDoc visibility annotations are forbidden.",
);
for (const failure of failures) console.error(`- ${failure}`);
process.exitCode = 1;
} else {
console.log(
"Private method check passed; internal methods use underscore prefixes and @internal without legacy visibility annotations.",
);
}
function* sourceFiles(directory) {
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
if (entry.name.startsWith(".") || skippedDirectories.has(entry.name)) {
continue;
}
const file = path.join(directory, entry.name);
if (entry.isDirectory()) {
yield* sourceFiles(file);
continue;
}
if (
entry.isFile() &&
/\.(?:cts|mts|tsx?)$/.test(entry.name) &&
!entry.name.endsWith(".d.ts")
) {
yield file;
}
}
}
function checkFile(file) {
const sourceText = fs.readFileSync(file, "utf8");
const sourceFile = ts.createSourceFile(
file,
sourceText,
ts.ScriptTarget.Latest,
true,
);
checkLegacyVisibilityAnnotations(file, sourceText, sourceFile);
visit(sourceFile);
function visit(node) {
if (isMethodLike(node) && node.name) checkMethod(node);
ts.forEachChild(node, visit);
}
function checkMethod(node) {
if (ts.isPrivateIdentifier(node.name)) {
report(node, node.name.text, "ECMAScript private method must use _name");
return;
}
if (!ts.isIdentifier(node.name) && !ts.isStringLiteralLike(node.name)) {
return;
}
const name = node.name.text;
const modifiers = ts.getModifiers(node) ?? [];
const hasPrivateVisibility = modifiers.some(
(modifier) =>
modifier.kind === ts.SyntaxKind.PrivateKeyword ||
modifier.kind === ts.SyntaxKind.ProtectedKeyword,
);
if (hasPrivateVisibility && !name.startsWith("_")) {
report(node, name, "private or protected method must use _name");
}
if (
name.startsWith("_") &&
!ts.getJSDocTags(node).some((tag) => tag.tagName.text === "internal")
) {
report(node, name, "underscore-prefixed method must have @internal");
}
}
function report(node, name, message) {
const position = sourceFile.getLineAndCharacterOfPosition(
node.name.getStart(sourceFile),
);
failures.push(
`${file}:${String(position.line + 1)}:${String(position.character + 1)} ${name}: ${message}`,
);
}
}
function checkLegacyVisibilityAnnotations(file, sourceText, sourceFile) {
const scanner = ts.createScanner(
ts.ScriptTarget.Latest,
false,
ts.LanguageVariant.Standard,
sourceText,
);
const legacyAnnotation = /@(ignore|private|protected)\b/g;
let token;
while ((token = scanner.scan()) !== ts.SyntaxKind.EndOfFileToken) {
if (
token !== ts.SyntaxKind.SingleLineCommentTrivia &&
token !== ts.SyntaxKind.MultiLineCommentTrivia
) {
continue;
}
const comment = scanner.getTokenText();
let match;
while ((match = legacyAnnotation.exec(comment))) {
const offset = scanner.getTokenPos() + match.index;
const position = sourceFile.getLineAndCharacterOfPosition(offset);
failures.push(
`${file}:${String(position.line + 1)}:${String(position.character + 1)} ${match[0]}: use TypeScript visibility and @internal instead`,
);
}
}
}
function isMethodLike(node) {
return (
ts.isMethodDeclaration(node) ||
ts.isMethodSignature(node) ||
ts.isGetAccessor(node) ||
ts.isSetAccessor(node)
);
}