-
Notifications
You must be signed in to change notification settings - Fork 3
/
ts-checker.ts
849 lines (760 loc) · 27.5 KB
/
ts-checker.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
import fs from 'fs-extra';
import findCacheDirectory from 'find-cache-dir';
import {convert as htmlToText} from 'html-to-text';
import repl from 'node:repl';
import {Readable, Writable} from 'node:stream';
import stableJsonStringify from 'fast-json-stable-stringify';
import _ from 'lodash';
import path, {resolve} from 'path';
import ts from 'typescript';
import {log} from './logger.js';
import {FailureLocation, fail, getLastFailReason} from './test-tracker.js';
import {writeTempFile, matchAndExtract, getTempDir, matchAll, sha256, tuple} from './utils.js';
import {CodeSample} from './types.js';
import {ExecErrorType, runNode} from './node-runner.js';
import {VERSION} from './version.js';
import {readPackageUpSync} from 'read-pkg-up';
export interface TypeScriptError {
line: number;
start: number; // inclusive
end: number; // exclusive
message: string;
}
export interface ExpectTypeAssertion {
line: number;
type: string;
}
export interface TwoslashAssertion {
/** Position in the source file that the twoslash assertion points at */
position: number;
line: number;
character: number;
/** The expected type in the twoslash comment */
type: string;
}
export type TypeScriptTypeAssertion = ExpectTypeAssertion | TwoslashAssertion;
export interface ConfigBundle {
options: ts.CompilerOptions;
host: ts.CompilerHost;
}
const COMMENT_PAT = /^( *\/\/) /;
const TILDE_PAT = / (~+)/g;
const POST_TILDE_PAT = /\/\/ [~ ]+(?:(.*))?/;
const TYPE_ASSERTION_PAT = /\/\/.*[tT]ype is (?:still )?(?:just )?(.*)\.?$/;
const TWOSLASH_PAT = /\/\/ (?: *)\^\? ?(.*)$/;
function isTwoslashAssertion(a: TypeScriptTypeAssertion): a is TwoslashAssertion {
return 'position' in a;
}
export function extractExpectedErrors(content: string): TypeScriptError[] {
const lines = content.split('\n');
const errors: TypeScriptError[] = [];
let lastCodeLine: number;
lines.forEach((line, i) => {
const comment = matchAndExtract(COMMENT_PAT, line);
if (!comment) {
lastCodeLine = i;
return;
}
const tildes = matchAll(TILDE_PAT, line);
if (tildes.length === 0) {
const lastError = errors[errors.length - 1];
if (lastError && lines[lastError.line + 1].startsWith(comment)) {
// Presumably this is a continuation of the error.
lastError.message += ' ' + line.slice(comment.length).trim();
}
return;
}
const messageMatch = POST_TILDE_PAT.exec(line);
const message = (messageMatch ? messageMatch[1] || '' : '').trim();
for (const m of tildes) {
const start = m.index + 1;
const end = start + m[1].length;
errors.push({line: lastCodeLine, start, end, message});
}
});
return errors;
}
function doErrorsOverlap(a: TypeScriptError, b: TypeScriptError) {
return a.line === b.line && a.start <= b.end && b.start <= a.end;
}
function checkMatchingErrors(expectedErrorsIn: TypeScriptError[], actualErrors: TypeScriptError[]) {
const expectedErrors = expectedErrorsIn.slice();
const matchedErrors: TypeScriptError[] = [];
let anyFailures = false;
for (const error of actualErrors) {
const i = _.findIndex(expectedErrors, e => doErrorsOverlap(e, error));
if (i >= 0) {
const matchedError = expectedErrors.splice(i, 1)[0];
matchedErrors.push(matchedError);
log('matched errors:');
log(' expected: ' + matchedError.message);
log(' actual: ' + error.message);
const posMismatch = [];
const dStart = error.start - matchedError.start;
const dEnd = error.end - matchedError.end;
if (dStart) posMismatch.push(`start: ${dStart}`);
if (dEnd) posMismatch.push(`end: ${dEnd}`);
if (posMismatch.length) {
log(' mismatched error span: ' + posMismatch.join(', '));
}
let matchType = '';
let disagree = false;
if (error.message === matchedError.message) {
matchType = 'perfect';
} else if (!error.message) {
matchType = 'empty message';
} else if (error.message.includes(matchedError.message)) {
matchType = 'subset';
} else if (matchedError.message.includes('...')) {
const parts = matchedError.message.split('...');
const allMatch = _.every(parts, part => error.message.includes(part));
if (allMatch) {
matchType = 'match with ...';
} else {
disagree = true;
}
} else {
disagree = true;
}
if (disagree) {
log(' error messages could not be matched!');
} else {
log(' error messages match: ' + matchType);
}
} else {
const {line, start, end, message} = error;
fail(`Unexpected TypeScript error: ${message}`, {location: {line, start, end}});
anyFailures = true;
}
}
for (const error of expectedErrors) {
const {line, start, end, message} = error;
fail(`Expected TypeScript error was not produced: ${message}`, {location: {line, start, end}});
anyFailures = true;
}
if (expectedErrorsIn.length) {
log(`Matched ${matchedErrors.length}/${expectedErrorsIn.length} errors.`);
} else if (actualErrors.length === 0) {
log(`Code passed type checker.`);
}
return !anyFailures;
}
export function hasTypeAssertions(content: string) {
const lines = content.split('\n');
return !!_.find(lines, line => TYPE_ASSERTION_PAT.exec(line) || TWOSLASH_PAT.exec(line));
}
export function extractTypeAssertions(source: ts.SourceFile): TypeScriptTypeAssertion[] {
const assertions: TypeScriptTypeAssertion[] = [];
const lineStarts = source.getLineStarts();
const text = source.getFullText();
let appliesToPreviousLine = false;
let colForContinuation = null;
let commentPrefixForContinuation = null; // expected whitespace after the "//" for twoslash
for (let i = 0; i < lineStarts.length; i++) {
const lineText = text.slice(
lineStarts[i],
i === lineStarts.length - 1 ? undefined : lineStarts[i + 1] - 1,
);
const isFullLineComment = !!lineText.match(/^ *\/\//);
if (!isFullLineComment) {
appliesToPreviousLine = false;
colForContinuation = null;
commentPrefixForContinuation = null;
}
appliesToPreviousLine = isFullLineComment;
const commentPos = lineText.indexOf('//');
if (commentPos === -1) {
continue;
}
const pos = lineStarts[i] + commentPos;
const lineChar = source.getLineAndCharacterOfPosition(pos);
let {line} = lineChar;
const {character} = lineChar;
const commentText = lineText.slice(commentPos);
if (
appliesToPreviousLine &&
character === colForContinuation &&
(commentPrefixForContinuation === null ||
commentText.startsWith(commentPrefixForContinuation))
) {
assertions[assertions.length - 1].type += ' ' + commentText.slice(2).trim();
} else {
const type = matchAndExtract(TYPE_ASSERTION_PAT, commentText);
if (type) {
if (appliesToPreviousLine) line -= 1;
assertions.push({line, type});
colForContinuation = character;
} else {
const type = matchAndExtract(TWOSLASH_PAT, commentText);
if (type === null) continue;
if (!isFullLineComment) {
throw new Error('Twoslash assertion must be first on line.');
}
const twoslashOffset = commentText.indexOf('^?');
const commentIndex = pos; // position of the "//" in source file
const caretIndex = commentIndex + twoslashOffset;
// The position of interest is wherever the "^" (caret) is, but on the previous line.
const position = caretIndex - (lineStarts[line] - lineStarts[line - 1]);
const lineAndChar = source.getLineAndCharacterOfPosition(position);
// line and char aren't strictly needed but they make the tests much more readable.
assertions.push({position, type, ...lineAndChar});
colForContinuation = character;
commentPrefixForContinuation = commentText.slice(0, twoslashOffset);
}
}
}
return assertions;
}
// Use this to figure out which node you really want:
// const debugWalkNode = (node: ts.Node, indent = '') => {
// console.log(`${indent}${node.kind}: ${node.getText()}`);
// for (const child of node.getChildren()) {
// debugWalkNode(child, ' ' + indent);
// }
// };
/**
* Figure out which node we should check the type of.
*
* For a declaration (`const x = 12`) we want to check the 'x'.
* This winds up being the first identifier in the AST.
*/
export function getNodeForType(node: ts.Node): ts.Node {
const findIdentifier = (node: ts.Node): ts.Node | null => {
if (
node.kind === ts.SyntaxKind.Identifier ||
node.kind === ts.SyntaxKind.PropertyAccessExpression ||
node.kind === ts.SyntaxKind.CallExpression
) {
return node;
}
for (const child of node.getChildren()) {
const childId = findIdentifier(child);
if (childId) {
return childId;
}
}
return null;
};
return findIdentifier(node) || node;
}
export function typesMatch(expected: string, actual: string) {
if (expected.endsWith('!')) {
expected = expected.slice(0, -1);
}
if (expected === actual) {
return true;
}
const n = expected.length;
return expected.endsWith('...') && actual.slice(0, n - 3) === expected.slice(0, n - 3);
}
export function checkExpectTypeAssertions(
source: ts.SourceFile,
checker: ts.TypeChecker,
assertions: ExpectTypeAssertion[],
): boolean {
const numAssertions = assertions.length;
let matchedAssertions = 0;
let anyFailures = false;
// Match assertions to the first node that appears on the line they apply to.
const walkTree = (node: ts.Node) => {
if (
node.kind !== ts.SyntaxKind.SourceFile &&
node.kind !== ts.SyntaxKind.SyntaxList &&
node.kind !== ts.SyntaxKind.Block
) {
const pos = node.getEnd();
const {line} = source.getLineAndCharacterOfPosition(pos);
const assertionIndex = _.findIndex(assertions, {line});
if (assertionIndex >= 0) {
const assertion = assertions[assertionIndex];
const nodeForType = getNodeForType(node);
const type = checker.getTypeAtLocation(nodeForType);
const actualType = checker.typeToString(type, nodeForType);
if (!typesMatch(assertion.type, actualType)) {
const {character: start} = source.getLineAndCharacterOfPosition(nodeForType.getStart());
const {character: end} = source.getLineAndCharacterOfPosition(nodeForType.getEnd());
const testedText = node !== nodeForType ? ` (tested \`${nodeForType.getText()}\`)` : '';
fail(
`Failed type assertion for \`${node.getText()}\`${testedText}\n` +
` Expected: ${assertion.type}\n` +
` Actual: ${actualType}`,
{
location: {
line,
start,
end,
},
},
);
anyFailures = true;
} else {
log(`Type assertion match:`);
log(` Expected: ${assertion.type}`);
log(` Actual: ${actualType}`);
matchedAssertions++;
}
assertions.splice(assertionIndex, 1);
}
}
ts.forEachChild(node, child => {
walkTree(child);
});
};
walkTree(source);
if (assertions.length) {
fail('Unable to attach all assertions to nodes');
return false;
} else if (numAssertions) {
log(` ${matchedAssertions}/${numAssertions} type assertions matched.`);
}
return !anyFailures;
}
function getNodeAtPosition(sourceFile: ts.SourceFile, position: number): ts.Node | undefined {
let candidate: ts.Node | undefined = undefined;
ts.forEachChild(sourceFile, function iterate(node) {
const start = node.getStart();
const end = node.getEnd();
if (position >= start && position <= end) {
candidate = node;
ts.forEachChild(node, iterate);
}
});
return candidate;
}
/** Normalize "B | A" -> "A | B" */
export function sortUnions(type: string): string {
let level = 0;
const topLevelPipes: number[] = [];
for (let i = 0; i < type.length; i++) {
const c = type.charAt(i);
if (c === '|' && level === 0) {
topLevelPipes.push(i);
} else if (c === '{' || c === '(' || c === '<') {
level += 1;
} else if (c === '}' || c === '}' || c === '>') {
level -= 1;
}
// TODO: quotes
}
if (level !== 0 || topLevelPipes.length === 0) {
return type; // do no harm if we don't understand the type.
}
topLevelPipes.splice(0, 0, -1);
const parts = topLevelPipes.map((idx, i) => type.slice(idx + 1, topLevelPipes[i + 1]).trim());
return _.sortBy(parts).join(' | ');
}
/** This variant of split puts the rest of the string in the last group, rather than truncating. */
export function limitedSplit(txt: string, pattern: string | RegExp, limit: number): string[] {
const groups = txt.split(pattern);
if (groups.length <= limit) {
return groups;
}
if (typeof pattern === 'string') {
return [...groups.slice(0, limit - 1), groups.slice(limit - 1).join(pattern)];
}
const globalPat = new RegExp(pattern, 'g');
const out = [];
let pos = 0;
for (const m of txt.matchAll(globalPat)) {
if (out.length < limit - 1) {
out.push(txt.slice(pos, m.index));
if (m.index === undefined) {
throw new Error();
}
pos = m.index + m[0].length;
} else {
out.push(txt.slice(pos));
break;
}
}
return out;
}
// TODO: it's much easier to normalize actual based on the displayParts
// This isn't 100% correct if a type has a space in it, e.g. type T = "string literal"
// (exported for testing)
export const normalize = (input: string) => {
const isFunction = !!input.match(/^ *function /) || !!input.match(/=>/);
let name: string;
let type: string;
if (!isFunction) {
const parts = limitedSplit(input, /[:=]/, 2);
if (parts.length !== 2) {
// this might be a typo, e.g. missing the ":" or "=" in a type assertion.
return input;
}
[name, type] = parts;
} else {
name = 'n/a';
type = input;
}
const normType = sortUnions(type)
.replace(/[\n\r ]+/g, ' ')
.replace(/\( */g, '(')
.replace(/ *\)/, ')')
.trim();
return isFunction ? normType : `${name}: ${normType}`;
};
export function matchModuloWhitespace(actual: string, expected: string): boolean {
const normActual = normalize(actual);
const normExpected = normalize(expected);
return normActual === normExpected;
}
export function checkTwoslashAssertions(
source: ts.SourceFile,
languageService: ts.LanguageService,
assertions: TwoslashAssertion[],
): boolean {
let anyFailures = false;
let matchedAssertions = 0;
for (const assertion of assertions) {
const {position, type} = assertion;
if (position === -1) {
// special case for a twoslash assertion on line 1.
fail(`Twoslash assertion on first line: ${type}`);
continue;
}
const node = getNodeAtPosition(source, position);
if (!node) {
fail(`Unable to find matching node for twoslash assertion: ${type}`);
continue;
}
const qi = languageService.getQuickInfoAtPosition(source.fileName, node.getStart());
if (!qi?.displayParts) {
fail(`Unable to get quickinfo for twoslash assertion ${type}`);
continue;
}
const actual = qi.displayParts.map(dp => dp.text).join('');
if (!matchModuloWhitespace(actual, type)) {
const {line, character: start} = source.getLineAndCharacterOfPosition(node.getStart());
const {character: end} = source.getLineAndCharacterOfPosition(node.getEnd());
fail(
`Failed type assertion for \`${node.getText()}\`\n` +
` Expected: ${type}\n` +
` Actual: ${actual}`,
{
location: {
line,
start,
end,
},
},
);
anyFailures = true;
} else {
log(`Twoslash type assertion match:`);
log(` Expected: ${type}`);
log(` Actual: ${actual}`);
matchedAssertions++;
}
}
if (assertions.length) {
log(` ${matchedAssertions}/${assertions.length} twoslash type assertions matched.`);
}
return !anyFailures;
}
export function checkTypeAssertions(
source: ts.SourceFile,
checker: ts.TypeChecker,
languageService: ts.LanguageService,
assertions: TypeScriptTypeAssertion[],
) {
const [twoslashAssertions, expectTypeAssertions] = _.partition(assertions, isTwoslashAssertion);
let ok = true;
if (expectTypeAssertions.length) {
ok = ok && checkExpectTypeAssertions(source, checker, expectTypeAssertions);
}
if (twoslashAssertions.length) {
ok = ok && checkTwoslashAssertions(source, languageService, twoslashAssertions);
}
return ok;
}
// See https://github.com/JoshuaKGoldberg/eslint-plugin-expect-type/blob/a55413/src/rules/expect.ts#L506-L521
export function getLanguageServiceHost(program: ts.Program): ts.LanguageServiceHost {
return {
getCompilationSettings: () => program.getCompilerOptions(),
getCurrentDirectory: () => program.getCurrentDirectory(),
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
getScriptFileNames: () => program.getSourceFiles().map(sourceFile => sourceFile.fileName),
getScriptSnapshot: name =>
ts.ScriptSnapshot.fromString(program.getSourceFile(name)?.text ?? ''),
getScriptVersion: () => '1',
// NB: We can't check `program` for files, it won't contain valid files like package.json
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
directoryExists: ts.sys.directoryExists,
getDirectories: ts.sys.getDirectories,
};
}
export interface CheckTsResult {
passed: boolean;
failure?: {message: string; location?: FailureLocation};
output?: ExecErrorType;
// TODO: include more details about errors
}
function getCheckTsCacheKey(inSample: CodeSample, runCode: boolean) {
const {descriptor: _1, id: _2, sectionHeader: _3, sourceFile: _4, ...sample} = inSample;
const key = {
sample,
runCode,
// `config` has compiler options but these are already in `sample`
versions: {
typeScript: ts.version,
literateTs: VERSION,
nodeJs: process.version,
},
};
return tuple(sha256(stableJsonStringify(key)), key);
}
export const CACHE_DIR = findCacheDirectory({name: 'literate-ts', create: true})!;
if (!CACHE_DIR) {
throw new Error(`Unable to find cache dir`);
}
export async function checkTs(
sample: CodeSample,
runCode: boolean,
config: ConfigBundle,
options: {skipCache: boolean},
): Promise<CheckTsResult> {
const [key, cacheKey] = getCheckTsCacheKey(sample, runCode);
const tempFilePath = path.join(CACHE_DIR, `${key}.json`);
const hit = await fs.pathExists(tempFilePath);
if (hit && !options.skipCache) {
const result = await fs.readFile(tempFilePath, 'utf8');
const {key: _, ...out} = JSON.parse(result) as CheckTsResult & {key: unknown};
if (out.failure) {
const {message, ...context} = out.failure;
fail(message, context);
}
return out;
}
const result = await uncachedCheckTs(sample, runCode, config);
if (result.passed === false) {
result.failure = getLastFailReason() ?? undefined;
}
if (!options.skipCache) {
await fs.writeFile(tempFilePath, JSON.stringify({...result, key: cacheKey}), 'utf8');
}
return result;
}
function setupNodeModules(sample: CodeSample, sampleDir: string, options: ts.CompilerOptions) {
const nodeModulesPath = path.join(sampleDir, 'node_modules');
fs.emptyDirSync(nodeModulesPath);
const allModules = [...sample.nodeModules];
const sourceFileAbsPath = path.resolve(process.cwd(), sample.sourceFile);
const copiedModules = new Set();
let nodeModule;
while ((nodeModule = allModules.shift()) !== undefined) {
if (copiedModules.has(nodeModule)) {
continue; // already resolved
}
// Use the TypeScript API to resolve a location for this module relative to the source file.
// Copy it over and add any transitive dependencies to the list.
const {resolvedModule} = ts.resolveModuleName(nodeModule, sourceFileAbsPath, options, ts.sys);
if (!resolvedModule) {
fail(
`Could not find requested (or transitive) node_module ${nodeModule}. See logs for details.`,
);
return {passed: false};
}
const {resolvedFileName} = resolvedModule;
const pkg = readPackageUpSync({cwd: resolvedFileName});
if (!pkg) {
fail(`Could not find package.json from ${resolvedFileName}.`);
return {passed: false};
}
const nodeModuleDir = path.dirname(pkg.path);
const nodeModuleDirRelative = nodeModuleDir.replace(/^.*?\/node_modules\//, '');
const dest = path.join(nodeModulesPath, nodeModuleDirRelative);
fs.copySync(nodeModuleDir, dest);
log(`Copied ${nodeModuleDir} -> ${dest}`);
copiedModules.add(nodeModule);
for (const dep of Object.keys(pkg.packageJson.dependencies ?? {})) {
allModules.push(dep); // no need to de-dupe here, it's done at the top of the loop.
}
}
}
/** Verify that a TypeScript sample has the expected errors and no others. */
async function uncachedCheckTs(
sample: CodeSample,
runCode: boolean,
config: ConfigBundle,
): Promise<CheckTsResult> {
const {id, content} = sample;
const fileName = sample.targetFilename || id + (sample.isTSX ? '.tsx' : `.${sample.language}`);
const tsFile = writeTempFile(fileName, content);
const sampleDir = getTempDir();
const allFiles = [tsFile];
for (const auxFile of sample.auxiliaryFiles) {
const {filename, content} = auxFile;
const absPathFile = writeTempFile(filename, content);
allFiles.push(absPathFile);
}
const options: ts.CompilerOptions = {
...config.options,
...sample.tsOptions,
};
setupNodeModules(sample, sampleDir, options);
if (!_.isEmpty(sample.nodeModules)) {
options.typeRoots = [path.join(sampleDir, 'node_modules', '@types')];
}
const program = ts.createProgram(allFiles, options, config.host);
const source = program.getSourceFile(tsFile);
if (!source) {
fail('Unable to load TS source file');
return {passed: false};
}
let diagnostics = ts.getPreEmitDiagnostics(program);
if (runCode) {
const emitResult = program.emit();
diagnostics = diagnostics.concat(emitResult.diagnostics);
if (emitResult.emitSkipped) {
fail('Failed to emit JavaScript for TypeScript sample.');
return {passed: false};
}
}
const expectedErrors = extractExpectedErrors(content);
const numLines = content.split('\n').length;
const actualErrorsRaw: TypeScriptError[] = diagnostics
.map(diagnostic => {
const {line, character} = diagnostic.file!.getLineAndCharacterOfPosition(diagnostic.start!);
return {
line,
start: character,
end: character + diagnostic.length!,
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
};
})
// sometimes library errors show up as being past the end of the source.
.filter(err => err.line < numLines);
// There's no way to represent expected errors with identical spans.
// Usually these go from least- to most-specific, so just take the last one.
const actualErrors = _(actualErrorsRaw)
.groupBy(e => `${e.line}:${e.start}-${e.end}`)
.mapValues(errs => errs.slice(-1)[0])
.values()
.value();
let ok = checkMatchingErrors(expectedErrors, actualErrors);
if (hasTypeAssertions(content)) {
const checker = program.getTypeChecker();
const assertions = extractTypeAssertions(source);
if (assertions.length) {
const languageService = ts.createLanguageService(getLanguageServiceHost(program));
ok = ok && checkTypeAssertions(source, checker, languageService, assertions);
} else {
fail('Unable to extract type assertions');
}
}
if (!ok) {
log(content);
log(`tsconfig options: ${JSON.stringify(options)}`);
}
if (!runCode) return {passed: ok};
const jsFile = tsFile.replace(/\.ts$/, '.js');
if (!fs.existsSync(jsFile)) {
fail(`Did not produce JS output in expected place: ${jsFile}`);
return {passed: false};
}
const output = await runNode(jsFile);
return {passed: false, output};
}
export async function checkProgramListing(
sample: CodeSample,
config: ConfigBundle,
): Promise<CheckTsResult> {
// The bit before the `<pre>` is presumably prepended material.
// We need to convert this to JS and load it separately.
const [preamble, listingHTML] = sample.content.split('<pre ');
const listing = htmlToText('<pre ' + listingHTML);
let jsPreambleFile;
if (preamble) {
// Note: some copying of uncachedCheckTs() here.
const tsFile = writeTempFile('programlisting-premable.ts', preamble);
const options: ts.CompilerOptions = {
...config.options,
...sample.tsOptions,
};
const program = ts.createProgram([tsFile], options, config.host);
const source = program.getSourceFile(tsFile);
if (!source) {
fail('Unable to load TS source file');
return {passed: false};
}
let diagnostics = ts.getPreEmitDiagnostics(program);
const emitResult = program.emit();
diagnostics = diagnostics.concat(emitResult.diagnostics);
jsPreambleFile = tsFile.replace(/\.ts$/, '.js');
if (!fs.existsSync(jsPreambleFile)) {
fail(`Did not produce JS output in expected place: ${jsPreambleFile}`);
return {passed: false};
}
if (emitResult.emitSkipped) {
fail('Failed to emit JavaScript for TypeScript sample.');
return {passed: false};
}
}
const [rawInputs, expectedOutputs] = _.partition(listing.trim().split('\n'), line =>
line.startsWith('> '),
);
let inputs = rawInputs.map(input => input.slice(2));
if (jsPreambleFile) {
inputs = [`.load ${jsPreambleFile}`, '"--reset--"', ...inputs];
}
const inputStream = new Readable();
for (const input of inputs) {
inputStream.push(input); // the string you want
inputStream.push('\n');
}
inputStream.push(null); // indicates end-of-file basically - the end of the stream
const capturedOutputs: string[] = [];
class CaptureStream extends Writable {
_write(chunk: Buffer, enc: string, next: () => void) {
const s = chunk.toString();
capturedOutputs.push(s);
next();
}
}
const outputStream = new CaptureStream();
const server = repl.start({
input: inputStream,
output: outputStream,
});
const replOutput = await new Promise<string[]>((resolve, reject) => {
inputStream.push(null);
server.addListener('exit', () => {});
server.addListener('close', () => {
outputStream.end();
let finalOutputs = [];
for (const output of capturedOutputs) {
if (output === '> ') continue;
if (output.trim() === `'--reset--'`) {
finalOutputs = [];
} else {
finalOutputs.push(output.trim());
}
}
resolve(finalOutputs);
});
});
if (_.isEqual(replOutput.slice(0, -1), expectedOutputs) && replOutput.at(-1) === 'undefined') {
// special case to allow commands that console.log instead of evaluating to anything.
} else if (!_.isEqual(replOutput, expectedOutputs)) {
// TODO: report exact mismatched spans
let message = `Node session did not match program listing.`;
if (replOutput.length === expectedOutputs.length) {
for (const [expected, actual] of _.zip(expectedOutputs, replOutput)) {
message += `\n - ${expected}\n + ${actual}`;
}
} else {
const expected = expectedOutputs.join('\n');
const actual = replOutput.join('\n');
message += `\nExpected:\n${expected}\nActual:\n${actual}`;
}
fail(message);
// console.log('expected:', JSON.stringify(expectedOutputs));
// console.log('actual:', JSON.stringify(replOutput));
return {passed: false};
}
log('Node session matched program listing.');
return {passed: true};
}