Skip to content

Commit a0e4ab9

Browse files
author
Andy Hanson
committed
Allow JS files
1 parent 240b474 commit a0e4ab9

8 files changed

+47
-23
lines changed

src/compiler/core.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,17 +2210,13 @@ namespace ts {
22102210
* Path must have a valid extension.
22112211
*/
22122212
export function extensionFromPath(path: string): Extension {
2213-
const ts = tryGetTypeScriptExtensionFromPath(path);
2214-
if (ts !== undefined) {
2215-
return ts;
2216-
}
2217-
const js = tryGetJavaScriptExtensionFromPath(path);
2218-
if (js !== undefined) {
2219-
return js;
2213+
const ext = tryGetExtensionFromPath(path);
2214+
if (ext !== undefined) {
2215+
return ext;
22202216
}
22212217
Debug.fail(`File ${path} has unknown extension.`);
22222218
}
2223-
export function tryGetTypeScriptExtensionFromPath(path: string): Extension | undefined {
2219+
export function tryGetExtensionFromPath(path: string): Extension | undefined {
22242220
if (fileExtensionIs(path, ".d.ts")) {
22252221
return Extension.Dts;
22262222
}
@@ -2230,8 +2226,6 @@ namespace ts {
22302226
if (fileExtensionIs(path, ".tsx")) {
22312227
return Extension.Tsx;
22322228
}
2233-
}
2234-
function tryGetJavaScriptExtensionFromPath(path: string): Extension | undefined {
22352229
if (fileExtensionIs(path, ".js")) {
22362230
return Extension.Js;
22372231
}

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ namespace ts {
519519
trace(state.host, Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path);
520520
}
521521
// A path mapping may have a ".ts" extension; in contrast to an import, which should omit it.
522-
const tsExtension = tryGetTypeScriptExtensionFromPath(candidate);
522+
const tsExtension = tryGetExtensionFromPath(candidate);
523523
if (tsExtension !== undefined) {
524524
const path = tryFile(candidate, failedLookupLocations, /*onlyRecordFailures*/false, state);
525525
return path && { path, extension: tsExtension };

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
export function foo() {}
66

7+
//// [bar.js]
8+
export function bar() {}
9+
710
//// [a.ts]
811
import { foo } from "foo";
12+
import { bar } from "bar";
913

1014

1115
//// [foo.js]
1216
"use strict";
1317
function foo() { }
1418
exports.foo = foo;
19+
//// [bar.js]
20+
"use strict";
21+
function bar() { }
22+
exports.bar = bar;
1523
//// [a.js]
1624
"use strict";

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension.symbols

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
import { foo } from "foo";
33
>foo : Symbol(foo, Decl(a.ts, 0, 8))
44

5+
import { bar } from "bar";
6+
>bar : Symbol(bar, Decl(a.ts, 1, 8))
7+
58
=== /foo/foo.ts ===
69

710
export function foo() {}
811
>foo : Symbol(foo, Decl(foo.ts, 0, 0))
912

13+
=== /bar/bar.js ===
14+
export function bar() {}
15+
>bar : Symbol(bar, Decl(bar.js, 0, 0))
16+

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension.trace.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@
77
"Trying substitution 'foo/foo.ts', candidate module location: 'foo/foo.ts'.",
88
"File '/foo/foo.ts' exist - use it as a name resolution result.",
99
"Resolving real path for '/foo/foo.ts', result '/foo/foo.ts'",
10-
"======== Module name 'foo' was successfully resolved to '/foo/foo.ts'. ========"
10+
"======== Module name 'foo' was successfully resolved to '/foo/foo.ts'. ========",
11+
"======== Resolving module 'bar' from '/a.ts'. ========",
12+
"Module resolution kind is not specified, using 'NodeJs'.",
13+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'bar'",
14+
"'paths' option is specified, looking for a pattern to match module name 'bar'.",
15+
"Module name 'bar', matched pattern 'bar'.",
16+
"Trying substitution 'bar/bar.js', candidate module location: 'bar/bar.js'.",
17+
"File '/bar/bar.js' exist - use it as a name resolution result.",
18+
"Resolving real path for '/bar/bar.js', result '/bar/bar.js'",
19+
"======== Module name 'bar' was successfully resolved to '/bar/bar.js'. ========"
1120
]

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension.types

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
import { foo } from "foo";
33
>foo : () => void
44

5+
import { bar } from "bar";
6+
>bar : () => void
7+
58
=== /foo/foo.ts ===
69

710
export function foo() {}
811
>foo : () => void
912

13+
=== /bar/bar.js ===
14+
export function bar() {}
15+
>bar : () => void
16+

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_failedLookup.trace.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414
"File '/node_modules/foo/index.ts' does not exist.",
1515
"File '/node_modules/foo/index.tsx' does not exist.",
1616
"File '/node_modules/foo/index.d.ts' does not exist.",
17-
"File '/node_modules/@types/foo.ts' does not exist.",
18-
"File '/node_modules/@types/foo.tsx' does not exist.",
1917
"File '/node_modules/@types/foo.d.ts' does not exist.",
2018
"File '/node_modules/@types/foo/package.json' does not exist.",
21-
"File '/node_modules/@types/foo/index.ts' does not exist.",
22-
"File '/node_modules/@types/foo/index.tsx' does not exist.",
2319
"File '/node_modules/@types/foo/index.d.ts' does not exist.",
2420
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo'",
2521
"'paths' option is specified, looking for a pattern to match module name 'foo'.",
@@ -32,10 +28,5 @@
3228
"File '/node_modules/foo/package.json' does not exist.",
3329
"File '/node_modules/foo/index.js' does not exist.",
3430
"File '/node_modules/foo/index.jsx' does not exist.",
35-
"File '/node_modules/@types/foo.js' does not exist.",
36-
"File '/node_modules/@types/foo.jsx' does not exist.",
37-
"File '/node_modules/@types/foo/package.json' does not exist.",
38-
"File '/node_modules/@types/foo/index.js' does not exist.",
39-
"File '/node_modules/@types/foo/index.jsx' does not exist.",
4031
"======== Module name 'foo' was not resolved. ========"
4132
]
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
// @noImplicitReferences: true
22
// @traceResolution: true
3+
// @allowJs: true
34

45
// @Filename: /foo/foo.ts
56
export function foo() {}
67

8+
// @Filename: /bar/bar.js
9+
export function bar() {}
10+
711
// @Filename: /a.ts
812
import { foo } from "foo";
13+
import { bar } from "bar";
914

1015
// @Filename: /tsconfig.json
1116
{
1217
"compilerOptions": {
1318
"baseUrl": ".",
1419
"paths": {
15-
"foo": ["foo/foo.ts"]
16-
}
20+
"foo": ["foo/foo.ts"],
21+
"bar": ["bar/bar.js"]
22+
},
23+
"allowJs": true,
24+
"outDir": "bin"
1725
}
1826
}

0 commit comments

Comments
 (0)