Skip to content
Merged
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
18 changes: 18 additions & 0 deletions internal/execute/tsctests/showconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ func TestShowConfig(t *testing.T) {
},
commandLineArgs: []string{"-p", "tsconfig.json", "--showConfig"},
},
{
subScenario: "Show TSConfig with exclude and outDir",
files: FileMap{
"/home/src/workspaces/project/src/index.ts": `export const a = 1;`,
"/home/src/workspaces/project/src/bin/tool.ts": `export const b = 2;`,
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
{
"compilerOptions": {
"strict": true,
"outDir": "./build"
},
"exclude": [
"build"
]
}`),
},
commandLineArgs: []string{"-p", "tsconfig.json", "--showConfig"},
},
}

for _, test := range testCases {
Expand Down
53 changes: 4 additions & 49 deletions internal/tsoptions/showconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/microsoft/typescript-go/internal/debug"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs/vfsmatch"
)

// computeFn wraps a typed getter method so it can be stored in an impliedOption's
Expand Down Expand Up @@ -63,29 +62,18 @@ type TSConfig struct {
// ConvertToTSConfig generates a complete tsconfig representation for --showConfig output,
// matching the behavior of TypeScript's convertToTSConfig function.
func ConvertToTSConfig(configParseResult *ParsedCommandLine, configFileName string) *TSConfig {
if configFileName == "" {
configFileName = "tsconfig.json"
}
normalizedConfigPath := tspath.GetNormalizedAbsolutePath(configFileName, configParseResult.GetCurrentDirectory())
comparePathsOptions := tspath.ComparePathsOptions{
CurrentDirectory: configParseResult.GetCurrentDirectory(),
UseCaseSensitiveFileNames: configParseResult.UseCaseSensitiveFileNames(),
}

// Build file list, filtering out files that match the include/exclude specs
var fileFilter func(string) bool
if configParseResult.ConfigFile != nil && configParseResult.ConfigFile.configFileSpecs != nil &&
len(configParseResult.ConfigFile.configFileSpecs.validatedIncludeSpecs) > 0 {
fileFilter = matchesSpecs(
configFileName,
configParseResult.ConfigFile.configFileSpecs.validatedIncludeSpecs,
configParseResult.ConfigFile.configFileSpecs.validatedExcludeSpecs,
configParseResult.UseCaseSensitiveFileNames(),
configParseResult.GetCurrentDirectory(),
)
}
// Build the list of all resolved files as relative paths from the config file.
var files []string
for _, f := range configParseResult.FileNames() {
if fileFilter != nil && !fileFilter(f) {
continue
}
normalizedFilePath := tspath.GetNormalizedAbsolutePath(f, configParseResult.GetCurrentDirectory())
relativePath := tspath.GetRelativePathFromFile(normalizedConfigPath, normalizedFilePath, comparePathsOptions)
files = append(files, relativePath)
Comment thread
jakebailey marked this conversation as resolved.
Expand Down Expand Up @@ -399,36 +387,3 @@ func serializeImpliedOptionValue(optionDecl *CommandLineOption, value any) any {
}
return value
}

// matchesSpecs returns a filter function that determines whether a file should appear
// in the --showConfig "files" list. It returns true for files to keep, false for files
// to omit. Files that match the include globs (and are not excluded) return false,
// since they're already covered by the "include" field.
func matchesSpecs(configFileName string, includeSpecs []string, excludeSpecs []string, useCaseSensitiveFileNames bool, currentDirectory string) func(string) bool {
if len(includeSpecs) == 0 {
return nil
}
// Use the directory containing the tsconfig, not the file itself, as the base path
// for wildcard pattern matching.
configDir := tspath.GetDirectoryPath(tspath.GetNormalizedAbsolutePath(configFileName, currentDirectory))

includeMatcher := vfsmatch.NewSpecMatcher(includeSpecs, configDir, vfsmatch.UsageFiles, useCaseSensitiveFileNames)
excludeMatcher := vfsmatch.NewSpecMatcher(excludeSpecs, configDir, vfsmatch.UsageExclude, useCaseSensitiveFileNames)

if includeMatcher != nil {
if excludeMatcher != nil {
return func(path string) bool {
return !(includeMatcher.MatchString(path) && !excludeMatcher.MatchString(path))
}
}
return func(path string) bool {
return !includeMatcher.MatchString(path)
}
}
if excludeMatcher != nil {
return func(path string) bool {
return excludeMatcher.MatchString(path)
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Output::
"path": "./test"
}
],
"files": [
"./src/index.ts"
],
"include": [
"src/*"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
currentDirectory::/home/src/workspaces/project
useCaseSensitiveFileNames::true
Input::
//// [/home/src/workspaces/project/src/bin/tool.ts] *new*
export const b = 2;
//// [/home/src/workspaces/project/src/index.ts] *new*
export const a = 1;
//// [/home/src/workspaces/project/tsconfig.json] *new*
{
"compilerOptions": {
"strict": true,
"outDir": "./build"
},
"exclude": [
"build"
]
}

tsgo -p tsconfig.json --showConfig
ExitStatus:: Success
Output::
{
"compilerOptions": {
"outDir": "./build",
"strict": true
},
"files": [
"./src/index.ts",
"./src/bin/tool.ts"
],
"exclude": [
"build"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Output::
"compilerOptions": {
"strict": true
},
"files": [
"./src/index.ts"
],
"exclude": [
"test"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Output::
"strict": true
},
"files": [
"./extra.ts"
"./extra.ts",
"./src/main.ts"
],
"include": [
"src/**/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Output::
{
"compilerOptions": {},
"files": [
"./project/file0.ts",
"./project/file1.ts",
"./project/file2.ts"
"./file0.ts",
"./file1.ts",
"./file2.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Output::
"compilerOptions": {
"strict": true
},
"files": [
"./src/main.ts",
"./src/util.ts"
],
"include": [
"src/**/*"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Output::
"esModuleInterop": true,
"useDefineForClassFields": false
},
"files": [
"./src/index.ts"
],
"include": [
"./src/**/*"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ Output::
{
"path": "./packages/b"
}
],
"files": [
"./src/index.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ Output::
"module": "nodenext",
"moduleResolution": "nodenext",
"moduleDetection": "force"
}
},
"files": [
"./src/index.ts"
]
}
Loading