Skip to content

Commit 3ee498c

Browse files
Support json module resolution in file loader (#565)
Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
1 parent 4170b5d commit 3ee498c

File tree

170 files changed

+1311
-2146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+1311
-2146
lines changed

internal/compiler/fileloader.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,13 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile)
274274

275275
// Don't add the file if it has a bad extension (e.g. 'tsx' if we don't have '--allowJs')
276276
// This may still end up being an untyped module -- the file won't be included but imports will be allowed.
277-
278-
shouldAddFile := resolution.IsResolved() && tspath.FileExtensionIsOneOf(resolvedFileName, []string{".ts", ".tsx", ".mts", ".cts"})
277+
hasAllowedExtension := false
278+
if p.compilerOptions.ResolveJsonModule.IsTrue() {
279+
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsWithJsonFlat)
280+
} else {
281+
hasAllowedExtension = tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsFlat)
282+
}
283+
shouldAddFile := resolution.IsResolved() && hasAllowedExtension
279284
// TODO(ercornel): !!!: other checks on whether or not to add the file
280285

281286
if shouldAddFile {

internal/tspath/extension.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
SupportedJSExtensionsFlat = []string{ExtensionJs, ExtensionJsx, ExtensionMjs, ExtensionCjs}
3434
AllSupportedExtensionsWithJson = slices.Concat(AllSupportedExtensions, [][]string{{ExtensionJson}})
3535
SupportedTSExtensionsWithJson = slices.Concat(SupportedTSExtensions, [][]string{{ExtensionJson}})
36+
SupportedTSExtensionsWithJsonFlat = slices.Concat(SupportedTSExtensionsFlat, []string{ExtensionJson})
3637
)
3738

3839
func ExtensionIsTs(ext string) bool {

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule.errors.txt

-11
This file was deleted.

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule.errors.txt.diff

-16
This file was deleted.

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule.symbols

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
import j = require("./j.json");
55
>j : Symbol(j, Decl(a.ts, 0, 0))
66

7+
=== /j.json ===
8+
9+
{}
10+

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule.symbols.diff

-10
This file was deleted.

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule_strict_outDir_commonJs.errors.txt

-11
This file was deleted.

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule_strict_outDir_commonJs.errors.txt.diff

-16
This file was deleted.

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule_strict_outDir_commonJs.symbols

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
import * as j from "./j.json";
55
>j : Symbol(j, Decl(a.ts, 0, 6))
66

7+
=== /j.json ===
8+
9+
{}
10+

testdata/baselines/reference/submodule/compiler/isolatedModules_resolveJsonModule_strict_outDir_commonJs.symbols.diff

-10
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsonFileImportChecksCallCorrectlyTwice.errors.txt

-25
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsonFileImportChecksCallCorrectlyTwice.errors.txt.diff

-30
This file was deleted.

testdata/baselines/reference/submodule/compiler/jsonFileImportChecksCallCorrectlyTwice.symbols

+17
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,31 @@ interface Foo {
1313

1414
fn(data.foo);
1515
>fn : Symbol(fn, Decl(index.ts, 7, 13))
16+
>data.foo : Symbol(foo, Decl(data.json, 0, 1))
1617
>data : Symbol(data, Decl(index.ts, 0, 6))
18+
>foo : Symbol(foo, Decl(data.json, 0, 1))
1719

1820
fn(data.foo); // <-- shouldn't error!
1921
>fn : Symbol(fn, Decl(index.ts, 7, 13))
22+
>data.foo : Symbol(foo, Decl(data.json, 0, 1))
2023
>data : Symbol(data, Decl(index.ts, 0, 6))
24+
>foo : Symbol(foo, Decl(data.json, 0, 1))
2125

2226
function fn(arg: Foo[]) { }
2327
>fn : Symbol(fn, Decl(index.ts, 7, 13))
2428
>arg : Symbol(arg, Decl(index.ts, 9, 12))
2529
>Foo : Symbol(Foo, Decl(index.ts, 0, 31))
2630

31+
=== data.json ===
32+
{
33+
"foo": [
34+
>"foo" : Symbol(foo, Decl(data.json, 0, 1))
35+
{
36+
"bool": true,
37+
>"bool" : Symbol(bool, Decl(data.json, 2, 7))
38+
39+
"str": "123"
40+
>"str" : Symbol(str, Decl(data.json, 3, 21))
41+
}
42+
]
43+
}

testdata/baselines/reference/submodule/compiler/jsonFileImportChecksCallCorrectlyTwice.symbols.diff

+18-13
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,35 @@
1111
fn(data.foo);
1212
>fn : Symbol(fn, Decl(index.ts, 7, 13))
1313
->data.foo : Symbol("foo", Decl(data.json, 0, 1))
14+
+>data.foo : Symbol(foo, Decl(data.json, 0, 1))
1415
>data : Symbol(data, Decl(index.ts, 0, 6))
1516
->foo : Symbol("foo", Decl(data.json, 0, 1))
17+
+>foo : Symbol(foo, Decl(data.json, 0, 1))
1618

1719
fn(data.foo); // <-- shouldn't error!
1820
>fn : Symbol(fn, Decl(index.ts, 7, 13))
1921
->data.foo : Symbol("foo", Decl(data.json, 0, 1))
22+
+>data.foo : Symbol(foo, Decl(data.json, 0, 1))
2023
>data : Symbol(data, Decl(index.ts, 0, 6))
2124
->foo : Symbol("foo", Decl(data.json, 0, 1))
25+
+>foo : Symbol(foo, Decl(data.json, 0, 1))
2226

2327
function fn(arg: Foo[]) { }
2428
>fn : Symbol(fn, Decl(index.ts, 7, 13))
25-
>arg : Symbol(arg, Decl(index.ts, 9, 12))
26-
>Foo : Symbol(Foo, Decl(index.ts, 0, 31))
27-
28-
-=== data.json ===
29-
-{
30-
- "foo": [
29+
@@= skipped -23, +23 lines =@@
30+
=== data.json ===
31+
{
32+
"foo": [
3133
->"foo" : Symbol("foo", Decl(data.json, 0, 1))
32-
- {
33-
- "bool": true,
34+
+>"foo" : Symbol(foo, Decl(data.json, 0, 1))
35+
{
36+
"bool": true,
3437
->"bool" : Symbol("bool", Decl(data.json, 2, 7))
35-
-
36-
- "str": "123"
38+
+>"bool" : Symbol(bool, Decl(data.json, 2, 7))
39+
40+
"str": "123"
3741
->"str" : Symbol("str", Decl(data.json, 3, 21))
38-
- }
39-
- ]
40-
-}
42+
+>"str" : Symbol(str, Decl(data.json, 3, 21))
43+
}
44+
]
45+
}

testdata/baselines/reference/submodule/compiler/modulePreserve5.errors.txt

-15
This file was deleted.

testdata/baselines/reference/submodule/compiler/modulePreserve5.errors.txt.diff

-20
This file was deleted.

testdata/baselines/reference/submodule/compiler/moduleResolutionWithSuffixes_one_jsonModule.errors.txt

-29
This file was deleted.

testdata/baselines/reference/submodule/compiler/moduleResolutionWithSuffixes_one_jsonModule.errors.txt.diff

-34
This file was deleted.

testdata/baselines/reference/submodule/compiler/moduleResolutionWithSuffixes_one_jsonModule.symbols

+7
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,12 @@ console.log(foo.ios);
88
>console.log : Symbol(log, Decl(lib.dom.d.ts, --, --))
99
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
1010
>log : Symbol(log, Decl(lib.dom.d.ts, --, --))
11+
>foo.ios : Symbol(ios, Decl(foo.ios.json, 0, 1))
1112
>foo : Symbol(foo, Decl(index.ts, 0, 6))
13+
>ios : Symbol(ios, Decl(foo.ios.json, 0, 1))
1214

15+
=== /foo.ios.json ===
16+
{
17+
"ios": "platform ios"
18+
>"ios" : Symbol(ios, Decl(foo.ios.json, 0, 1))
19+
}

testdata/baselines/reference/submodule/compiler/moduleResolutionWithSuffixes_one_jsonModule.symbols.diff

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
->log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
1111
->foo.ios : Symbol("ios", Decl(foo.ios.json, 0, 1))
1212
+>log : Symbol(log, Decl(lib.dom.d.ts, --, --))
13+
+>foo.ios : Symbol(ios, Decl(foo.ios.json, 0, 1))
1314
>foo : Symbol(foo, Decl(index.ts, 0, 6))
1415
->ios : Symbol("ios", Decl(foo.ios.json, 0, 1))
16+
+>ios : Symbol(ios, Decl(foo.ios.json, 0, 1))
1517

16-
-=== /foo.ios.json ===
17-
-{
18-
- "ios": "platform ios"
18+
=== /foo.ios.json ===
19+
{
20+
"ios": "platform ios"
1921
->"ios" : Symbol("ios", Decl(foo.ios.json, 0, 1))
20-
-}
22+
+>"ios" : Symbol(ios, Decl(foo.ios.json, 0, 1))
23+
}

0 commit comments

Comments
 (0)