Skip to content

Set implied node format during source file creation #637

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 35 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
66f4788
Set implied node format during source file creation
frodi-karlsson Mar 16, 2025
48873eb
Merge branch 'main' into implied-node-format-setting
frodi-karlsson Mar 16, 2025
fb0f304
Move into file loader
frodi-karlsson Mar 16, 2025
3e7b92c
Merge branch 'implied-node-format-setting' of github.com:frodi-karlss…
frodi-karlsson Mar 16, 2025
8bba782
Revert back to main to start from scratch with storing on Program per…
frodi-karlsson Mar 16, 2025
d1f5959
Try a v3 with parser-set fields, keep hacking at it
frodi-karlsson Mar 17, 2025
e5645d2
Merge branch 'main' into implied-node-format-setting
frodi-karlsson Mar 17, 2025
e825898
Resolve bad history linearly
frodi-karlsson Mar 17, 2025
1f3b470
Fix languageservice <-> project dependency muck up
frodi-karlsson Mar 17, 2025
1a66c19
Pass around package json type instead of cache entry
frodi-karlsson Mar 17, 2025
0112b00
Remove left-over !!!
frodi-karlsson Mar 17, 2025
8366e88
Remove GetImpliedNodeFormat
frodi-karlsson Mar 17, 2025
d33133b
Remove harnessUtil package json garbage
frodi-karlsson Mar 17, 2025
b81a9d1
Remove unnecessary newline
frodi-karlsson Mar 17, 2025
406918a
Move off of AST into a map in program
frodi-karlsson Mar 17, 2025
44d6aff
Move creation of metadata into fileloader
frodi-karlsson Mar 17, 2025
54fc786
Change arg to fileName and remove packageType argument in caching method
frodi-karlsson Mar 17, 2025
0644fd3
Appease linter and remove unnecessary convert
frodi-karlsson Mar 17, 2025
8d1f096
Use extension constants
frodi-karlsson Mar 18, 2025
344710a
Merge branch 'main' into implied-node-format-setting (-X theirs for
frodi-karlsson Mar 18, 2025
f6fcb49
Pass single sourceFileMetaData from program into emitContext
frodi-karlsson Mar 18, 2025
a1d63f5
Merge branch 'main' into implied-node-format-setting
frodi-karlsson Mar 18, 2025
3c902b8
Accept insane new diff wins
frodi-karlsson Mar 18, 2025
08333cf
Switch back to map
frodi-karlsson Mar 18, 2025
ba527ba
Use path as map key
frodi-karlsson Mar 18, 2025
4f740f1
Revert walking originals
frodi-karlsson Mar 18, 2025
d1e2b2e
Create MetaDataProvider
frodi-karlsson Mar 18, 2025
7da5ddc
Move commonjs metadata provider adding to struct declaration
frodi-karlsson Mar 18, 2025
e2c7a77
Use program as SourceFileMetaDataProvider and share fakeProgram in te…
frodi-karlsson Mar 18, 2025
3099425
Revert moving to util and create smaller fakeSourceFileMetaDataProvider
frodi-karlsson Mar 18, 2025
1697b75
Realign naming to provider
frodi-karlsson Mar 18, 2025
d86780d
Remove unnecessary struct
frodi-karlsson Mar 18, 2025
ac71490
Fix henceforth "load"SourceFileMetaData, spacings and leftover
frodi-karlsson Mar 18, 2025
286beed
Switch to RWMutex and include map creation in w lock
frodi-karlsson Mar 19, 2025
ed015b3
Fix deadlock
frodi-karlsson Mar 19, 2025
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
4 changes: 3 additions & 1 deletion internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"sync/atomic"

"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
)
Expand Down Expand Up @@ -8695,6 +8696,8 @@ type SourceFile struct {
ReferencedFiles []*FileReference
TypeReferenceDirectives []*FileReference
LibReferenceDirectives []*FileReference
PackageJsonScope *packagejson.InfoCacheEntry
ImpliedNodeFormat core.ModuleKind

// Fields set by binder

Expand Down Expand Up @@ -8723,7 +8726,6 @@ type SourceFile struct {

// !!!

ImpliedNodeFormat core.ModuleKind
CommonJsModuleIndicator *Node
ExternalModuleIndicator *Node
JsGlobalAugmentations SymbolTable
Expand Down
8 changes: 4 additions & 4 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2368,13 +2368,13 @@ func GetImpliedNodeFormatForEmitWorker(sourceFile *SourceFile, options *core.Com
return sourceFile.ImpliedNodeFormat
}
if sourceFile.ImpliedNodeFormat == core.ModuleKindCommonJS &&
( /*sourceFile.packageJsonScope.contents.packageJsonContent.type == "commonjs" ||*/ // !!!
tspath.FileExtensionIsOneOf(sourceFile.FileName(), []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
(sourceFile.PackageJsonScope != nil && sourceFile.PackageJsonScope.Contents.Type.Value != "module" || // !!!
tspath.FileExtensionIsOneOf(sourceFile.FileName(), []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
return core.ModuleKindCommonJS
}
if sourceFile.ImpliedNodeFormat == core.ModuleKindESNext &&
( /*sourceFile.packageJsonScope?.contents.packageJsonContent.type === "module" ||*/ // !!!
tspath.FileExtensionIsOneOf(sourceFile.fileName, []string{tspath.ExtensionMjs, tspath.ExtensionMts})) {
(sourceFile.PackageJsonScope != nil && sourceFile.PackageJsonScope.Contents.Type.Value == "module" ||
tspath.FileExtensionIsOneOf(sourceFile.fileName, []string{tspath.ExtensionMjs, tspath.ExtensionMts})) {
return core.ModuleKindESNext
}
return core.ModuleKindNone
Expand Down
4 changes: 2 additions & 2 deletions internal/astnav/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestGetTokenAtPosition(t *testing.T) {
return 0;
}
`
file := parser.ParseSourceFile("/file.ts", "/file.ts", fileText, core.ScriptTargetLatest, scanner.JSDocParsingModeParseAll)
file := parser.ParseSourceFile("/file.ts", "/file.ts", fileText, core.ScriptTargetLatest, scanner.JSDocParsingModeParseAll, core.ResolutionModeESM, nil)
assert.Equal(t, astnav.GetTokenAtPosition(file, 0), astnav.GetTokenAtPosition(file, 0))
})
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func baselineTokens(t *testing.T, testName string, getTSTokens func(fileText str
positions[i] = i
}
tsTokens := getTSTokens(string(fileText), positions)
file := parser.ParseSourceFile("/file.ts", "/file.ts", string(fileText), core.ScriptTargetLatest, scanner.JSDocParsingModeParseAll)
file := parser.ParseSourceFile("/file.ts", "/file.ts", string(fileText), core.ScriptTargetLatest, scanner.JSDocParsingModeParseAll, core.ResolutionModeESM, nil)

var output strings.Builder
currentRange := core.NewTextRange(0, 0)
Expand Down
2 changes: 1 addition & 1 deletion internal/binder/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func BenchmarkBind(b *testing.B) {

sourceFiles := make([]*ast.SourceFile, b.N)
for i := range b.N {
sourceFiles[i] = parser.ParseSourceFile(fileName, path, sourceText, core.ScriptTargetESNext, scanner.JSDocParsingModeParseAll)
sourceFiles[i] = parser.ParseSourceFile(fileName, path, sourceText, core.ScriptTargetESNext, scanner.JSDocParsingModeParseAll, core.ResolutionModeESM, nil)
}

compilerOptions := &core.CompilerOptions{Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext}
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (t *parseTask) start(loader *fileLoader) {

func (p *fileLoader) parseSourceFile(fileName string) *ast.SourceFile {
path := tspath.ToPath(fileName, p.host.GetCurrentDirectory(), p.host.FS().UseCaseSensitiveFileNames())
sourceFile := p.host.GetSourceFile(fileName, path, p.compilerOptions.GetEmitScriptTarget())
sourceFile := p.host.GetSourceFile(fileName, path, p.compilerOptions.GetEmitScriptTarget(), p.resolver.GetPackageScopeForPath(fileName))
return sourceFile
}

Expand Down
35 changes: 32 additions & 3 deletions internal/compiler/host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package compiler

import (
"strings"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/scanner"
Expand All @@ -15,7 +18,8 @@ type CompilerHost interface {
GetCurrentDirectory() string
NewLine() string
Trace(msg string)
GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget) *ast.SourceFile
GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget, packageJsonScope *packagejson.InfoCacheEntry) *ast.SourceFile
GetImpliedNodeFormat(fileName string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode
}

type FileInfo struct {
Expand Down Expand Up @@ -68,10 +72,35 @@ func (h *compilerHost) Trace(msg string) {
//!!! TODO: implement
}

func (h *compilerHost) GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget) *ast.SourceFile {
func (h *compilerHost) GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget, packageJsonScope *packagejson.InfoCacheEntry) *ast.SourceFile {
text, _ := h.FS().ReadFile(fileName)
if tspath.FileExtensionIs(fileName, tspath.ExtensionJson) {
return parser.ParseJSONText(fileName, path, text)
}
return parser.ParseSourceFile(fileName, path, text, languageVersion, scanner.JSDocParsingModeParseForTypeErrors)
return parser.ParseSourceFile(fileName, path, text, languageVersion, scanner.JSDocParsingModeParseForTypeErrors, h.GetImpliedNodeFormat(fileName, packageJsonScope), packageJsonScope)
}

func (h *compilerHost) GetImpliedNodeFormatForFileWorker(path string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode {
var moduleResolution core.ModuleResolutionKind
if h.options != nil {
moduleResolution = h.options.GetModuleResolutionKind()
}

shouldLookupFromPackageJson := core.ModuleResolutionKindNode16 <= moduleResolution && moduleResolution <= core.ModuleResolutionKindNodeNext || strings.Contains(path, "/node_modules/")

if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDmts, tspath.ExtensionMts, tspath.ExtensionMjs}) {
return core.ResolutionModeESM
}
if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDcts, tspath.ExtensionCts, tspath.ExtensionCjs}) {
return core.ResolutionModeCommonJS
}
if shouldLookupFromPackageJson && packageJsonScope != nil && tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDts, tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionJs, tspath.ExtensionJsx}) {
return core.IfElse(packageJsonScope.Contents.Type.Value == "module", core.ResolutionModeESM, core.ResolutionModeCommonJS)
}

return core.ResolutionModeNone
}

func (h *compilerHost) GetImpliedNodeFormat(fileName string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode {
return h.GetImpliedNodeFormatForFileWorker(fileName, packageJsonScope)
}
4 changes: 3 additions & 1 deletion internal/ls/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ls
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand All @@ -19,7 +20,8 @@ type Host interface {
GetRootFileNames() []string
// GetCompilerOptions was called GetCompilationSettings in the original code.
GetCompilerOptions() *core.CompilerOptions
GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget) *ast.SourceFile
GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget, packageJsonScope *packagejson.InfoCacheEntry) *ast.SourceFile
GetImpliedNodeFormat(fileName string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode
// This responsibility was moved from the language service to the project,
// because they were bidirectionally interdependent.
GetProgram() *compiler.Program
Expand Down
9 changes: 7 additions & 2 deletions internal/ls/languageservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ls
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand Down Expand Up @@ -46,8 +47,12 @@ func (l *LanguageService) Trace(msg string) {
}

// GetSourceFile implements compiler.CompilerHost.
func (l *LanguageService) GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget) *ast.SourceFile {
return l.host.GetSourceFile(fileName, path, languageVersion)
func (l *LanguageService) GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget, packageJsonScope *packagejson.InfoCacheEntry) *ast.SourceFile {
return l.host.GetSourceFile(fileName, path, languageVersion, packageJsonScope)
}

func (l *LanguageService) GetImpliedNodeFormat(fileName string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode {
return l.host.GetImpliedNodeFormat(fileName, packageJsonScope)
}

// GetProgram updates the program if the project version has changed.
Expand Down
33 changes: 21 additions & 12 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler/diagnostics"
"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/tspath"
Expand Down Expand Up @@ -51,14 +52,16 @@ type Parser struct {
scanner *scanner.Scanner
factory ast.NodeFactory

fileName string
path tspath.Path
sourceText string
languageVersion core.ScriptTarget
scriptKind core.ScriptKind
languageVariant core.LanguageVariant
diagnostics []*ast.Diagnostic
jsdocDiagnostics []*ast.Diagnostic
fileName string
path tspath.Path
sourceText string
languageVersion core.ScriptTarget
scriptKind core.ScriptKind
languageVariant core.LanguageVariant
diagnostics []*ast.Diagnostic
jsdocDiagnostics []*ast.Diagnostic
impliedNodeFormat core.ResolutionMode
packageJsonScope *packagejson.InfoCacheEntry

token ast.Kind
sourceFlags ast.NodeFlags
Expand Down Expand Up @@ -92,18 +95,18 @@ func putParser(p *Parser) {
parserPool.Put(p)
}

func ParseSourceFile(fileName string, path tspath.Path, sourceText string, languageVersion core.ScriptTarget, jsdocParsingMode scanner.JSDocParsingMode) *ast.SourceFile {
func ParseSourceFile(fileName string, path tspath.Path, sourceText string, languageVersion core.ScriptTarget, jsdocParsingMode scanner.JSDocParsingMode, impliedNodeFormat core.ResolutionMode, packageJsonScope *packagejson.InfoCacheEntry) *ast.SourceFile {
p := getParser()
defer putParser(p)
p.initializeState(fileName, path, sourceText, languageVersion, core.ScriptKindUnknown, jsdocParsingMode)
p.initializeState(fileName, path, sourceText, languageVersion, core.ScriptKindUnknown, jsdocParsingMode, impliedNodeFormat, packageJsonScope)
p.nextToken()
return p.parseSourceFileWorker()
}

func ParseJSONText(fileName string, path tspath.Path, sourceText string) *ast.SourceFile {
p := getParser()
defer putParser(p)
p.initializeState(fileName, path, sourceText, core.ScriptTargetES2015, core.ScriptKindJSON, scanner.JSDocParsingModeParseAll)
p.initializeState(fileName, path, sourceText, core.ScriptTargetES2015, core.ScriptKindJSON, scanner.JSDocParsingModeParseAll, core.ResolutionModeNone, nil)
p.nextToken()
pos := p.nodePos()
var statements *ast.NodeList
Expand Down Expand Up @@ -176,7 +179,7 @@ func ParseJSONText(fileName string, path tspath.Path, sourceText string) *ast.So
return result
}

func (p *Parser) initializeState(fileName string, path tspath.Path, sourceText string, languageVersion core.ScriptTarget, scriptKind core.ScriptKind, jsdocParsingMode scanner.JSDocParsingMode) {
func (p *Parser) initializeState(fileName string, path tspath.Path, sourceText string, languageVersion core.ScriptTarget, scriptKind core.ScriptKind, jsdocParsingMode scanner.JSDocParsingMode, impliedNodeFormat core.ResolutionMode, packageJsonScope *packagejson.InfoCacheEntry) {
if p.scanner == nil {
p.scanner = scanner.NewScanner()
} else {
Expand All @@ -196,6 +199,8 @@ func (p *Parser) initializeState(fileName string, path tspath.Path, sourceText s
default:
p.contextFlags = ast.NodeFlagsNone
}
p.impliedNodeFormat = impliedNodeFormat
p.packageJsonScope = packageJsonScope
p.scanner.SetText(p.sourceText)
p.scanner.SetOnError(p.scanError)
p.scanner.SetScriptTarget(p.languageVersion)
Expand Down Expand Up @@ -325,8 +330,12 @@ func (p *Parser) finishSourceFile(result *ast.SourceFile, isDeclarationFile bool
result.Flags |= p.sourceFlags
result.Identifiers = p.identifiers
result.SetJSDocCache(p.jsdocCache)
result.ImpliedNodeFormat = p.impliedNodeFormat
result.PackageJsonScope = p.packageJsonScope
p.jsdocCache = nil
p.identifiers = nil
p.impliedNodeFormat = core.ResolutionModeNone
p.packageJsonScope = nil
}

func (p *Parser) parseToplevelStatement(i int) *ast.Node {
Expand Down
6 changes: 3 additions & 3 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func BenchmarkParse(b *testing.B) {
b.Run(jsdoc.name, func(b *testing.B) {
jsdocMode := jsdoc.mode
for b.Loop() {
ParseSourceFile(fileName, path, sourceText, core.ScriptTargetESNext, jsdocMode)
ParseSourceFile(fileName, path, sourceText, core.ScriptTargetESNext, jsdocMode, core.ResolutionModeESM, nil)
}
})
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestParseTypeScriptRepo(t *testing.T) {
if strings.HasSuffix(f.name, ".json") {
sourceFile = ParseJSONText(fileName, path, string(sourceText))
} else {
sourceFile = ParseSourceFile(fileName, path, string(sourceText), core.ScriptTargetESNext, scanner.JSDocParsingModeParseAll)
sourceFile = ParseSourceFile(fileName, path, string(sourceText), core.ScriptTargetESNext, scanner.JSDocParsingModeParseAll, core.ResolutionModeESM, nil)
}

if !test.ignoreErrors {
Expand Down Expand Up @@ -179,6 +179,6 @@ func FuzzParser(f *testing.F) {
return
}

ParseSourceFile(fileName, path, sourceText, scriptTarget, jsdocParsingMode)
ParseSourceFile(fileName, path, sourceText, scriptTarget, jsdocParsingMode, core.ResolutionModeESM, nil)
})
}
4 changes: 2 additions & 2 deletions internal/project/documentregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (r *documentRegistry) getDocumentWorker(
// the script snapshot. If so, update it appropriately.
entry := entryAny.(*registryEntry)
if entry.sourceFile.Version != scriptInfo.version {
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfo.text, scriptTarget, scanner.JSDocParsingModeParseAll)
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfo.text, scriptTarget, scanner.JSDocParsingModeParseAll, core.ResolutionModeNone, nil)
sourceFile.Version = scriptInfo.version
entry.mu.Lock()
defer entry.mu.Unlock()
Expand All @@ -102,7 +102,7 @@ func (r *documentRegistry) getDocumentWorker(
return entry.sourceFile
} else {
// Have never seen this file with these settings. Create a new source file for it.
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfo.text, scriptTarget, scanner.JSDocParsingModeParseAll)
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfo.text, scriptTarget, scanner.JSDocParsingModeParseAll, core.ResolutionModeNone, nil)
sourceFile.Version = scriptInfo.version
entryAny, _ := r.documents.LoadOrStore(key, &registryEntry{
sourceFile: sourceFile,
Expand Down
24 changes: 23 additions & 1 deletion internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/ls"
"github.com/microsoft/typescript-go/internal/tspath"
Expand Down Expand Up @@ -124,7 +125,7 @@ func (p *Project) GetRootFileNames() []string {
}

// GetSourceFile implements LanguageServiceHost.
func (p *Project) GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget) *ast.SourceFile {
func (p *Project) GetSourceFile(fileName string, path tspath.Path, languageVersion core.ScriptTarget, packageJsonScope *packagejson.InfoCacheEntry) *ast.SourceFile {
scriptKind := p.getScriptKind(fileName)
if scriptInfo := p.getOrCreateScriptInfoAndAttachToProject(fileName, scriptKind); scriptInfo != nil {
var (
Expand All @@ -140,6 +141,27 @@ func (p *Project) GetSourceFile(fileName string, path tspath.Path, languageVersi
return nil
}

func (p *Project) GetImpliedNodeFormatForFileWorker(path string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode {
moduleResolution := p.GetProgram().Options().GetModuleResolutionKind()
shouldLookupFromPackageJson := core.ModuleResolutionKindNode16 <= moduleResolution && moduleResolution <= core.ModuleResolutionKindNodeNext || strings.Contains(path, "/node_modules/")

if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDmts, tspath.ExtensionMts, tspath.ExtensionMjs}) {
return core.ResolutionModeESM
}
if tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDcts, tspath.ExtensionCts, tspath.ExtensionCjs}) {
return core.ResolutionModeCommonJS
}
if shouldLookupFromPackageJson && tspath.FileExtensionIsOneOf(path, []string{tspath.ExtensionDts, tspath.ExtensionTs, tspath.ExtensionTsx, tspath.ExtensionJs, tspath.ExtensionJsx}) {
return core.IfElse(packageJsonScope.Contents.Type.Value == "module", core.ResolutionModeESM, core.ResolutionModeCommonJS)
}

return core.ResolutionModeNone
}

func (p *Project) GetImpliedNodeFormat(fileName string, packageJsonScope *packagejson.InfoCacheEntry) core.ResolutionMode {
return p.GetImpliedNodeFormatForFileWorker(fileName, packageJsonScope)
}

// GetProgram implements LanguageServiceHost. Updates the program if needed.
func (p *Project) GetProgram() *compiler.Program {
p.updateIfDirty()
Expand Down
Loading