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
8 changes: 5 additions & 3 deletions tsc/internal/tsoptions/tsconfigparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,11 @@ func GetContentMapperOptionDiagnosticLocation(config *ParsedCommandLine, mapper
if node == nil {
node = mapperNode
}
// An entry inherited through "extends" has no syntax in this config file, so its diagnostic has
// no location, like other program-time config errors.
if node == nil {
return nil, core.UndefinedTextRange()
}
for _, segment := range path {
var next *ast.Node
switch {
Expand All @@ -1825,9 +1830,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
150 changes: 150 additions & 0 deletions tsc/internal/tsoptions/tsconfigparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,156 @@ 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)

// The project's own config file has no "contentMappers" syntax, so the diagnostic has no location,
// like other program-time config errors from an extended config.
for name, test := range map[string]struct {
mapper *contentmapper.Mapper
path []contentmapper.OptionPathSegment
}{
"nested option": {
mapper: parsed.ContentMappers()[0],
path: []contentmapper.OptionPathSegment{{Property: "plugins"}, {Index: 0, IsIndex: true}, {Property: "name"}},
},
"array element": {
mapper: parsed.ContentMappers()[0],
path: []contentmapper.OptionPathSegment{{Property: "plugins"}, {Index: 0, IsIndex: true}},
},
"options object": {
mapper: parsed.ContentMappers()[0],
},
"unknown mapper": {
mapper: &contentmapper.Mapper{},
path: []contentmapper.OptionPathSegment{{Property: "plugins"}},
},
} {
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, loc, core.UndefinedTextRange())
})
}
}

func TestContentMapperOptionDiagnosticLocationWithExtends(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") + ` }`,
},
},
"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 } }`,
},
},
"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"},
})
if test.wantFile == "" {
assert.Assert(t, file == nil)
assert.Equal(t, loc, core.UndefinedTextRange())
return
}
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::
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; };