Skip to content

Commit e34eca1

Browse files
Add support for resolution mode (#851)
Co-authored-by: Andrew Branch <[email protected]>
1 parent e0c0e9c commit e34eca1

File tree

926 files changed

+9751
-15394
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

926 files changed

+9751
-15394
lines changed

internal/ast/ast.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,6 +4180,10 @@ func IsImportDeclaration(node *Node) bool {
41804180
return node.Kind == KindImportDeclaration
41814181
}
41824182

4183+
func IsImportDeclarationOrJSImportDeclaration(node *Node) bool {
4184+
return node.Kind == KindImportDeclaration || node.Kind == KindJSImportDeclaration
4185+
}
4186+
41834187
// ImportSpecifier
41844188

41854189
type ImportSpecifier struct {
@@ -7528,6 +7532,53 @@ func IsImportAttributes(node *Node) bool {
75287532
return node.Kind == KindImportAttributes
75297533
}
75307534

7535+
func (node *ImportAttributesNode) GetResolutionModeOverride( /* !!! grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => void*/ ) (core.ResolutionMode, bool) {
7536+
if node == nil {
7537+
return core.ResolutionModeNone, false
7538+
}
7539+
7540+
attributes := node.AsImportAttributes().Attributes
7541+
7542+
if len(attributes.Nodes) != 1 {
7543+
// !!!
7544+
// grammarErrorOnNode?.(
7545+
// node,
7546+
// node.token === SyntaxKind.WithKeyword
7547+
// ? Diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require
7548+
// : Diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require,
7549+
// );
7550+
return core.ResolutionModeNone, false
7551+
}
7552+
7553+
elem := attributes.Nodes[0].AsImportAttribute()
7554+
if !IsStringLiteralLike(elem.Name()) {
7555+
return core.ResolutionModeNone, false
7556+
}
7557+
if elem.Name().Text() != "resolution-mode" {
7558+
// !!!
7559+
// grammarErrorOnNode?.(
7560+
// elem.name,
7561+
// node.token === SyntaxKind.WithKeyword
7562+
// ? Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_attributes
7563+
// : Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_assertions,
7564+
// );
7565+
return core.ResolutionModeNone, false
7566+
}
7567+
if !IsStringLiteralLike(elem.Value) {
7568+
return core.ResolutionModeNone, false
7569+
}
7570+
if elem.Value.Text() != "import" && elem.Value.Text() != "require" {
7571+
// !!!
7572+
// grammarErrorOnNode?.(elem.value, Diagnostics.resolution_mode_should_be_either_require_or_import);
7573+
return core.ResolutionModeNone, false
7574+
}
7575+
if elem.Value.Text() == "import" {
7576+
return core.ResolutionModeESM, true
7577+
} else {
7578+
return core.ModuleKindCommonJS, true
7579+
}
7580+
}
7581+
75317582
// TypeQueryNode
75327583

75337584
type TypeQueryNode struct {
@@ -9918,8 +9969,9 @@ type CommentDirective struct {
99189969
// SourceFile
99199970

99209971
type SourceFileMetaData struct {
9921-
PackageJsonType string
9922-
ImpliedNodeFormat core.ResolutionMode
9972+
PackageJsonType string
9973+
PackageJsonDirectory string
9974+
ImpliedNodeFormat core.ResolutionMode
99239975
}
99249976

99259977
type CheckJsDirective struct {

internal/ast/utilities.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ func GetImpliedNodeFormatForFile(path string, packageJsonType string) core.Modul
24032403
impliedNodeFormat = core.ResolutionModeESM
24042404
} else if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDcts, tspath.ExtensionCts, tspath.ExtensionCjs}) {
24052405
impliedNodeFormat = core.ResolutionModeCommonJS
2406-
} else if packageJsonType != "" && tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDts, tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionJs, tspath.ExtensionJsx}) {
2406+
} else if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDts, tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionJs, tspath.ExtensionJsx}) {
24072407
impliedNodeFormat = core.IfElse(packageJsonType == "module", core.ResolutionModeESM, core.ResolutionModeCommonJS)
24082408
}
24092409

@@ -2739,6 +2739,22 @@ func IsTypeOnlyImportOrExportDeclaration(node *Node) bool {
27392739
return IsTypeOnlyImportDeclaration(node) || isTypeOnlyExportDeclaration(node)
27402740
}
27412741

2742+
func IsExclusivelyTypeOnlyImportOrExport(node *Node) bool {
2743+
switch node.Kind {
2744+
case KindExportDeclaration:
2745+
return node.AsExportDeclaration().IsTypeOnly
2746+
case KindImportDeclaration, KindJSImportDeclaration:
2747+
if importClause := node.AsImportDeclaration().ImportClause; importClause != nil {
2748+
return importClause.AsImportClause().IsTypeOnly
2749+
}
2750+
case KindJSDocImportTag:
2751+
if importClause := node.AsJSDocImportTag().ImportClause; importClause != nil {
2752+
return importClause.AsImportClause().IsTypeOnly
2753+
}
2754+
}
2755+
return false
2756+
}
2757+
27422758
func GetClassLikeDeclarationOfSymbol(symbol *Symbol) *Node {
27432759
return core.Find(symbol.Declarations, IsClassLike)
27442760
}
@@ -2941,6 +2957,59 @@ func GetPropertyNameForPropertyNameNode(name *Node) string {
29412957
panic("Unhandled case in getPropertyNameForPropertyNameNode")
29422958
}
29432959

2960+
func IsPartOfTypeOnlyImportOrExportDeclaration(node *Node) bool {
2961+
return FindAncestor(node, IsTypeOnlyImportOrExportDeclaration) != nil
2962+
}
2963+
2964+
func IsPartOfExclusivelyTypeOnlyImportOrExportDeclaration(node *Node) bool {
2965+
return FindAncestor(node, IsExclusivelyTypeOnlyImportOrExport) != nil
2966+
}
2967+
2968+
func IsEmittableImport(node *Node) bool {
2969+
switch node.Kind {
2970+
case KindImportDeclaration:
2971+
return node.AsImportDeclaration().ImportClause == nil || !node.AsImportDeclaration().ImportClause.IsTypeOnly()
2972+
case KindExportDeclaration:
2973+
return !node.AsExportDeclaration().IsTypeOnly
2974+
case KindImportEqualsDeclaration:
2975+
return !node.AsImportEqualsDeclaration().IsTypeOnly
2976+
case KindCallExpression:
2977+
return IsImportCall(node)
2978+
}
2979+
return false
2980+
}
2981+
2982+
func IsResolutionModeOverrideHost(node *Node) bool {
2983+
if node == nil {
2984+
return false
2985+
}
2986+
switch node.Kind {
2987+
case KindImportType, KindExportDeclaration, KindImportDeclaration, KindJSImportDeclaration:
2988+
return true
2989+
}
2990+
return false
2991+
}
2992+
2993+
func HasResolutionModeOverride(node *Node) bool {
2994+
if node == nil {
2995+
return false
2996+
}
2997+
var attributes *ImportAttributesNode
2998+
switch node.Kind {
2999+
case KindImportType:
3000+
attributes = node.AsImportTypeNode().Attributes
3001+
case KindImportDeclaration, KindJSImportDeclaration:
3002+
attributes = node.AsImportDeclaration().Attributes
3003+
case KindExportDeclaration:
3004+
attributes = node.AsExportDeclaration().Attributes
3005+
}
3006+
if attributes != nil {
3007+
_, ok := attributes.GetResolutionModeOverride()
3008+
return ok
3009+
}
3010+
return false
3011+
}
3012+
29443013
func IsStringTextContainingNode(node *Node) bool {
29453014
return node.Kind == KindStringLiteral || IsTemplateLiteralKind(node.Kind)
29463015
}

0 commit comments

Comments
 (0)