Skip to content

Commit

Permalink
Add new option "libReplacement" (microsoft#60829)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Jan 24, 2025
1 parent c5058f0 commit 220706e
Show file tree
Hide file tree
Showing 49 changed files with 1,230 additions and 71 deletions.
8 changes: 8 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,14 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
affectsBuildInfo: true,
affectsSemanticDiagnostics: true,
},
{
name: "libReplacement",
type: "boolean",
affectsProgramStructure: true,
category: Diagnostics.Language_and_Environment,
description: Diagnostics.Enable_lib_replacement,
defaultValueDescription: true,
},

// Strict Type Checks
{
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6502,6 +6502,10 @@
"category": "Error",
"code": 6807
},
"Enable lib replacement.": {
"category": "Message",
"code": 6808
},

"one of:": {
"category": "Message",
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3937,6 +3937,17 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
const existing = resolvedLibProcessing?.get(libFileName);
if (existing) return existing;

if (options.libReplacement === false) {
const result: LibResolution = {
resolution: {
resolvedModule: undefined,
},
actual: combinePaths(defaultLibraryPath, libFileName),
};
(resolvedLibProcessing ??= new Map()).set(libFileName, result);
return result;
}

if (structureIsReused !== StructureIsReused.Not && oldProgram && !hasInvalidatedLibResolutions(libFileName)) {
const oldResolution = oldProgram.resolvedLibReferences?.get(libFileName);
if (oldResolution) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7411,6 +7411,7 @@ export interface CompilerOptions {
/** @deprecated */
keyofStringsOnly?: boolean;
lib?: string[];
libReplacement?: boolean;
/** @internal */ listEmittedFiles?: boolean;
/** @internal */ listFiles?: boolean;
/** @internal */ explainFiles?: boolean;
Expand Down
11 changes: 7 additions & 4 deletions src/testRunner/unittests/helpers/libraryResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ function getSysForLibResolution(libRedirection?: boolean, forTsserver?: boolean)
/// <reference lib="es5"/>
`,
"/home/src/workspace/projects/project1/tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, typeRoots: ["./typeroot1"], lib: ["es5", "dom"], traceResolution: true },
compilerOptions: { composite: true, typeRoots: ["./typeroot1"], lib: ["es5", "dom"], traceResolution: true, libReplacement: libRedirection },
}),
"/home/src/workspace/projects/project1/typeroot1/sometype/index.d.ts": `export type TheNum = "type1";`,
"/home/src/workspace/projects/project2/utils.d.ts": `export const y = 10;`,
"/home/src/workspace/projects/project2/index.ts": `export const y = 10`,
"/home/src/workspace/projects/project2/tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, lib: ["es5", "dom"], traceResolution: true },
compilerOptions: { composite: true, lib: ["es5", "dom"], traceResolution: true, libReplacement: libRedirection },
}),
"/home/src/workspace/projects/project3/utils.d.ts": `export const y = 10;`,
"/home/src/workspace/projects/project3/index.ts": `export const z = 10`,
"/home/src/workspace/projects/project3/tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, lib: ["es5", "dom"], traceResolution: true },
compilerOptions: { composite: true, lib: ["es5", "dom"], traceResolution: true, libReplacement: libRedirection },
}),
"/home/src/workspace/projects/project4/utils.d.ts": `export const y = 10;`,
"/home/src/workspace/projects/project4/index.ts": `export const z = 10`,
"/home/src/workspace/projects/project4/tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, lib: ["esnext", "dom", "webworker"], traceResolution: true },
compilerOptions: { composite: true, lib: ["esnext", "dom", "webworker"], traceResolution: true, libReplacement: libRedirection },
}),
[getTypeScriptLibTestLocation("dom")]: "interface DOMInterface { }",
[getTypeScriptLibTestLocation("webworker")]: "interface WebWorkerInterface { }",
Expand Down Expand Up @@ -71,6 +71,7 @@ function getLibResolutionEditOptions(
typeRoots: ["./typeroot1", "./typeroot2"],
lib: ["es5", "dom"],
traceResolution: true,
libReplacement: true,
},
}),
),
Expand All @@ -90,6 +91,7 @@ function getLibResolutionEditOptions(
typeRoots: ["./typeroot1"],
lib: ["es5", "dom"],
traceResolution: true,
libReplacement: true,
},
}),
);
Expand Down Expand Up @@ -235,6 +237,7 @@ export function getSysForLibResolutionUnknown(): TestServerHost {
compilerOptions: {
composite: true,
traceResolution: true,
libReplacement: true,
},
}),
[getTypeScriptLibTestLocation("webworker")]: "interface WebWorkerInterface { }",
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7027,6 +7027,7 @@ declare namespace ts {
/** @deprecated */
keyofStringsOnly?: boolean;
lib?: string[];
libReplacement?: boolean;
locale?: string;
mapRoot?: string;
maxNodeModuleJsDepth?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"jsx": "react", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["es5","es2015.promise"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["es5","es2015.core"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"libReplacement": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit 220706e

Please sign in to comment.