|
1 | 1 | package build |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "slices" |
| 4 | + "strconv" |
5 | 5 | "testing" |
6 | 6 |
|
| 7 | + "github.com/microsoft/TypeScript/tsc/internal/execute/incremental" |
7 | 8 | "github.com/microsoft/TypeScript/tsc/internal/tspath" |
8 | 9 | "gotest.tools/v3/assert" |
9 | 10 | ) |
10 | 11 |
|
11 | | -func TestIsContentMapperSupplementalBuildInfoPath(t *testing.T) { |
| 12 | +func TestContentMapperVirtualFilePaths(t *testing.T) { |
12 | 13 | t.Parallel() |
13 | | - roots := []tspath.PathKey{"/src/app.vue", "/src/index.ts"} |
| 14 | + buildInfo := &incremental.BuildInfo{ |
| 15 | + Root: []*incremental.BuildInfoRoot{ |
| 16 | + {NonIncremental: "/src/a.vue"}, |
| 17 | + {NonIncremental: "/src/b.ts"}, |
| 18 | + {NonIncremental: "/src/a.vue.0"}, |
| 19 | + }, |
| 20 | + } |
| 21 | + roots := buildInfo.GetBuildInfoRootInfoReader("/", tspath.CaseSensitive) |
14 | 22 |
|
15 | | - assert.Assert(t, isContentMapperSupplementalBuildInfoPath("/src/app.vue.0.ts", slices.Values(roots))) |
16 | | - assert.Assert(t, isContentMapperSupplementalBuildInfoPath("/src/app.vue.12.mts", slices.Values(roots))) |
17 | | - assert.Assert(t, !isContentMapperSupplementalBuildInfoPath("/src/app.vue.ts", slices.Values(roots))) |
18 | | - assert.Assert(t, !isContentMapperSupplementalBuildInfoPath("/src/app.vue.0.txt", slices.Values(roots))) |
19 | | - assert.Assert(t, !isContentMapperSupplementalBuildInfoPath("/src/other.vue.0.ts", slices.Values(roots))) |
| 23 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.ts", roots)) |
| 24 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.1.tsx", roots)) |
| 25 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.12.mts", roots)) |
| 26 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.2.cts", roots)) |
| 27 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.js", roots)) |
| 28 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.jsx", roots)) |
| 29 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.mjs", roots)) |
| 30 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.cjs", roots)) |
| 31 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.json", roots)) |
| 32 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/b.ts.0.ts", roots)) |
| 33 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.1.ts", roots)) |
| 34 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.+1.ts", roots)) |
| 35 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.-1.ts", roots)) |
| 36 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.001.ts", roots)) |
| 37 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.ts", roots)) |
| 38 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.0.txt", roots)) |
| 39 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.0.d.ts", roots)) |
| 40 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.0.d.mts", roots)) |
| 41 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.0.d.cts", roots)) |
| 42 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.0", roots)) |
| 43 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue..ts", roots)) |
| 44 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.x.ts", roots)) |
| 45 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.99999999999999999999.ts", roots)) |
| 46 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vue.1.0.ts", roots)) |
| 47 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/b.vue.0.ts", roots)) |
| 48 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.vuex.0.ts", roots)) |
| 49 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/a.ts", roots)) |
| 50 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/.0.ts", roots)) |
| 51 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/..0.ts", roots)) |
| 52 | +} |
| 53 | + |
| 54 | +func TestContentMapperVirtualFilePathsUseOriginalRoots(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + buildInfo := &incremental.BuildInfo{ |
| 57 | + FileNames: []incremental.BuildInfoPath{"./src/a.vue", "./src/b.vue"}, |
| 58 | + Root: []*incremental.BuildInfoRoot{{Start: 2}}, |
| 59 | + ResolvedRoot: []*incremental.BuildInfoResolvedRoot{{Resolved: 2, Root: 1}}, |
| 60 | + } |
| 61 | + roots := buildInfo.GetBuildInfoRootInfoReader("/", tspath.CaseSensitive) |
| 62 | + |
| 63 | + assert.Assert(t, isContentMapperVirtualFilePath("/src/a.vue.0.ts", roots)) |
| 64 | + assert.Assert(t, !isContentMapperVirtualFilePath("/src/b.vue.0.ts", roots)) |
| 65 | +} |
| 66 | + |
| 67 | +func BenchmarkContentMapperVirtualFilePaths(b *testing.B) { |
| 68 | + for _, count := range []int{100, 1000, 10000} { |
| 69 | + buildInfo := &incremental.BuildInfo{} |
| 70 | + for i := range count { |
| 71 | + buildInfo.Root = append(buildInfo.Root, &incremental.BuildInfoRoot{ |
| 72 | + NonIncremental: incremental.BuildInfoPath("/project/packages/a/src/components/a" + strconv.Itoa(i) + ".vue"), |
| 73 | + }) |
| 74 | + } |
| 75 | + roots := buildInfo.GetBuildInfoRootInfoReader("/", tspath.CaseSensitive) |
| 76 | + b.Run("roots="+strconv.Itoa(count), func(b *testing.B) { |
| 77 | + b.Run("declarationFile", func(b *testing.B) { |
| 78 | + b.ReportAllocs() |
| 79 | + for b.Loop() { |
| 80 | + isContentMapperVirtualFilePath("/project/node_modules/a/b.d.ts", roots) |
| 81 | + } |
| 82 | + }) |
| 83 | + b.Run("sourceFile", func(b *testing.B) { |
| 84 | + b.ReportAllocs() |
| 85 | + for b.Loop() { |
| 86 | + isContentMapperVirtualFilePath("/project/node_modules/a/b.js", roots) |
| 87 | + } |
| 88 | + }) |
| 89 | + b.Run("virtualFile", func(b *testing.B) { |
| 90 | + path := tspath.PathKeyFromCanonical(buildInfo.Root[count-1].NonIncremental.AsString() + ".0.ts") |
| 91 | + b.ReportAllocs() |
| 92 | + for b.Loop() { |
| 93 | + isContentMapperVirtualFilePath(path, roots) |
| 94 | + } |
| 95 | + }) |
| 96 | + }) |
| 97 | + } |
20 | 98 | } |
0 commit comments