Skip to content
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

Reuse existing module specifiers in js declaration emit #52089

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
30 changes: 18 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9000,25 +9000,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Is bound into file.symbol.globalExports instead, which we don't currently traverse
addResult(factory.createNamespaceExportDeclaration(idText((node as NamespaceExportDeclaration).name)), ModifierFlags.None);
break;
case SyntaxKind.ImportClause:
case SyntaxKind.ImportClause: {
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as ImportClause).parent.moduleSpecifier;
addResult(factory.createImportDeclaration(
/*modifiers*/ undefined,
factory.createImportClause(/*isTypeOnly*/ false, factory.createIdentifier(localName), /*namedBindings*/ undefined),
// We use `target.parent || target` below as `target.parent` is unset when the target is a module which has been export assigned
// And then made into a default by the `esModuleInterop` or `allowSyntheticDefaultImports` flag
// In such cases, the `target` refers to the module itself already
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
/*assertClause*/ undefined
specifier,
(node as ImportClause).parent.assertClause
), ModifierFlags.None);
break;
case SyntaxKind.NamespaceImport:
}
case SyntaxKind.NamespaceImport: {
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as NamespaceImport).parent.parent.moduleSpecifier;
addResult(factory.createImportDeclaration(
/*modifiers*/ undefined,
factory.createImportClause(/*isTypeOnly*/ false, /*importClause*/ undefined, factory.createNamespaceImport(factory.createIdentifier(localName))),
factory.createStringLiteral(getSpecifierForModuleSymbol(target, context)),
/*assertClause*/ undefined
specifier,
(node as NamespaceImport).parent.parent.assertClause
), ModifierFlags.None);
break;
}
case SyntaxKind.NamespaceExport:
addResult(factory.createExportDeclaration(
/*modifiers*/ undefined,
Expand All @@ -9027,7 +9030,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
factory.createStringLiteral(getSpecifierForModuleSymbol(target, context))
), ModifierFlags.None);
break;
case SyntaxKind.ImportSpecifier:
case SyntaxKind.ImportSpecifier: {
const generatedSpecifier = getSpecifierForModuleSymbol(target.parent || target, context); // generate specifier (even though we're reusing and existing one) for ambient module reference include side effects
const specifier = bundled ? factory.createStringLiteral(generatedSpecifier) : (node as ImportSpecifier).parent.parent.parent.moduleSpecifier;
addResult(factory.createImportDeclaration(
/*modifiers*/ undefined,
factory.createImportClause(
Expand All @@ -9040,10 +9045,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
factory.createIdentifier(localName)
)
])),
factory.createStringLiteral(getSpecifierForModuleSymbol(target.parent || target, context)),
/*assertClause*/ undefined
specifier,
(node as ImportSpecifier).parent.parent.parent.assertClause,
), ModifierFlags.None);
break;
}
case SyntaxKind.ExportSpecifier:
// does not use localName because the symbol name in this case refers to the name in the exports table,
// which we must exactly preserve
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/importDeclFromTypeNodeInJsSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Foo3 extends d {
}
export class Foo4 extends c {
}
import { EventEmitter } from "events";
import { n3 } from "nestNamespaceModule";
import { d } from "nestNamespaceModule";
import { c } from "renameModule";
import { EventEmitter } from 'events';
import { n3 } from 'nestNamespaceModule';
import { d } from 'nestNamespaceModule';
import { c } from 'renameModule';
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//// [tests/cases/compiler/jsDeclarationEmitExportedClassWithExtends.ts] ////

//// [package.json]
{
"name": "lit",
"version": "0.0.1",
"type": "module",
"exports": {
".": {
"types": "./development/index.d.ts"
}
}
}
//// [index.d.ts]
export * from "lit-element/lit-element.js";
//// [package.json]
{
"name": "lit-element",
"version": "0.0.1",
"type": "module",
"exports": {
".": {
"types": "./development/index.d.ts"
},
"./lit-element.js": {
"types": "./development/lit-element.d.ts"
}
}
}
//// [index.d.ts]
export * from "./lit-element.js";
//// [lit-element.d.ts]
export class LitElement {}
//// [package.json]
{
"type": "module",
"private": true
}
//// [index.js]
import { LitElement, LitElement as LitElement2 } from "lit";
export class ElementB extends LitElement {}
export class ElementC extends LitElement2 {}

//// [index.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { LitElement, LitElement as LitElement2 } from "lit";
var ElementB = /** @class */ (function (_super) {
__extends(ElementB, _super);
function ElementB() {
return _super !== null && _super.apply(this, arguments) || this;
}
return ElementB;
}(LitElement));
export { ElementB };
var ElementC = /** @class */ (function (_super) {
__extends(ElementC, _super);
function ElementC() {
return _super !== null && _super.apply(this, arguments) || this;
}
return ElementC;
}(LitElement2));
export { ElementC };


//// [index.d.ts]
export class ElementB extends LitElement {
}
export class ElementC extends LitElement {
}
import { LitElement } from "lit";
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/node_modules/lit/development/index.d.ts ===

export * from "lit-element/lit-element.js";
=== tests/cases/compiler/node_modules/lit-element/development/index.d.ts ===

export * from "./lit-element.js";
=== tests/cases/compiler/node_modules/lit-element/development/lit-element.d.ts ===
export class LitElement {}
>LitElement : Symbol(LitElement, Decl(lit-element.d.ts, 0, 0))

=== tests/cases/compiler/index.js ===
import { LitElement, LitElement as LitElement2 } from "lit";
>LitElement : Symbol(LitElement, Decl(index.js, 0, 8))
>LitElement : Symbol(LitElement, Decl(lit-element.d.ts, 0, 0))
>LitElement2 : Symbol(LitElement2, Decl(index.js, 0, 20))

export class ElementB extends LitElement {}
>ElementB : Symbol(ElementB, Decl(index.js, 0, 60))
>LitElement : Symbol(LitElement, Decl(index.js, 0, 8))

export class ElementC extends LitElement2 {}
>ElementC : Symbol(ElementC, Decl(index.js, 1, 43))
>LitElement2 : Symbol(LitElement2, Decl(index.js, 0, 20))

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/node_modules/lit/development/index.d.ts ===

export * from "lit-element/lit-element.js";
=== tests/cases/compiler/node_modules/lit-element/development/index.d.ts ===

export * from "./lit-element.js";
=== tests/cases/compiler/node_modules/lit-element/development/lit-element.d.ts ===
export class LitElement {}
>LitElement : LitElement

=== tests/cases/compiler/index.js ===
import { LitElement, LitElement as LitElement2 } from "lit";
>LitElement : typeof LitElement
>LitElement : typeof LitElement
>LitElement2 : typeof LitElement

export class ElementB extends LitElement {}
>ElementB : ElementB
>LitElement : LitElement

export class ElementC extends LitElement2 {}
>ElementC : ElementC
>LitElement2 : LitElement

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export namespace testFnTypes {
* @returns {number|null} Result.
*/
export function testFn(input: testFnTypes.input): number | null;
import { myTypes } from "./file.js";
import { myTypes } from './file.js';
/**
* @namespace testFnTypes
* @global
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/jsDeclarationsReactComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,4 @@ declare namespace Tree {
export const parentSource: string;
}
}
import PropTypes from "prop-types";
import PropTypes from 'prop-types';
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ declare namespace defaultProps {
const bar_1: boolean;
export { bar_1 as bar };
}
import PropTypes from "prop-types";
import PropTypes from 'prop-types';
45 changes: 45 additions & 0 deletions tests/cases/compiler/jsDeclarationEmitExportedClassWithExtends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @checkJs: true
// @outDir: tests/cases/compiler/out
// @declaration: true
// @module: esnext
// @moduleResolution: nodenext
// @filename: node_modules/lit/package.json
{
"name": "lit",
"version": "0.0.1",
"type": "module",
"exports": {
".": {
"types": "./development/index.d.ts"
}
}
}
// @filename: node_modules/lit/development/index.d.ts
export * from "lit-element/lit-element.js";
// @filename: node_modules/lit-element/package.json
{
"name": "lit-element",
"version": "0.0.1",
"type": "module",
"exports": {
".": {
"types": "./development/index.d.ts"
},
"./lit-element.js": {
"types": "./development/lit-element.d.ts"
}
}
}
// @filename: node_modules/lit-element/development/index.d.ts
export * from "./lit-element.js";
// @filename: node_modules/lit-element/development//lit-element.d.ts
export class LitElement {}
// @filename: package.json
{
"type": "module",
"private": true
}
// @filename: index.js
import { LitElement, LitElement as LitElement2 } from "lit";
export class ElementB extends LitElement {}
export class ElementC extends LitElement2 {}