diff --git a/tsc/internal/execute/tsctests/tsc_test.go b/tsc/internal/execute/tsctests/tsc_test.go index ee429a826f94a..4b8ac3f5891e8 100644 --- a/tsc/internal/execute/tsctests/tsc_test.go +++ b/tsc/internal/execute/tsctests/tsc_test.go @@ -5519,6 +5519,29 @@ func TestTscContentMapperOptionDiagnostics(t *testing.T) { }).run(t, "contentMapperOptionDiagnostics") } +func TestTscContentMapperOptionDiagnosticsFromExtendedConfig(t *testing.T) { + t.Parallel() + (&tscInput{ + subScenario: "mapper option diagnostic from extended config in another directory", + files: FileMap{ + "/home/src/workspaces/base/tsconfig.base.json": stringtestutil.Dedent(` + { + "contentMappers": [ + { + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + } + ] + }`), + "/home/src/workspaces/project/tsconfig.json": `{ "extends": "../base/tsconfig.base.json" }`, + "/home/src/workspaces/project/app.vue": `export const value = 1;`, + "/home/src/workspaces/node_modules/mapper/package.json": contentmappertest.PackageJSON(contentmappertest.DynamicVerbatimMapper), + }, + commandLineArgs: []string{"--runExternalCode", "--pretty", "false"}, + }).run(t, "contentMapperOptionDiagnostics") +} + func TestTscContentMapperFailures(t *testing.T) { t.Parallel() failMapperPackageJSON := stringtestutil.Dedent(` diff --git a/tsc/internal/tsoptions/parsedcommandline.go b/tsc/internal/tsoptions/parsedcommandline.go index 8a116bf3fcb46..2d850750bdc77 100644 --- a/tsc/internal/tsoptions/parsedcommandline.go +++ b/tsc/internal/tsoptions/parsedcommandline.go @@ -76,6 +76,9 @@ type ParsedCommandLine struct { contentMapperExtensionRewritesOnce sync.Once contentMapperFileMatcherOnce sync.Once contentMapperFileMatcher *contentmapper.FileMatcher + // contentMapperSourceFile is the config file whose "contentMappers" setting is in effect: + // this config's own file, or the extended config the setting was inherited from. + contentMapperSourceFile *ast.SourceFile locale locale.Locale localeOnce sync.Once @@ -115,6 +118,7 @@ func (p *ParsedCommandLine) WithFileNames(fileNames []tspath.RootedFilePath) *Pa includeGlobs: p.includeGlobs, literalFileNamesLen: p.literalFileNamesLen, rootFileNamesForDiagnostics: p.rootFileNamesForDiagnostics, + contentMapperSourceFile: p.contentMapperSourceFile, } } @@ -586,6 +590,7 @@ func (p *ParsedCommandLine) ReloadFileNamesOfParsedCommandLine(fs vfs.FS) *Parse includeGlobs: p.includeGlobs, literalFileNamesLen: literalFileNamesLen, rootFileNamesForDiagnostics: p.rootFileNamesForDiagnostics, + contentMapperSourceFile: p.contentMapperSourceFile, } return &parsedCommandLine } diff --git a/tsc/internal/tsoptions/tsconfigparsing.go b/tsc/internal/tsoptions/tsconfigparsing.go index 92ba6b48d09ad..89d3fd9c504b7 100644 --- a/tsc/internal/tsoptions/tsconfigparsing.go +++ b/tsc/internal/tsoptions/tsconfigparsing.go @@ -1615,6 +1615,8 @@ func parseJsonConfigFileContentWorker( baseDirectory: basePathForFileNames, caseSensitivity: fs.CaseSensitivity(), literalFileNamesLen: literalFileNamesLen, + + contentMapperSourceFile: contentMapperSourceFile, } } @@ -1801,12 +1803,21 @@ func GetContentMapperOptionDiagnosticLocation(config *ParsedCommandLine, mapper if config == nil || config.ConfigFile == nil { return nil, core.UndefinedTextRange() } + // The "contentMappers" setting may be inherited through "extends", so look for its syntax in the + // config file that declared it, as the diagnostics reported while parsing it do. + file := config.contentMapperSourceFile + if file == nil { + file = config.ConfigFile.SourceFile + } index := slices.Index(config.ContentMappers(), mapper) - mapperNode := getContentMapperSyntax(config.ConfigFile.SourceFile, index, "") - node := getContentMapperSyntax(config.ConfigFile.SourceFile, index, "options") + mapperNode := getContentMapperSyntax(file, index, "") + node := getContentMapperSyntax(file, index, "options") if node == nil { node = mapperNode } + if node == nil { + return nil, core.UndefinedTextRange() + } for _, segment := range path { var next *ast.Node switch { @@ -1825,10 +1836,6 @@ func GetContentMapperOptionDiagnosticLocation(config *ParsedCommandLine, mapper } node = next } - if node == nil { - return nil, core.UndefinedTextRange() - } - file := config.ConfigFile.SourceFile return file, core.NewTextRange(scanner.SkipTrivia(file.Text(), node.Pos()), node.End()) } diff --git a/tsc/internal/tsoptions/tsconfigparsing_test.go b/tsc/internal/tsoptions/tsconfigparsing_test.go index 5c66aa449b4be..f5a67e50521f7 100644 --- a/tsc/internal/tsoptions/tsconfigparsing_test.go +++ b/tsc/internal/tsoptions/tsconfigparsing_test.go @@ -1398,6 +1398,163 @@ func TestContentMapperOptionDiagnosticLocation(t *testing.T) { assert.Equal(t, file.Text()[loc.Pos():loc.End()], "1") } +func TestContentMapperOptionDiagnosticLocationInheritedFromExtendedConfig(t *testing.T) { + t.Parallel() + config := testConfig{ + jsonText: `{ "extends": "../base/tsconfig.base.json" }`, + configFileName: "tsconfig.json", + basePath: "/project", + allFileList: map[string]string{ + "/base/tsconfig.base.json": `{ + "contentMappers": [{ + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + }] + }`, + "/project/index.ts": "export {};", + "/node_modules/mapper/package.json": `{ "name": "mapper", "version": "1.0.0", "typescript": { "contentMapper": { "exec": ["mapper"] } } }`, + }, + existingOptions: &core.CompilerOptions{RunExternalCode: core.TSTrue}, + } + fs := tsoptionstest.NewVFS(config.allFileList, tspath.CaseSensitive /*caseSensitivity*/) + parsed := getParsedWithJsonSourceFileApi(config, fs, config.basePath) + assert.Equal(t, len(parsed.Errors), 0) + assert.Equal(t, len(parsed.ContentMappers()), 1) + + for name, test := range map[string]struct { + mapper *contentmapper.Mapper + path []contentmapper.OptionPathSegment + want string + }{ + "nested option": { + mapper: parsed.ContentMappers()[0], + path: []contentmapper.OptionPathSegment{{Property: "plugins"}, {Index: 0, IsIndex: true}, {Property: "name"}}, + want: "1", + }, + "array element": { + mapper: parsed.ContentMappers()[0], + path: []contentmapper.OptionPathSegment{{Property: "plugins"}, {Index: 0, IsIndex: true}}, + want: `{ "name": 1 }`, + }, + "options object": { + mapper: parsed.ContentMappers()[0], + want: `{ "plugins": [{ "name": 1 }] }`, + }, + "unknown mapper": { + mapper: &contentmapper.Mapper{}, + path: []contentmapper.OptionPathSegment{{Property: "plugins"}}, + want: `[{ + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + }]`, + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + file, loc := tsoptions.GetContentMapperOptionDiagnosticLocation(parsed, test.mapper, test.path) + assert.Assert(t, file != nil) + assert.Equal(t, file.FileName(), tspath.RootedFilePath("/base/tsconfig.base.json")) + assert.Equal(t, file.Text()[loc.Pos():loc.End()], test.want) + }) + } +} + +func TestContentMapperOptionDiagnosticLocationIsInDeclaringConfig(t *testing.T) { + t.Parallel() + contentMappers := func(name string) string { + return `"contentMappers": [{ "package": "mapper", "extensions": [".vue"], "options": { "plugins": [{ "name": "` + name + `" }] } }]` + } + for name, test := range map[string]struct { + jsonText string + files map[string]string + wantFile tspath.RootedFilePath + want string + }{ + "two levels of extends": { + jsonText: `{ "extends": "../config/middle.json" }`, + files: map[string]string{ + "/config/middle.json": `{ "extends": "./base.json" }`, + "/config/base.json": `{ ` + contentMappers("base") + ` }`, + }, + wantFile: "/config/base.json", + want: `"base"`, + }, + "earlier entry of an extends list": { + jsonText: `{ "extends": ["../config/base.json", "../config/other.json"] }`, + files: map[string]string{ + "/config/base.json": `{ ` + contentMappers("base") + ` }`, + "/config/other.json": `{ "compilerOptions": { "strict": true } }`, + }, + wantFile: "/config/base.json", + want: `"base"`, + }, + "overridden in the child": { + jsonText: `{ "extends": "../config/base.json", ` + contentMappers("child") + ` }`, + files: map[string]string{ + "/config/base.json": `{ ` + contentMappers("base") + ` }`, + }, + wantFile: "/project/tsconfig.json", + want: `"child"`, + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + config := testConfig{ + jsonText: test.jsonText, + configFileName: "tsconfig.json", + basePath: "/project", + allFileList: map[string]string{ + "/project/index.ts": "export {};", + "/node_modules/mapper/package.json": `{ "name": "mapper", "version": "1.0.0", "typescript": { "contentMapper": { "exec": ["mapper"] } } }`, + }, + existingOptions: &core.CompilerOptions{RunExternalCode: core.TSTrue}, + } + maps.Copy(config.allFileList, test.files) + fs := tsoptionstest.NewVFS(config.allFileList, tspath.CaseSensitive /*caseSensitivity*/) + parsed := getParsedWithJsonSourceFileApi(config, fs, config.basePath) + assert.Equal(t, len(parsed.Errors), 0) + assert.Equal(t, len(parsed.ContentMappers()), 1) + file, loc := tsoptions.GetContentMapperOptionDiagnosticLocation(parsed, parsed.ContentMappers()[0], []contentmapper.OptionPathSegment{ + {Property: "plugins"}, + {Index: 0, IsIndex: true}, + {Property: "name"}, + }) + assert.Assert(t, file != nil) + assert.Equal(t, file.FileName(), test.wantFile) + assert.Equal(t, file.Text()[loc.Pos():loc.End()], test.want) + }) + } +} + +func TestContentMapperOptionDiagnosticLocationWithoutContentMappersSyntax(t *testing.T) { + t.Parallel() + config := testConfig{ + jsonText: `{}`, + configFileName: "tsconfig.json", + basePath: "/", + allFileList: map[string]string{ + "/index.ts": "export {};", + }, + } + fs := tsoptionstest.NewVFS(config.allFileList, tspath.CaseSensitive /*caseSensitivity*/) + parsed := getParsedWithJsonSourceFileApi(config, fs, config.basePath) + mapper := &contentmapper.Mapper{} + parsed.ParsedConfig.ContentMappers = []*contentmapper.Mapper{mapper} + for name, path := range map[string][]contentmapper.OptionPathSegment{ + "property": {{Property: "plugins"}}, + "index": {{Index: 0, IsIndex: true}}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + file, loc := tsoptions.GetContentMapperOptionDiagnosticLocation(parsed, mapper, path) + assert.Assert(t, file == nil) + assert.Equal(t, loc, core.UndefinedTextRange()) + }) + } +} + func TestContentMappersAreInheritedFromExtendedConfig(t *testing.T) { t.Parallel() config := testConfig{ diff --git a/tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js b/tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js new file mode 100644 index 0000000000000..52cc168d820b4 --- /dev/null +++ b/tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js @@ -0,0 +1,52 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/base/tsconfig.base.json] *new* +{ + "contentMappers": [ + { + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + } + ] +} +//// [/home/src/workspaces/node_modules/mapper/package.json] *new* +{ + "name": "mapper", + "version": "1.0.0", + "typescript": { "contentMapper": { "exec": ["dynamic-verbatim-mapper"], "dynamicConfig": true } } +} +//// [/home/src/workspaces/project/app.vue] *new* +export const value = 1; +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ "extends": "../base/tsconfig.base.json" } + +tsc --runExternalCode --pretty false +ExitStatus:: DiagnosticsPresent_OutputsGenerated +Output:: +../base/tsconfig.base.json(6,48): error mapper123: Option 'name' requires a string. +//// [/home/src/tslibs/TS/Lib/lib.es2026.full.d.ts] *Lib* +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } +interface ReadonlyArray {} +interface SymbolConstructor { + (desc?: string | number): symbol; + for(name: string): symbol; + readonly toStringTag: symbol; +} +declare var Symbol: SymbolConstructor; +interface Symbol { + readonly [Symbol.toStringTag]: string; +} +declare const console: { log(msg: any): void; }; +