Skip to content

Commit 332797c

Browse files
https://github.com/microsoft/typescript-go/pull/671
1 parent 0ca5839 commit 332797c

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

internal/tsoptions/tsconfigparsing.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package tsoptions
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
57
"reflect"
68
"regexp"
79
"slices"
@@ -1432,32 +1434,41 @@ func getFileNamesFromConfigSpecs(
14321434
extraFileExtensions []fileExtensionInfo,
14331435
) []string {
14341436
extraFileExtensions = []fileExtensionInfo{}
1437+
1438+
if basePath == "" {
1439+
basePath, _ = os.Getwd()
1440+
} else if !filepath.IsAbs(basePath) {
1441+
basePath, _ = filepath.Abs(basePath)
1442+
}
1443+
14351444
basePath = tspath.NormalizePath(basePath)
1436-
keyMappper := func(value string) string { return tspath.GetCanonicalFileName(value, host.UseCaseSensitiveFileNames()) }
1437-
// Literal file names (provided via the "files" array in tsconfig.json) are stored in a
1438-
// file map with a possibly case insensitive key. We use this map later when when including
1439-
// wildcard paths.
1440-
var literalFileMap collections.OrderedMap[string, string]
1441-
// Wildcard paths (provided via the "includes" array in tsconfig.json) are stored in a
1442-
// file map with a possibly case insensitive key. We use this map to store paths matched
1443-
// via wildcard, and to handle extension priority.
1444-
var wildcardFileMap collections.OrderedMap[string, string]
1445-
// Wildcard paths of json files (provided via the "includes" array in tsconfig.json) are stored in a
1446-
// file map with a possibly case insensitive key. We use this map to store paths matched
1447-
// via wildcard of *.json kind
1448-
var wildCardJsonFileMap collections.OrderedMap[string, string]
1449-
validatedFilesSpec := configFileSpecs.validatedFilesSpec
1450-
validatedIncludeSpecs := configFileSpecs.validatedIncludeSpecs
1451-
validatedExcludeSpecs := configFileSpecs.validatedExcludeSpecs
1452-
// Rather than re-query this for each file and filespec, we query the supported extensions
1453-
// once and store it on the expansion context.
1454-
supportedExtensions := GetSupportedExtensions(options, extraFileExtensions)
1455-
supportedExtensionsWithJsonIfResolveJsonModule := GetSupportedExtensionsWithJsonIfResolveJsonModule(options, supportedExtensions)
1456-
// Literal files are always included verbatim. An "include" or "exclude" specification cannot
1457-
// remove a literal file.
1445+
keyMapper := func(value string) string { return tspath.GetCanonicalFileName(value, host.UseCaseSensitiveFileNames()) }
1446+
1447+
var (
1448+
literalFileMap collections.OrderedMap[string, string]
1449+
wildcardFileMap collections.OrderedMap[string, string]
1450+
wildCardJsonFileMap collections.OrderedMap[string, string]
1451+
)
1452+
1453+
var (
1454+
validatedFilesSpec = configFileSpecs.validatedFilesSpec
1455+
validatedIncludeSpecs = configFileSpecs.validatedIncludeSpecs
1456+
validatedExcludeSpecs = configFileSpecs.validatedExcludeSpecs
1457+
)
1458+
1459+
var (
1460+
supportedExtensions = GetSupportedExtensions(options, extraFileExtensions)
1461+
supportedExtensionsWithJsonIfResolveJsonModule = GetSupportedExtensionsWithJsonIfResolveJsonModule(options, supportedExtensions)
1462+
)
1463+
14581464
for _, fileName := range validatedFilesSpec {
14591465
file := tspath.GetNormalizedAbsolutePath(fileName, basePath)
1460-
literalFileMap.Set(keyMappper(fileName), file)
1466+
1467+
if !filepath.IsAbs(file) {
1468+
panic(fmt.Sprintf("Path %q is not absolute (base: %q)", file, basePath))
1469+
}
1470+
1471+
literalFileMap.Set(keyMapper(fileName), file)
14611472
}
14621473

14631474
var jsonOnlyIncludeRegexes []*regexp2.Regexp
@@ -1467,7 +1478,7 @@ func getFileNamesFromConfigSpecs(
14671478
if tspath.FileExtensionIs(file, tspath.ExtensionJson) {
14681479
if jsonOnlyIncludeRegexes == nil {
14691480
includes := core.Filter(validatedIncludeSpecs, func(include string) bool { return strings.HasSuffix(include, tspath.ExtensionJson) })
1470-
includeFilePatterns := core.Map(getRegularExpressionsForWildcards(includes, basePath, "files"), func(pattern string) string { return fmt.Sprintf("^%s$", pattern) })
1481+
includeFilePatterns := core.Map(getRegularExpressionsForWildcards(includes, basePath, "files"), func(pattern string) string { return "^" + pattern + "$" })
14711482
if includeFilePatterns != nil {
14721483
jsonOnlyIncludeRegexes = core.Map(includeFilePatterns, func(pattern string) *regexp2.Regexp {
14731484
return getRegexFromPattern(pattern, host.UseCaseSensitiveFileNames())
@@ -1478,28 +1489,18 @@ func getFileNamesFromConfigSpecs(
14781489
}
14791490
includeIndex := core.FindIndex(jsonOnlyIncludeRegexes, func(re *regexp2.Regexp) bool { return core.Must(re.MatchString(file)) })
14801491
if includeIndex != -1 {
1481-
key := keyMappper(file)
1492+
key := keyMapper(file)
14821493
if !literalFileMap.Has(key) && !wildCardJsonFileMap.Has(key) {
14831494
wildCardJsonFileMap.Set(key, file)
14841495
}
14851496
}
14861497
continue
14871498
}
1488-
// If we have already included a literal or wildcard path with a
1489-
// higher priority extension, we should skip this file.
1490-
//
1491-
// This handles cases where we may encounter both <file>.ts and
1492-
// <file>.d.ts (or <file>.js if "allowJs" is enabled) in the same
1493-
// directory when they are compilation outputs.
1494-
if hasFileWithHigherPriorityExtension(file, literalFileMap, wildcardFileMap, supportedExtensions, keyMappper) {
1499+
if hasFileWithHigherPriorityExtension(file, literalFileMap, wildcardFileMap, supportedExtensions, keyMapper) {
14951500
continue
14961501
}
1497-
// We may have included a wildcard path with a lower priority
1498-
// extension due to the user-defined order of entries in the
1499-
// "include" array. If there is a lower priority extension in the
1500-
// same directory, we should remove it.
1501-
removeWildcardFilesWithLowerPriorityExtension(file, wildcardFileMap, supportedExtensions, keyMappper)
1502-
key := keyMappper(file)
1502+
removeWildcardFilesWithLowerPriorityExtension(file, wildcardFileMap, supportedExtensions, keyMapper)
1503+
key := keyMapper(file)
15031504
if !literalFileMap.Has(key) && !wildcardFileMap.Has(key) {
15041505
wildcardFileMap.Set(key, file)
15051506
}

0 commit comments

Comments
 (0)