Skip to content

Commit c90897c

Browse files
author
Andy Hanson
committed
Treat "." and ".." as relative module names
1 parent f9d2937 commit c90897c

7 files changed

Lines changed: 102 additions & 11 deletions

File tree

src/compiler/core.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,7 @@ namespace ts {
900900
}
901901

902902
export function fileExtensionIs(path: string, extension: string): boolean {
903-
const pathLen = path.length;
904-
const extLen = extension.length;
905-
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
903+
return stringEndsWith(path, extension);
906904
}
907905

908906
export function fileExtensionIsAny(path: string, extensions: string[]): boolean {
@@ -915,6 +913,17 @@ namespace ts {
915913
return false;
916914
}
917915

916+
// Should act like String.prototype.startsWith
917+
export function stringStartsWith(s: string, start: string): boolean {
918+
return s.length > start.length && s.substr(0, start.length) === start;
919+
}
920+
921+
// Should act like String.prototype.endsWith
922+
export function stringEndsWith(s: string, end: string): boolean {
923+
const sLen = s.length;
924+
const endLen = end.length;
925+
return sLen > endLen && s.substr(sLen - endLen, endLen) === end;
926+
}
918927

919928
// Reserved characters, forces escaping of any non-word (or digit), non-whitespace character.
920929
// It may be inefficient (we could just match (/[-[\]{}()*+?.,\\^$|#\s]/g), but this is future

src/compiler/program.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,7 @@ namespace ts {
112112
}
113113

114114
function moduleHasNonRelativeName(moduleName: string): boolean {
115-
if (isRootedDiskPath(moduleName)) {
116-
return false;
117-
}
118-
119-
const i = moduleName.lastIndexOf("./", 1);
120-
const startsWithDotSlashOrDotDotSlash = i === 0 || (i === 1 && moduleName.charCodeAt(0) === CharacterCodes.dot);
121-
return !startsWithDotSlashOrDotDotSlash;
115+
return !(isRootedDiskPath(moduleName) || isExternalModuleNameRelative(moduleName));
122116
}
123117

124118
interface ModuleResolutionState {

src/compiler/utilities.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,23 @@ namespace ts {
12181218
export function isExternalModuleNameRelative(moduleName: string): boolean {
12191219
// TypeScript 1.0 spec (April 2014): 11.2.1
12201220
// An external module name is "relative" if the first term is "." or "..".
1221-
return moduleName.substr(0, 2) === "./" || moduleName.substr(0, 3) === "../" || moduleName.substr(0, 2) === ".\\" || moduleName.substr(0, 3) === "..\\";
1221+
if (moduleName.charCodeAt(0) === CharacterCodes.dot) {
1222+
if (moduleName.length === 1) {
1223+
return true;
1224+
}
1225+
switch (moduleName.charCodeAt(1)) {
1226+
case CharacterCodes.slash:
1227+
case CharacterCodes.backslash:
1228+
return true;
1229+
case CharacterCodes.dot:
1230+
if (moduleName.length === 2) {
1231+
return true;
1232+
}
1233+
const ch2 = moduleName.charCodeAt(2);
1234+
return ch2 === CharacterCodes.slash || ch2 === CharacterCodes.backslash;
1235+
}
1236+
}
1237+
return false;
12221238
}
12231239

12241240
export function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/relativeModuleWithoutSlash.ts] ////
2+
3+
//// [index.ts]
4+
export default 0;
5+
6+
//// [index.ts]
7+
export default 1;
8+
9+
//// [a.ts]
10+
import parent from "..";
11+
import here from ".";
12+
parent + here;
13+
14+
15+
//// [index.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports["default"] = 0;
19+
//// [index.js]
20+
"use strict";
21+
exports.__esModule = true;
22+
exports["default"] = 1;
23+
//// [a.js]
24+
"use strict";
25+
var __1 = require("..");
26+
var _1 = require(".");
27+
__1["default"] + _1["default"];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/index.ts ===
2+
export default 0;
3+
No type information for this code.
4+
No type information for this code.=== tests/cases/compiler/a/index.ts ===
5+
export default 1;
6+
No type information for this code.
7+
No type information for this code.=== tests/cases/compiler/a/a.ts ===
8+
import parent from "..";
9+
>parent : Symbol(parent, Decl(a.ts, 0, 6))
10+
11+
import here from ".";
12+
>here : Symbol(here, Decl(a.ts, 1, 6))
13+
14+
parent + here;
15+
>parent : Symbol(parent, Decl(a.ts, 0, 6))
16+
>here : Symbol(here, Decl(a.ts, 1, 6))
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/index.ts ===
2+
export default 0;
3+
No type information for this code.
4+
No type information for this code.=== tests/cases/compiler/a/index.ts ===
5+
export default 1;
6+
No type information for this code.
7+
No type information for this code.=== tests/cases/compiler/a/a.ts ===
8+
import parent from "..";
9+
>parent : number
10+
11+
import here from ".";
12+
>here : number
13+
14+
parent + here;
15+
>parent + here : number
16+
>parent : number
17+
>here : number
18+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @Filename: index.ts
2+
export default 0;
3+
4+
// @Filename: a/index.ts
5+
export default 1;
6+
7+
// @Filename: a/a.ts
8+
import parent from "..";
9+
import here from ".";
10+
parent + here;

0 commit comments

Comments
 (0)