Skip to content

Commit ea8bef8

Browse files
committed
feat: allow .ts .tsx import and resolution
1 parent c7f9136 commit ea8bef8

9 files changed

+29
-27
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,13 +1077,21 @@ namespace ts {
10771077

10781078
// If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one;
10791079
// e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts"
1080-
if (hasJSFileExtension(candidate)) {
1080+
if (hasJSFileExtension(candidate, state.compilerOptions)) {
10811081
const extensionless = removeFileExtension(candidate);
10821082
if (state.traceEnabled) {
10831083
const extension = candidate.substring(extensionless.length);
10841084
trace(state.host, Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension);
10851085
}
10861086
return tryAddingExtensions(extensionless, extensions, onlyRecordFailures, state);
1087+
} else if (hasTSFileExtension(candidate)) {
1088+
// If import './x.ts' or './x.tsx', resolve it
1089+
// if import from '.d.ts', bypass it
1090+
const ext = tryExtractTSExtension(candidate)!;
1091+
if (ext !== '.d.ts') {
1092+
const extensionLess = tryRemoveExtension(candidate, ext)!;
1093+
return tryAddingExtensions(extensionLess, extensions, onlyRecordFailures, state);
1094+
}
10871095
}
10881096
}
10891097

tests/baselines/reference/decoratorOnClassConstructor2.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ export class C extends base{
2525
>prop : Symbol(prop, Decl(2.ts, 3, 16))
2626

2727
super();
28+
>super : Symbol(base, Decl(0.ts, 0, 0))
2829
}
2930
}

tests/baselines/reference/decoratorOnClassConstructor2.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ export function foo(target: Object, propertyKey: string | symbol, parameterIndex
1010

1111
=== tests/cases/conformance/decorators/class/constructor/2.ts ===
1212
import {base} from "./0.ts"
13-
>base : any
13+
>base : typeof base
1414

1515
import {foo} from "./0.ts"
16-
>foo : any
16+
>foo : (target: Object, propertyKey: string | symbol, parameterIndex: number) => void
1717

1818
export class C extends base{
1919
>C : C
20-
>base : error
20+
>base : base
2121

2222
constructor(@foo prop: any) {
23-
>foo : error
23+
>foo : (target: Object, propertyKey: string | symbol, parameterIndex: number) => void
2424
>prop : any
2525

2626
super();
2727
>super() : void
28-
>super : error
28+
>super : typeof base
2929
}
3030
}

tests/baselines/reference/emitExtensionDisabled.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ export default 1
33
No type information for this code.
44
No type information for this code.=== tests/cases/compiler/1.ts ===
55
import a from './0.ts'
6-
>a : any
6+
>a : 1
77

88
a + 1
9-
>a + 1 : error
10-
>a : error
9+
>a + 1 : number
10+
>a : 1
1111
>1 : 1
1212

tests/baselines/reference/emitExtensionEnabled.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ export default 1
33
No type information for this code.
44
No type information for this code.=== tests/cases/compiler/1.ts ===
55
import a from './0.ts'
6-
>a : any
6+
>a : 1
77

88
a + 1
9-
>a + 1 : error
10-
>a : error
9+
>a + 1 : number
10+
>a : 1
1111
>1 : 1
1212

tests/baselines/reference/emitExtensionInvalidValue.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ export default 1
33
No type information for this code.
44
No type information for this code.=== tests/cases/compiler/1.ts ===
55
import a from './0.ts'
6-
>a : any
6+
>a : 1
77

88
a + 1
9-
>a + 1 : any
10-
>a : any
9+
>a + 1 : number
10+
>a : 1
1111
>1 : 1
1212

tests/baselines/reference/moduleResolutionNoTs.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export default x;
1313

1414
=== tests/cases/compiler/user.ts ===
1515
import x from "./x.ts";
16-
>x : any
16+
>x : 0
1717

1818
import y from "./y.tsx";
19-
>y : any
19+
>y : 0
2020

2121
import z from "./z.d.ts";
2222
>z : any

tests/baselines/reference/pathMappingBasedModuleResolution_withExtensionInName.trace.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"File '/foo/zone.tsx.ts' does not exist.",
3131
"File '/foo/zone.tsx.tsx' does not exist.",
3232
"File '/foo/zone.tsx.d.ts' does not exist.",
33+
"File '/foo/zone.ts' does not exist.",
34+
"File '/foo/zone.tsx' does not exist.",
35+
"File '/foo/zone.d.ts' does not exist.",
3336
"File '/foo/zone.tsx/package.json' does not exist.",
3437
"File '/foo/zone.tsx/index.ts' does not exist.",
3538
"File '/foo/zone.tsx/index.tsx' does not exist.",
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
// @jsx: Preserve
2-
// @filename: x.ts
3-
export default 0;
4-
5-
// @filename: y.tsx
6-
export default 0;
7-
82
// @filename: z.d.ts
93
declare const x: number;
104
export default x;
115

126
// @filename: user.ts
13-
import x from "./x.ts";
14-
import y from "./y.tsx";
157
import z from "./z.d.ts";
168

179
// Making sure the suggested fixes are valid:
18-
import x2 from "./x";
19-
import y2 from "./y";
2010
import z2 from "./z";

0 commit comments

Comments
 (0)