Skip to content

When source file is redirected, set the prototype correctly in node factory #48862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5275,7 +5275,7 @@ namespace ts {
hasNoDefaultLib: boolean,
libReferences: readonly FileReference[]
) {
const node = baseFactory.createBaseSourceFileNode(SyntaxKind.SourceFile) as Mutable<SourceFile>;
const node = (source.redirectInfo ? Object.create(source.redirectInfo.redirectTarget) : baseFactory.createBaseSourceFileNode(SyntaxKind.SourceFile)) as Mutable<SourceFile>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is setting the prototype pretty typical in factory functions and whatnot @rbuckton?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally don't, but apparently we use Object.create in program.ts for redirected source files.

for (const p in source) {
if (p === "emitNode" || hasProperty(node, p) || !hasProperty(source, p)) continue;
(node as any)[p] = (source as any)[p];
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"unittests/tsc/incremental.ts",
"unittests/tsc/listFilesOnly.ts",
"unittests/tsc/projectReferences.ts",
"unittests/tsc/redirect.ts",
"unittests/tsc/runWithoutArgs.ts",
"unittests/tscWatch/consoleClearing.ts",
"unittests/tscWatch/emit.ts",
Expand Down
34 changes: 34 additions & 0 deletions src/testRunner/unittests/tsc/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace ts {
describe("unittests:: tsc:: redirect::", () => {
verifyTsc({
scenario: "redirect",
subScenario: "when redirecting ts file",
fs: () => loadProjectFromFiles({
"/src/project/tsconfig.json": JSON.stringify({
compilerOptions: {
outDir: "out"
},
include: [
"copy1/node_modules/target/*",
"copy2/node_modules/target/*",
]
}),
"/src/project/copy1/node_modules/target/index.ts": "export const a = 1;",
"/src/project/copy1/node_modules/target/import.ts": `import {} from "./";`,
"/src/project/copy1/node_modules/target/package.json": JSON.stringify({
name: "target",
version: "1.0.0",
main: "index.js",
}),
"/src/project/copy2/node_modules/target/index.ts": "export const a = 1;",
"/src/project/copy2/node_modules/target/import.ts": `import {} from "./";`,
"/src/project/copy2/node_modules/target/package.json": JSON.stringify({
name: "target",
version: "1.0.0",
main: "index.js",
}),
}),
commandLineArgs: ["-p", "src/project"],
});
});
}
68 changes: 68 additions & 0 deletions tests/baselines/reference/tsc/redirect/when-redirecting-ts-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Input::
//// [/lib/lib.d.ts]
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
declare const console: { log(msg: any): void; };

//// [/src/project/copy1/node_modules/target/import.ts]
import {} from "./";

//// [/src/project/copy1/node_modules/target/index.ts]
export const a = 1;

//// [/src/project/copy1/node_modules/target/package.json]
{"name":"target","version":"1.0.0","main":"index.js"}

//// [/src/project/copy2/node_modules/target/import.ts]
import {} from "./";

//// [/src/project/copy2/node_modules/target/index.ts]
export const a = 1;

//// [/src/project/copy2/node_modules/target/package.json]
{"name":"target","version":"1.0.0","main":"index.js"}

//// [/src/project/tsconfig.json]
{"compilerOptions":{"outDir":"out"},"include":["copy1/node_modules/target/*","copy2/node_modules/target/*"]}



Output::
/lib/tsc -p src/project
exitCode:: ExitStatus.Success


//// [/src/project/out/copy1/node_modules/target/import.js]
"use strict";
exports.__esModule = true;


//// [/src/project/out/copy1/node_modules/target/index.js]
"use strict";
exports.__esModule = true;
exports.a = void 0;
exports.a = 1;


//// [/src/project/out/copy2/node_modules/target/import.js]
"use strict";
exports.__esModule = true;


//// [/src/project/out/copy2/node_modules/target/index.js]
"use strict";
exports.__esModule = true;
exports.a = void 0;
exports.a = 1;