Skip to content

Commit 228ebe4

Browse files
committed
Typings Intaller initial work
1 parent d0c9e32 commit 228ebe4

File tree

22 files changed

+1641
-101
lines changed

22 files changed

+1641
-101
lines changed

cmd/tsgo/api.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/microsoft/typescript-go/internal/api"
1111
"github.com/microsoft/typescript-go/internal/bundled"
1212
"github.com/microsoft/typescript-go/internal/core"
13+
"github.com/microsoft/typescript-go/internal/project"
1314
)
1415

1516
func runAPI(args []string) int {
@@ -20,14 +21,15 @@ func runAPI(args []string) int {
2021
}
2122

2223
defaultLibraryPath := bundled.LibPath()
23-
24+
typingsLocation := project.GetGlobalTypingsCacheLocation()
2425
s := api.NewServer(&api.ServerOptions{
2526
In: os.Stdin,
2627
Out: os.Stdout,
2728
Err: os.Stderr,
2829
Cwd: *cwd,
2930
NewLine: "\n",
3031
DefaultLibraryPath: defaultLibraryPath,
32+
TypingsLocation: typingsLocation,
3133
})
3234

3335
if err := s.Run(); err != nil && !errors.Is(err, io.EOF) {

cmd/tsgo/lsp.go

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/microsoft/typescript-go/internal/core"
1212
"github.com/microsoft/typescript-go/internal/lsp"
1313
"github.com/microsoft/typescript-go/internal/pprof"
14+
"github.com/microsoft/typescript-go/internal/project"
1415
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
1516
)
1617

@@ -39,6 +40,7 @@ func runLSP(args []string) int {
3940

4041
fs := bundled.WrapFS(osvfs.FS())
4142
defaultLibraryPath := bundled.LibPath()
43+
typingsLocation := project.GetGlobalTypingsCacheLocation()
4244

4345
s := lsp.NewServer(&lsp.ServerOptions{
4446
In: os.Stdin,
@@ -47,6 +49,7 @@ func runLSP(args []string) int {
4749
Cwd: core.Must(os.Getwd()),
4850
FS: fs,
4951
DefaultLibraryPath: defaultLibraryPath,
52+
TypingsLocation: typingsLocation,
5053
})
5154

5255
if err := s.Run(); err != nil && !errors.Is(err, io.EOF) {

internal/api/api.go

+22
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type API struct {
3939
symbols handleMap[ast.Symbol]
4040
typesMu sync.Mutex
4141
types handleMap[checker.Type]
42+
43+
typingsInstaller *project.TypingsInstaller
4244
}
4345

4446
var _ project.ProjectHost = (*API)(nil)
@@ -72,6 +74,21 @@ func (api *API) DefaultLibraryPath() string {
7274
return api.host.DefaultLibraryPath()
7375
}
7476

77+
// TypingsInstaller implements ProjectHost
78+
func (api *API) TypingsInstaller() *project.TypingsInstaller {
79+
if api.typingsInstaller != nil {
80+
return api.typingsInstaller
81+
}
82+
83+
if typingsLocation := api.host.TypingsLocation(); typingsLocation != "" {
84+
api.typingsInstaller = &project.TypingsInstaller{
85+
TypingsLocation: typingsLocation,
86+
ThrottleLimit: 5,
87+
}
88+
}
89+
return api.typingsInstaller
90+
}
91+
7592
// DocumentRegistry implements ProjectHost.
7693
func (api *API) DocumentRegistry() *project.DocumentRegistry {
7794
return api.documentRegistry
@@ -109,6 +126,11 @@ func (api *API) Log(s string) {
109126
api.options.Logger.Info(s)
110127
}
111128

129+
// Log implements ProjectHost.
130+
func (api *API) HasLevel(level project.LogLevel) bool {
131+
return api.options.Logger.HasLevel(level)
132+
}
133+
112134
// NewLine implements ProjectHost.
113135
func (api *API) NewLine() string {
114136
return api.host.NewLine()

internal/api/host.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "github.com/microsoft/typescript-go/internal/vfs"
55
type APIHost interface {
66
FS() vfs.FS
77
DefaultLibraryPath() string
8+
TypingsLocation() string
89
GetCurrentDirectory() string
910
NewLine() string
1011
}

internal/api/server.go

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type ServerOptions struct {
5959
Cwd string
6060
NewLine string
6161
DefaultLibraryPath string
62+
TypingsLocation string
6263
}
6364

6465
var (
@@ -75,6 +76,7 @@ type Server struct {
7576
newLine string
7677
fs vfs.FS
7778
defaultLibraryPath string
79+
typingsLocation string
7880

7981
callbackMu sync.Mutex
8082
enabledCallbacks Callback
@@ -97,6 +99,7 @@ func NewServer(options *ServerOptions) *Server {
9799
newLine: options.NewLine,
98100
fs: bundled.WrapFS(osvfs.FS()),
99101
defaultLibraryPath: options.DefaultLibraryPath,
102+
typingsLocation: options.TypingsLocation,
100103
}
101104
logger := project.NewLogger([]io.Writer{options.Err}, "", project.LogLevelVerbose)
102105
api := NewAPI(server, APIOptions{
@@ -127,6 +130,11 @@ func (s *Server) NewLine() string {
127130
return s.newLine
128131
}
129132

133+
// TypingsInstaller implements APIHost
134+
func (s *Server) TypingsLocation() string {
135+
return s.typingsLocation
136+
}
137+
130138
func (s *Server) Run() error {
131139
for {
132140
messageType, method, payload, err := s.readRequest("")

internal/compiler/fileloader.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile)
381381
// This may still end up being an untyped module -- the file won't be included but imports will be allowed.
382382
hasAllowedExtension := false
383383
if p.compilerOptions.ResolveJsonModule.IsTrue() {
384-
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsWithJsonFlat)
384+
hasAllowedExtension = tspath.ExtensionIsOneOf(resolution.Extension, tspath.SupportedTSExtensionsWithJsonFlat)
385385
} else if p.compilerOptions.AllowJs.IsTrue() {
386-
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedJSExtensionsFlat) || tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsFlat)
386+
hasAllowedExtension = tspath.ExtensionIsOneOf(resolution.Extension, tspath.SupportedJSExtensionsFlat) || tspath.ExtensionIsOneOf(resolution.Extension, tspath.SupportedTSExtensionsFlat)
387387
} else {
388-
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsFlat)
388+
hasAllowedExtension = tspath.ExtensionIsOneOf(resolution.Extension, tspath.SupportedTSExtensionsFlat)
389389
}
390390
shouldAddFile := resolution.IsResolved() && hasAllowedExtension
391391
// TODO(ercornel): !!!: other checks on whether or not to add the file

internal/compiler/program.go

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type ProgramOptions struct {
2727
SingleThreaded bool
2828
ProjectReference []core.ProjectReference
2929
ConfigFileParsingDiagnostics []*ast.Diagnostic
30+
31+
TypingsLocation string
32+
ProjectName string
3033
}
3134

3235
type Program struct {
@@ -135,6 +138,8 @@ func NewProgram(options ProgramOptions) *Program {
135138
}
136139

137140
p.resolver = module.NewResolver(p.host, p.compilerOptions)
141+
p.resolver.TypingsLocation = p.programOptions.TypingsLocation
142+
p.resolver.ProjectName = p.programOptions.ProjectName
138143

139144
var libs []string
140145

internal/core/nodemodules.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package core
2+
3+
import "maps"
4+
5+
var UnprefixedNodeCoreModules = map[string]bool{
6+
"assert": true,
7+
"assert/strict": true,
8+
"async_hooks": true,
9+
"buffer": true,
10+
"child_process": true,
11+
"cluster": true,
12+
"console": true,
13+
"constants": true,
14+
"crypto": true,
15+
"dgram": true,
16+
"diagnostics_channel": true,
17+
"dns": true,
18+
"dns/promises": true,
19+
"domain": true,
20+
"events": true,
21+
"fs": true,
22+
"fs/promises": true,
23+
"http": true,
24+
"http2": true,
25+
"https": true,
26+
"inspector": true,
27+
"inspector/promises": true,
28+
"module": true,
29+
"net": true,
30+
"os": true,
31+
"path": true,
32+
"path/posix": true,
33+
"path/win32": true,
34+
"perf_hooks": true,
35+
"process": true,
36+
"punycode": true,
37+
"querystring": true,
38+
"readline": true,
39+
"readline/promises": true,
40+
"repl": true,
41+
"stream": true,
42+
"stream/consumers": true,
43+
"stream/promises": true,
44+
"stream/web": true,
45+
"string_decoder": true,
46+
"sys": true,
47+
"test/mock_loader": true,
48+
"timers": true,
49+
"timers/promises": true,
50+
"tls": true,
51+
"trace_events": true,
52+
"tty": true,
53+
"url": true,
54+
"util": true,
55+
"util/types": true,
56+
"v8": true,
57+
"vm": true,
58+
"wasi": true,
59+
"worker_threads": true,
60+
"zlib": true,
61+
}
62+
63+
var ExclusivelyPrefixedNodeCoreModules = map[string]bool{
64+
"node:sea": true,
65+
"node:sqlite": true,
66+
"node:test": true,
67+
"node:test/reporters": true,
68+
}
69+
70+
var nodeCoreModules = map[string]bool{}
71+
72+
func ensureNodeCoreModules() {
73+
if len(nodeCoreModules) != 0 {
74+
return
75+
}
76+
for unprefixed := range UnprefixedNodeCoreModules {
77+
nodeCoreModules[unprefixed] = true
78+
nodeCoreModules["node:"+unprefixed] = true
79+
}
80+
maps.Copy(nodeCoreModules, ExclusivelyPrefixedNodeCoreModules)
81+
}
82+
83+
func NonRelativeModuleNameForTypingCache(moduleName string) string {
84+
ensureNodeCoreModules()
85+
if nodeCoreModules[moduleName] {
86+
return "node"
87+
}
88+
return moduleName
89+
}

internal/core/version.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package core
22

33
const (
4-
VersionMajorMinor = "7.0"
5-
Version = "7.0.0-dev"
4+
VersionMajorMinor = "5.9"
5+
Version = "5.9.0-dev"
66
)

internal/lsp/server.go

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type ServerOptions struct {
2626
NewLine core.NewLineKind
2727
FS vfs.FS
2828
DefaultLibraryPath string
29+
TypingsLocation string
2930
}
3031

3132
func NewServer(opts *ServerOptions) *Server {
@@ -40,6 +41,7 @@ func NewServer(opts *ServerOptions) *Server {
4041
newLine: opts.NewLine,
4142
fs: opts.FS,
4243
defaultLibraryPath: opts.DefaultLibraryPath,
44+
typingsLocation: opts.TypingsLocation,
4345
}
4446
}
4547

@@ -58,6 +60,7 @@ type Server struct {
5860
newLine core.NewLineKind
5961
fs vfs.FS
6062
defaultLibraryPath string
63+
typingsLocation string
6164

6265
initializeParams *lsproto.InitializeParams
6366
positionEncoding lsproto.PositionEncodingKind
@@ -77,6 +80,11 @@ func (s *Server) DefaultLibraryPath() string {
7780
return s.defaultLibraryPath
7881
}
7982

83+
// TypingsLocation implements project.ProjectServiceHost.
84+
func (s *Server) TypingsLocation() string {
85+
return s.typingsLocation
86+
}
87+
8088
// GetCurrentDirectory implements project.ProjectServiceHost.
8189
func (s *Server) GetCurrentDirectory() string {
8290
return s.cwd

internal/module/resolver.go

+32
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ type Resolver struct {
109109
caches
110110
host ResolutionHost
111111
compilerOptions *core.CompilerOptions
112+
TypingsLocation string
113+
ProjectName string
112114
// reportDiagnostic: DiagnosticReporter
113115
}
114116

@@ -229,6 +231,36 @@ func (r *Resolver) ResolveModuleName(moduleName string, containingFile string, r
229231
}
230232
}
231233

234+
return r.tryResolveFromTypingsLocation(moduleName, containingDirectory, result)
235+
}
236+
237+
func (r *Resolver) tryResolveFromTypingsLocation(moduleName string, containingDirectory string, originalResult *ResolvedModule) *ResolvedModule {
238+
if r.TypingsLocation == "" ||
239+
tspath.IsExternalModuleNameRelative(moduleName) ||
240+
(originalResult.ResolvedFileName != "" && tspath.ExtensionIsOneOf(originalResult.Extension, tspath.SupportedTSExtensionsWithJsonFlat)) {
241+
return originalResult
242+
}
243+
244+
state := newResolutionState(
245+
moduleName,
246+
containingDirectory,
247+
false, /*isTypeReferenceDirective*/
248+
core.ModuleKindNone, // resolutionMode,
249+
r.compilerOptions,
250+
nil, // redirectedReference,
251+
r,
252+
)
253+
if r.traceEnabled() {
254+
r.host.Trace(diagnostics.Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2.Format(r.ProjectName, moduleName, r.TypingsLocation))
255+
}
256+
globalResolved := state.loadModuleFromImmediateNodeModulesDirectory(extensionsDeclaration, r.TypingsLocation, false)
257+
if globalResolved == nil {
258+
return originalResult
259+
}
260+
result := state.createResolvedModule(globalResolved, true)
261+
result.FailedLookupLocations = append(originalResult.FailedLookupLocations, result.FailedLookupLocations...)
262+
result.AffectingLocations = append(originalResult.AffectingLocations, result.AffectingLocations...)
263+
result.ResolutionDiagnostics = append(originalResult.ResolutionDiagnostics, result.ResolutionDiagnostics...)
232264
return result
233265
}
234266

internal/packagejson/packagejson.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type PathFields struct {
2222

2323
type DependencyFields struct {
2424
Dependencies Expected[map[string]string] `json:"dependencies"`
25+
DevDependencies Expected[map[string]string] `json:"devDependencies"`
2526
PeerDependencies Expected[map[string]string] `json:"peerDependencies"`
2627
OptionalDependencies Expected[map[string]string] `json:"optionalDependencies"`
2728
}

0 commit comments

Comments
 (0)