Skip to content

Reparse @import as synthetic type-only import declaration #831

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sandersn
Copy link
Member

It is slightly stricter than Strada's treatment because Corsa now treats the synthetic node exactly as a type-only declaration.

It is slightly stricter than Strada's treatment because Corsa now treats
the synthetic node *exactly* as a type-only declaration.
@Copilot Copilot AI review requested due to automatic review settings April 28, 2025 15:21
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new syntax node (KindJSImportDeclaration) to support the re-parsing of @import annotations as synthetic type‑only import declarations. The updates ensure that all relevant components—including transformers, printers, checkers, AST utilities, and unit tests—properly recognize and handle the new Kind alongside the traditional import declaration.

  • Extended switch-case statements and error handling in multiple modules to include KindJSImportDeclaration.
  • Updated AST creation methods and reparse logic to correctly mark synthetic jsimport declarations as type‑only.

Reviewed Changes

Copilot reviewed 52 out of 53 changed files in this pull request and generated no comments.

Show a summary per file
File Description
testdata/baselines/reference/submodule/conformance/importTag15(module=esnext).errors.txt Adjusted error output to account for the additional type‑only import errors.
testdata/baselines/reference/submodule/conformance/importTag15(module=es2015).errors.txt Updated error messages and counts for invalid use of import attributes.
internal/transformers/typeeraser.go Added KindJSImportDeclaration in the type eraser transformer to ensure proper traversal.
internal/transformers/importelision.go Extended import elision logic to handle synthetic jsimport declarations.
internal/transformers/externalmoduleinfo.go Modified external module info collection to include KindJSImportDeclaration.
internal/transformers/esnext.go Updated transformer switch to recognize KindJSImportDeclaration.
internal/transformers/esmodule.go Added a case to handle KindJSImportDeclaration during ES module transformation.
internal/transformers/commonjsmodule.go Added handling for KindJSImportDeclaration as a no‑op, aligning with CommonJS semantics.
internal/printer/printer.go Extended the printer to generate nodes for KindJSImportDeclaration.
internal/printer/namegenerator.go Modified name generation logic to include KindJSImportDeclaration along with existing types.
internal/parser/reparser.go Implemented reparse logic for @import JSDoc tags to create synthetic jsimport declarations marked as type‑only.
internal/ls/utilities.go Updated language service utilities to recognize KindJSImportDeclaration in import specifier resolution.
internal/checker/grammarchecks.go Adjusted grammar checks to include KindJSImportDeclaration when validating ambient import modifiers.
internal/checker/checker.go Integrated KindJSImportDeclaration in multiple checking routines for import declarations.
internal/ast/utilities.go Updated various AST utility functions to support KindJSImportDeclaration in their logic.
internal/ast/kind_stringer_generated.go Revised generated string mappings to include KindJSImportDeclaration.
internal/ast/kind.go Declared the new constant KindJSImportDeclaration.
internal/ast/ast.go Updated factory methods for creating import declarations to allow creation of KindJSImportDeclaration.
Files not reviewed (1)
  • testdata/baselines/reference/submodule/conformance/importTag1.errors.txt: Language not supported

export function foo(a, b) {}
->foo : (a: Foo, b: I) => void
->a : Foo
+>foo : (a: default, b: I) => void
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know how typescript behaves, but I've changed so little code that I guess that a: default is closer to TS than a: Foo was.

Copy link
Member

Choose a reason for hiding this comment

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

Isn’t this saying that TS has a: Foo where tsgo has a: default? We definitely shouldn’t be calling that type default.

No longer used; everything now uses JSImportDeclaration.
@sandersn
Copy link
Member Author

@a-tarasyuk you wrote the original implementation of @import, so you might be interested to see how it looks in Corsa.

}

func (f *NodeFactory) NewJSImportDeclaration(modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
return f.newImportOrJSImportDeclaration(KindJSImportDeclaration, modifiers, importClause, moduleSpecifier, attributes)
}

func (f *NodeFactory) UpdateImportDeclaration(node *ImportDeclaration, modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
Copy link
Member

Choose a reason for hiding this comment

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

VisitEachChild is going to have issues if you don't differentiate by Kind in UpdateImportDeclaration, since it would turn a JSImportDeclaration into a normal ImportDeclaration.

@@ -3953,7 +3953,7 @@ func (p *Printer) emitStatement(node *ast.Statement) {
p.emitNamespaceExportDeclaration(node.AsNamespaceExportDeclaration())
case ast.KindImportEqualsDeclaration:
p.emitImportEqualsDeclaration(node.AsImportEqualsDeclaration())
case ast.KindImportDeclaration:
Copy link
Member

Choose a reason for hiding this comment

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

Printing KindJSImportDeclaration should either panic (if you expect it to always be transformed), or have its own distinct emit.

@@ -63,6 +63,8 @@ func (tx *CommonJSModuleTransformer) visitTopLevel(node *ast.Node) *ast.Node {
switch node.Kind {
case ast.KindImportDeclaration:
node = tx.visitTopLevelImportDeclaration(node.AsImportDeclaration())
case ast.KindJSImportDeclaration:
Copy link
Member

Choose a reason for hiding this comment

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

IMO, these should be handled by something like typeeraser.go or importellision.go so that other transformers shouldn't have to deal with special cases like JSImportDeclaration. Handling them in the module transforms means they have to exist as part of all other transforms first, and its better if we limit syntax in those transforms to ones expected by that edition of JS.

@@ -39,6 +39,8 @@ func (tx *ESModuleTransformer) visit(node *ast.Node) *ast.Node {
node = tx.visitSourceFile(node.AsSourceFile())
case ast.KindImportDeclaration:
node = tx.visitImportDeclaration(node.AsImportDeclaration())
case ast.KindJSImportDeclaration:
node = nil
Copy link
Member

Choose a reason for hiding this comment

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

This should be handled in an earlier transform.

@@ -368,6 +368,7 @@ func (tx *ESNextTransformer) transformUsingDeclarations(statementsIn []*ast.Stat

switch node.Kind {
case ast.KindImportDeclaration,
ast.KindJSImportDeclaration,
Copy link
Member

Choose a reason for hiding this comment

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

This should be handled in an earlier transform.

@@ -47,11 +47,12 @@ func (c *externalModuleInfoCollector) collect() *externalModuleInfo {
hasImportDefault := false
for _, node := range c.sourceFile.Statements.Nodes {
switch node.Kind {
case ast.KindImportDeclaration:
case ast.KindImportDeclaration, ast.KindJSImportDeclaration:
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? Aren't these imports type-only and just elided?

@@ -28,7 +28,7 @@ func (tx *ImportElisionTransformer) visit(node *ast.Node) *ast.Node {
return nil
}
return tx.visitor.VisitEachChild(node)
case ast.KindImportDeclaration:
case ast.KindImportDeclaration, ast.KindJSImportDeclaration:
Copy link
Member

Choose a reason for hiding this comment

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

This should be handled by typeeraser.go and should never reach this point

@@ -166,7 +166,7 @@ func (tx *ImportElisionTransformer) isElisionBlocked(node *ast.Node /*ImportDecl
}

switch node.Kind {
case ast.KindImportDeclaration:
case ast.KindImportDeclaration, ast.KindJSImportDeclaration:
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be necessary as it should be handled by typeeraser.go

@@ -255,7 +255,7 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
}
return tx.visitor.VisitEachChild(node)

case ast.KindImportDeclaration:
case ast.KindImportDeclaration, ast.KindJSImportDeclaration:
Copy link
Member

Choose a reason for hiding this comment

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

This should have its own branch and should just be elided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants