Skip to content

Looser import/export elision blocking in visitElidableStatement #57223

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 2 commits into from
Jan 31, 2024
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
69 changes: 63 additions & 6 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ import {
isComputedPropertyName,
isDecorator,
isElementAccessExpression,
isEntityName,
isEnumConst,
isExportAssignment,
isExportDeclaration,
isExportOrDefaultModifier,
isExportSpecifier,
isExpression,
Expand All @@ -96,6 +99,8 @@ import {
isHeritageClause,
isIdentifier,
isImportClause,
isImportDeclaration,
isImportEqualsDeclaration,
isImportSpecifier,
isInJSFile,
isInstantiatedModule,
Expand Down Expand Up @@ -425,13 +430,65 @@ export function transformTypeScript(context: TransformationContext) {
}
}

function visitElidableStatement(node: ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration): VisitResult<Node | undefined> {
/**
* Determines whether import/export elision is blocked for this statement.
*
* @description
* We generally block import/export elision if the statement was modified by a `before` custom
* transform, although we will continue to allow it if the statement hasn't replaced a node of a different kind and
* as long as the local bindings for the declarations are unchanged.
*/
function isElisionBlocked(node: ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration) {
const parsed = getParseTreeNode(node);
if (parsed !== node) {
// If the node has been transformed by a `before` transformer, perform no ellision on it
// As the type information we would attempt to lookup to perform ellision is potentially unavailable for the synthesized nodes
// We do not reuse `visitorWorker`, as the ellidable statement syntax kinds are technically unrecognized by the switch-case in `visitTypeScript`,
// and will trigger debug failures when debug verbosity is turned up
if (parsed === node || isExportAssignment(node)) {
Copy link
Member

Choose a reason for hiding this comment

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

Are export assignments never blocked? When I test export = of a type, it's elided: https://www.typescriptlang.org/play?module=1#code/C4TwDgpgBAYg9nKBeKBGATAZgCwG4BQ+EAHmHAE7DKwK5A

Or is there something else going on and I'm confused?

Copy link
Member Author

@rbuckton rbuckton Jan 31, 2024

Choose a reason for hiding this comment

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

The purpose of blocking statement elision is to handle cases where a custom transform modifies the list of export bindings to catch cases like one where a user adds a new binding to an existing import, but we then just blindly elide the whole statement.

For export = and export default there's no change you can make to the binding list that would matter, since there isn't a binding list to begin with. The only change you could make is to the expression and we don't look at the expression of a transformed ExportAssignment when determining whether to elide, we instead look at the original parse tree node that the transformed ExportAssignment replaces.

return false;
}

if (!parsed || parsed.kind !== node.kind) {
// no longer safe to elide as the declaration was replaced with a node of a different kind
return true;
}

switch (node.kind) {
case SyntaxKind.ImportDeclaration:
Debug.assertNode(parsed, isImportDeclaration);
if (node.importClause !== parsed.importClause) {
return true; // no longer safe to elide as the import clause has changed
}
if (node.attributes !== parsed.attributes) {
return true; // no longer safe to elide as the import attributes have changed
}
break;
case SyntaxKind.ImportEqualsDeclaration:
Debug.assertNode(parsed, isImportEqualsDeclaration);
if (node.name !== parsed.name) {
return true; // no longer safe to elide as local binding has changed
}
if (node.isTypeOnly !== parsed.isTypeOnly) {
return true; // no longer safe to elide as `type` modifier has changed
}
if (node.moduleReference !== parsed.moduleReference && (isEntityName(node.moduleReference) || isEntityName(parsed.moduleReference))) {
return true; // no longer safe to elide as EntityName reference has changed.
}
break;
case SyntaxKind.ExportDeclaration:
Debug.assertNode(parsed, isExportDeclaration);
if (node.exportClause !== parsed.exportClause) {
return true; // no longer safe to elide as the export clause has changed
}
if (node.attributes !== parsed.attributes) {
return true; // no longer safe to elide as the export attributes have changed
}
break;
}

return false;
}

function visitElidableStatement(node: ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration): VisitResult<Node | undefined> {
if (isElisionBlocked(node)) {
// We do not reuse `visitorWorker`, as the ellidable statement syntax kinds are technically unrecognized by
// the switch-case in `visitTypeScript`, and will trigger debug failures when debug verbosity is turned up.
if (node.transformFlags & TransformFlags.ContainsTypeScript) {
// This node contains TypeScript, so we should visit its children.
return visitEachChild(node, visitor, context);
Expand Down
45 changes: 45 additions & 0 deletions src/testRunner/unittests/customTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,49 @@ describe("unittests:: customTransforms", () => {
},
],
}, { sourceMap: true, outFile: "source.js" });

emitsCorrectly("importDeclarationBeforeTransformElision", [
{
file: "a.ts",
text: "export type A = string;",
},
{
file: "index.ts",
text: "import { A } from './a.js';\nexport { A } from './a.js';",
},
], {
before: [
context => {
const { factory } = context;
return (s: ts.SourceFile) => ts.visitEachChild(s, visitor, context);

function visitor(node: ts.Node): ts.Node {
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
return factory.updateImportDeclaration(
node,
node.modifiers,
node.importClause,
factory.createStringLiteral(node.moduleSpecifier.text),
node.attributes,
);
}

if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
return factory.updateExportDeclaration(
node,
node.modifiers,
node.isTypeOnly,
node.exportClause,
factory.createStringLiteral(node.moduleSpecifier.text),
node.attributes,
);
}
return node;
}
},
],
}, {
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.ESNext,
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// [a.js]
export {};


// [index.js]
export {};