Skip to content

Commit fe94a21

Browse files
committed
Use same metho as importHelpers to add synthetic import
1 parent 8493ee8 commit fe94a21

File tree

4 files changed

+58
-62
lines changed

4 files changed

+58
-62
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,8 @@ namespace ts {
402402
*/
403403
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
404404
// Use explicit type list from tsconfig.json
405-
// jsxImportSource, if present and in use, creates implicit imports
406-
const implicitImport = getJSXRuntimeImport(getJSXImplicitImportBase(options), options);
407405
if (options.types) {
408-
return [...options.types, ...(implicitImport ? [implicitImport] : [])];
406+
return options.types;
409407
}
410408

411409
// Walk the primary type lookup locations
@@ -436,9 +434,6 @@ namespace ts {
436434
}
437435
}
438436
}
439-
if (implicitImport) {
440-
result.push(implicitImport);
441-
}
442437
return result;
443438
}
444439

src/compiler/program.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ namespace ts {
14941494
}
14951495
// try to verify results of module resolution
14961496
for (const { oldFile: oldSourceFile, newFile: newSourceFile } of modifiedSourceFiles) {
1497-
const moduleNames = getModuleNames(newSourceFile, options);
1497+
const moduleNames = getModuleNames(newSourceFile);
14981498
const resolutions = resolveModuleNamesReusingOldState(moduleNames, newSourceFile);
14991499
// ensure that module resolution results are still correct
15001500
const resolutionsChanged = hasChangesInResolutions(moduleNames, resolutions, oldSourceFile.resolvedModules, moduleResolutionIsEqualTo);
@@ -2201,6 +2201,15 @@ namespace ts {
22012201
: b.kind === SyntaxKind.StringLiteral && a.text === b.text;
22022202
}
22032203

2204+
function createSyntheticImport(text: string, file: SourceFile) {
2205+
const externalHelpersModuleReference = factory.createStringLiteral(text);
2206+
const importDecl = factory.createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, /*importClause*/ undefined, externalHelpersModuleReference);
2207+
addEmitFlags(importDecl, EmitFlags.NeverApplyImportHelper);
2208+
setParent(externalHelpersModuleReference, importDecl);
2209+
setParent(importDecl, file);
2210+
return externalHelpersModuleReference;
2211+
}
2212+
22042213
function collectExternalModuleReferences(file: SourceFile): void {
22052214
if (file.imports) {
22062215
return;
@@ -2216,16 +2225,18 @@ namespace ts {
22162225

22172226
// If we are importing helpers, we need to add a synthetic reference to resolve the
22182227
// helpers library.
2219-
if (options.importHelpers
2220-
&& (options.isolatedModules || isExternalModuleFile)
2228+
if ((options.isolatedModules || isExternalModuleFile)
22212229
&& !file.isDeclarationFile) {
2222-
// synthesize 'import "tslib"' declaration
2223-
const externalHelpersModuleReference = factory.createStringLiteral(externalHelpersModuleNameText);
2224-
const importDecl = factory.createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, /*importClause*/ undefined, externalHelpersModuleReference);
2225-
addEmitFlags(importDecl, EmitFlags.NeverApplyImportHelper);
2226-
setParent(externalHelpersModuleReference, importDecl);
2227-
setParent(importDecl, file);
2228-
imports = [externalHelpersModuleReference];
2230+
if (options.importHelpers) {
2231+
// synthesize 'import "tslib"' declaration
2232+
imports = [createSyntheticImport(externalHelpersModuleNameText, file)];
2233+
}
2234+
const jsxImport = getJSXRuntimeImport(getJSXImplicitImportBase(options, file), options);
2235+
if (jsxImport) {
2236+
// synthesize `import "base/jsx-runtime"` declaration
2237+
imports ||= [];
2238+
imports.push(createSyntheticImport(jsxImport, file));
2239+
}
22292240
}
22302241

22312242
for (const node of file.statements) {
@@ -2840,16 +2851,11 @@ namespace ts {
28402851
if (resolvedTypeReferenceDirective.isExternalLibraryImport) currentNodeModulesDepth--;
28412852
}
28422853
else {
2843-
// Don't issue an error when auto-inclusion lookup fails for the jsxImportSource (at this point)
2844-
// It may be provided by an ambient module in the compilation, instead.
2845-
// A usage of a JSX tag later should report that the module couldn't be resolved if it is not supplied.
2846-
if (refFile || typeReferenceDirective !== getJSXRuntimeImport(getJSXImplicitImportBase(options), options)) {
2847-
fileProcessingDiagnostics.add(createRefFileDiagnostic(
2848-
refFile,
2849-
Diagnostics.Cannot_find_type_definition_file_for_0,
2850-
typeReferenceDirective
2851-
));
2852-
}
2854+
fileProcessingDiagnostics.add(createRefFileDiagnostic(
2855+
refFile,
2856+
Diagnostics.Cannot_find_type_definition_file_for_0,
2857+
typeReferenceDirective
2858+
));
28532859
}
28542860

28552861
if (saveResolution) {
@@ -2896,9 +2902,9 @@ namespace ts {
28962902

28972903
function processImportedModules(file: SourceFile) {
28982904
collectExternalModuleReferences(file);
2899-
if (file.imports.length || file.moduleAugmentations.length || getJSXImplicitImportBase(options, file)) {
2905+
if (file.imports.length || file.moduleAugmentations.length) {
29002906
// Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
2901-
const moduleNames = getModuleNames(file, options);
2907+
const moduleNames = getModuleNames(file);
29022908
const resolutions = resolveModuleNamesReusingOldState(moduleNames, file);
29032909
Debug.assert(resolutions.length === moduleNames.length);
29042910
for (let i = 0; i < moduleNames.length; i++) {
@@ -3887,19 +3893,14 @@ namespace ts {
38873893
}
38883894
}
38893895

3890-
function getModuleNames(file: SourceFile, options: CompilerOptions): string[] {
3891-
const { imports, moduleAugmentations } = file;
3896+
function getModuleNames({ imports, moduleAugmentations }: SourceFile): string[] {
38923897
const res = imports.map(i => i.text);
38933898
for (const aug of moduleAugmentations) {
38943899
if (aug.kind === SyntaxKind.StringLiteral) {
38953900
res.push(aug.text);
38963901
}
38973902
// Do nothing if it's an Identifier; we don't need to do module resolution for `declare global`.
38983903
}
3899-
const jsxRuntimeImport = getJSXRuntimeImport(getJSXImplicitImportBase(options, file), options);
3900-
if (jsxRuntimeImport) {
3901-
res.push(jsxRuntimeImport);
3902-
}
39033904
return res;
39043905
}
39053906
}

tests/baselines/reference/tscWatch/incremental/jsxImportSource-option-changed-incremental.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ Program options: {"module":1,"jsx":4,"incremental":true,"jsxImportSource":"react
6464
Program structureReused: Not
6565
Program files::
6666
/a/lib/lib.d.ts
67-
/users/username/projects/project/index.tsx
6867
/users/username/projects/project/node_modules/react/jsx-runtime/index.d.ts
68+
/users/username/projects/project/index.tsx
6969

7070
Semantic diagnostics in builder refreshed for::
7171
/a/lib/lib.d.ts
72-
/users/username/projects/project/index.tsx
7372
/users/username/projects/project/node_modules/react/jsx-runtime/index.d.ts
73+
/users/username/projects/project/index.tsx
7474

7575
WatchedFiles::
7676

@@ -98,15 +98,15 @@ exports.App = App;
9898
"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; };",
9999
"affectsGlobalScope": true
100100
},
101-
"./index.tsx": {
102-
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
103-
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
104-
"affectsGlobalScope": false
105-
},
106101
"./node_modules/react/jsx-runtime/index.d.ts": {
107102
"version": "-35656056833-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propA?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
108103
"signature": "-35656056833-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propA?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
109104
"affectsGlobalScope": false
105+
},
106+
"./index.tsx": {
107+
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
108+
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
109+
"affectsGlobalScope": false
110110
}
111111
},
112112
"options": {
@@ -156,13 +156,13 @@ Program options: {"module":1,"jsx":4,"incremental":true,"jsxImportSource":"preac
156156
Program structureReused: Not
157157
Program files::
158158
/a/lib/lib.d.ts
159-
/users/username/projects/project/index.tsx
160159
/users/username/projects/project/node_modules/preact/jsx-runtime/index.d.ts
160+
/users/username/projects/project/index.tsx
161161

162162
Semantic diagnostics in builder refreshed for::
163163
/a/lib/lib.d.ts
164-
/users/username/projects/project/index.tsx
165164
/users/username/projects/project/node_modules/preact/jsx-runtime/index.d.ts
165+
/users/username/projects/project/index.tsx
166166

167167
WatchedFiles::
168168

@@ -190,15 +190,15 @@ exports.App = App;
190190
"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; };",
191191
"affectsGlobalScope": true
192192
},
193-
"./index.tsx": {
194-
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
195-
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
196-
"affectsGlobalScope": false
197-
},
198193
"./node_modules/preact/jsx-runtime/index.d.ts": {
199194
"version": "-17896129664-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propB?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
200195
"signature": "-17896129664-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propB?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
201196
"affectsGlobalScope": false
197+
},
198+
"./index.tsx": {
199+
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
200+
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
201+
"affectsGlobalScope": false
202202
}
203203
},
204204
"options": {

tests/baselines/reference/tscWatch/incremental/jsxImportSource-option-changed-watch.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ Program options: {"module":1,"jsx":4,"incremental":true,"jsxImportSource":"react
6969
Program structureReused: Not
7070
Program files::
7171
/a/lib/lib.d.ts
72-
/users/username/projects/project/index.tsx
7372
/users/username/projects/project/node_modules/react/jsx-runtime/index.d.ts
73+
/users/username/projects/project/index.tsx
7474

7575
Semantic diagnostics in builder refreshed for::
7676
/a/lib/lib.d.ts
77-
/users/username/projects/project/index.tsx
7877
/users/username/projects/project/node_modules/react/jsx-runtime/index.d.ts
78+
/users/username/projects/project/index.tsx
7979

8080
WatchedFiles::
8181
/users/username/projects/project/tsconfig.json:
@@ -117,15 +117,15 @@ exports.App = App;
117117
"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; };",
118118
"affectsGlobalScope": true
119119
},
120-
"./index.tsx": {
121-
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
122-
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
123-
"affectsGlobalScope": false
124-
},
125120
"./node_modules/react/jsx-runtime/index.d.ts": {
126121
"version": "-35656056833-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propA?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
127122
"signature": "-35656056833-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propA?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
128123
"affectsGlobalScope": false
124+
},
125+
"./index.tsx": {
126+
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
127+
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
128+
"affectsGlobalScope": false
129129
}
130130
},
131131
"options": {
@@ -178,13 +178,13 @@ Program options: {"module":1,"jsx":4,"incremental":true,"jsxImportSource":"preac
178178
Program structureReused: Not
179179
Program files::
180180
/a/lib/lib.d.ts
181-
/users/username/projects/project/index.tsx
182181
/users/username/projects/project/node_modules/preact/jsx-runtime/index.d.ts
182+
/users/username/projects/project/index.tsx
183183

184184
Semantic diagnostics in builder refreshed for::
185185
/a/lib/lib.d.ts
186-
/users/username/projects/project/index.tsx
187186
/users/username/projects/project/node_modules/preact/jsx-runtime/index.d.ts
187+
/users/username/projects/project/index.tsx
188188

189189
WatchedFiles::
190190
/users/username/projects/project/tsconfig.json:
@@ -226,15 +226,15 @@ exports.App = App;
226226
"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; };",
227227
"affectsGlobalScope": true
228228
},
229-
"./index.tsx": {
230-
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
231-
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
232-
"affectsGlobalScope": false
233-
},
234229
"./node_modules/preact/jsx-runtime/index.d.ts": {
235230
"version": "-17896129664-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propB?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
236231
"signature": "-17896129664-export namespace JSX {\n interface Element {}\n interface IntrinsicElements {\n div: {\n propB?: boolean;\n };\n }\n}\nexport function jsx(...args: any[]): void;\nexport function jsxs(...args: any[]): void;\nexport const Fragment: unique symbol;\n",
237232
"affectsGlobalScope": false
233+
},
234+
"./index.tsx": {
235+
"version": "-14760199789-export const App = () => <div propA={true}></div>;",
236+
"signature": "-17269688391-export declare const App: () => import(\"react/jsx-runtime\").JSX.Element;\n",
237+
"affectsGlobalScope": false
238238
}
239239
},
240240
"options": {

0 commit comments

Comments
 (0)