Skip to content

Commit c768b05

Browse files
committed
Rewrite PR to generate list of libs, export new methods
1 parent 1577535 commit c768b05

File tree

5 files changed

+85
-23
lines changed

5 files changed

+85
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tests/baselines/reference/testresults.tap
1919
tests/services/baselines/prototyping/local/*
2020
tests/services/browser/typescriptServices.js
2121
src/harness/*.js
22+
src/compiler/libs.generated.ts
2223
src/compiler/diagnosticInformationMap.generated.ts
2324
src/compiler/diagnosticMessages.generated.json
2425
src/parser/diagnosticInformationMap.generated.ts

Herebyfile.mjs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@ export const buildScripts = task({
3535
run: () => buildProject("scripts")
3636
});
3737

38+
const libsJson = "src/lib/libs.json";
39+
const libsGenerated = "src/compiler/libs.generated.ts";
40+
3841
const libs = memoize(() => {
3942
/** @type {{ libs: string[]; paths: Record<string, string | undefined>; }} */
40-
const libraries = readJson("./src/lib/libs.json");
43+
const libraries = readJson(libsJson);
4144
const libs = libraries.libs.map(lib => {
4245
const relativeSources = ["header.d.ts", lib + ".d.ts"];
43-
const relativeTarget = libraries.paths && libraries.paths[lib] || ("lib." + lib + ".d.ts");
46+
const target = libraries.paths && libraries.paths[lib] || ("lib." + lib + ".d.ts");
4447
const sources = relativeSources.map(s => path.posix.join("src/lib", s));
45-
const target = `built/local/${relativeTarget}`;
4648
return { target, sources };
4749
});
4850
return libs;
4951
});
5052

51-
52-
export const generateLibs = task({
53+
export const lib = task({
5354
name: "lib",
5455
description: "Builds the library targets",
5556
run: async () => {
@@ -62,11 +63,41 @@ export const generateLibs = task({
6263
output += "\n" + contents.replace(/\r\n/g, "\n");
6364
}
6465

65-
await fs.promises.writeFile(lib.target, output);
66+
await fs.promises.writeFile(path.join("./built/local", lib.target), output);
67+
}
68+
},
69+
});
70+
71+
export const generateLibs = task({
72+
name: "generate-lib",
73+
description: "Builds the library targets",
74+
run: async () => {
75+
const libNames = libs().map(lib => lib.target).sort();
76+
const result = [
77+
"// <auto-generated />",
78+
`// generated from '${libsJson}'`,
79+
"",
80+
"/** @internal */",
81+
"export const allLibFiles: readonly string[] = [",
82+
];
83+
84+
for (const name of libNames) {
85+
result.push(` ${JSON.stringify(name)},`);
6686
}
87+
88+
result.push("];");
89+
90+
await fs.promises.writeFile(libsGenerated, result.join("\r\n"));
6791
},
6892
});
6993

94+
const cleanLib = task({
95+
name: "clean-lib",
96+
description: "Cleans generated lib files in src.",
97+
hiddenFromTaskList: true,
98+
run: () => del([libsGenerated]),
99+
});
100+
70101

71102
const diagnosticInformationMapTs = "src/compiler/diagnosticInformationMap.generated.ts";
72103
const diagnosticMessagesJson = "src/compiler/diagnosticMessages.json";
@@ -82,11 +113,16 @@ export const generateDiagnostics = task({
82113

83114
const cleanDiagnostics = task({
84115
name: "clean-diagnostics",
85-
description: "Generates a diagnostic file in TypeScript based on an input JSON file",
116+
description: "Cleans generated diagnostic files in src.",
86117
hiddenFromTaskList: true,
87118
run: () => del([diagnosticInformationMapTs, diagnosticMessagesGeneratedJson]),
88119
});
89120

121+
export const generate = task({
122+
name: "generate",
123+
description: "Generate code for src.",
124+
dependencies: [generateLibs, generateDiagnostics],
125+
});
90126

91127
// Localize diagnostics
92128
/**
@@ -119,15 +155,15 @@ const localize = task({
119155
export const buildSrc = task({
120156
name: "build-src",
121157
description: "Builds the src project (all code)",
122-
dependencies: [generateDiagnostics],
158+
dependencies: [generate],
123159
run: () => buildProject("src"),
124160
});
125161

126162
export const watchSrc = task({
127163
name: "watch-src",
128164
description: "Watches the src project (all code)",
129165
hiddenFromTaskList: true,
130-
dependencies: [generateDiagnostics],
166+
dependencies: [generate],
131167
run: () => watchProject("src"),
132168
});
133169

@@ -341,25 +377,25 @@ function entrypointBuildTask(options) {
341377
const { main: tsc, watch: watchTsc } = entrypointBuildTask({
342378
name: "tsc",
343379
description: "Builds the command-line compiler",
344-
buildDeps: [generateDiagnostics],
380+
buildDeps: [generate],
345381
project: "src/tsc",
346382
srcEntrypoint: "./src/tsc/tsc.ts",
347383
builtEntrypoint: "./built/local/tsc/tsc.js",
348384
output: "./built/local/tsc.js",
349-
mainDeps: [generateLibs],
385+
mainDeps: [lib],
350386
});
351387
export { tsc, watchTsc };
352388

353389

354390
const { main: services, build: buildServices, watch: watchServices } = entrypointBuildTask({
355391
name: "services",
356392
description: "Builds the typescript.js library",
357-
buildDeps: [generateDiagnostics],
393+
buildDeps: [generate],
358394
project: "src/typescript",
359395
srcEntrypoint: "./src/typescript/typescript.ts",
360396
builtEntrypoint: "./built/local/typescript/typescript.js",
361397
output: "./built/local/typescript.js",
362-
mainDeps: [generateLibs],
398+
mainDeps: [lib],
363399
bundlerOptions: { exportIsTsObject: true },
364400
});
365401
export { services, watchServices };
@@ -379,12 +415,12 @@ export const dtsServices = task({
379415
const { main: tsserver, watch: watchTsserver } = entrypointBuildTask({
380416
name: "tsserver",
381417
description: "Builds the language server",
382-
buildDeps: [generateDiagnostics],
418+
buildDeps: [generate],
383419
project: "src/tsserver",
384420
srcEntrypoint: "./src/tsserver/server.ts",
385421
builtEntrypoint: "./built/local/tsserver/server.js",
386422
output: "./built/local/tsserver.js",
387-
mainDeps: [generateLibs],
423+
mainDeps: [lib],
388424
});
389425
export { tsserver, watchTsserver };
390426

@@ -407,12 +443,12 @@ export const watchMin = task({
407443
const { main: lssl, build: buildLssl, watch: watchLssl } = entrypointBuildTask({
408444
name: "lssl",
409445
description: "Builds language service server library",
410-
buildDeps: [generateDiagnostics],
446+
buildDeps: [generate],
411447
project: "src/tsserverlibrary",
412448
srcEntrypoint: "./src/tsserverlibrary/tsserverlibrary.ts",
413449
builtEntrypoint: "./built/local/tsserverlibrary/tsserverlibrary.js",
414450
output: "./built/local/tsserverlibrary.js",
415-
mainDeps: [generateLibs],
451+
mainDeps: [lib],
416452
bundlerOptions: { exportIsTsObject: true },
417453
});
418454
export { lssl, watchLssl };
@@ -439,12 +475,12 @@ const watchTestsEmitter = new EventEmitter();
439475
const { main: tests, watch: watchTests } = entrypointBuildTask({
440476
name: "tests",
441477
description: "Builds the test infrastructure",
442-
buildDeps: [generateDiagnostics],
478+
buildDeps: [generate],
443479
project: "src/testRunner",
444480
srcEntrypoint: "./src/testRunner/_namespaces/Harness.ts",
445481
builtEntrypoint: "./built/local/testRunner/runner.js",
446482
output: testRunner,
447-
mainDeps: [generateLibs],
483+
mainDeps: [lib],
448484
bundlerOptions: {
449485
// Ensure we never drop any dead code, which might be helpful while debugging.
450486
treeShaking: false,
@@ -496,7 +532,7 @@ const { main: cancellationToken, watch: watchCancellationToken } = entrypointBui
496532

497533
const { main: typingsInstaller, watch: watchTypingsInstaller } = entrypointBuildTask({
498534
name: "typings-installer",
499-
buildDeps: [generateDiagnostics],
535+
buildDeps: [generate],
500536
project: "src/typingsInstaller",
501537
srcEntrypoint: "./src/typingsInstaller/nodeTypingsInstaller.ts",
502538
builtEntrypoint: "./built/local/typingsInstaller/nodeTypingsInstaller.js",
@@ -530,7 +566,7 @@ export const generateTypesMap = task({
530566
const builtLocalDiagnosticMessagesGeneratedJson = "built/local/diagnosticMessages.generated.json";
531567
const copyBuiltLocalDiagnosticMessages = task({
532568
name: "copy-built-local-diagnostic-messages",
533-
dependencies: [generateDiagnostics],
569+
dependencies: [generate],
534570
run: async () => {
535571
const contents = await fs.promises.readFile(diagnosticMessagesGeneratedJson, "utf-8");
536572
JSON.parse(contents); // Validates that the JSON parses.
@@ -566,7 +602,7 @@ export const watchLocal = task({
566602
dependencies: [localize, watchTsc, watchTsserver, watchServices, watchLssl, watchOtherOutputs, dts, watchSrc],
567603
});
568604

569-
const runtestsDeps = [tests, generateLibs].concat(cmdLineOptions.typecheck ? [dts] : []);
605+
const runtestsDeps = [tests, lib].concat(cmdLineOptions.typecheck ? [dts] : []);
570606

571607
export const runTests = task({
572608
name: "runtests",
@@ -852,7 +888,7 @@ export const cleanBuilt = task({
852888
export const clean = task({
853889
name: "clean",
854890
description: "Cleans build outputs",
855-
dependencies: [cleanBuilt, cleanDiagnostics],
891+
dependencies: [cleanBuilt, cleanDiagnostics, cleanLib],
856892
});
857893

858894
export const configureNightly = task({

src/compiler/_namespaces/ts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from "../types";
1111
export * from "../sys";
1212
export * from "../path";
1313
export * from "../diagnosticInformationMap.generated";
14+
export * from "../libs.generated";
1415
export * from "../scanner";
1516
export * from "../utilitiesPublic";
1617
export * from "../utilities";

src/compiler/commandLineParser.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ const libEntries: [string, string][] = [
232232
*/
233233
export const libs = libEntries.map(entry => entry[0]);
234234

235+
/**
236+
* Gets the list of valid "lib" names, as used in compiler options or reference directives.
237+
*/
238+
export function getLibs(): readonly string[] {
239+
return libs.slice();
240+
}
241+
235242
/**
236243
* A map of lib names to lib files. This map is used both for parsing the "lib" command line
237244
* option as well as for resolving lib reference directives.
@@ -240,6 +247,13 @@ export const libs = libEntries.map(entry => entry[0]);
240247
*/
241248
export const libMap = new Map(libEntries);
242249

250+
/**
251+
* Maps a lib name to its filename in the TypeScript package.
252+
*/
253+
export function getLibFileName(libName: string): string | undefined {
254+
return libMap.get(libName);
255+
}
256+
243257
// Watch related options
244258
/** @internal */
245259
export const optionsForWatch: CommandLineOption[] = [

src/compiler/utilitiesPublic.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
__String,
33
AccessExpression,
44
AccessorDeclaration,
5+
allLibFiles,
56
ArrayBindingElement,
67
ArrayBindingOrAssignmentElement,
78
ArrayBindingOrAssignmentPattern,
@@ -291,6 +292,15 @@ export function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics:
291292
return sortAndDeduplicate<T>(diagnostics, compareDiagnostics);
292293
}
293294

295+
/**
296+
* Gets a full list of all lib files included with this build of TypeScript.
297+
* This list includes all files referenced by {@link getLibs} and {@link getLibFileName},
298+
* as well as the "default" lib file names returned by {@link getDefaultLibFileName}.
299+
*/
300+
export function getAllLibFileNames(): readonly string[] {
301+
return allLibFiles.slice();
302+
}
303+
294304
export function getDefaultLibFileName(options: CompilerOptions): string {
295305
switch (getEmitScriptTarget(options)) {
296306
case ScriptTarget.ESNext:

0 commit comments

Comments
 (0)