Skip to content

Commit 0b71f5f

Browse files
author
Andy Hanson
committed
An import ending in "/" is always an import of a directory.
1 parent ddb5a00 commit 0b71f5f

9 files changed

+52
-38
lines changed

src/compiler/core.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,20 @@ namespace ts {
829829
export function normalizePath(path: string): string {
830830
path = normalizeSlashes(path);
831831
const rootLength = getRootLength(path);
832+
const root = path.substr(0, rootLength);
832833
const normalized = getNormalizedParts(path, rootLength);
833-
return path.substr(0, rootLength) + normalized.join(directorySeparator);
834+
if (normalized.length) {
835+
const joinedParts = root + normalized.join(directorySeparator);
836+
return isPathToDirectory(path) ? joinedParts + "/" : joinedParts;
837+
}
838+
else {
839+
return root;
840+
}
841+
}
842+
843+
/** A path ending with '/' refers to a directory only, never a file. */
844+
export function isPathToDirectory(path: string): boolean {
845+
return path.charCodeAt(path.length - 1) === CharacterCodes.slash;
834846
}
835847

836848
export function getDirectoryPath(path: Path): Path;

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ namespace ts {
646646
trace(state.host, Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate);
647647
}
648648

649-
const resolvedFileName = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state);
649+
const resolvedFileName = !isPathToDirectory(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state);
650650

651651
return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state);
652652
}

tests/baselines/reference/pathMappingBasedModuleResolution7_classic.trace.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"======== Module name './project/file2' was successfully resolved to 'c:/root/generated/src/project/file2.ts'. ========",
1515
"======== Resolving module 'module3' from 'c:/root/src/file1.ts'. ========",
1616
"Module resolution kind is not specified, using 'Classic'.",
17-
"'baseUrl' option is set to 'c:/root', using this value to resolve non-relative module name 'module3'",
17+
"'baseUrl' option is set to 'c:/root/', using this value to resolve non-relative module name 'module3'",
1818
"'paths' option is specified, looking for a pattern to match module name 'module3'.",
1919
"Module name 'module3', matched pattern '*'.",
2020
"Trying substitution '*', candidate module location: 'module3'.",
@@ -32,7 +32,7 @@
3232
"======== Module name 'module3' was successfully resolved to 'c:/module3.d.ts'. ========",
3333
"======== Resolving module 'module1' from 'c:/root/generated/src/project/file2.ts'. ========",
3434
"Module resolution kind is not specified, using 'Classic'.",
35-
"'baseUrl' option is set to 'c:/root', using this value to resolve non-relative module name 'module1'",
35+
"'baseUrl' option is set to 'c:/root/', using this value to resolve non-relative module name 'module1'",
3636
"'paths' option is specified, looking for a pattern to match module name 'module1'.",
3737
"Module name 'module1', matched pattern '*'.",
3838
"Trying substitution '*', candidate module location: 'module1'.",
@@ -44,7 +44,7 @@
4444
"======== Module name 'module1' was successfully resolved to 'c:/shared/module1.d.ts'. ========",
4545
"======== Resolving module 'templates/module2' from 'c:/root/generated/src/project/file2.ts'. ========",
4646
"Module resolution kind is not specified, using 'Classic'.",
47-
"'baseUrl' option is set to 'c:/root', using this value to resolve non-relative module name 'templates/module2'",
47+
"'baseUrl' option is set to 'c:/root/', using this value to resolve non-relative module name 'templates/module2'",
4848
"'paths' option is specified, looking for a pattern to match module name 'templates/module2'.",
4949
"Module name 'templates/module2', matched pattern 'templates/*'.",
5050
"Trying substitution 'generated/src/templates/*', candidate module location: 'generated/src/templates/module2'.",

tests/baselines/reference/pathMappingBasedModuleResolution7_node.trace.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"======== Module name './project/file2' was successfully resolved to 'c:/root/generated/src/project/file2.ts'. ========",
2323
"======== Resolving module 'module3' from 'c:/root/src/file1.ts'. ========",
2424
"Module resolution kind is not specified, using 'NodeJs'.",
25-
"'baseUrl' option is set to 'c:/root', using this value to resolve non-relative module name 'module3'",
25+
"'baseUrl' option is set to 'c:/root/', using this value to resolve non-relative module name 'module3'",
2626
"'paths' option is specified, looking for a pattern to match module name 'module3'.",
2727
"Module name 'module3', matched pattern '*'.",
2828
"Trying substitution '*', candidate module location: 'module3'.",
@@ -79,7 +79,7 @@
7979
"======== Module name 'module3' was successfully resolved to 'c:/node_modules/module3.d.ts'. ========",
8080
"======== Resolving module 'module1' from 'c:/root/generated/src/project/file2.ts'. ========",
8181
"Module resolution kind is not specified, using 'NodeJs'.",
82-
"'baseUrl' option is set to 'c:/root', using this value to resolve non-relative module name 'module1'",
82+
"'baseUrl' option is set to 'c:/root/', using this value to resolve non-relative module name 'module1'",
8383
"'paths' option is specified, looking for a pattern to match module name 'module1'.",
8484
"Module name 'module1', matched pattern '*'.",
8585
"Trying substitution '*', candidate module location: 'module1'.",
@@ -104,7 +104,7 @@
104104
"======== Module name 'module1' was successfully resolved to 'c:/shared/module1/index.d.ts'. ========",
105105
"======== Resolving module 'templates/module2' from 'c:/root/generated/src/project/file2.ts'. ========",
106106
"Module resolution kind is not specified, using 'NodeJs'.",
107-
"'baseUrl' option is set to 'c:/root', using this value to resolve non-relative module name 'templates/module2'",
107+
"'baseUrl' option is set to 'c:/root/', using this value to resolve non-relative module name 'templates/module2'",
108108
"'paths' option is specified, looking for a pattern to match module name 'templates/module2'.",
109109
"Module name 'templates/module2', matched pattern 'templates/*'.",
110110
"Trying substitution 'generated/src/templates/*', candidate module location: 'generated/src/templates/module2'.",

tests/baselines/reference/relativeModuleWithoutSlash.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ export default { aIndex: 0 };
1111
import a from ".";
1212
import aIndex from "./";
1313
a.a;
14-
aIndex.a; //aIndex.aIndex; See GH#9690
14+
aIndex.aIndex;
1515

1616
//// [test.ts]
1717
import a from "..";
1818
import aIndex from "../";
1919
a.a;
20-
aIndex.a; //aIndex.aIndex;
20+
aIndex.aIndex;
2121

2222

2323
//// [a.js]
@@ -33,10 +33,10 @@ exports["default"] = { aIndex: 0 };
3333
var _1 = require(".");
3434
var _2 = require("./");
3535
_1["default"].a;
36-
_2["default"].a; //aIndex.aIndex; See GH#9690
36+
_2["default"].aIndex;
3737
//// [test.js]
3838
"use strict";
3939
var __1 = require("..");
4040
var _1 = require("../");
4141
__1["default"].a;
42-
_1["default"].a; //aIndex.aIndex;
42+
_1["default"].aIndex;

tests/baselines/reference/relativeModuleWithoutSlash.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ a.a;
1919
>a : Symbol(a, Decl(test.ts, 0, 6))
2020
>a : Symbol(a, Decl(a.ts, 1, 16))
2121

22-
aIndex.a; //aIndex.aIndex; See GH#9690
23-
>aIndex.a : Symbol(a, Decl(a.ts, 1, 16))
22+
aIndex.aIndex;
23+
>aIndex.aIndex : Symbol(aIndex, Decl(index.ts, 0, 16))
2424
>aIndex : Symbol(aIndex, Decl(test.ts, 1, 6))
25-
>a : Symbol(a, Decl(a.ts, 1, 16))
25+
>aIndex : Symbol(aIndex, Decl(index.ts, 0, 16))
2626

2727
=== /a/b/test.ts ===
2828
import a from "..";
@@ -36,8 +36,8 @@ a.a;
3636
>a : Symbol(a, Decl(test.ts, 0, 6))
3737
>a : Symbol(a, Decl(a.ts, 1, 16))
3838

39-
aIndex.a; //aIndex.aIndex;
40-
>aIndex.a : Symbol(a, Decl(a.ts, 1, 16))
39+
aIndex.aIndex;
40+
>aIndex.aIndex : Symbol(aIndex, Decl(index.ts, 0, 16))
4141
>aIndex : Symbol(aIndex, Decl(test.ts, 1, 6))
42-
>a : Symbol(a, Decl(a.ts, 1, 16))
42+
>aIndex : Symbol(aIndex, Decl(index.ts, 0, 16))
4343

tests/baselines/reference/relativeModuleWithoutSlash.trace.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
"======== Module name '.' was successfully resolved to '/a.ts'. ========",
88
"======== Resolving module './' from '/a/test.ts'. ========",
99
"Explicitly specified module resolution kind: 'NodeJs'.",
10-
"Loading module as file / folder, candidate module location '/a'.",
11-
"File '/a.ts' exist - use it as a name resolution result.",
12-
"Resolving real path for '/a.ts', result '/a.ts'",
13-
"======== Module name './' was successfully resolved to '/a.ts'. ========",
10+
"Loading module as file / folder, candidate module location '/a/'.",
11+
"File '/a/package.json' does not exist.",
12+
"File '/a/index.ts' exist - use it as a name resolution result.",
13+
"Resolving real path for '/a/index.ts', result '/a/index.ts'",
14+
"======== Module name './' was successfully resolved to '/a/index.ts'. ========",
1415
"======== Resolving module '..' from '/a/b/test.ts'. ========",
1516
"Explicitly specified module resolution kind: 'NodeJs'.",
1617
"Loading module as file / folder, candidate module location '/a'.",
@@ -19,8 +20,9 @@
1920
"======== Module name '..' was successfully resolved to '/a.ts'. ========",
2021
"======== Resolving module '../' from '/a/b/test.ts'. ========",
2122
"Explicitly specified module resolution kind: 'NodeJs'.",
22-
"Loading module as file / folder, candidate module location '/a'.",
23-
"File '/a.ts' exist - use it as a name resolution result.",
24-
"Resolving real path for '/a.ts', result '/a.ts'",
25-
"======== Module name '../' was successfully resolved to '/a.ts'. ========"
23+
"Loading module as file / folder, candidate module location '/a/'.",
24+
"File '/a/package.json' does not exist.",
25+
"File '/a/index.ts' exist - use it as a name resolution result.",
26+
"Resolving real path for '/a/index.ts', result '/a/index.ts'",
27+
"======== Module name '../' was successfully resolved to '/a/index.ts'. ========"
2628
]

tests/baselines/reference/relativeModuleWithoutSlash.types

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@ import a from ".";
1616
>a : { a: number; }
1717

1818
import aIndex from "./";
19-
>aIndex : { a: number; }
19+
>aIndex : { aIndex: number; }
2020

2121
a.a;
2222
>a.a : number
2323
>a : { a: number; }
2424
>a : number
2525

26-
aIndex.a; //aIndex.aIndex; See GH#9690
27-
>aIndex.a : number
28-
>aIndex : { a: number; }
29-
>a : number
26+
aIndex.aIndex;
27+
>aIndex.aIndex : number
28+
>aIndex : { aIndex: number; }
29+
>aIndex : number
3030

3131
=== /a/b/test.ts ===
3232
import a from "..";
3333
>a : { a: number; }
3434

3535
import aIndex from "../";
36-
>aIndex : { a: number; }
36+
>aIndex : { aIndex: number; }
3737

3838
a.a;
3939
>a.a : number
4040
>a : { a: number; }
4141
>a : number
4242

43-
aIndex.a; //aIndex.aIndex;
44-
>aIndex.a : number
45-
>aIndex : { a: number; }
46-
>a : number
43+
aIndex.aIndex;
44+
>aIndex.aIndex : number
45+
>aIndex : { aIndex: number; }
46+
>aIndex : number
4747

tests/cases/compiler/relativeModuleWithoutSlash.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export default { aIndex: 0 };
1111
import a from ".";
1212
import aIndex from "./";
1313
a.a;
14-
aIndex.a; //aIndex.aIndex; See GH#9690
14+
aIndex.aIndex;
1515

1616
// @Filename: /a/b/test.ts
1717
import a from "..";
1818
import aIndex from "../";
1919
a.a;
20-
aIndex.a; //aIndex.aIndex;
20+
aIndex.aIndex;

0 commit comments

Comments
 (0)