Skip to content

Commit 0056761

Browse files
authored
Reuse existing module specifiers in js declaration emit (#52089)
1 parent 11bc7b7 commit 0056761

9 files changed

+202
-19
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8999,25 +8999,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
89998999
// Is bound into file.symbol.globalExports instead, which we don't currently traverse
90009000
addResult(factory.createNamespaceExportDeclaration(idText((node as NamespaceExportDeclaration).name)), ModifierFlags.None);
90019001
break;
9002-
case SyntaxKind.ImportClause:
9002+
case SyntaxKind.ImportClause: {
9003+
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
9004+
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as ImportClause).parent.moduleSpecifier;
90039005
addResult(factory.createImportDeclaration(
90049006
/*modifiers*/ undefined,
90059007
factory.createImportClause(/*isTypeOnly*/ false, factory.createIdentifier(localName), /*namedBindings*/ undefined),
9006-
// We use `target.parent || target` below as `target.parent` is unset when the target is a module which has been export assigned
9007-
// And then made into a default by the `esModuleInterop` or `allowSyntheticDefaultImports` flag
9008-
// In such cases, the `target` refers to the module itself already
9009-
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
9010-
/*assertClause*/ undefined
9008+
specifier,
9009+
(node as ImportClause).parent.assertClause
90119010
), ModifierFlags.None);
90129011
break;
9013-
case SyntaxKind.NamespaceImport:
9012+
}
9013+
case SyntaxKind.NamespaceImport: {
9014+
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
9015+
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as NamespaceImport).parent.parent.moduleSpecifier;
90149016
addResult(factory.createImportDeclaration(
90159017
/*modifiers*/ undefined,
90169018
factory.createImportClause(/*isTypeOnly*/ false, /*importClause*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))),
9017-
factory.createStringLiteral(getSpecifierForModuleSymbol(target, context)),
9018-
/*assertClause*/ undefined
9019+
specifier,
9020+
(node as NamespaceImport).parent.parent.assertClause
90199021
), ModifierFlags.None);
90209022
break;
9023+
}
90219024
case SyntaxKind.NamespaceExport:
90229025
addResult(factory.createExportDeclaration(
90239026
/*modifiers*/ undefined,
@@ -9026,7 +9029,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
90269029
factory.createStringLiteral(getSpecifierForModuleSymbol(target, context))
90279030
), ModifierFlags.None);
90289031
break;
9029-
case SyntaxKind.ImportSpecifier:
9032+
case SyntaxKind.ImportSpecifier: {
9033+
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
9034+
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as ImportSpecifier).parent.parent.parent.moduleSpecifier;
90309035
addResult(factory.createImportDeclaration(
90319036
/*modifiers*/ undefined,
90329037
factory.createImportClause(
@@ -9039,10 +9044,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
90399044
factory.createIdentifier(localName)
90409045
)
90419046
])),
9042-
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
9043-
/*assertClause*/ undefined
9047+
specifier,
9048+
(node as ImportSpecifier).parent.parent.parent.assertClause,
90449049
), ModifierFlags.None);
90459050
break;
9051+
}
90469052
case SyntaxKind.ExportSpecifier:
90479053
// does not use localName because the symbol name in this case refers to the name in the exports table,
90489054
// which we must exactly preserve

tests/baselines/reference/importDeclFromTypeNodeInJsSource.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class Foo3 extends d {
7777
}
7878
export class Foo4 extends c {
7979
}
80-
import { EventEmitter } from "events";
81-
import { n3 } from "nestNamespaceModule";
82-
import { d } from "nestNamespaceModule";
83-
import { c } from "renameModule";
80+
import { EventEmitter } from 'events';
81+
import { n3 } from 'nestNamespaceModule';
82+
import { d } from 'nestNamespaceModule';
83+
import { c } from 'renameModule';
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitExportedClassWithExtends.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "lit",
6+
"version": "0.0.1",
7+
"type": "module",
8+
"exports": {
9+
".": {
10+
"types": "./development/index.d.ts"
11+
}
12+
}
13+
}
14+
//// [index.d.ts]
15+
export * from "lit-element/lit-element.js";
16+
//// [package.json]
17+
{
18+
"name": "lit-element",
19+
"version": "0.0.1",
20+
"type": "module",
21+
"exports": {
22+
".": {
23+
"types": "./development/index.d.ts"
24+
},
25+
"./lit-element.js": {
26+
"types": "./development/lit-element.d.ts"
27+
}
28+
}
29+
}
30+
//// [index.d.ts]
31+
export * from "./lit-element.js";
32+
//// [lit-element.d.ts]
33+
export class LitElement {}
34+
//// [package.json]
35+
{
36+
"type": "module",
37+
"private": true
38+
}
39+
//// [index.js]
40+
import { LitElement, LitElement as LitElement2 } from "lit";
41+
export class ElementB extends LitElement {}
42+
export class ElementC extends LitElement2 {}
43+
44+
//// [index.js]
45+
var __extends = (this && this.__extends) || (function () {
46+
var extendStatics = function (d, b) {
47+
extendStatics = Object.setPrototypeOf ||
48+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
49+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
50+
return extendStatics(d, b);
51+
};
52+
return function (d, b) {
53+
if (typeof b !== "function" && b !== null)
54+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
55+
extendStatics(d, b);
56+
function __() { this.constructor = d; }
57+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
58+
};
59+
})();
60+
import { LitElement, LitElement as LitElement2 } from "lit";
61+
var ElementB = /** @class */ (function (_super) {
62+
__extends(ElementB, _super);
63+
function ElementB() {
64+
return _super !== null && _super.apply(this, arguments) || this;
65+
}
66+
return ElementB;
67+
}(LitElement));
68+
export { ElementB };
69+
var ElementC = /** @class */ (function (_super) {
70+
__extends(ElementC, _super);
71+
function ElementC() {
72+
return _super !== null && _super.apply(this, arguments) || this;
73+
}
74+
return ElementC;
75+
}(LitElement2));
76+
export { ElementC };
77+
78+
79+
//// [index.d.ts]
80+
export class ElementB extends LitElement {
81+
}
82+
export class ElementC extends LitElement {
83+
}
84+
import { LitElement } from "lit";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/node_modules/lit/development/index.d.ts ===
2+
3+
export * from "lit-element/lit-element.js";
4+
=== tests/cases/compiler/node_modules/lit-element/development/index.d.ts ===
5+
6+
export * from "./lit-element.js";
7+
=== tests/cases/compiler/node_modules/lit-element/development/lit-element.d.ts ===
8+
export class LitElement {}
9+
>LitElement : Symbol(LitElement, Decl(lit-element.d.ts, 0, 0))
10+
11+
=== tests/cases/compiler/index.js ===
12+
import { LitElement, LitElement as LitElement2 } from "lit";
13+
>LitElement : Symbol(LitElement, Decl(index.js, 0, 8))
14+
>LitElement : Symbol(LitElement, Decl(lit-element.d.ts, 0, 0))
15+
>LitElement2 : Symbol(LitElement2, Decl(index.js, 0, 20))
16+
17+
export class ElementB extends LitElement {}
18+
>ElementB : Symbol(ElementB, Decl(index.js, 0, 60))
19+
>LitElement : Symbol(LitElement, Decl(index.js, 0, 8))
20+
21+
export class ElementC extends LitElement2 {}
22+
>ElementC : Symbol(ElementC, Decl(index.js, 1, 43))
23+
>LitElement2 : Symbol(LitElement2, Decl(index.js, 0, 20))
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/node_modules/lit/development/index.d.ts ===
2+
3+
export * from "lit-element/lit-element.js";
4+
=== tests/cases/compiler/node_modules/lit-element/development/index.d.ts ===
5+
6+
export * from "./lit-element.js";
7+
=== tests/cases/compiler/node_modules/lit-element/development/lit-element.d.ts ===
8+
export class LitElement {}
9+
>LitElement : LitElement
10+
11+
=== tests/cases/compiler/index.js ===
12+
import { LitElement, LitElement as LitElement2 } from "lit";
13+
>LitElement : typeof LitElement
14+
>LitElement : typeof LitElement
15+
>LitElement2 : typeof LitElement
16+
17+
export class ElementB extends LitElement {}
18+
>ElementB : ElementB
19+
>LitElement : LitElement
20+
21+
export class ElementC extends LitElement2 {}
22+
>ElementC : ElementC
23+
>LitElement2 : LitElement
24+

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export namespace testFnTypes {
8383
* @returns {number|null} Result.
8484
*/
8585
export function testFn(input: testFnTypes.input): number | null;
86-
import { myTypes } from "./file.js";
86+
import { myTypes } from './file.js';
8787
/**
8888
* @namespace testFnTypes
8989
* @global

tests/baselines/reference/jsDeclarationsReactComponents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,4 @@ declare namespace Tree {
243243
export const parentSource: string;
244244
}
245245
}
246-
import PropTypes from "prop-types";
246+
import PropTypes from 'prop-types';

tests/baselines/reference/jsxDeclarationsWithEsModuleInteropNoCrash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ declare namespace defaultProps {
3939
const bar_1: boolean;
4040
export { bar_1 as bar };
4141
}
42-
import PropTypes from "prop-types";
42+
import PropTypes from 'prop-types';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @checkJs: true
2+
// @outDir: tests/cases/compiler/out
3+
// @declaration: true
4+
// @module: esnext
5+
// @moduleResolution: nodenext
6+
// @filename: node_modules/lit/package.json
7+
{
8+
"name": "lit",
9+
"version": "0.0.1",
10+
"type": "module",
11+
"exports": {
12+
".": {
13+
"types": "./development/index.d.ts"
14+
}
15+
}
16+
}
17+
// @filename: node_modules/lit/development/index.d.ts
18+
export * from "lit-element/lit-element.js";
19+
// @filename: node_modules/lit-element/package.json
20+
{
21+
"name": "lit-element",
22+
"version": "0.0.1",
23+
"type": "module",
24+
"exports": {
25+
".": {
26+
"types": "./development/index.d.ts"
27+
},
28+
"./lit-element.js": {
29+
"types": "./development/lit-element.d.ts"
30+
}
31+
}
32+
}
33+
// @filename: node_modules/lit-element/development/index.d.ts
34+
export * from "./lit-element.js";
35+
// @filename: node_modules/lit-element/development//lit-element.d.ts
36+
export class LitElement {}
37+
// @filename: package.json
38+
{
39+
"type": "module",
40+
"private": true
41+
}
42+
// @filename: index.js
43+
import { LitElement, LitElement as LitElement2 } from "lit";
44+
export class ElementB extends LitElement {}
45+
export class ElementC extends LitElement2 {}

0 commit comments

Comments
 (0)