/
lint.ts
196 lines (176 loc) · 6.26 KB
/
lint.ts
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
import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
import { withoutStart } from "@definitelytyped/utils";
import assert = require("assert");
import { join as joinPaths, normalize, resolve } from "path";
import { ESLint } from "eslint";
import * as TsType from "typescript";
import { createProgram } from "./createProgram";
import { typeScriptPath } from "./typescript-installer";
export async function lint(
dirPath: string,
minVersion: TsVersion,
maxVersion: TsVersion,
isLatest: boolean,
expectOnly: boolean,
tsLocal: string | undefined,
): Promise<string | undefined> {
const tsconfigPath = joinPaths(dirPath, "tsconfig.json");
// If this package has been linked for local development,
// we may end up with duplicate copies of typescript-estree.
// Clear both so we are sure that we've cleared all caches.
const estrees = [
tryResolve("@typescript-eslint/typescript-estree"),
tryResolve("@typescript-eslint/typescript-estree", { paths: [dirPath] }),
tryResolve("@typescript-eslint/typescript-estree", { paths: [resolve(__dirname, "../../eslint-plugin")] }),
];
process.env.TSESTREE_SINGLE_RUN = "true";
const lintProgram = createProgram(tsconfigPath);
const files = [];
for (const file of lintProgram.getSourceFiles()) {
if (lintProgram.isSourceFileDefaultLibrary(file)) {
continue;
}
const { fileName, text } = file;
if (!fileName.includes("node_modules")) {
const err = testNoLintDisables(text);
if (err) {
const { pos, message } = err;
const place = file.getLineAndCharacterOfPosition(pos);
return `At ${fileName}:${JSON.stringify(place)}: ${message}`;
}
}
// External dependencies should have been handled by `testDependencies`;
// typesVersions should be handled in a separate lint
if (!isExternalDependency(file, dirPath, lintProgram) && (!isLatest || !isTypesVersionPath(fileName, dirPath))) {
files.push(fileName);
}
}
const options = getEslintOptions(expectOnly, minVersion, maxVersion, tsLocal);
const eslint = new ESLint(options);
const formatter = await eslint.loadFormatter("stylish");
const results = await eslint.lintFiles(files);
const output = formatter.format(results);
for (const estreePath of estrees) {
if (!estreePath) continue;
const estree = require(estreePath) as typeof import("@typescript-eslint/typescript-estree");
estree.clearCaches();
}
return output;
}
function tryResolve(path: string, options?: { paths?: string[] | undefined }): string | undefined {
try {
return require.resolve(path, options);
} catch {
return undefined;
}
}
function getEslintOptions(
expectOnly: boolean,
minVersion: TsVersion,
maxVersion: TsVersion,
tsLocal: string | undefined,
): ESLint.Options {
const versionsToTest = range(minVersion, maxVersion).map((versionName) => ({
versionName,
path: typeScriptPath(versionName, tsLocal),
}));
const allFiles = ["*.ts", "*.cts", "*.mts", "*.tsx"];
const overrideConfig: ESLint.Options["overrideConfig"] = {
settings: {
dt: {
versionsToTest,
},
},
overrides: [
{
files: allFiles,
rules: {
// This prevents anyone from disabling this rule.
"@definitelytyped/expect": ["error"],
},
},
],
};
if (expectOnly) {
return {
useEslintrc: false,
overrideConfig: {
plugins: ["@definitelytyped", "@typescript-eslint", "jsdoc"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
warnOnUnsupportedTypeScriptVersion: false,
},
...overrideConfig,
},
};
}
return {
overrideConfig,
};
}
export function isExternalDependency(file: TsType.SourceFile, dirPath: string, program: TsType.Program): boolean {
return !startsWithDirectory(file.fileName, dirPath) || program.isSourceFileFromExternalLibrary(file);
}
function normalizePath(file: string) {
// replaces '\' with '/' and forces all DOS drive letters to be upper-case
return normalize(file)
.replace(/\\/g, "/")
.replace(/^[a-z](?=:)/, (c) => c.toUpperCase());
}
function isTypesVersionPath(fileName: string, dirPath: string) {
const normalFileName = normalizePath(fileName);
const normalDirPath = normalizePath(dirPath);
const subdirPath = withoutStart(normalFileName, normalDirPath);
return subdirPath && /^\/ts\d+\.\d/.test(subdirPath);
}
function startsWithDirectory(filePath: string, dirPath: string): boolean {
const normalFilePath = normalizePath(filePath);
const normalDirPath = normalizePath(dirPath).replace(/\/$/, "");
return normalFilePath.startsWith(normalDirPath + "/") || normalFilePath.startsWith(normalDirPath + "\\");
}
interface Err {
pos: number;
message: string;
}
function testNoLintDisables(text: string): Err | undefined {
const disabler = "eslint-disable";
let lastIndex = 0;
while (true) {
const pos = text.indexOf(disabler, lastIndex);
if (pos === -1) {
return undefined;
}
const end = pos + disabler.length;
const nextChar = text.charAt(end);
const nextChar2 = text.charAt(end + 1);
if (nextChar !== "-" && !(nextChar === " " && nextChar2 !== "*")) {
const message =
`'${disabler}' is forbidden. ` +
"Per-line and per-rule disabling is allowed, for example: " +
"'eslint-disable:rulename', eslint-disable-line' and 'eslint-disable-next-line' are allowed.";
return { pos, message };
}
lastIndex = end;
}
}
function range(minVersion: TsVersion, maxVersion: TsVersion): readonly TsVersion[] {
if (minVersion === "local") {
assert(maxVersion === "local");
return ["local"];
}
if (minVersion === TypeScriptVersion.latest) {
assert(maxVersion === TypeScriptVersion.latest);
return [TypeScriptVersion.latest];
}
assert(maxVersion !== "local");
const minIdx = TypeScriptVersion.supported.indexOf(minVersion);
assert(minIdx >= 0);
if (maxVersion === TypeScriptVersion.latest) {
return [...TypeScriptVersion.supported.slice(minIdx), TypeScriptVersion.latest];
}
const maxIdx = TypeScriptVersion.supported.indexOf(maxVersion as TypeScriptVersion);
assert(maxIdx >= minIdx);
return TypeScriptVersion.supported.slice(minIdx, maxIdx + 1);
}
export type TsVersion = TypeScriptVersion | "local";