-
Notifications
You must be signed in to change notification settings - Fork 130
/
lint.js
269 lines (221 loc) · 8.85 KB
/
lint.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// @ts-check
// Loops through all the sample code and ensures that twoslash doesn't raise
// All
// yarn validate-twoslash
// Watcher
// yarn validate-twoslash --watch
// Just italian
// yarn validate-twoslash it/
const chalk = require("chalk");
const { readFileSync, watch } = require("fs");
const { join, basename, sep, dirname } = require("path");
const readline = require("readline");
const ts = require("typescript");
const remark = require("remark");
const remarkTwoSlash = require("gatsby-remark-shiki-twoslash");
const { read } = require("gray-matter");
const { recursiveReadDirSync } = require("./recursiveReadDirSync");
const docsPath = join(__dirname, "..", "docs");
const docs = recursiveReadDirSync(docsPath);
const tick = chalk.bold.greenBright("✓");
const cross = chalk.bold.redBright("⤫");
// Pass in a 2nd arg which either triggers watch mode, or to filter which markdowns to run
const filterString = process.argv[2] ? process.argv[2] : "";
if (filterString === "--watch") {
const clear = () => {
const blank = "\n".repeat(process.stdout.rows);
console.log(blank);
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
};
if (process.platform === "linux") throw new Error("Sorry linux peeps, the node watcher doesn't support linux.");
watch(join(__dirname, "..", "docs"), { recursive: true }, (_, filename) => {
clear();
process.stdout.write("♲ ");
validateAtPaths([join(docsPath, filename)]);
});
clear();
console.log(`${chalk.bold("Started the watcher")}, pressing save on a file in ./docs will lint that file.`);
} else {
const toValidate = docs
.filter((f) => !f.includes("/en/"))
.filter((f) => (filterString.length > 0 ? f.toLowerCase().includes(filterString.toLowerCase()) : true));
validateAtPaths(toValidate);
}
/** @param {string[]} docs */
function validateAtPaths(docs) {
let errorReports = [];
docs.forEach((docAbsPath, i) => {
const docPath = docAbsPath;
const filename = basename(docPath);
let lintFunc = undefined;
if (docAbsPath.includes("typescriptlang") && docAbsPath.endsWith(".ts")) {
lintFunc = lintTSLanguageFile;
} else if (docAbsPath.endsWith(".md")) {
lintFunc = lintMarkdownFile;
}
const isLast = i === docs.length - 1;
const suffix = isLast ? "" : ", ";
if (!lintFunc) {
process.stdout.write(chalk.gray(filename + " skipped" + suffix));
return;
}
const errors = lintFunc(docPath);
errorReports = errorReports.concat(errors);
const sigil = errors.length ? cross : tick;
const name = errors.length ? chalk.red(filename) : filename;
process.stdout.write(name + " " + sigil + suffix);
});
if (errorReports.length) {
process.exitCode = 1;
console.log("");
errorReports.forEach((err) => {
console.log(`\n> ${chalk.bold.red(err.path)}\n`);
err.error.stack = undefined;
console.log(err.error.message);
if (err.error.stack) {
console.log(err.error.stack);
}
});
console.log("\n");
if (!filterString) {
console.log(
"Note: you can add an extra argument to the lint script ( yarn workspace glossary lint [opt] ) to just run one lint."
);
}
} else {
console.log(chalk.green("\n\nAll good"));
}
}
/** @param {string} docPath */
function lintMarkdownFile(docPath) {
/** @type { Error[] } */
const errors = [];
const markdown = readFileSync(docPath, "utf8");
const markdownAST = remark().parse(markdown);
const greyMD = read(docPath);
try {
remarkTwoSlash.runTwoSlashAcrossDocument({ markdownAST }, {});
} catch (error) {
errors.push(error);
}
const relativePath = docPath.replace(docsPath, "");
const docType = relativePath.split(sep)[1];
const lang = relativePath.split(sep)[2];
if (docType === "documentation") {
if (!greyMD.data.title) {
errors.push(new Error("Did not have a 'display' property in the YML header"));
}
if (greyMD.data.layout !== "docs") {
errors.push(new Error("Expected 'layout: docs' in the YML header"));
}
if (!greyMD.data.permalink.startsWith("/" + lang)) {
errors.push(new Error(`Expected 'permalink:' in the YML header to start with '/${lang}'`));
}
} else if (docType === "tsconfig") {
if (relativePath.includes("options")) {
if (!greyMD.data.display) {
errors.push(new Error("Did not have a 'display' property in the YML header"));
}
if (!greyMD.data.display) {
errors.push(new Error("Did not have a 'oneline' property in the YML header"));
}
}
}
return errors.map((e) => ({ path: docPath, error: e }));
}
/** @param {string} file */
function lintTSLanguageFile(file) {
/** @type {Error[]} */
const errors = [];
const content = readFileSync(file, "utf8");
const sourceFile = ts.createSourceFile(
file,
content,
ts.ScriptTarget.Latest,
/*setParentNodes*/ false,
ts.ScriptKind.TS
);
const filename = basename(file, ".ts");
const lastDir = dirname(file).split(sep).pop();
const isRootImport = filename === lastDir;
if (isRootImport) {
// This is the import for the language which pulls in all the existing messages
//
const notImportStatements = sourceFile.statements.filter((f) => f.kind !== 261);
const lastStatementIsDeclaration = sourceFile.statements[0].kind !== 232;
const onlyImportsAndOneExport = lastStatementIsDeclaration && notImportStatements.length === 1;
if (!onlyImportsAndOneExport) {
errors.push(new Error("A root language import can only include imports and an export called 'lang' "));
}
if (lastStatementIsDeclaration) {
/** @type {import("typescript").VariableDeclarationList} */
// @ts-ignore
const declarationList = notImportStatements[0].declarationList;
const declaration = declarationList.declarations[0];
if (!declaration.initializer) errors.push(new Error(`Something is off with the export in this file`));
/** @type {import("typescript").CallExpression} */
// @ts-ignore
const callExpression = declaration.initializer.expression;
if (callExpression.getText(sourceFile) !== "defineMessages")
errors.push(new Error(`The export needs to call define messages`));
/** @type {import("typescript").ObjectLiteralExpression} */
// @ts-ignore
const arg0 = declaration.initializer.arguments[0];
arg0.properties.forEach((p) => {
if (p.kind !== 290) errors.push(new Error(`You can only have spreads (...) in the export`));
});
}
sourceFile.statements.forEach((s) => {
if (!ts.isImportDeclaration(s)) return;
if (!s.importClause)
errors.push(new Error(`The import ${s.moduleSpecifier.getText(sourceFile)} is not importing an object`));
const allowed = ['"react-intl"'];
const specifier = s.moduleSpecifier.getText(sourceFile);
if (!allowed.includes(specifier) && !specifier.startsWith('".')) {
errors.push(new Error(`The import ${specifier} is not allowlisted ([${allowed.join(", ")}]) nor relative`));
}
});
} else {
// This should just be a simple lint that it only has a declaration
const tooManyStatements = sourceFile.statements.length > 1;
const notDeclarationList = sourceFile.statements.length > 0 && sourceFile.statements[0].kind !== 232;
if (tooManyStatements) {
errors.push(
new Error("TS files had more than one statement (e.g. more than `export const somethingCopy = { ... }` ")
);
}
if (notDeclarationList) {
errors.push(new Error("TS files should only look like: `export const somethingCopy = { ... }` "));
}
// @ts-ignore
if (sourceFile.statements[0] && sourceFile.statements[0].declarationList) {
// @ts-ignore
const lastStatement = sourceFile.statements[0].declarationList;
if (!ts.isVariableDeclarationList(lastStatement)) {
errors.push(new Error("TS files should only look like: `export const somethingCopy = { ... }` "));
} else {
/** @type {import("typescript").ObjectLiteralExpression} */
// @ts-ignore
const init = lastStatement.declarations[0].initializer;
if (!init) {
errors.push(new Error("Something is off in the const in that file"));
} else {
init.properties.forEach((prop) => {
/** @type {import("typescript").PropertyAssignment} */
// @ts-ignore
const init = prop.initializer;
if (init.kind !== 10 && init.kind !== 14) {
if (init.kind === 218) {
errors.push(new Error(`The template string at ${prop.name.getText(sourceFile)} can't have evaluated code ( no \${} allowed )`));
} else {
errors.push(new Error(`The value at ${prop.name.getText(sourceFile)} isn't a string`));
}
}
});
}
}
}
}
return errors.map((e) => ({ path: file, error: e }));
}