Skip to content

If import is used in the file, prefer that import specifier over calculating new one #42224

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 7 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4378,7 +4378,8 @@ namespace ts {
getProjectReferenceRedirect: fileName => host.getProjectReferenceRedirect(fileName),
isSourceOfProjectReferenceRedirect: fileName => host.isSourceOfProjectReferenceRedirect(fileName),
fileExists: fileName => host.fileExists(fileName),
getCompilerOptions: () => host.getCompilerOptions()
getCompilerOptions: () => host.getCompilerOptions(),
getFileIncludeReasons: () => host.getFileIncludeReasons(),
} : undefined },
encounteredError: false,
visitedTypes: undefined,
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ namespace ts {
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
getProgramBuildInfo: returnUndefined,
getSourceFileFromReference: returnUndefined,
redirectTargetsMap: createMultiMap()
redirectTargetsMap: createMultiMap(),
getFileIncludeReasons: notImplemented,
};
emitFiles(
notImplementedResolver,
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ namespace ts.moduleSpecifiers {
const moduleSourceFile = getSourceFileOfNode(moduleSymbol.valueDeclaration || getNonAugmentationDeclaration(moduleSymbol));
const modulePaths = getAllModulePaths(importingSourceFile.path, moduleSourceFile.originalFileName, host);

const existingSpecifier = forEach(modulePaths, modulePath => forEach(
host.getFileIncludeReasons().get(toPath(modulePath.path, host.getCurrentDirectory(), info.getCanonicalFileName)),
reason => reason.kind === FileIncludeKind.Import && reason.file === importingSourceFile.path ?
getModuleNameStringLiteralAt(importingSourceFile, reason.index).text :
undefined
));
if (existingSpecifier) return [existingSpecifier];

const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile);
const importedFileIsInNodeModules = some(modulePaths, p => p.isInNodeModules);

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,7 @@ namespace ts {
getProgramBuildInfo: () => program.getProgramBuildInfo && program.getProgramBuildInfo(),
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
redirectTargetsMap,
getFileIncludeReasons: program.getFileIncludeReasons,
};
}

Expand Down Expand Up @@ -3973,7 +3974,8 @@ namespace ts {
return res;
}

function getModuleNameStringLiteralAt({ imports, moduleAugmentations }: SourceFile, index: number): StringLiteralLike {
/* @internal */
export function getModuleNameStringLiteralAt({ imports, moduleAugmentations }: SourceFile, index: number): StringLiteralLike {
if (index < imports.length) return imports[index];
let augIndex = imports.length;
for (const aug of moduleAugmentations) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7947,6 +7947,7 @@ namespace ts {
getProjectReferenceRedirect(fileName: string): string | undefined;
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
getCompilerOptions(): CompilerOptions;
getFileIncludeReasons(): MultiMap<Path, FileIncludeReason>;
}

// Note: this used to be deprecated in our public API, but is still used internally
Expand Down
1 change: 1 addition & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,7 @@ namespace ts {
isSourceOfProjectReferenceRedirect: fileName => program.isSourceOfProjectReferenceRedirect(fileName),
getCompilerOptions: () => program.getCompilerOptions(),
getNearestAncestorDirectoryWithPackageJson: maybeBind(host, host.getNearestAncestorDirectoryWithPackageJson),
getFileIncludeReasons: () => program.getFileIncludeReasons(),
};
}

Expand Down
39 changes: 39 additions & 0 deletions src/testRunner/unittests/tsbuild/declarationEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,44 @@ declare type MyNominal<T, Name extends string> = T & {
}),
commandLineArgs: ["--b", "/src/solution/tsconfig.json", "--verbose"]
});

verifyTsc({
scenario: "declarationEmit",
subScenario: "when declaration file used inferred type from referenced project",
fs: () => loadProjectFromFiles({
"/src/tsconfig.json": JSON.stringify({
compilerOptions: {
composite: true,
baseUrl: ".",
paths: { "@fluentui/*": ["packages/*/src"] }
}
}),
"/src/packages/pkg1/src/index.ts": Utils.dedent`
export interface IThing {
a: string;
}
export interface IThings {
thing1: IThing;
}`,
"/src/packages/pkg1/tsconfig.json": JSON.stringify({
extends: "../../tsconfig",
compilerOptions: { outDir: "lib" },
include: ["src"]
}),
"/src/packages/pkg2/src/index.ts": Utils.dedent`
import { IThings } from '@fluentui/pkg1';
export function fn4() {
const a: IThings = { thing1: { a: 'b' } };
return a.thing1;
}`,
"/src/packages/pkg2/tsconfig.json": JSON.stringify({
extends: "../../tsconfig",
compilerOptions: { outDir: "lib" },
include: ["src"],
references: [{ path: "../pkg1" }]
}),
}),
commandLineArgs: ["--b", "/src/packages/pkg2/tsconfig.json", "--verbose"]
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
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/packages/pkg1/src/index.ts]
export interface IThing {
a: string;
}
export interface IThings {
thing1: IThing;
}

//// [/src/packages/pkg1/tsconfig.json]
{"extends":"../../tsconfig","compilerOptions":{"outDir":"lib"},"include":["src"]}

//// [/src/packages/pkg2/src/index.ts]
import { IThings } from '@fluentui/pkg1';
export function fn4() {
const a: IThings = { thing1: { a: 'b' } };
return a.thing1;
}

//// [/src/packages/pkg2/tsconfig.json]
{"extends":"../../tsconfig","compilerOptions":{"outDir":"lib"},"include":["src"],"references":[{"path":"../pkg1"}]}

//// [/src/tsconfig.json]
{"compilerOptions":{"composite":true,"baseUrl":".","paths":{"@fluentui/*":["packages/*/src"]}}}



Output::
/lib/tsc --b /src/packages/pkg2/tsconfig.json --verbose
[12:00:00 AM] Projects in this build:
* src/packages/pkg1/tsconfig.json
* src/packages/pkg2/tsconfig.json

[12:00:00 AM] Project 'src/packages/pkg1/tsconfig.json' is out of date because output file 'src/packages/pkg1/lib/src/index.js' does not exist

[12:00:00 AM] Building project '/src/packages/pkg1/tsconfig.json'...

[12:00:00 AM] Project 'src/packages/pkg2/tsconfig.json' is out of date because output file 'src/packages/pkg2/lib/src/index.js' does not exist

[12:00:00 AM] Building project '/src/packages/pkg2/tsconfig.json'...

exitCode:: ExitStatus.Success


//// [/src/packages/pkg1/lib/src/index.d.ts]
export interface IThing {
a: string;
}
export interface IThings {
thing1: IThing;
}


//// [/src/packages/pkg1/lib/src/index.js]
"use strict";
exports.__esModule = true;


//// [/src/packages/pkg1/lib/tsconfig.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../../../lib/lib.d.ts": {
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
"affectsGlobalScope": true
},
"../src/index.ts": {
"version": "-2072077482-export interface IThing {\n a: string;\n}\nexport interface IThings {\n thing1: IThing;\n}",
"signature": "-5386205042-export interface IThing {\r\n a: string;\r\n}\r\nexport interface IThings {\r\n thing1: IThing;\r\n}\r\n",
"affectsGlobalScope": false
}
},
"options": {
"composite": true,
"baseUrl": "../../..",
"paths": {
"@fluentui/*": [
"packages/*/src"
]
},
"pathsBasePath": "/src",
"outDir": "./",
"configFilePath": "../tsconfig.json"
},
"referencedMap": {},
"exportedModulesMap": {},
"semanticDiagnosticsPerFile": [
"../../../../lib/lib.d.ts",
"../src/index.ts"
]
},
"version": "FakeTSVersion"
}

//// [/src/packages/pkg2/lib/src/index.d.ts]
export declare function fn4(): import("@fluentui/pkg1").IThing;


//// [/src/packages/pkg2/lib/src/index.js]
"use strict";
exports.__esModule = true;
exports.fn4 = void 0;
function fn4() {
var a = { thing1: { a: 'b' } };
return a.thing1;
}
exports.fn4 = fn4;


//// [/src/packages/pkg2/lib/tsconfig.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../../../lib/lib.d.ts": {
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
"affectsGlobalScope": true
},
"../../pkg1/lib/src/index.d.ts": {
"version": "-5386205042-export interface IThing {\r\n a: string;\r\n}\r\nexport interface IThings {\r\n thing1: IThing;\r\n}\r\n",
"signature": "-5386205042-export interface IThing {\r\n a: string;\r\n}\r\nexport interface IThings {\r\n thing1: IThing;\r\n}\r\n",
"affectsGlobalScope": false
},
"../src/index.ts": {
"version": "8515046367-import { IThings } from '@fluentui/pkg1';\nexport function fn4() {\n const a: IThings = { thing1: { a: 'b' } };\n return a.thing1;\n}",
"signature": "-9447422063-export declare function fn4(): import(\"@fluentui/pkg1\").IThing;\r\n",
"affectsGlobalScope": false
}
},
"options": {
"composite": true,
"baseUrl": "../../..",
"paths": {
"@fluentui/*": [
"packages/*/src"
]
},
"pathsBasePath": "/src",
"outDir": "./",
"configFilePath": "../tsconfig.json"
},
"referencedMap": {
"../src/index.ts": [
"../../pkg1/lib/src/index.d.ts"
]
},
"exportedModulesMap": {
"../src/index.ts": [
"../../pkg1/lib/src/index.d.ts"
]
},
"semanticDiagnosticsPerFile": [
"../../../../lib/lib.d.ts",
"../../pkg1/lib/src/index.d.ts",
"../src/index.ts"
]
},
"version": "FakeTSVersion"
}