@@ -27,30 +27,26 @@ func NewImportElisionTransformer(opt *transformers.TransformOptions) *transforme
2727func (tx * ImportElisionTransformer ) visit (node * ast.Node ) * ast.Node {
2828 switch node .Kind {
2929 case ast .KindImportEqualsDeclaration :
30- if ! tx .isElisionBlocked (node ) {
31- if ast .IsExternalModuleImportEqualsDeclaration (node ) {
32- if ! tx .shouldEmitAliasDeclaration (node ) {
33- return nil
34- }
35- } else {
36- if ! tx .shouldEmitImportEqualsDeclaration (node .AsImportEqualsDeclaration ()) {
37- return nil
38- }
30+ if ast .IsExternalModuleImportEqualsDeclaration (node ) {
31+ if ! tx .shouldEmitAliasDeclaration (node ) {
32+ return nil
33+ }
34+ } else {
35+ if ! tx .shouldEmitImportEqualsDeclaration (node .AsImportEqualsDeclaration ()) {
36+ return nil
3937 }
4038 }
4139 return tx .Visitor ().VisitEachChild (node )
4240 case ast .KindImportDeclaration :
43- if ! tx .isElisionBlocked (node ) {
44- n := node .AsImportDeclaration ()
45- // Do not elide a side-effect only import declaration.
46- // import "foo";
47- if n .ImportClause != nil {
48- importClause := tx .Visitor ().VisitNode (n .ImportClause )
49- if importClause == nil {
50- return nil
51- }
52- return tx .Factory ().UpdateImportDeclaration (n , n .Modifiers (), importClause , n .ModuleSpecifier , tx .Visitor ().VisitNode (n .Attributes ))
41+ n := node .AsImportDeclaration ()
42+ // Do not elide a side-effect only import declaration.
43+ // import "foo";
44+ if n .ImportClause != nil {
45+ importClause := tx .Visitor ().VisitNode (n .ImportClause )
46+ if importClause == nil {
47+ return nil
5348 }
49+ return tx .Factory ().UpdateImportDeclaration (n , n .Modifiers (), importClause , n .ModuleSpecifier , tx .Visitor ().VisitNode (n .Attributes ))
5450 }
5551 return tx .Visitor ().VisitEachChild (node )
5652 case ast .KindImportClause :
@@ -83,25 +79,22 @@ func (tx *ImportElisionTransformer) visit(node *ast.Node) *ast.Node {
8379 }
8480 return node
8581 case ast .KindExportAssignment :
86- if ! tx .isElisionBlocked ( node ) && ! tx . compilerOptions .VerbatimModuleSyntax .IsTrue () && ! tx .isValueAliasDeclaration (node ) {
82+ if ! tx .compilerOptions .VerbatimModuleSyntax .IsTrue () && ! tx .isValueAliasDeclaration (node ) {
8783 // elide unused import
8884 return nil
8985 }
9086 return tx .Visitor ().VisitEachChild (node )
9187 case ast .KindExportDeclaration :
92- if ! tx .isElisionBlocked (node ) {
93- n := node .AsExportDeclaration ()
94- var exportClause * ast.Node
95- if n .ExportClause != nil {
96- exportClause = tx .Visitor ().VisitNode (n .ExportClause )
97- if exportClause == nil {
98- // all export bindings were elided
99- return nil
100- }
88+ n := node .AsExportDeclaration ()
89+ var exportClause * ast.Node
90+ if n .ExportClause != nil {
91+ exportClause = tx .Visitor ().VisitNode (n .ExportClause )
92+ if exportClause == nil {
93+ // all export bindings were elided
94+ return nil
10195 }
102- return tx .Factory ().UpdateExportDeclaration (n , nil /*modifiers*/ , false /*isTypeOnly*/ , exportClause , tx .Visitor ().VisitNode (n .ModuleSpecifier ), tx .Visitor ().VisitNode (n .Attributes ))
10396 }
104- return tx .Visitor ().VisitEachChild ( node )
97+ return tx .Factory (). UpdateExportDeclaration ( n , nil /*modifiers*/ , false /*isTypeOnly*/ , exportClause , tx . Visitor ().VisitNode ( n . ModuleSpecifier ), tx . Visitor (). VisitNode ( n . Attributes ) )
10598 case ast .KindNamedExports :
10699 n := node .AsNamedExports ()
107100 elements := tx .Visitor ().VisitNodes (n .Elements )
@@ -152,56 +145,3 @@ func (tx *ImportElisionTransformer) isTopLevelValueImportEqualsWithEntityName(no
152145 node = tx .EmitContext ().ParseNode (node )
153146 return node != nil && tx .emitResolver .IsTopLevelValueImportEqualsWithEntityName (node )
154147}
155-
156- // Determines whether import/export elision is blocked for this statement.
157- //
158- // @description
159- // We generally block import/export elision if the statement was modified by a `before` custom
160- // transform, although we will continue to allow it if the statement hasn't replaced a node of a different kind and
161- // as long as the local bindings for the declarations are unchanged.
162- func (tx * ImportElisionTransformer ) isElisionBlocked (node * ast.Node /*ImportDeclaration | ImportEqualsDeclaration | ExportAssignment | ExportDeclaration*/ ) bool {
163- parsed := tx .EmitContext ().ParseNode (node )
164- if parsed == node || ast .IsExportAssignment (node ) {
165- return false
166- }
167-
168- if parsed == nil || parsed .Kind != node .Kind {
169- // no longer safe to elide as the declaration was replaced with a node of a different kind
170- return true
171- }
172-
173- switch node .Kind {
174- case ast .KindImportDeclaration :
175- n := node .AsImportDeclaration ()
176- p := parsed .AsImportDeclaration ()
177- if n .ImportClause != p .ImportClause {
178- return true // no longer safe to elide as the import clause has changed
179- }
180- if n .Attributes != p .Attributes {
181- return true // no longer safe to elide as the import attributes have changed
182- }
183- case ast .KindImportEqualsDeclaration :
184- n := node .AsImportEqualsDeclaration ()
185- p := parsed .AsImportEqualsDeclaration ()
186- if n .Name () != p .Name () {
187- return true // no longer safe to elide as local binding has changed
188- }
189- if n .IsTypeOnly != p .IsTypeOnly {
190- return true // no longer safe to elide as `type` modifier has changed
191- }
192- if n .ModuleReference != p .ModuleReference && (ast .IsEntityName (n .ModuleReference ) || ast .IsEntityName (p .ModuleReference )) {
193- return true // no longer safe to elide as EntityName reference has changed.
194- }
195- case ast .KindExportDeclaration :
196- n := node .AsExportDeclaration ()
197- p := parsed .AsExportDeclaration ()
198- if n .ExportClause != p .ExportClause {
199- return true // no longer safe to elide as the export clause has changed
200- }
201- if n .Attributes != p .Attributes {
202- return true // no longer safe to elide as the export attributes have changed
203- }
204- }
205-
206- return false
207- }
0 commit comments