Skip to content

Commit dc37b52

Browse files
authored
Fix nil pointer panic when extending an empty config file (#4377)
1 parent e578159 commit dc37b52

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

internal/tsoptions/tsconfigparsing.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,9 @@ func readJsonConfigFile(fileName string, path tspath.Path, readFile func(fileNam
960960
}, text, core.ScriptKindJSON),
961961
}, diagnostic
962962
} else {
963+
factory := &ast.NodeFactory{}
963964
file := &TsConfigSourceFile{
964-
SourceFile: (&ast.NodeFactory{}).NewSourceFile(ast.SourceFileParseOptions{FileName: fileName, Path: path}, "", nil, (&ast.NodeFactory{}).NewToken(ast.KindEndOfFile)).AsSourceFile(),
965+
SourceFile: factory.NewSourceFile(ast.SourceFileParseOptions{FileName: fileName, Path: path}, "", factory.NewNodeList([]*ast.Node{}), factory.NewToken(ast.KindEndOfFile)).AsSourceFile(),
965966
}
966967
file.SourceFile.SetDiagnostics(diagnostic)
967968
return file, diagnostic

internal/tsoptions/tsconfigparsing_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,40 @@ func TestParseJsonSourceFileConfigFileContentReportsInvalidExtendedConfig(t *tes
887887
}
888888
}
889889

890+
// Extending an empty config file used to panic on nil Statements (#4265).
891+
func TestParseJsonSourceFileConfigFileContentWithEmptyExtendedConfig(t *testing.T) {
892+
t.Parallel()
893+
files := map[string]string{
894+
"/project/tsconfig.json": `{
895+
"extends": "./base.json"
896+
}`,
897+
"/project/base.json": "",
898+
"/project/main.ts": "export const x = 1;",
899+
}
900+
host := tsoptionstest.NewVFSParseConfigHost(files, "/project", true /*useCaseSensitiveFileNames*/)
901+
configFileName := "/project/tsconfig.json"
902+
configFile := tsoptions.NewTsconfigSourceFileFromFilePath(
903+
configFileName,
904+
tspath.ToPath(configFileName, host.GetCurrentDirectory(), host.FS().UseCaseSensitiveFileNames()),
905+
files[configFileName],
906+
)
907+
908+
parsed := tsoptions.ParseJsonSourceFileConfigFileContent(
909+
configFile,
910+
host,
911+
host.GetCurrentDirectory(),
912+
nil,
913+
nil,
914+
configFileName,
915+
nil,
916+
nil,
917+
nil,
918+
)
919+
920+
assert.Assert(t, parsed != nil)
921+
assert.DeepEqual(t, parsed.FileNames(), []string{"/project/main.ts"})
922+
}
923+
890924
func TestParseJsonSourceFileConfigFileContentDoesNotDuplicateUnquotedKeyDiagnostics(t *testing.T) {
891925
t.Parallel()
892926
parsed := tsoptionstest.GetParsedCommandLine(t, `{

0 commit comments

Comments
 (0)