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
2 changes: 1 addition & 1 deletion internal/api/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestEncodeSourceFileWithUnicodeEscapes(t *testing.T) {

func BenchmarkEncodeSourceFile(b *testing.B) {
repo.SkipIfNoTypeScriptSubmodule(b)
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")
fileContent, err := os.ReadFile(filePath)
assert.NilError(b, err)
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
Expand Down
2 changes: 1 addition & 1 deletion internal/astnav/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

var testFiles = []string{
filepath.Join(repo.TypeScriptSubmodulePath, "src/services/mapCode.ts"),
filepath.Join(repo.TypeScriptSubmodulePath(), "src/services/mapCode.ts"),
}

func TestGetTokenAtPosition(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/bundled/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
)

var (
libInputDir = filepath.Join(repo.TypeScriptSubmodulePath, "src", "lib")
copyrightNotice = filepath.Join(repo.TypeScriptSubmodulePath, "scripts", "CopyrightNotice.txt")
libInputDir = filepath.Join(repo.TypeScriptSubmodulePath(), "src", "lib")
copyrightNotice = filepath.Join(repo.TypeScriptSubmodulePath(), "scripts", "CopyrightNotice.txt")
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion internal/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func BenchmarkNewChecker(b *testing.B) {
fs := osvfs.FS()
fs = bundled.WrapFS(fs)

rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath()), "src", "compiler")

host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, nil, host, nil)
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func BenchmarkNewProgram(b *testing.B) {
b.Run("compiler", func(b *testing.B) {
repo.SkipIfNoTypeScriptSubmodule(b)

rootPath := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler"))
rootPath := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "compiler"))

fs := osvfs.FS()
fs = bundled.WrapFS(fs)
Expand Down
6 changes: 3 additions & 3 deletions internal/diagnostics/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
return
}

rawDiagnosticMessages := readRawMessages(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler", "diagnosticMessages.json"))
rawDiagnosticMessages := readRawMessages(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "compiler", "diagnosticMessages.json"))

_, filename, _, ok := runtime.Caller(0)
if !ok {
Expand Down Expand Up @@ -198,12 +198,12 @@ func generateLocalizations(knownKeys map[string]bool, locDir string) *bytes.Buff
}

// Generate locale maps
localeFiles, err := filepath.Glob(filepath.Join(repo.TypeScriptSubmodulePath, "src", "loc", "lcl", "*", "diagnosticMessages", "diagnosticMessages.generated.json.lcl"))
localeFiles, err := filepath.Glob(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "loc", "lcl", "*", "diagnosticMessages", "diagnosticMessages.generated.json.lcl"))
if err != nil {
log.Fatalf("failed to find locale files: %v", err)
}
if len(localeFiles) == 0 {
log.Fatalf("no locale files found in %s", filepath.Join(repo.TypeScriptSubmodulePath, "src", "loc", "lcl"))
log.Fatalf("no locale files found in %s", filepath.Join(repo.TypeScriptSubmodulePath(), "src", "loc", "lcl"))
}
slices.Sort(localeFiles)

Expand Down
4 changes: 2 additions & 2 deletions internal/format/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestFormat(t *testing.T) {
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
}, "\n")
repo.SkipIfNoTypeScriptSubmodule(t)
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")
fileContent, err := os.ReadFile(filePath)
assert.NilError(t, err)
text := string(fileContent)
Expand Down Expand Up @@ -80,7 +80,7 @@ func BenchmarkFormat(b *testing.B) {
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
}, "\n")
repo.SkipIfNoTypeScriptSubmodule(b)
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")
fileContent, err := os.ReadFile(filePath)
assert.NilError(b, err)
text := string(fileContent)
Expand Down
4 changes: 2 additions & 2 deletions internal/packagejson/packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

var packageJsonFixtures = []filefixture.Fixture{
filefixture.FromFile("package.json", filepath.Join(repo.RootPath, "package.json")),
filefixture.FromFile("date-fns.json", filepath.Join(repo.TestDataPath, "fixtures", "packagejson", "date-fns.json")),
filefixture.FromFile("package.json", filepath.Join(repo.RootPath(), "package.json")),
filefixture.FromFile("date-fns.json", filepath.Join(repo.TestDataPath(), "fixtures", "packagejson", "date-fns.json")),
}

func BenchmarkPackageJSON(b *testing.B) {
Expand Down
8 changes: 4 additions & 4 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func FuzzParser(f *testing.F) {
}

for _, test := range tests {
root := filepath.Join(repo.TypeScriptSubmodulePath, test)
root := filepath.Join(repo.TypeScriptSubmodulePath(), test)

for file := range allParsableFiles(f, root) {
sourceText, err := os.ReadFile(file.path)
Expand All @@ -117,9 +117,9 @@ func FuzzParser(f *testing.F) {
}

testDirs := []string{
filepath.Join(repo.TypeScriptSubmodulePath, "tests/cases/compiler"),
filepath.Join(repo.TypeScriptSubmodulePath, "tests/cases/conformance"),
filepath.Join(repo.TestDataPath, "tests/cases/compiler"),
filepath.Join(repo.TypeScriptSubmodulePath(), "tests/cases/compiler"),
filepath.Join(repo.TypeScriptSubmodulePath(), "tests/cases/conformance"),
filepath.Join(repo.TestDataPath(), "tests/cases/compiler"),
}

for _, testDir := range testDirs {
Expand Down
56 changes: 40 additions & 16 deletions internal/repo/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,63 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)

var (
RootPath string
TypeScriptSubmodulePath string
TestDataPath string
)

func init() {
var rootPath = sync.OnceValue(func() string {
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("could not get current filename")
}
filename = filepath.FromSlash(filename) // runtime.Caller always returns forward slashes; https://go.dev/issues/3335, https://go.dev/cl/603275
RootPath = findGoMod(filepath.Dir(filename))
TypeScriptSubmodulePath = filepath.Join(RootPath, "_submodules", "TypeScript")
TestDataPath = filepath.Join(RootPath, "testdata")
}

func findGoMod(dir string) string {
root := filepath.VolumeName(dir)
for dir != root {
if strings.HasPrefix(filename, "github.com/") {
panic("repo root cannot be found when built with -trimpath")
}

if !filepath.IsAbs(filename) {
panic(filename + " is not an absolute path")
}

root := filepath.VolumeName(filename) + string(filepath.Separator)

dir := filepath.Dir(filename)
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
if dir == root {
break
}
dir = filepath.Dir(dir)
}
panic("could not find go.mod")

panic("could not find go.mod above " + filename)
})

func RootPath() string {
return rootPath()
}

var typeScriptSubmodulePath = sync.OnceValue(func() string {
return filepath.Join(rootPath(), "_submodules", "TypeScript")
})

func TypeScriptSubmodulePath() string {
return typeScriptSubmodulePath()
}

var testDataPath = sync.OnceValue(func() string {
return filepath.Join(rootPath(), "testdata")
})

func TestDataPath() string {
return testDataPath()
}

var typeScriptSubmoduleExists = sync.OnceValue(func() bool {
p := filepath.Join(TypeScriptSubmodulePath, "package.json")
p := filepath.Join(typeScriptSubmodulePath(), "package.json")
if _, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
return false
Expand Down
10 changes: 5 additions & 5 deletions internal/testrunner/compiler_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (r *CompilerBaselineRunner) RunTests(t *testing.T) {
}
}

var localBasePath = filepath.Join(repo.TestDataPath, "baselines", "local")
var localBasePath = filepath.Join(repo.TestDataPath(), "baselines", "local")

func (r *CompilerBaselineRunner) cleanUpLocal(t *testing.T) {
localPath := filepath.Join(localBasePath, core.IfElse(r.isSubmodule, "diff", ""), r.testSuitName)
Expand Down Expand Up @@ -402,7 +402,7 @@ func (c *compilerTest) verifyJavaScriptOutput(t *testing.T, suiteName string, is
}

defer testutil.RecoverAndFail(t, "Panic on creating js output for test "+c.filename)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath(), c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
Expand All @@ -425,7 +425,7 @@ func (c *compilerTest) verifyJavaScriptOutput(t *testing.T, suiteName string, is
func (c *compilerTest) verifySourceMapOutput(t *testing.T, suiteName string, isSubmodule bool) {
t.Run("sourcemap", func(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on creating source map output for test "+c.filename)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath(), c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
Expand All @@ -445,7 +445,7 @@ func (c *compilerTest) verifySourceMapOutput(t *testing.T, suiteName string, isS
func (c *compilerTest) verifySourceMapRecord(t *testing.T, suiteName string, isSubmodule bool) {
t.Run("sourcemap record", func(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on creating source map record for test "+c.filename)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath(), c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
Expand Down Expand Up @@ -475,7 +475,7 @@ func (c *compilerTest) verifyTypesAndSymbols(t *testing.T, suiteName string, isS
},
)

headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath(), c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
Expand Down
8 changes: 4 additions & 4 deletions internal/testutil/baseline/baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Run(t *testing.T, fileName string, actual string, opts Options) {
var submoduleAcceptedFileNames = sync.OnceValue(func() *collections.Set[string] {
var set collections.Set[string]

submoduleAccepted := filepath.Join(repo.TestDataPath, "submoduleAccepted.txt")
submoduleAccepted := filepath.Join(repo.TestDataPath(), "submoduleAccepted.txt")
if content, err := os.ReadFile(submoduleAccepted); err == nil {
for line := range strings.SplitSeq(string(content), "\n") {
line = strings.TrimSpace(line)
Expand Down Expand Up @@ -214,7 +214,7 @@ func writeComparison(t *testing.T, actualContent string, local, reference string
}

var (
localRoot = filepath.Join(repo.TestDataPath, "baselines", "local")
referenceRoot = filepath.Join(repo.TestDataPath, "baselines", "reference")
submoduleReferenceRoot = filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference")
localRoot = filepath.Join(repo.TestDataPath(), "baselines", "local")
referenceRoot = filepath.Join(repo.TestDataPath(), "baselines", "reference")
submoduleReferenceRoot = filepath.Join(repo.TypeScriptSubmodulePath(), "tests", "baselines", "reference")
)
8 changes: 4 additions & 4 deletions internal/testutil/fixtures/benchfixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

var BenchFixtures = []filefixture.Fixture{
filefixture.FromString("empty.ts", "empty.ts", ""),
filefixture.FromFile("checker.ts", filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")),
filefixture.FromFile("dom.generated.d.ts", filepath.Join(repo.TypeScriptSubmodulePath, "src/lib/dom.generated.d.ts")),
filefixture.FromFile("Herebyfile.mjs", filepath.Join(repo.TypeScriptSubmodulePath, "Herebyfile.mjs")),
filefixture.FromFile("jsxComplexSignatureHasApplicabilityError.tsx", filepath.Join(repo.TypeScriptSubmodulePath, "tests/cases/compiler/jsxComplexSignatureHasApplicabilityError.tsx")),
filefixture.FromFile("checker.ts", filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")),
filefixture.FromFile("dom.generated.d.ts", filepath.Join(repo.TypeScriptSubmodulePath(), "src/lib/dom.generated.d.ts")),
filefixture.FromFile("Herebyfile.mjs", filepath.Join(repo.TypeScriptSubmodulePath(), "Herebyfile.mjs")),
filefixture.FromFile("jsxComplexSignatureHasApplicabilityError.tsx", filepath.Join(repo.TypeScriptSubmodulePath(), "tests/cases/compiler/jsxComplexSignatureHasApplicabilityError.tsx")),
}
4 changes: 2 additions & 2 deletions internal/testutil/harnessutil/harnessutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func CompileFilesEx(

var testLibFolderMap = sync.OnceValue(func() map[string]any {
testfs := make(map[string]any)
libfs := os.DirFS(filepath.Join(repo.TypeScriptSubmodulePath, "tests", "lib"))
libfs := os.DirFS(filepath.Join(repo.TypeScriptSubmodulePath(), "tests", "lib"))
err := fs.WalkDir(libfs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -971,7 +971,7 @@ func listFiles(path string, spec *regexp.Regexp, recursive bool) ([]string, erro
}

func listFilesWorker(spec *regexp.Regexp, recursive bool, folder string) ([]string, error) {
folder = tspath.GetNormalizedAbsolutePath(folder, repo.TestDataPath)
folder = tspath.GetNormalizedAbsolutePath(folder, repo.TestDataPath())
entries, err := os.ReadDir(folder)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/jstest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func EvalNodeScriptWithTS[T any](t testing.TB, script string, dir string, args .
if dir == "" {
dir = t.TempDir()
}
tsSrc := tspath.NormalizePath(filepath.Join(repo.RootPath, "node_modules/typescript/lib/typescript.js"))
tsSrc := tspath.NormalizePath(filepath.Join(repo.RootPath(), "node_modules/typescript/lib/typescript.js"))
if tsSrc[0] == '/' {
tsSrc = "file://" + tsSrc
} else {
Expand Down
4 changes: 2 additions & 2 deletions internal/tsoptions/commandlineparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (f commandLineSubScenario) assertBuildParseResultWithTsBaseline(t *testing.
// f.workerDiagnostic is either defined or set to default pointer in `createSubScenario`
parsed := tsoptions.ParseBuildCommandLine(f.commandLine, &tsoptionstest.VfsParseConfigHost{
Vfs: osvfs.FS(),
CurrentDirectory: tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath),
CurrentDirectory: tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath()),
})

newBaselineProjects := strings.Join(parsed.Projects, ",")
Expand Down Expand Up @@ -416,7 +416,7 @@ func createSubScenario(scenarioKind string, subScenarioName string, commandline
baselineFileName := "tests/baselines/reference/config/commandLineParsing/" + subScenarioName + ".js"

result := &commandLineSubScenario{
filefixture.FromFile(subScenarioName, filepath.Join(repo.TypeScriptSubmodulePath, baselineFileName)),
filefixture.FromFile(subScenarioName, filepath.Join(repo.TypeScriptSubmodulePath(), baselineFileName)),
subScenarioName,
commandline,
nil,
Expand Down
8 changes: 4 additions & 4 deletions internal/tsoptions/tsconfigparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ func TestParseSrcCompiler(t *testing.T) {

repo.SkipIfNoTypeScriptSubmodule(t)

compilerDir := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler"))
compilerDir := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "compiler"))
tsconfigFileName := tspath.CombinePaths(compilerDir, "tsconfig.json")

fs := osvfs.FS()
Expand Down Expand Up @@ -1061,7 +1061,7 @@ func TestParseSrcCompiler(t *testing.T) {
Module: core.ModuleKindNodeNext,
ModuleResolution: core.ModuleResolutionKindNodeNext,
NewLine: core.NewLineKindLF,
OutDir: tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "built", "local")),
OutDir: tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath(), "built", "local")),
Target: core.ScriptTargetES2020,
Types: []string{"node"},
ConfigFilePath: tsconfigFileName,
Expand All @@ -1073,7 +1073,7 @@ func TestParseSrcCompiler(t *testing.T) {
IsolatedDeclarations: core.TSTrue,
NoImplicitOverride: core.TSTrue,
PreserveConstEnums: core.TSTrue,
RootDir: tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src")),
RootDir: tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath(), "src")),
SkipLibCheck: core.TSTrue,
Strict: core.TSTrue,
StrictBindCallApply: core.TSFalse,
Expand Down Expand Up @@ -1177,7 +1177,7 @@ func TestParseSrcCompiler(t *testing.T) {
func BenchmarkParseSrcCompiler(b *testing.B) {
repo.SkipIfNoTypeScriptSubmodule(b)

compilerDir := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler"))
compilerDir := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "compiler"))
tsconfigFileName := tspath.CombinePaths(compilerDir, "tsconfig.json")

fs := osvfs.FS()
Expand Down
2 changes: 1 addition & 1 deletion internal/vfs/osvfs/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestOS(t *testing.T) {
t.Run("ReadFile", func(t *testing.T) {
t.Parallel()

goMod := filepath.Join(repo.RootPath, "go.mod")
goMod := filepath.Join(repo.RootPath(), "go.mod")
goModPath := tspath.NormalizePath(goMod)

expectedRaw, err := os.ReadFile(goMod)
Expand Down
2 changes: 1 addition & 1 deletion internal/vfs/vfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func BenchmarkReadFile(b *testing.B) {
}

if repo.TypeScriptSubmoduleExists() {
checkerPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler", "checker.ts")
checkerPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath()), "src", "compiler", "checker.ts")

checkerContents, ok := osFS.ReadFile(checkerPath)
assert.Assert(b, ok)
Expand Down