Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions tsc/internal/execute/tsctests/tsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
5 changes: 5 additions & 0 deletions tsc/internal/tsoptions/parsedcommandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,6 +118,7 @@ func (p *ParsedCommandLine) WithFileNames(fileNames []tspath.RootedFilePath) *Pa
includeGlobs: p.includeGlobs,
literalFileNamesLen: p.literalFileNamesLen,
rootFileNamesForDiagnostics: p.rootFileNamesForDiagnostics,
contentMapperSourceFile: p.contentMapperSourceFile,
}
}

Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 13 additions & 6 deletions tsc/internal/tsoptions/tsconfigparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,8 @@ func parseJsonConfigFileContentWorker(
baseDirectory: basePathForFileNames,
caseSensitivity: fs.CaseSensitivity(),
literalFileNamesLen: literalFileNamesLen,

contentMapperSourceFile: contentMapperSourceFile,
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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())
}

Expand Down
157 changes: 157 additions & 0 deletions tsc/internal/tsoptions/tsconfigparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
@@ -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*
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
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; };