Skip to content

Commit 7bcea23

Browse files
authored
Hardcode contents of typesMap.json (#1002)
1 parent e8d352c commit 7bcea23

File tree

5 files changed

+544
-117
lines changed

5 files changed

+544
-117
lines changed

internal/project/ti.go renamed to internal/project/ata.go

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ import (
1414
"github.com/microsoft/typescript-go/internal/tspath"
1515
)
1616

17-
type TypesMapFile struct {
18-
TypesMap map[string]SafeListEntry `json:"typesMap"`
19-
SimpleMap map[string]string `json:"simpleMap,omitzero"`
20-
}
21-
22-
type SafeListEntry struct {
23-
Match string `json:"match"`
24-
Exclude [][]any `json:"exclude"`
25-
Types []string `json:"types"`
26-
}
27-
2817
type PendingRequest struct {
2918
requestId int32
3019
packageNames []string
@@ -65,8 +54,6 @@ type TypingsInstaller struct {
6554
missingTypingsSet collections.SyncMap[string, bool]
6655

6756
typesRegistry map[string]map[string]string
68-
typesMap *TypesMapFile
69-
safeList map[string]string
7057

7158
installRunCount atomic.Int32
7259
inFlightRequestCount int
@@ -142,7 +129,7 @@ func (ti *TypingsInstaller) InstallPackage(p *Project, fileName string, packageN
142129

143130
func (ti *TypingsInstaller) EnqueueInstallTypingsRequest(p *Project, typingsInfo *TypingsInfo) {
144131
// because we arent using buffers, no need to throttle for requests here
145-
p.Log("TI:: Got install request for: " + p.Name())
132+
p.Log("ATA:: Got install request for: " + p.Name())
146133
go ti.discoverAndInstallTypings(
147134
p,
148135
typingsInfo,
@@ -154,15 +141,12 @@ func (ti *TypingsInstaller) EnqueueInstallTypingsRequest(p *Project, typingsInfo
154141
func (ti *TypingsInstaller) discoverAndInstallTypings(p *Project, typingsInfo *TypingsInfo, fileNames []string, projectRootPath string) {
155142
ti.init((p))
156143

157-
ti.initializeSafeList(p)
158-
159144
cachedTypingPaths, newTypingNames, filesToWatch := DiscoverTypings(
160145
p.FS(),
161146
p.Log,
162147
typingsInfo,
163148
fileNames,
164149
projectRootPath,
165-
ti.safeList,
166150
&ti.packageNameToTypingLocation,
167151
ti.typesRegistry,
168152
)
@@ -178,9 +162,9 @@ func (ti *TypingsInstaller) discoverAndInstallTypings(p *Project, typingsInfo *T
178162
ti.installTypings(p, typingsInfo, requestId, cachedTypingPaths, filteredTypings)
179163
return
180164
}
181-
p.Log("TI:: All typings are known to be missing or invalid - no need to install more typings")
165+
p.Log("ATA:: All typings are known to be missing or invalid - no need to install more typings")
182166
} else {
183-
p.Log("TI:: No new typings were requested as a result of typings discovery")
167+
p.Log("ATA:: No new typings were requested as a result of typings discovery")
184168
}
185169
p.UpdateTypingFiles(typingsInfo, cachedTypingPaths)
186170
// !!! sheetal events to send
@@ -275,7 +259,7 @@ func (ti *TypingsInstaller) invokeRoutineToInstallTypings(
275259
}
276260

277261
if success {
278-
p.Logf("TI:: Installed typings %v", packageNames)
262+
p.Logf("ATA:: Installed typings %v", packageNames)
279263
var installedTypingFiles []string
280264
resolver := module.NewResolver(p, &core.CompilerOptions{ModuleResolution: core.ModuleResolutionKindNodeNext}, "", "")
281265
for _, packageName := range request.filteredTypings {
@@ -296,12 +280,12 @@ func (ti *TypingsInstaller) invokeRoutineToInstallTypings(
296280
ti.packageNameToTypingLocation.Store(packageName, newTyping)
297281
installedTypingFiles = append(installedTypingFiles, typingFile)
298282
}
299-
p.Logf("TI:: Installed typing files %v", installedTypingFiles)
283+
p.Logf("ATA:: Installed typing files %v", installedTypingFiles)
300284
p.UpdateTypingFiles(request.typingsInfo, append(request.currentlyCachedTypings, installedTypingFiles...))
301285
// DO we really need these events
302286
// this.event(response, "setTypings");
303287
} else {
304-
p.Logf("TI:: install request failed, marking packages as missing to prevent repeated requests: %v", request.filteredTypings)
288+
p.Logf("ATA:: install request failed, marking packages as missing to prevent repeated requests: %v", request.filteredTypings)
305289
for _, typing := range request.filteredTypings {
306290
ti.missingTypingsSet.Store(typing, true)
307291
}
@@ -362,15 +346,15 @@ func (ti *TypingsInstaller) installWorker(
362346
success bool,
363347
),
364348
) {
365-
p.Logf("TI:: #%d with cwd: %s arguments: %v", requestId, cwd, packageNames)
349+
p.Logf("ATA:: #%d with cwd: %s arguments: %v", requestId, cwd, packageNames)
366350
hasError := InstallNpmPackages(packageNames, func(packageNames []string, hasError *atomic.Bool) {
367351
var npmArgs []string
368352
npmArgs = append(npmArgs, "install", "--ignore-scripts")
369353
npmArgs = append(npmArgs, packageNames...)
370354
npmArgs = append(npmArgs, "--save-dev", "--user-agent=\"typesInstaller/"+core.Version()+"\"")
371355
output, err := ti.options.NpmInstall(cwd, npmArgs)
372356
if err != nil {
373-
p.Logf("TI:: Output is: %s", output)
357+
p.Logf("ATA:: Output is: %s", output)
374358
hasError.Store(true)
375359
}
376360
})
@@ -418,23 +402,23 @@ func (ti *TypingsInstaller) filterTypings(
418402
for _, typing := range typingsToInstall {
419403
typingKey := module.MangleScopedPackageName(typing)
420404
if _, ok := ti.missingTypingsSet.Load(typingKey); ok {
421-
p.Logf("TI:: '%s':: '%s' is in missingTypingsSet - skipping...", typing, typingKey)
405+
p.Logf("ATA:: '%s':: '%s' is in missingTypingsSet - skipping...", typing, typingKey)
422406
continue
423407
}
424408
validationResult, name, isScopeName := ValidatePackageName(typing)
425409
if validationResult != NameOk {
426410
// add typing name to missing set so we won't process it again
427411
ti.missingTypingsSet.Store(typingKey, true)
428-
p.Log("TI:: " + RenderPackageNameValidationFailure(typing, validationResult, name, isScopeName))
412+
p.Log("ATA:: " + RenderPackageNameValidationFailure(typing, validationResult, name, isScopeName))
429413
continue
430414
}
431415
typesRegistryEntry, ok := ti.typesRegistry[typingKey]
432416
if !ok {
433-
p.Logf("TI:: '%s':: Entry for package '%s' does not exist in local types registry - skipping...", typing, typingKey)
417+
p.Logf("ATA:: '%s':: Entry for package '%s' does not exist in local types registry - skipping...", typing, typingKey)
434418
continue
435419
}
436420
if typingLocation, ok := ti.packageNameToTypingLocation.Load(typingKey); ok && IsTypingUpToDate(typingLocation, typesRegistryEntry) {
437-
p.Logf("TI:: '%s':: '%s' already has an up-to-date typing - skipping...", typing, typingKey)
421+
p.Logf("ATA:: '%s':: '%s' already has an up-to-date typing - skipping...", typing, typingKey)
438422
continue
439423
}
440424
result = append(result, typingKey)
@@ -444,7 +428,7 @@ func (ti *TypingsInstaller) filterTypings(
444428

445429
func (ti *TypingsInstaller) init(p *Project) {
446430
ti.initOnce.Do(func() {
447-
p.Log("TI:: Global cache location '" + ti.TypingsLocation + "'") //, safe file path '" + safeListPath + "', types map path '" + typesMapLocation + "`")
431+
p.Log("ATA:: Global cache location '" + ti.TypingsLocation + "'") //, safe file path '" + safeListPath + "', types map path '" + typesMapLocation + "`")
448432
ti.processCacheLocation(p)
449433

450434
// !!! sheetal handle npm path here if we would support it
@@ -459,11 +443,11 @@ func (ti *TypingsInstaller) init(p *Project) {
459443
// }
460444

461445
ti.ensureTypingsLocationExists(p)
462-
p.Log("TI:: Updating types-registry@latest npm package...")
446+
p.Log("ATA:: Updating types-registry@latest npm package...")
463447
if _, err := ti.options.NpmInstall(ti.TypingsLocation, []string{"install", "--ignore-scripts", "types-registry@latest"}); err == nil {
464-
p.Log("TI:: Updated types-registry npm package")
448+
p.Log("ATA:: Updated types-registry npm package")
465449
} else {
466-
p.Logf("TI:: Error updating types-registry package: %v", err)
450+
p.Logf("ATA:: Error updating types-registry package: %v", err)
467451
// !!! sheetal events to send
468452
// // store error info to report it later when it is known that server is already listening to events from typings installer
469453
// this.delayedInitializationError = {
@@ -496,18 +480,18 @@ type NpmLock struct {
496480
}
497481

498482
func (ti *TypingsInstaller) processCacheLocation(p *Project) {
499-
p.Log("TI:: Processing cache location " + ti.TypingsLocation)
483+
p.Log("ATA:: Processing cache location " + ti.TypingsLocation)
500484
packageJson := tspath.CombinePaths(ti.TypingsLocation, "package.json")
501485
packageLockJson := tspath.CombinePaths(ti.TypingsLocation, "package-lock.json")
502-
p.Log("TI:: Trying to find '" + packageJson + "'...")
486+
p.Log("ATA:: Trying to find '" + packageJson + "'...")
503487
if p.FS().FileExists(packageJson) && p.FS().FileExists((packageLockJson)) {
504488
var npmConfig NpmConfig
505489
npmConfigContents := parseNpmConfigOrLock(p, packageJson, &npmConfig)
506490
var npmLock NpmLock
507491
npmLockContents := parseNpmConfigOrLock(p, packageLockJson, &npmLock)
508492

509-
p.Log("TI:: Loaded content of " + packageJson + ": " + npmConfigContents)
510-
p.Log("TI:: Loaded content of " + packageLockJson + ": " + npmLockContents)
493+
p.Log("ATA:: Loaded content of " + packageJson + ": " + npmConfigContents)
494+
p.Log("ATA:: Loaded content of " + packageLockJson + ": " + npmLockContents)
511495

512496
// !!! sheetal strada uses Node10
513497
resolver := module.NewResolver(p, &core.CompilerOptions{ModuleResolution: core.ModuleResolutionKindNodeNext}, "", "")
@@ -535,9 +519,9 @@ func (ti *TypingsInstaller) processCacheLocation(p *Project) {
535519
if existingTypingFile.TypingsLocation == typingFile {
536520
continue
537521
}
538-
p.Log("TI:: New typing for package " + packageName + " from " + typingFile + " conflicts with existing typing file " + existingTypingFile.TypingsLocation)
522+
p.Log("ATA:: New typing for package " + packageName + " from " + typingFile + " conflicts with existing typing file " + existingTypingFile.TypingsLocation)
539523
}
540-
p.Log("TI:: Adding entry into typings cache: " + packageName + " => " + typingFile)
524+
p.Log("ATA:: Adding entry into typings cache: " + packageName + " => " + typingFile)
541525
version := npmLockValue.Version
542526
if version == "" {
543527
continue
@@ -551,7 +535,7 @@ func (ti *TypingsInstaller) processCacheLocation(p *Project) {
551535
}
552536
}
553537
}
554-
p.Log("TI:: Finished processing cache location " + ti.TypingsLocation)
538+
p.Log("ATA:: Finished processing cache location " + ti.TypingsLocation)
555539
}
556540

557541
func parseNpmConfigOrLock[T NpmConfig | NpmLock](p *Project, location string, config *T) string {
@@ -562,13 +546,13 @@ func parseNpmConfigOrLock[T NpmConfig | NpmLock](p *Project, location string, co
562546

563547
func (ti *TypingsInstaller) ensureTypingsLocationExists(p *Project) {
564548
npmConfigPath := tspath.CombinePaths(ti.TypingsLocation, "package.json")
565-
p.Log("TI:: Npm config file: " + npmConfigPath)
549+
p.Log("ATA:: Npm config file: " + npmConfigPath)
566550

567551
if !p.FS().FileExists(npmConfigPath) {
568-
p.Logf("TI:: Npm config file: '%s' is missing, creating new one...", npmConfigPath)
552+
p.Logf("ATA:: Npm config file: '%s' is missing, creating new one...", npmConfigPath)
569553
err := p.FS().WriteFile(npmConfigPath, "{ \"private\": true }", false)
570554
if err != nil {
571-
p.Logf("TI:: Npm config file write failed: %v", err)
555+
p.Logf("ATA:: Npm config file write failed: %v", err)
572556
}
573557
}
574558
}
@@ -589,61 +573,13 @@ func (ti *TypingsInstaller) loadTypesRegistryFile(p *Project) map[string]map[str
589573
return typesRegistry
590574
}
591575
}
592-
p.Logf("TI:: Error when loading types registry file '%s': %v", typesRegistryFile, err)
576+
p.Logf("ATA:: Error when loading types registry file '%s': %v", typesRegistryFile, err)
593577
} else {
594-
p.Logf("TI:: Error reading types registry file '%s'", typesRegistryFile)
578+
p.Logf("ATA:: Error reading types registry file '%s'", typesRegistryFile)
595579
}
596580
return map[string]map[string]string{}
597581
}
598582

599-
func (ti *TypingsInstaller) initializeSafeList(p *Project) {
600-
if ti.safeList != nil {
601-
return
602-
}
603-
ti.loadTypesMap(p)
604-
if ti.typesMap.SimpleMap != nil {
605-
p.Logf("TI:: Loaded safelist from types map file '%s'", tspath.CombinePaths(p.DefaultLibraryPath(), "typesMap.json"))
606-
ti.safeList = ti.typesMap.SimpleMap
607-
return
608-
}
609-
610-
p.Logf("TI:: Failed to load safelist from types map file '$%s'", tspath.CombinePaths(p.DefaultLibraryPath(), "typesMap.json"))
611-
ti.loadSafeList(p)
612-
}
613-
614-
func (ti *TypingsInstaller) loadTypesMap(p *Project) {
615-
if ti.typesMap != nil {
616-
return
617-
}
618-
typesMapLocation := tspath.CombinePaths(p.DefaultLibraryPath(), "typesMap.json")
619-
typesMapContents, ok := p.FS().ReadFile(typesMapLocation)
620-
if ok {
621-
err := json.Unmarshal([]byte(typesMapContents), &ti.typesMap)
622-
if err != nil {
623-
return
624-
}
625-
p.Logf("TI:: Error when parsing typesMapLocation '%s': %v", typesMapLocation, err)
626-
} else {
627-
p.Logf("TI:: Error reading typesMapLocation '%s'", typesMapLocation)
628-
}
629-
ti.typesMap = &TypesMapFile{}
630-
}
631-
632-
func (ti *TypingsInstaller) loadSafeList(p *Project) {
633-
safeListLocation := tspath.CombinePaths(p.DefaultLibraryPath(), "typingSafeList.json")
634-
safeListContents, ok := p.FS().ReadFile(safeListLocation)
635-
if ok {
636-
err := json.Unmarshal([]byte(safeListContents), &ti.safeList)
637-
if err != nil {
638-
return
639-
}
640-
p.Logf("TI:: Error when parsing safeListLocation '%s': %v", safeListLocation, err)
641-
} else {
642-
p.Logf("TI:: Error reading safeListLocation '%s'", safeListLocation)
643-
}
644-
ti.safeList = map[string]string{}
645-
}
646-
647583
func NpmInstall(cwd string, npmInstallArgs []string) ([]byte, error) {
648584
cmd := exec.Command("npm", npmInstallArgs...)
649585
cmd.Dir = cwd

internal/project/ti_test.go renamed to internal/project/ata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gotest.tools/v3/assert"
1313
)
1414

15-
func TestTi(t *testing.T) {
15+
func TestAta(t *testing.T) {
1616
t.Parallel()
1717
if !bundled.Embedded {
1818
t.Skip("bundled files are not embedded")

internal/project/discovertypings.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func DiscoverTypings(
3535
typingsInfo *TypingsInfo,
3636
fileNames []string,
3737
projectRootPath string,
38-
safeList map[string]string,
3938
packageNameToTypingLocation *collections.SyncMap[string, *CachedTyping],
4039
typesRegistry map[string]map[string]string,
4140
) (cachedTypingPaths []string, newTypingNames []string, filesToWatch []string) {
@@ -66,7 +65,7 @@ func DiscoverTypings(
6665
}
6766

6867
if !typingsInfo.TypeAcquisition.DisableFilenameBasedTypeAcquisition.IsTrue() {
69-
getTypingNamesFromSourceFileNames(fs, log, inferredTypings, safeList, fileNames)
68+
getTypingNamesFromSourceFileNames(fs, log, inferredTypings, fileNames)
7069
}
7170

7271
// add typings for unresolved imports
@@ -76,7 +75,7 @@ func DiscoverTypings(
7675
// Remove typings that the user has added to the exclude list
7776
for _, excludeTypingName := range exclude {
7877
delete(inferredTypings, excludeTypingName)
79-
log(fmt.Sprintf("TI:: Typing for %s is in exclude list, will be ignored.", excludeTypingName))
78+
log(fmt.Sprintf("ATA:: Typing for %s is in exclude list, will be ignored.", excludeTypingName))
8079
}
8180

8281
// Add the cached typing locations for inferred typings that are already installed
@@ -95,7 +94,7 @@ func DiscoverTypings(
9594
newTypingNames = append(newTypingNames, typing)
9695
}
9796
}
98-
log(fmt.Sprintf("TI:: Finished typings discovery: cachedTypingsPaths: %v newTypingNames: %v, filesToWatch %v", cachedTypingPaths, newTypingNames, filesToWatch))
97+
log(fmt.Sprintf("ATA:: Finished typings discovery: cachedTypingsPaths: %v newTypingNames: %v, filesToWatch %v", cachedTypingPaths, newTypingNames, filesToWatch))
9998
return cachedTypingPaths, newTypingNames, filesToWatch
10099
}
101100

@@ -111,7 +110,7 @@ func addInferredTypings(
111110
inferredTypings map[string]string,
112111
typingNames []string, message string,
113112
) {
114-
log(fmt.Sprintf("TI:: %s: %v", message, typingNames))
113+
log(fmt.Sprintf("ATA:: %s: %v", message, typingNames))
115114
for _, typingName := range typingNames {
116115
addInferredTyping(inferredTypings, typingName)
117116
}
@@ -127,7 +126,6 @@ func getTypingNamesFromSourceFileNames(
127126
fs vfs.FS,
128127
log func(s string),
129128
inferredTypings map[string]string,
130-
safeList map[string]string,
131129
fileNames []string,
132130
) {
133131
hasJsxFile := false
@@ -136,15 +134,15 @@ func getTypingNamesFromSourceFileNames(
136134
hasJsxFile = hasJsxFile || tspath.FileExtensionIs(fileName, tspath.ExtensionJsx)
137135
inferredTypingName := tspath.RemoveFileExtension(tspath.ToFileNameLowerCase(tspath.GetBaseFileName(fileName)))
138136
cleanedTypingName := removeMinAndVersionNumbers(inferredTypingName)
139-
if safeName, ok := safeList[cleanedTypingName]; ok {
140-
fromFileNames = append(fromFileNames, safeName)
137+
if typeName, ok := safeFileNameToTypeName[cleanedTypingName]; ok {
138+
fromFileNames = append(fromFileNames, typeName)
141139
}
142140
}
143141
if len(fromFileNames) > 0 {
144142
addInferredTypings(fs, log, inferredTypings, fromFileNames, "Inferred typings from file names")
145143
}
146144
if hasJsxFile {
147-
log("TI:: Inferred 'react' typings due to presence of '.jsx' extension")
145+
log("ATA:: Inferred 'react' typings due to presence of '.jsx' extension")
148146
addInferredTyping(inferredTypings, "react")
149147
}
150148
}
@@ -244,7 +242,7 @@ func addTypingNamesAndGetFilesToWatch(
244242

245243
}
246244

247-
log(fmt.Sprintf("TI:: Searching for typing names in %s; all files: %v", packagesFolderPath, dependencyManifestNames))
245+
log(fmt.Sprintf("ATA:: Searching for typing names in %s; all files: %v", packagesFolderPath, dependencyManifestNames))
248246

249247
// Once we have the names of things to look up, we iterate over
250248
// and either collect their included typings, or add them to the
@@ -267,10 +265,10 @@ func addTypingNamesAndGetFilesToWatch(
267265
if len(ownTypes) != 0 {
268266
absolutePath := tspath.GetNormalizedAbsolutePath(ownTypes, tspath.GetDirectoryPath(manifestPath))
269267
if fs.FileExists(absolutePath) {
270-
log(fmt.Sprintf("TI:: Package '%s' provides its own types.", manifest.Name.Value))
268+
log(fmt.Sprintf("ATA:: Package '%s' provides its own types.", manifest.Name.Value))
271269
inferredTypings[manifest.Name.Value] = absolutePath
272270
} else {
273-
log(fmt.Sprintf("TI:: Package '%s' provides its own types but they are missing.", manifest.Name.Value))
271+
log(fmt.Sprintf("ATA:: Package '%s' provides its own types but they are missing.", manifest.Name.Value))
274272
}
275273
} else {
276274
packageNames = append(packageNames, manifest.Name.Value)

0 commit comments

Comments
 (0)