Skip to content

Commit 2fed590

Browse files
authored
Eliminate tests/lib/lib.d.ts, fix up fourslash to use real libs and defaults (#63056)
1 parent 46ec6af commit 2fed590

File tree

782 files changed

+25577
-34186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

782 files changed

+25577
-34186
lines changed

Herebyfile.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ export const watchLocal = task({
677677
dependencies: [localize, watchTsc, watchTsserver, watchServices, lssl, watchOtherOutputs, dts, watchSrc],
678678
});
679679

680-
const runtestsDeps = [tests, generateLibs].concat(cmdLineOptions.typecheck ? [dts] : []);
680+
const runtestsDeps = [tests, generateLibs, generateTypesMap].concat(cmdLineOptions.typecheck ? [dts] : []);
681681

682682
export const runTests = task({
683683
name: "runtests",

src/harness/fourslashImpl.ts

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import * as vpath from "./_namespaces/vpath.js";
99
import { LoggerWithInMemoryLogs } from "./tsserverLogger.js";
1010

1111
import ArrayOrSingle = FourSlashInterface.ArrayOrSingle;
12-
import {
13-
harnessSessionLibLocation,
14-
harnessTypingInstallerCacheLocation,
15-
} from "./harnessLanguageService.js";
12+
import { harnessTypingInstallerCacheLocation } from "./harnessLanguageService.js";
1613
import { ensureWatchablePath } from "./watchUtils.js";
1714

1815
export const enum FourSlashTestType {
@@ -105,7 +102,7 @@ const enum MetadataOptionNames {
105102
const fileMetadataNames = [MetadataOptionNames.fileName, MetadataOptionNames.emitThisFile, MetadataOptionNames.resolveReference, MetadataOptionNames.symlink];
106103

107104
function convertGlobalOptionsToCompilerOptions(globalOptions: Harness.TestCaseParser.CompilerSettings): ts.CompilerOptions {
108-
const settings: ts.CompilerOptions = { target: ts.ScriptTarget.ES5, newLine: ts.NewLineKind.CarriageReturnLineFeed };
105+
const settings: ts.CompilerOptions = { ...ts.getDefaultCompilerOptions(), jsx: undefined, newLine: ts.NewLineKind.CarriageReturnLineFeed };
109106
Harness.Compiler.setCompilerOptionsFromHarnessSetting(globalOptions, settings);
110107
return settings;
111108
}
@@ -367,11 +364,6 @@ export class TestState {
367364
}
368365
}
369366

370-
const libName = (name: string) =>
371-
this.testType !== FourSlashTestType.Server ?
372-
name :
373-
`${harnessSessionLibLocation}/${name}`;
374-
375367
let configParseResult: ts.ParsedCommandLine | undefined;
376368
if (configFileName) {
377369
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
@@ -422,24 +414,6 @@ export class TestState {
422414
const importedFilePath = this.basePath + "/" + importedFile.fileName;
423415
this.addMatchedInputFile(importedFilePath, exts);
424416
});
425-
426-
this.languageServiceAdapterHost.addScript(
427-
libName(Harness.Compiler.defaultLibFileName),
428-
Harness.Compiler.getDefaultLibrarySourceFile()!.text,
429-
/*isRootFile*/ false,
430-
);
431-
432-
compilationOptions.lib?.forEach(fileName => {
433-
const libFile = Harness.Compiler.getDefaultLibrarySourceFile(fileName);
434-
ts.Debug.assertIsDefined(libFile, `Could not find lib file '${fileName}'`);
435-
if (libFile) {
436-
this.languageServiceAdapterHost.addScript(
437-
libName(fileName),
438-
libFile.text,
439-
/*isRootFile*/ false,
440-
);
441-
}
442-
});
443417
}
444418
else {
445419
// resolveReference file-option is not specified then do not resolve any files and include all inputFiles
@@ -452,24 +426,6 @@ export class TestState {
452426
this.languageServiceAdapterHost.addScript(fileName, file, isRootFile);
453427
}
454428
});
455-
456-
if (!compilationOptions.noLib) {
457-
const seen = new Set<string>();
458-
const addSourceFile = (fileName: string) => {
459-
if (seen.has(fileName)) return;
460-
seen.add(fileName);
461-
const libFile = Harness.Compiler.getDefaultLibrarySourceFile(fileName);
462-
ts.Debug.assertIsDefined(libFile, `Could not find lib file '${fileName}'`);
463-
this.languageServiceAdapterHost.addScript(libName(fileName), libFile.text, /*isRootFile*/ false);
464-
if (!ts.some(libFile.libReferenceDirectives)) return;
465-
for (const directive of libFile.libReferenceDirectives) {
466-
addSourceFile(`lib.${directive.fileName}.d.ts`);
467-
}
468-
};
469-
470-
addSourceFile(Harness.Compiler.defaultLibFileName);
471-
compilationOptions.lib?.forEach(addSourceFile);
472-
}
473429
}
474430

475431
for (const file of testData.files) {
@@ -557,7 +513,13 @@ export class TestState {
557513
}
558514
private tryGetFileContent(fileName: string): string | undefined {
559515
const script = this.languageServiceAdapterHost.getScriptInfo(fileName);
560-
return script && script.content;
516+
if (script) return script.content;
517+
try {
518+
return this.languageServiceAdapterHost.vfs.readFileSync(fileName, "utf8");
519+
}
520+
catch {
521+
return undefined;
522+
}
561523
}
562524

563525
// Entry points from fourslash.ts
@@ -4870,18 +4832,6 @@ function parseTestData(basePath: string, contents: string, fileName: string): Fo
48704832
}
48714833
}
48724834

4873-
// @Filename is the only directive that can be used in a test that contains tsconfig.json file.
4874-
const config = ts.find(files, isConfig);
4875-
if (config) {
4876-
let directive = getNonFileNameOptionInFileList(files);
4877-
if (!directive) {
4878-
directive = getNonFileNameOptionInObject(globalOptions);
4879-
}
4880-
if (directive) {
4881-
throw Error(`It is not allowed to use ${config.fileName} along with directive '${directive}'`);
4882-
}
4883-
}
4884-
48854835
return {
48864836
markerPositions,
48874837
markers,
@@ -4896,24 +4846,6 @@ function isConfig(file: FourSlashFile): boolean {
48964846
return Harness.getConfigNameFromFileName(file.fileName) !== undefined;
48974847
}
48984848

4899-
function getNonFileNameOptionInFileList(files: FourSlashFile[]): string | undefined {
4900-
return ts.forEach(files, f => getNonFileNameOptionInObject(f.fileOptions));
4901-
}
4902-
4903-
function getNonFileNameOptionInObject(optionObject: { [s: string]: string; }): string | undefined {
4904-
for (const option in optionObject) {
4905-
switch (option) {
4906-
case MetadataOptionNames.fileName:
4907-
case MetadataOptionNames.baselineFile:
4908-
case MetadataOptionNames.emitThisFile:
4909-
break;
4910-
default:
4911-
return option;
4912-
}
4913-
}
4914-
return undefined;
4915-
}
4916-
49174849
const enum State {
49184850
none,
49194851
inSlashStarMarker,

src/harness/harnessIO.ts

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -229,48 +229,6 @@ export namespace Compiler {
229229
return result;
230230
}
231231

232-
export const defaultLibFileName = "lib.d.ts";
233-
export const es2015DefaultLibFileName = "lib.es2015.d.ts";
234-
235-
// Cache of lib files from "built/local"
236-
export let libFileNameSourceFileMap: Map<string, { file: ts.SourceFile; stringified: string; }> | undefined;
237-
238-
export function getDefaultLibrarySourceFile(fileName: string = defaultLibFileName): ts.SourceFile | undefined {
239-
if (!isDefaultLibraryFile(fileName)) {
240-
return undefined;
241-
}
242-
243-
if (!libFileNameSourceFileMap) {
244-
const file = createSourceFileAndAssertInvariants(defaultLibFileName, IO.readFile(libFolder + "lib.es5.d.ts")!, /*languageVersion*/ ts.ScriptTarget.Latest);
245-
libFileNameSourceFileMap = new Map(Object.entries({
246-
[defaultLibFileName]: { file, stringified: JSON.stringify(file.text) },
247-
}));
248-
}
249-
250-
let sourceFile = libFileNameSourceFileMap.get(fileName);
251-
if (!sourceFile) {
252-
const file = createSourceFileAndAssertInvariants(fileName, IO.readFile(libFolder + fileName)!, ts.ScriptTarget.Latest);
253-
sourceFile = { file, stringified: JSON.stringify(file.text) };
254-
libFileNameSourceFileMap.set(fileName, sourceFile);
255-
}
256-
return sourceFile.file;
257-
}
258-
259-
export function getDefaultLibFileName(options: ts.CompilerOptions): string {
260-
switch (ts.getEmitScriptTarget(options)) {
261-
case ts.ScriptTarget.ESNext:
262-
case ts.ScriptTarget.ES2017:
263-
return "lib.es2017.d.ts";
264-
case ts.ScriptTarget.ES2016:
265-
return "lib.es2016.d.ts";
266-
case ts.ScriptTarget.ES2015:
267-
return es2015DefaultLibFileName;
268-
269-
default:
270-
return defaultLibFileName;
271-
}
272-
}
273-
274232
// Cache these between executions so we don't have to re-parse them for every test
275233
export const fourslashFileName = "fourslash.ts";
276234
export let fourslashSourceFile: ts.SourceFile;
@@ -281,7 +239,6 @@ export namespace Compiler {
281239

282240
interface HarnessOptions {
283241
useCaseSensitiveFileNames?: boolean;
284-
includeBuiltFile?: string;
285242
baselineFile?: string;
286243
libFiles?: string;
287244
noTypesAndSymbols?: boolean;
@@ -293,7 +250,6 @@ export namespace Compiler {
293250
{ name: "allowNonTsExtensions", type: "boolean", defaultValueDescription: false },
294251
{ name: "useCaseSensitiveFileNames", type: "boolean", defaultValueDescription: false },
295252
{ name: "baselineFile", type: "string" },
296-
{ name: "includeBuiltFile", type: "string" },
297253
{ name: "fileName", type: "string" },
298254
{ name: "libFiles", type: "string" },
299255
{ name: "noErrorTruncation", type: "boolean", defaultValueDescription: false },
@@ -412,19 +368,9 @@ export namespace Compiler {
412368
.map(file => options.configFile ? ts.getNormalizedAbsolutePath(file.unitName, currentDirectory) : file.unitName)
413369
.filter(fileName => !ts.fileExtensionIs(fileName, ts.Extension.Json));
414370

415-
// Files from built\local that are requested by test "@includeBuiltFiles" to be in the context.
416-
// Treat them as library files, so include them in build, but not in baselines.
417-
if (options.includeBuiltFile) {
418-
programFileNames.push(vpath.combine(vfs.builtFolder, options.includeBuiltFile));
419-
}
420-
421371
// Files from tests\lib that are requested by "@libFiles"
422372
if (options.libFiles) {
423373
for (const fileName of options.libFiles.split(",")) {
424-
if (fileName === "lib.d.ts" && !options.noLib) {
425-
// Hack from Corsa.
426-
continue;
427-
}
428374
programFileNames.push(vpath.combine(vfs.testLibFolder, fileName));
429375
}
430376
}

0 commit comments

Comments
 (0)