Skip to content

Add support for resolution mode #851

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 10 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
56 changes: 54 additions & 2 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4152,6 +4152,10 @@ func IsImportDeclaration(node *Node) bool {
return node.Kind == KindImportDeclaration
}

func IsImportDeclarationOrJSImportDeclaration(node *Node) bool {
return node.Kind == KindImportDeclaration || node.Kind == KindJSImportDeclaration
}

// ImportSpecifier

type ImportSpecifier struct {
Expand Down Expand Up @@ -7500,6 +7504,53 @@ func IsImportAttributes(node *Node) bool {
return node.Kind == KindImportAttributes
}

func (node *ImportAttributesNode) GetResolutionModeOverride( /* !!! grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => void*/ ) (core.ResolutionMode, bool) {
if node == nil {
return core.ResolutionModeNone, false
}

attributes := node.AsImportAttributes().Attributes

if len(attributes.Nodes) != 1 {
// !!!
// grammarErrorOnNode?.(
// node,
// node.token === SyntaxKind.WithKeyword
// ? Diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require
// : Diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require,
// );
return core.ResolutionModeNone, false
}

elem := attributes.Nodes[0].AsImportAttribute()
if !IsStringLiteralLike(elem.Name()) {
return core.ResolutionModeNone, false
}
if elem.Name().Text() != "resolution-mode" {
// !!!
// grammarErrorOnNode?.(
// elem.name,
// node.token === SyntaxKind.WithKeyword
// ? Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_attributes
// : Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_assertions,
// );
return core.ResolutionModeNone, false
}
if !IsStringLiteralLike(elem.Value) {
return core.ResolutionModeNone, false
}
if elem.Value.Text() != "import" && elem.Value.Text() != "require" {
// !!!
// grammarErrorOnNode?.(elem.value, Diagnostics.resolution_mode_should_be_either_require_or_import);
return core.ResolutionModeNone, false
}
if elem.Value.Text() == "import" {
return core.ResolutionModeESM, true
} else {
return core.ModuleKindCommonJS, true
}
}

// TypeQueryNode

type TypeQueryNode struct {
Expand Down Expand Up @@ -9889,8 +9940,9 @@ type CommentDirective struct {
// SourceFile

type SourceFileMetaData struct {
PackageJsonType string
ImpliedNodeFormat core.ResolutionMode
PackageJsonType string
PackageJsonDirectory string
ImpliedNodeFormat core.ResolutionMode
}

type CheckJsDirective struct {
Expand Down
71 changes: 70 additions & 1 deletion internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ func GetImpliedNodeFormatForFile(path string, packageJsonType string) core.Modul
impliedNodeFormat = core.ResolutionModeESM
} else if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDcts, tspath.ExtensionCts, tspath.ExtensionCjs}) {
impliedNodeFormat = core.ResolutionModeCommonJS
} else if packageJsonType != "" && tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDts, tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionJs, tspath.ExtensionJsx}) {
} else if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDts, tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionJs, tspath.ExtensionJsx}) {
impliedNodeFormat = core.IfElse(packageJsonType == "module", core.ResolutionModeESM, core.ResolutionModeCommonJS)
}

Expand Down Expand Up @@ -2733,6 +2733,22 @@ func IsTypeOnlyImportOrExportDeclaration(node *Node) bool {
return IsTypeOnlyImportDeclaration(node) || isTypeOnlyExportDeclaration(node)
}

func IsExclusivelyTypeOnlyImportOrExport(node *Node) bool {
switch node.Kind {
case KindExportDeclaration:
return node.AsExportDeclaration().IsTypeOnly
case KindImportDeclaration, KindJSImportDeclaration:
if importClause := node.AsImportDeclaration().ImportClause; importClause != nil {
return importClause.AsImportClause().IsTypeOnly
}
case KindJSDocImportTag:
if importClause := node.AsJSDocImportTag().ImportClause; importClause != nil {
return importClause.AsImportClause().IsTypeOnly
}
}
return false
}

func GetSourceFileOfModule(module *Symbol) *SourceFile {
declaration := module.ValueDeclaration
if declaration == nil {
Expand Down Expand Up @@ -2952,3 +2968,56 @@ func GetPropertyNameForPropertyNameNode(name *Node) string {
}
panic("Unhandled case in getPropertyNameForPropertyNameNode")
}

func IsPartOfTypeOnlyImportOrExportDeclaration(node *Node) bool {
return FindAncestor(node, IsTypeOnlyImportOrExportDeclaration) != nil
}

func IsPartOfExclusivelyTypeOnlyImportOrExportDeclaration(node *Node) bool {
return FindAncestor(node, IsExclusivelyTypeOnlyImportOrExport) != nil
}

func IsEmittableImport(node *Node) bool {
switch node.Kind {
case KindImportDeclaration:
return node.AsImportDeclaration().ImportClause == nil || !node.AsImportDeclaration().ImportClause.IsTypeOnly()
case KindExportDeclaration:
return !node.AsExportDeclaration().IsTypeOnly
case KindImportEqualsDeclaration:
return !node.AsImportEqualsDeclaration().IsTypeOnly
case KindCallExpression:
return IsImportCall(node)
}
return false
}

func IsResolutionModeOverrideHost(node *Node) bool {
if node == nil {
return false
}
switch node.Kind {
case KindImportType, KindExportDeclaration, KindImportDeclaration, KindJSImportDeclaration:
return true
}
return false
}

func HasResolutionModeOverride(node *Node) bool {
if node == nil {
return false
}
var attributes *ImportAttributesNode
switch node.Kind {
case KindImportType:
attributes = node.AsImportTypeNode().Attributes
case KindImportDeclaration, KindJSImportDeclaration:
attributes = node.AsImportDeclaration().Attributes
case KindExportDeclaration:
attributes = node.AsExportDeclaration().Attributes
}
if attributes != nil {
_, ok := attributes.GetResolutionModeOverride()
return ok
}
return false
}
Loading
Loading