第4项:对页面、表单及两版共享规则启用严格类型检查
This commit is contained in:
1 parent
389952f31c
commit
f570929cfe
77 files changed
+7629
-2981
No files matched your search
@@ -0,0 +1,32 @@
|
||||
// 检查实际编译文件和共享 JS 的 JSDoc,避免新页面再次掉出严格检查。
|
||||
import process from 'node:process';
|
||||
import console from 'node:console';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import ts from 'typescript';
|
||||
const config = ts.readConfigFile('tsconfig.json', ts.sys.readFile);
|
||||
const { options, fileNames } = ts.parseJsonConfigFileContent(config.config, ts.sys, process.cwd());
|
||||
const inputs = new Set(fileNames.map(file => path.resolve(file)));
|
||||
const errors = [];
|
||||
if (!options.strict || !options.noImplicitAny || !options.checkJs) errors.push('严格类型检查未启用');
|
||||
for (const name of fs.readdirSync('src', { recursive: true })) {
|
||||
if (!/\.[jt]sx?$/.test(name)) continue;
|
||||
if (!inputs.has(path.resolve('src', name))) errors.push('未检查 src/' + name);
|
||||
if (/\.jsx?$/.test(name)) errors.push('React 业务代码需使用 TS/TSX:' + name);
|
||||
}
|
||||
for (const file of inputs) {
|
||||
const local = path.relative(process.cwd(), file);
|
||||
if (!local.startsWith('src/') && !local.startsWith('../public/')) continue;
|
||||
const source = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
|
||||
if (/@ts-(?:ignore|expect-error|nocheck)\b/.test(source.text)) errors.push(local + ' 有类型检查豁免');
|
||||
function visit(node) {
|
||||
if (node.kind === ts.SyntaxKind.AnyKeyword || node.kind === ts.SyntaxKind.JSDocAllType) {
|
||||
errors.push(local + ':' + (source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1) + ' 显式 any');
|
||||
}
|
||||
for (const doc of node.jsDoc ?? []) visit(doc);
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(source);
|
||||
}
|
||||
if (errors.length) throw new Error(errors.join('\n'));
|
||||
console.log('前端类型覆盖与禁止豁免检查通过');
|
||||
Reference in new issue
Block a user