Skip to content

Commit 4252f06

Browse files
authored
Port type acquisition parsing from tsconfig.json (#852)
1 parent f822b4d commit 4252f06

File tree

84 files changed

+1045
-75
lines changed

Some content is hidden

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

84 files changed

+1045
-75
lines changed

internal/core/parsedoptions.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
type ParsedOptions struct {
44
CompilerOptions *CompilerOptions `json:"compilerOptions"`
55
WatchOptions *WatchOptions `json:"watchOptions"`
6+
TypeAcquisition *TypeAcquisition `json:"typeAcquisition"`
67

78
FileNames []string `json:"fileNames"`
89
ProjectReferences []ProjectReference `json:"projectReferences"`

internal/core/typeacquisition.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package core
2+
3+
type TypeAcquisition struct {
4+
Enable Tristate `json:"enable,omitzero"`
5+
Include []string `json:"include,omitzero"`
6+
Exclude []string `json:"exclude,omitzero"`
7+
DisableFilenameBasedTypeAcquisition Tristate `json:"disableFilenameBasedTypeAcquisition,omitzero"`
8+
}

internal/execute/tsc_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,30 @@ func TestExtends(t *testing.T) {
252252
c.verify(t, "extends")
253253
}
254254
}
255+
256+
func TestTypeAcquisition(t *testing.T) {
257+
t.Parallel()
258+
if !bundled.Embedded {
259+
// Without embedding, we'd need to read all of the lib files out from disk into the MapFS.
260+
// Just skip this for now.
261+
t.Skip("bundled files are not embedded")
262+
}
263+
(&tscInput{
264+
subScenario: "parse tsconfig with typeAcquisition",
265+
sys: newTestSys(FileMap{"/home/src/workspaces/project/tsconfig.json": `{
266+
"compilerOptions": {
267+
"composite": true,
268+
"noEmit": true,
269+
},
270+
"typeAcquisition": {
271+
"enable": true,
272+
"include": ["0.d.ts", "1.d.ts"],
273+
"exclude": ["0.js", "1.js"],
274+
"disableFilenameBasedTypeAcquisition": true,
275+
},
276+
}`},
277+
"/home/src/workspaces/project",
278+
),
279+
commandLineArgs: []string{},
280+
}).verify(t, "typeAcquisition")
281+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tsoptions
2+
3+
var typeAcquisitionDeclaration = &CommandLineOption{
4+
Name: "typeAcquisition",
5+
Kind: CommandLineOptionTypeObject,
6+
ElementOptions: commandLineOptionsToMap(typeAcquisitionDecls),
7+
}
8+
9+
// Do not delete this without updating the website's tsconfig generation.
10+
var typeAcquisitionDecls = []*CommandLineOption{
11+
{
12+
Name: "enable",
13+
Kind: CommandLineOptionTypeBoolean,
14+
DefaultValueDescription: false,
15+
},
16+
{
17+
Name: "include",
18+
Kind: CommandLineOptionTypeList,
19+
},
20+
{
21+
Name: "exclude",
22+
Kind: CommandLineOptionTypeList,
23+
},
24+
{
25+
Name: "disableFilenameBasedTypeAcquisition",
26+
Kind: CommandLineOptionTypeBoolean,
27+
DefaultValueDescription: false,
28+
},
29+
}

internal/tsoptions/parsinghelpers.go

+31
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func parseJsonToStringKey(json any) *collections.OrderedMap[string, any] {
109109
if v, ok := m.Get("excludes"); ok {
110110
result.Set("excludes", v)
111111
}
112+
if v, ok := m.Get("typeAcquisition"); ok {
113+
result.Set("typeAcquisition", v)
114+
}
112115
}
113116
return result
114117
}
@@ -133,6 +136,14 @@ func (o *watchOptionsParser) ParseOption(key string, value any) []*ast.Diagnosti
133136
return ParseWatchOptions(key, value, o.WatchOptions)
134137
}
135138

139+
type typeAcquisitionParser struct {
140+
*core.TypeAcquisition
141+
}
142+
143+
func (o *typeAcquisitionParser) ParseOption(key string, value any) []*ast.Diagnostic {
144+
return ParseTypeAcquisition(key, value, o.TypeAcquisition)
145+
}
146+
136147
func ParseCompilerOptions(key string, value any, allOptions *core.CompilerOptions) []*ast.Diagnostic {
137148
if value == nil {
138149
return nil
@@ -436,6 +447,26 @@ func ParseWatchOptions(key string, value any, allOptions *core.WatchOptions) []*
436447
return nil
437448
}
438449

450+
func ParseTypeAcquisition(key string, value any, allOptions *core.TypeAcquisition) []*ast.Diagnostic {
451+
if value == nil {
452+
return nil
453+
}
454+
if allOptions == nil {
455+
return nil
456+
}
457+
switch key {
458+
case "enable":
459+
allOptions.Enable = parseTristate(value)
460+
case "include":
461+
allOptions.Include = parseStringArray(value)
462+
case "exclude":
463+
allOptions.Exclude = parseStringArray(value)
464+
case "disableFilenameBasedTypeAcquisition":
465+
allOptions.DisableFilenameBasedTypeAcquisition = parseTristate(value)
466+
}
467+
return nil
468+
}
469+
439470
// mergeCompilerOptions merges the source compiler options into the target compiler options.
440471
// Fields in the source options will overwrite the corresponding fields in the target options.
441472
func mergeCompilerOptions(targetOptions, sourceOptions *core.CompilerOptions) *core.CompilerOptions {

internal/tsoptions/tsconfigparsing.go

+66-40
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ var tsconfigRootOptionsMap = &CommandLineOption{
5757
Kind: CommandLineOptionTypeObject,
5858
ElementOptions: commandLineOptionsToMap([]*CommandLineOption{
5959
compilerOptionsDeclaration,
60+
// watchOptionsDeclaration,
61+
typeAcquisitionDeclaration,
6062
extendsOptionDeclaration,
6163
{
6264
Name: "references",
@@ -107,8 +109,8 @@ type ExtendedConfigCacheEntry struct {
107109
type parsedTsconfig struct {
108110
raw any
109111
options *core.CompilerOptions
110-
// watchOptions *compiler.WatchOptions
111-
// typeAcquisition *compiler.TypeAcquisition
112+
// watchOptions *core.WatchOptions
113+
typeAcquisition *core.TypeAcquisition
112114
// Note that the case of the config path has not yet been normalized, as no files have been imported into the project yet
113115
extendedConfigPath any
114116
}
@@ -119,8 +121,8 @@ func parseOwnConfigOfJsonSourceFile(
119121
basePath string,
120122
configFileName string,
121123
) (*parsedTsconfig, []*ast.Diagnostic) {
122-
options := getDefaultCompilerOptions(configFileName)
123-
// var typeAcquisition *compiler.TypeAcquisition
124+
compilerOptions := getDefaultCompilerOptions(configFileName)
125+
typeAcquisition := getDefaultTypeAcquisition(configFileName)
124126
// var watchOptions *compiler.WatchOptions
125127
var extendedConfigPath any
126128
var rootCompilerOptions []*ast.PropertyName
@@ -139,7 +141,14 @@ func parseOwnConfigOfJsonSourceFile(
139141
}
140142
if parentOption != nil && parentOption.Name != "undefined" && value != nil {
141143
if option != nil && option.Name != "" {
142-
propertySetErrors = append(propertySetErrors, ParseCompilerOptions(option.Name, value, options)...)
144+
var parseDiagnostics []*ast.Diagnostic
145+
switch parentOption.Name {
146+
case "compilerOptions":
147+
parseDiagnostics = ParseCompilerOptions(option.Name, value, compilerOptions)
148+
case "typeAcquisition":
149+
parseDiagnostics = ParseTypeAcquisition(option.Name, value, typeAcquisition)
150+
}
151+
propertySetErrors = append(propertySetErrors, parseDiagnostics...)
143152
} else if keyText != "" {
144153
if parentOption.ElementOptions != nil {
145154
// !!! TODO: support suggestion
@@ -178,9 +187,9 @@ func parseOwnConfigOfJsonSourceFile(
178187
// }
179188
return &parsedTsconfig{
180189
raw: json,
181-
options: options,
190+
options: compilerOptions,
182191
// watchOptions: watchOptions,
183-
// typeAcquisition: typeAcquisition,
192+
typeAcquisition: typeAcquisition,
184193
extendedConfigPath: extendedConfigPath,
185194
}, errors
186195
}
@@ -479,19 +488,19 @@ type tsConfigOptions struct {
479488
notDefined string
480489
}
481490

482-
func commandLineOptionsToMap(options []*CommandLineOption) map[string]*CommandLineOption {
491+
func commandLineOptionsToMap(compilerOptions []*CommandLineOption) map[string]*CommandLineOption {
483492
result := make(map[string]*CommandLineOption)
484-
for i := range options {
485-
result[(options[i]).Name] = options[i]
493+
for i := range compilerOptions {
494+
result[(compilerOptions[i]).Name] = compilerOptions[i]
486495
}
487496
return result
488497
}
489498

490499
var commandLineCompilerOptionsMap map[string]*CommandLineOption = commandLineOptionsToMap(OptionsDeclarations)
491500

492-
func convertMapToOptions[O optionParser](options *collections.OrderedMap[string, any], result O) O {
501+
func convertMapToOptions[O optionParser](compilerOptions *collections.OrderedMap[string, any], result O) O {
493502
// this assumes any `key`, `value` pair in `options` will have `value` already be the correct type. this function should no error handling
494-
for key, value := range options.Entries() {
503+
for key, value := range compilerOptions.Entries() {
495504
result.ParseOption(key, value)
496505
}
497506
return result
@@ -758,6 +767,14 @@ func getDefaultCompilerOptions(configFileName string) *core.CompilerOptions {
758767
return options
759768
}
760769

770+
func getDefaultTypeAcquisition(configFileName string) *core.TypeAcquisition {
771+
options := &core.TypeAcquisition{}
772+
if configFileName != "" && tspath.GetBaseFileName(configFileName) == "jsconfig.json" {
773+
options.Enable = core.TSTrue
774+
}
775+
return options
776+
}
777+
761778
func convertCompilerOptionsFromJsonWorker(jsonOptions any, basePath string, configFileName string) (*core.CompilerOptions, []*ast.Diagnostic) {
762779
options := getDefaultCompilerOptions(configFileName)
763780
_, errors := convertOptionsFromJson(commandLineCompilerOptionsMap, jsonOptions, basePath, &compilerOptionsParser{options})
@@ -767,6 +784,12 @@ func convertCompilerOptionsFromJsonWorker(jsonOptions any, basePath string, conf
767784
return options, errors
768785
}
769786

787+
func convertTypeAcquisitionFromJsonWorker(jsonOptions any, basePath string, configFileName string) (*core.TypeAcquisition, []*ast.Diagnostic) {
788+
options := getDefaultTypeAcquisition(configFileName)
789+
_, errors := convertOptionsFromJson(typeAcquisitionDeclaration.ElementOptions, jsonOptions, basePath, &typeAcquisitionParser{options})
790+
return options, errors
791+
}
792+
770793
func parseOwnConfigOfJson(
771794
json *collections.OrderedMap[string, any],
772795
host ParseConfigHost,
@@ -778,8 +801,8 @@ func parseOwnConfigOfJson(
778801
errors = append(errors, ast.NewCompilerDiagnostic(diagnostics.Unknown_option_excludes_Did_you_mean_exclude))
779802
}
780803
options, err := convertCompilerOptionsFromJsonWorker(json.GetOrZero("compilerOptions"), basePath, configFileName)
781-
errors = append(errors, err...)
782-
// typeAcquisition := convertTypeAcquisitionFromJsonWorker(json.typeAcquisition, basePath, errors, configFileName)
804+
typeAcquisition, err2 := convertTypeAcquisitionFromJsonWorker(json.GetOrZero("typeAcquisition"), basePath, configFileName)
805+
errors = append(append(errors, err...), err2...)
783806
// watchOptions := convertWatchOptionsFromJsonWorker(json.watchOptions, basePath, errors)
784807
// json.compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors)
785808
var extendedConfigPath []string
@@ -790,6 +813,7 @@ func parseOwnConfigOfJson(
790813
parsedConfig := &parsedTsconfig{
791814
raw: json,
792815
options: options,
816+
typeAcquisition: typeAcquisition,
793817
extendedConfigPath: extendedConfigPath,
794818
}
795819
return parsedConfig, errors
@@ -1176,7 +1200,9 @@ func parseJsonConfigFileContentWorker(
11761200

11771201
return &ParsedCommandLine{
11781202
ParsedConfig: &core.ParsedOptions{
1179-
CompilerOptions: parsedConfig.options,
1203+
CompilerOptions: parsedConfig.options,
1204+
TypeAcquisition: parsedConfig.typeAcquisition,
1205+
// WatchOptions: nil,
11801206
FileNames: getFileNames(basePathForFileNames),
11811207
ProjectReferences: getProjectReferences(basePathForFileNames),
11821208
},
@@ -1321,43 +1347,43 @@ func substituteStringArrayWithConfigDirTemplate(list []string, basePath string)
13211347
}
13221348
}
13231349

1324-
func handleOptionConfigDirTemplateSubstitution(options *core.CompilerOptions, basePath string) {
1325-
if options == nil {
1350+
func handleOptionConfigDirTemplateSubstitution(compilerOptions *core.CompilerOptions, basePath string) {
1351+
if compilerOptions == nil {
13261352
return
13271353
}
13281354

13291355
// !!! don't hardcode this; use options declarations?
13301356

1331-
for v := range options.Paths.Values() {
1357+
for v := range compilerOptions.Paths.Values() {
13321358
substituteStringArrayWithConfigDirTemplate(v, basePath)
13331359
}
13341360

1335-
substituteStringArrayWithConfigDirTemplate(options.RootDirs, basePath)
1336-
substituteStringArrayWithConfigDirTemplate(options.TypeRoots, basePath)
1361+
substituteStringArrayWithConfigDirTemplate(compilerOptions.RootDirs, basePath)
1362+
substituteStringArrayWithConfigDirTemplate(compilerOptions.TypeRoots, basePath)
13371363

1338-
if startsWithConfigDirTemplate(options.GenerateCpuProfile) {
1339-
options.GenerateCpuProfile = getSubstitutedPathWithConfigDirTemplate(options.GenerateCpuProfile, basePath)
1364+
if startsWithConfigDirTemplate(compilerOptions.GenerateCpuProfile) {
1365+
compilerOptions.GenerateCpuProfile = getSubstitutedPathWithConfigDirTemplate(compilerOptions.GenerateCpuProfile, basePath)
13401366
}
1341-
if startsWithConfigDirTemplate(options.GenerateTrace) {
1342-
options.GenerateTrace = getSubstitutedPathWithConfigDirTemplate(options.GenerateTrace, basePath)
1367+
if startsWithConfigDirTemplate(compilerOptions.GenerateTrace) {
1368+
compilerOptions.GenerateTrace = getSubstitutedPathWithConfigDirTemplate(compilerOptions.GenerateTrace, basePath)
13431369
}
1344-
if startsWithConfigDirTemplate(options.OutFile) {
1345-
options.OutFile = getSubstitutedPathWithConfigDirTemplate(options.OutFile, basePath)
1370+
if startsWithConfigDirTemplate(compilerOptions.OutFile) {
1371+
compilerOptions.OutFile = getSubstitutedPathWithConfigDirTemplate(compilerOptions.OutFile, basePath)
13461372
}
1347-
if startsWithConfigDirTemplate(options.OutDir) {
1348-
options.OutDir = getSubstitutedPathWithConfigDirTemplate(options.OutDir, basePath)
1373+
if startsWithConfigDirTemplate(compilerOptions.OutDir) {
1374+
compilerOptions.OutDir = getSubstitutedPathWithConfigDirTemplate(compilerOptions.OutDir, basePath)
13491375
}
1350-
if startsWithConfigDirTemplate(options.RootDir) {
1351-
options.RootDir = getSubstitutedPathWithConfigDirTemplate(options.RootDir, basePath)
1376+
if startsWithConfigDirTemplate(compilerOptions.RootDir) {
1377+
compilerOptions.RootDir = getSubstitutedPathWithConfigDirTemplate(compilerOptions.RootDir, basePath)
13521378
}
1353-
if startsWithConfigDirTemplate(options.TsBuildInfoFile) {
1354-
options.TsBuildInfoFile = getSubstitutedPathWithConfigDirTemplate(options.TsBuildInfoFile, basePath)
1379+
if startsWithConfigDirTemplate(compilerOptions.TsBuildInfoFile) {
1380+
compilerOptions.TsBuildInfoFile = getSubstitutedPathWithConfigDirTemplate(compilerOptions.TsBuildInfoFile, basePath)
13551381
}
1356-
if startsWithConfigDirTemplate(options.BaseUrl) {
1357-
options.BaseUrl = getSubstitutedPathWithConfigDirTemplate(options.BaseUrl, basePath)
1382+
if startsWithConfigDirTemplate(compilerOptions.BaseUrl) {
1383+
compilerOptions.BaseUrl = getSubstitutedPathWithConfigDirTemplate(compilerOptions.BaseUrl, basePath)
13581384
}
1359-
if startsWithConfigDirTemplate(options.DeclarationDir) {
1360-
options.DeclarationDir = getSubstitutedPathWithConfigDirTemplate(options.DeclarationDir, basePath)
1385+
if startsWithConfigDirTemplate(compilerOptions.DeclarationDir) {
1386+
compilerOptions.DeclarationDir = getSubstitutedPathWithConfigDirTemplate(compilerOptions.DeclarationDir, basePath)
13611387
}
13621388
}
13631389

@@ -1517,8 +1543,8 @@ func getFileNamesFromConfigSpecs(
15171543
return files
15181544
}
15191545

1520-
func GetSupportedExtensions(options *core.CompilerOptions, extraFileExtensions []fileExtensionInfo) [][]string {
1521-
needJSExtensions := options.GetAllowJS()
1546+
func GetSupportedExtensions(compilerOptions *core.CompilerOptions, extraFileExtensions []fileExtensionInfo) [][]string {
1547+
needJSExtensions := compilerOptions.GetAllowJS()
15221548
if len(extraFileExtensions) == 0 {
15231549
if needJSExtensions {
15241550
return tspath.AllSupportedExtensions
@@ -1543,8 +1569,8 @@ func GetSupportedExtensions(options *core.CompilerOptions, extraFileExtensions [
15431569
return extensions
15441570
}
15451571

1546-
func GetSupportedExtensionsWithJsonIfResolveJsonModule(options *core.CompilerOptions, supportedExtensions [][]string) [][]string {
1547-
if options == nil || !options.GetResolveJsonModule() {
1572+
func GetSupportedExtensionsWithJsonIfResolveJsonModule(compilerOptions *core.CompilerOptions, supportedExtensions [][]string) [][]string {
1573+
if compilerOptions == nil || !compilerOptions.GetResolveJsonModule() {
15481574
return supportedExtensions
15491575
}
15501576
if core.Same(supportedExtensions, tspath.AllSupportedExtensions) {

0 commit comments

Comments
 (0)