This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathexpectRule.ts
401 lines (350 loc) · 15.4 KB
/
expectRule.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import os = require("os");
import { basename, dirname, join, resolve as resolvePath } from "path";
import * as Lint from "tslint";
import * as TsType from "typescript";
import { last } from "../util";
type Program = TsType.Program;
type SourceFile = TsType.SourceFile;
// Based on https://github.com/danvk/typings-checker
const cacheDir = join(os.homedir(), ".dts");
const perfDir = join(os.homedir(), ".dts", "perf");
export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
static metadata: Lint.IRuleMetadata = {
ruleName: "expect",
description: "Asserts types with $ExpectType and presence of errors with $ExpectError.",
optionsDescription: "Not configurable.",
options: null,
type: "functionality",
typescriptOnly: true,
requiresTypeInfo: true,
};
/* tslint:enable:object-literal-sort-keys */
static FAILURE_STRING_DUPLICATE_ASSERTION = "This line has 2 $ExpectType assertions.";
static FAILURE_STRING_ASSERTION_MISSING_NODE = "Can not match a node to this assertion.";
static FAILURE_STRING_EXPECTED_ERROR = "Expected an error on this line, but found none.";
static FAILURE_STRING(expectedVersion: string, expectedType: string, actualType: string): string {
return `TypeScript@${expectedVersion} expected type to be:\n ${expectedType}\ngot:\n ${actualType}`;
}
applyWithProgram(sourceFile: SourceFile, lintProgram: Program): Lint.RuleFailure[] {
const options = this.ruleArguments[0] as Options | undefined;
if (!options) {
return this.applyWithFunction(sourceFile, ctx =>
walk(ctx, lintProgram, TsType, "next", /*nextHigherVersion*/ undefined));
}
const { tsconfigPath, versionsToTest } = options;
const getFailures = (
{ versionName, path }: VersionToTest,
nextHigherVersion: string | undefined,
writeOutput: boolean,
) => {
const ts = require(path);
const program = getProgram(tsconfigPath, ts, versionName, lintProgram);
const failures = this.applyWithFunction(sourceFile, ctx => walk(ctx, program, ts, versionName, nextHigherVersion));
if (writeOutput) {
const packageName = basename(dirname(tsconfigPath));
if (!packageName.match(/v\d+/) && !packageName.match(/ts\d\.\d/)) {
const d = {
[packageName]: {
typeCount: (program as any).getTypeCount(),
memory: ts.sys.getMemoryUsage ? ts.sys.getMemoryUsage() : 0,
},
};
if (!existsSync(cacheDir)) {
mkdirSync(cacheDir);
}
if (!existsSync(perfDir)) {
mkdirSync(perfDir);
}
writeFileSync(join(perfDir, `${packageName}.json`), JSON.stringify(d));
}
}
return failures;
};
const maxFailures = getFailures(last(versionsToTest), undefined, /*writeOutput*/ true);
if (maxFailures.length) {
return maxFailures;
}
// As an optimization, check the earliest version for errors;
// assume that if it works on min and max, it works for everything in between.
const minFailures = getFailures(versionsToTest[0], undefined, /*writeOutput*/ false);
if (!minFailures.length) {
return [];
}
// There are no failures in the max version, but there are failures in the min version.
// Work backward to find the newest version with failures.
for (let i = versionsToTest.length - 2; i >= 0; i--) {
const failures = getFailures(versionsToTest[i], options.versionsToTest[i + 1].versionName, /*writeOutput*/ false);
if (failures.length) {
return failures;
}
}
throw new Error(); // unreachable -- at least the min version should have failures.
}
}
export interface Options {
readonly tsconfigPath: string;
// These should be sorted with oldest first.
readonly versionsToTest: ReadonlyArray<VersionToTest>;
}
export interface VersionToTest {
readonly versionName: string;
readonly path: string;
}
const programCache = new WeakMap<Program, Map<string, Program>>();
/** Maps a tslint Program to one created with the version specified in `options`. */
export function getProgram(configFile: string, ts: typeof TsType, versionName: string, lintProgram: Program): Program {
let versionToProgram = programCache.get(lintProgram);
if (versionToProgram === undefined) {
versionToProgram = new Map<string, Program>();
programCache.set(lintProgram, versionToProgram);
}
let newProgram = versionToProgram.get(versionName);
if (newProgram === undefined) {
newProgram = createProgram(configFile, ts);
versionToProgram.set(versionName, newProgram);
}
return newProgram;
}
function createProgram(configFile: string, ts: typeof TsType): Program {
const projectDirectory = dirname(configFile);
const { config } = ts.readConfigFile(configFile, ts.sys.readFile);
const parseConfigHost: TsType.ParseConfigHost = {
fileExists: existsSync,
readDirectory: ts.sys.readDirectory,
readFile: file => readFileSync(file, "utf8"),
useCaseSensitiveFileNames: true,
};
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, resolvePath(projectDirectory), {noEmit: true});
const host = ts.createCompilerHost(parsed.options, true);
return ts.createProgram(parsed.fileNames, parsed.options, host);
}
function walk(
ctx: Lint.WalkContext<void>,
program: Program,
ts: typeof TsType,
versionName: string,
nextHigherVersion: string | undefined): void {
const { fileName } = ctx.sourceFile;
const sourceFile = program.getSourceFile(fileName)!;
if (!sourceFile) {
ctx.addFailure(0, 0,
`Program source files differ between TypeScript versions. This may be a dtslint bug.\n` +
`Expected to find a file '${fileName}' present in ${TsType.version}, but did not find it in ts@${versionName}.`);
return;
}
const checker = program.getTypeChecker();
// Don't care about emit errors.
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
if (sourceFile.isDeclarationFile || !/\$Expect(Type|Error)/.test(sourceFile.text)) {
// Normal file.
for (const diagnostic of diagnostics) {
addDiagnosticFailure(diagnostic);
}
return;
}
const { errorLines, typeAssertions, duplicates } = parseAssertions(sourceFile);
for (const line of duplicates) {
addFailureAtLine(line, Rule.FAILURE_STRING_DUPLICATE_ASSERTION);
}
const seenDiagnosticsOnLine = new Set<number>();
for (const diagnostic of diagnostics) {
const line = lineOfPosition(diagnostic.start!, sourceFile);
seenDiagnosticsOnLine.add(line);
if (!errorLines.has(line)) {
addDiagnosticFailure(diagnostic);
}
}
for (const line of errorLines) {
if (!seenDiagnosticsOnLine.has(line)) {
addFailureAtLine(line, Rule.FAILURE_STRING_EXPECTED_ERROR);
}
}
const { unmetExpectations, unusedAssertions } = getExpectTypeFailures(sourceFile, typeAssertions, checker, ts);
for (const { node, expected, actual } of unmetExpectations) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING(versionName, expected, actual));
}
for (const line of unusedAssertions) {
addFailureAtLine(line, Rule.FAILURE_STRING_ASSERTION_MISSING_NODE);
}
function addDiagnosticFailure(diagnostic: TsType.Diagnostic): void {
const intro = getIntro();
if (diagnostic.file === sourceFile) {
const msg = `${intro}\n${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`;
ctx.addFailureAt(diagnostic.start!, diagnostic.length!, msg);
} else {
ctx.addFailureAt(0, 0, `${intro}\n${fileName}${diagnostic.messageText}`);
}
}
function getIntro(): string {
if (nextHigherVersion === undefined) {
return `TypeScript@${versionName} compile error: `;
} else {
const msg = `Compile error in typescript@${versionName} but not in typescript@${nextHigherVersion}.\n`;
const explain = nextHigherVersion === "next"
? "TypeScript@next features not yet supported."
: `Fix with a comment '// TypeScript Version: ${nextHigherVersion}' just under the header.`;
return msg + explain;
}
}
function addFailureAtLine(line: number, failure: string): void {
const start = sourceFile.getPositionOfLineAndCharacter(line, 0);
let end = start + sourceFile.text.split("\n")[line].length;
if (sourceFile.text[end - 1] === "\r") {
end--;
}
ctx.addFailure(start, end, `TypeScript@${versionName}: ${failure}`);
}
}
interface Assertions {
/** Lines with an $ExpectError. */
readonly errorLines: ReadonlySet<number>;
/** Map from a line number to the expected type at that line. */
readonly typeAssertions: Map<number, string>;
/** Lines with more than one assertion (these are errors). */
readonly duplicates: ReadonlyArray<number>;
}
function parseAssertions(sourceFile: SourceFile): Assertions {
const errorLines = new Set<number>();
const typeAssertions = new Map<number, string>();
const duplicates: number[] = [];
const { text } = sourceFile;
const commentRegexp = /\/\/(.*)/g;
const lineStarts = sourceFile.getLineStarts();
let curLine = 0;
while (true) {
const commentMatch = commentRegexp.exec(text);
if (commentMatch === null) {
break;
}
// Match on the contents of that comment so we do nothing in a commented-out assertion,
// i.e. `// foo; // $ExpectType number`
const match = /^ \$Expect((Type (.*))|Error)$/.exec(commentMatch[1]);
if (match === null) {
continue;
}
const line = getLine(commentMatch.index);
if (match[1] === "Error") {
if (errorLines.has(line)) {
duplicates.push(line);
}
errorLines.add(line);
} else {
const expectedType = match[3];
// Don't bother with the assertion if there are 2 assertions on 1 line. Just fail for the duplicate.
if (typeAssertions.delete(line)) {
duplicates.push(line);
} else {
typeAssertions.set(line, expectedType);
}
}
}
return { errorLines, typeAssertions, duplicates };
function getLine(pos: number): number {
// advance curLine to be the line preceding 'pos'
while (lineStarts[curLine + 1] <= pos) {
curLine++;
}
// If this is the first token on the line, it applies to the next line.
// Otherwise, it applies to the text to the left of it.
return isFirstOnLine(text, lineStarts[curLine], pos) ? curLine + 1 : curLine;
}
}
function isFirstOnLine(text: string, lineStart: number, pos: number): boolean {
for (let i = lineStart; i < pos; i++) {
if (text[i] !== " ") {
return false;
}
}
return true;
}
interface ExpectTypeFailures {
/** Lines with an $ExpectType, but a different type was there. */
readonly unmetExpectations: ReadonlyArray<{ node: TsType.Node, expected: string, actual: string }>;
/** Lines with an $ExpectType, but no node could be found. */
readonly unusedAssertions: Iterable<number>;
}
function matchReadonlyArray(actual: string, expected: string) {
if (!(/\breadonly\b/.test(actual) && /\bReadonlyArray\b/.test(expected))) return false;
const readonlyArrayRegExp = /\bReadonlyArray</y;
const readonlyModifierRegExp = /\breadonly /y;
// A<ReadonlyArray<B<ReadonlyArray<C>>>>
// A<readonly B<readonly C[]>[]>
let expectedPos = 0;
let actualPos = 0;
let depth = 0;
while (expectedPos < expected.length && actualPos < actual.length) {
const expectedChar = expected.charAt(expectedPos);
const actualChar = actual.charAt(actualPos);
if (expectedChar === actualChar) {
expectedPos++;
actualPos++;
continue;
}
// check for end of readonly array
if (depth > 0 && expectedChar === ">" && actualChar === "[" && actualPos < actual.length - 1 &&
actual.charAt(actualPos + 1) === "]") {
depth--;
expectedPos++;
actualPos += 2;
continue;
}
// check for start of readonly array
readonlyArrayRegExp.lastIndex = expectedPos;
readonlyModifierRegExp.lastIndex = actualPos;
if (readonlyArrayRegExp.test(expected) && readonlyModifierRegExp.test(actual)) {
depth++;
expectedPos += 14; // "ReadonlyArray<".length;
actualPos += 9; // "readonly ".length;
continue;
}
return false;
}
return true;
}
function getExpectTypeFailures(
sourceFile: SourceFile,
typeAssertions: Map<number, string>,
checker: TsType.TypeChecker,
ts: typeof TsType,
): ExpectTypeFailures {
const unmetExpectations: Array<{ node: TsType.Node, expected: string, actual: string }> = [];
// Match assertions to the first node that appears on the line they apply to.
// `forEachChild` isn't available as a method in older TypeScript versions, so must use `ts.forEachChild` instead.
ts.forEachChild(sourceFile, function iterate(node) {
const line = lineOfPosition(node.getStart(sourceFile), sourceFile);
const expected = typeAssertions.get(line);
if (expected !== undefined) {
// https://github.com/Microsoft/TypeScript/issues/14077
if (node.kind === ts.SyntaxKind.ExpressionStatement) {
node = (node as TsType.ExpressionStatement).expression;
}
const type = checker.getTypeAtLocation(getNodeForExpectType(node, ts));
const actual = type
? checker.typeToString(type, /*enclosingDeclaration*/ undefined, ts.TypeFormatFlags.NoTruncation)
: "";
if (!expected.split(/\s*\|\|\s*/).some(s => actual === s || matchReadonlyArray(actual, s))) {
unmetExpectations.push({ node, expected, actual });
}
typeAssertions.delete(line);
}
ts.forEachChild(node, iterate);
});
return { unmetExpectations, unusedAssertions: typeAssertions.keys() };
}
function getNodeForExpectType(node: TsType.Node, ts: typeof TsType): TsType.Node {
if (node.kind === ts.SyntaxKind.VariableStatement) { // ts2.0 doesn't have `isVariableStatement`
const { declarationList: { declarations } } = node as TsType.VariableStatement;
if (declarations.length === 1) {
const { initializer } = declarations[0];
if (initializer) {
return initializer;
}
}
}
return node;
}
function lineOfPosition(pos: number, sourceFile: SourceFile): number {
return sourceFile.getLineAndCharacterOfPosition(pos).line;
}