Skip to content
Open
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
108 changes: 24 additions & 84 deletions internal/transformers/tstransforms/importelision.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,26 @@ func NewImportElisionTransformer(opt *transformers.TransformOptions) *transforme
func (tx *ImportElisionTransformer) visit(node *ast.Node) *ast.Node {
switch node.Kind {
case ast.KindImportEqualsDeclaration:
if !tx.isElisionBlocked(node) {
if ast.IsExternalModuleImportEqualsDeclaration(node) {
if !tx.shouldEmitAliasDeclaration(node) {
return nil
}
} else {
if !tx.shouldEmitImportEqualsDeclaration(node.AsImportEqualsDeclaration()) {
return nil
}
if ast.IsExternalModuleImportEqualsDeclaration(node) {
if !tx.shouldEmitAliasDeclaration(node) {
return nil
}
} else {
if !tx.shouldEmitImportEqualsDeclaration(node.AsImportEqualsDeclaration()) {
return nil
}
}
return tx.Visitor().VisitEachChild(node)
case ast.KindImportDeclaration:
if !tx.isElisionBlocked(node) {
n := node.AsImportDeclaration()
// Do not elide a side-effect only import declaration.
// import "foo";
if n.ImportClause != nil {
importClause := tx.Visitor().VisitNode(n.ImportClause)
if importClause == nil {
return nil
}
return tx.Factory().UpdateImportDeclaration(n, n.Modifiers(), importClause, n.ModuleSpecifier, tx.Visitor().VisitNode(n.Attributes))
n := node.AsImportDeclaration()
// Do not elide a side-effect only import declaration.
// import "foo";
if n.ImportClause != nil {
importClause := tx.Visitor().VisitNode(n.ImportClause)
if importClause == nil {
return nil
}
return tx.Factory().UpdateImportDeclaration(n, n.Modifiers(), importClause, n.ModuleSpecifier, tx.Visitor().VisitNode(n.Attributes))
}
return tx.Visitor().VisitEachChild(node)
case ast.KindImportClause:
Expand Down Expand Up @@ -83,25 +79,22 @@ func (tx *ImportElisionTransformer) visit(node *ast.Node) *ast.Node {
}
return node
case ast.KindExportAssignment:
if !tx.isElisionBlocked(node) && !tx.compilerOptions.VerbatimModuleSyntax.IsTrue() && !tx.isValueAliasDeclaration(node) {
if !tx.compilerOptions.VerbatimModuleSyntax.IsTrue() && !tx.isValueAliasDeclaration(node) {
// elide unused import
return nil
}
return tx.Visitor().VisitEachChild(node)
case ast.KindExportDeclaration:
if !tx.isElisionBlocked(node) {
n := node.AsExportDeclaration()
var exportClause *ast.Node
if n.ExportClause != nil {
exportClause = tx.Visitor().VisitNode(n.ExportClause)
if exportClause == nil {
// all export bindings were elided
return nil
}
n := node.AsExportDeclaration()
var exportClause *ast.Node
if n.ExportClause != nil {
exportClause = tx.Visitor().VisitNode(n.ExportClause)
if exportClause == nil {
// all export bindings were elided
return nil
}
return tx.Factory().UpdateExportDeclaration(n, nil /*modifiers*/, false /*isTypeOnly*/, exportClause, tx.Visitor().VisitNode(n.ModuleSpecifier), tx.Visitor().VisitNode(n.Attributes))
}
return tx.Visitor().VisitEachChild(node)
return tx.Factory().UpdateExportDeclaration(n, nil /*modifiers*/, false /*isTypeOnly*/, exportClause, tx.Visitor().VisitNode(n.ModuleSpecifier), tx.Visitor().VisitNode(n.Attributes))
case ast.KindNamedExports:
n := node.AsNamedExports()
elements := tx.Visitor().VisitNodes(n.Elements)
Expand Down Expand Up @@ -152,56 +145,3 @@ func (tx *ImportElisionTransformer) isTopLevelValueImportEqualsWithEntityName(no
node = tx.EmitContext().ParseNode(node)
return node != nil && tx.emitResolver.IsTopLevelValueImportEqualsWithEntityName(node)
}

// 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.
func (tx *ImportElisionTransformer) isElisionBlocked(node *ast.Node /*ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration*/) bool {
parsed := tx.EmitContext().ParseNode(node)
if parsed == node || ast.IsExportAssignment(node) {
return false
}

if parsed == nil || 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 ast.KindImportDeclaration:
n := node.AsImportDeclaration()
p := parsed.AsImportDeclaration()
if n.ImportClause != p.ImportClause {
return true // no longer safe to elide as the import clause has changed
}
if n.Attributes != p.Attributes {
return true // no longer safe to elide as the import attributes have changed
}
case ast.KindImportEqualsDeclaration:
n := node.AsImportEqualsDeclaration()
p := parsed.AsImportEqualsDeclaration()
if n.Name() != p.Name() {
return true // no longer safe to elide as local binding has changed
}
if n.IsTypeOnly != p.IsTypeOnly {
return true // no longer safe to elide as `type` modifier has changed
}
if n.ModuleReference != p.ModuleReference && (ast.IsEntityName(n.ModuleReference) || ast.IsEntityName(p.ModuleReference)) {
return true // no longer safe to elide as EntityName reference has changed.
}
case ast.KindExportDeclaration:
n := node.AsExportDeclaration()
p := parsed.AsExportDeclaration()
if n.ExportClause != p.ExportClause {
return true // no longer safe to elide as the export clause has changed
}
if n.Attributes != p.Attributes {
return true // no longer safe to elide as the export attributes have changed
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type; // Error (cannot resolve name)
as; // Error (used in emitting position)
export {};
//// [d.js]
import "./mod.js"; // Error
export {};
//// [e.js]
import { type as type } from "./mod.js";
type;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface LocalInterface extends RequireInterface, ImportInterface {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// not exclusively type-only
require("pkg");


//// [index.d.ts]
Expand Down

This file was deleted.