Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,7 @@ namespace ts {
}

export function fileExtensionIs(path: string, extension: string): boolean {
const pathLen = path.length;
const extLen = extension.length;
return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension;
return path.length > extension.length && endsWith(path, extension);
}

export function fileExtensionIsAny(path: string, extensions: string[]): boolean {
Expand All @@ -915,7 +913,6 @@ namespace ts {
return false;
}


// Reserved characters, forces escaping of any non-word (or digit), non-whitespace character.
// It may be inefficient (we could just match (/[-[\]{}()*+?.,\\^$|#\s]/g), but this is future
// proof.
Expand Down
8 changes: 1 addition & 7 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ namespace ts {
}

function moduleHasNonRelativeName(moduleName: string): boolean {
if (isRootedDiskPath(moduleName)) {
return false;
}

const i = moduleName.lastIndexOf("./", 1);
const startsWithDotSlashOrDotDotSlash = i === 0 || (i === 1 && moduleName.charCodeAt(0) === CharacterCodes.dot);
return !startsWithDotSlashOrDotDotSlash;
return !(isRootedDiskPath(moduleName) || isExternalModuleNameRelative(moduleName));
}

interface ModuleResolutionState {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ namespace ts {
export function isExternalModuleNameRelative(moduleName: string): boolean {
// TypeScript 1.0 spec (April 2014): 11.2.1
// An external module name is "relative" if the first term is "." or "..".
return moduleName.substr(0, 2) === "./" || moduleName.substr(0, 3) === "../" || moduleName.substr(0, 2) === ".\\" || moduleName.substr(0, 3) === "..\\";
return /^\.\.?($|[\\/])/.test(moduleName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use /^\.\.?($|[\\/])/.test(moduleName)? It's a regex, but I think it's still easier to read than 16 lines of imperative code.

}

export function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean) {
Expand Down Expand Up @@ -3120,6 +3120,6 @@ namespace ts {

export function endsWith(str: string, suffix: string): boolean {
const expectedPos = str.length - suffix.length;
return str.indexOf(suffix, expectedPos) === expectedPos;
return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos;
}
}
16 changes: 6 additions & 10 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1377,31 +1377,27 @@ namespace Harness {
writeByteOrderMark: boolean;
}

function stringEndsWith(str: string, end: string) {
return str.substr(str.length - end.length) === end;
}

export function isTS(fileName: string) {
return stringEndsWith(fileName, ".ts");
return ts.endsWith(fileName, ".ts");
}

export function isTSX(fileName: string) {
return stringEndsWith(fileName, ".tsx");
return ts.endsWith(fileName, ".tsx");
}

export function isDTS(fileName: string) {
return stringEndsWith(fileName, ".d.ts");
return ts.endsWith(fileName, ".d.ts");
}

export function isJS(fileName: string) {
return stringEndsWith(fileName, ".js");
return ts.endsWith(fileName, ".js");
}
export function isJSX(fileName: string) {
return stringEndsWith(fileName, ".jsx");
return ts.endsWith(fileName, ".jsx");
}

export function isJSMap(fileName: string) {
return stringEndsWith(fileName, ".js.map") || stringEndsWith(fileName, ".jsx.map");
return ts.endsWith(fileName, ".js.map") || ts.endsWith(fileName, ".jsx.map");
}

/** Contains the code and errors of a compilation and some helper methods to check its status. */
Expand Down
10 changes: 0 additions & 10 deletions src/services/patternMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,16 +514,6 @@ namespace ts {
return str === str.toLowerCase();
}

function startsWith(string: string, search: string) {
for (let i = 0, n = search.length; i < n; i++) {
if (string.charCodeAt(i) !== search.charCodeAt(i)) {
return false;
}
}

return true;
}

// Assumes 'value' is already lowercase.
function indexOfIgnoringCase(string: string, value: string): number {
for (let i = 0, n = string.length - value.length; i <= n; i++) {
Expand Down
42 changes: 42 additions & 0 deletions tests/baselines/reference/relativeModuleWithoutSlash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [tests/cases/compiler/relativeModuleWithoutSlash.ts] ////

//// [a.ts]

export default { a: 0 };

//// [index.ts]
export default { aIndex: 0 };

//// [test.ts]
import a from ".";
import aIndex from "./";
a.a;
aIndex.a; //aIndex.aIndex; See GH#9690

//// [test.ts]
import a from "..";
import aIndex from "../";
a.a;
aIndex.a; //aIndex.aIndex;


//// [a.js]
"use strict";
exports.__esModule = true;
exports["default"] = { a: 0 };
//// [index.js]
"use strict";
exports.__esModule = true;
exports["default"] = { aIndex: 0 };
//// [test.js]
"use strict";
var _1 = require(".");
var _2 = require("./");
_1["default"].a;
_2["default"].a; //aIndex.aIndex; See GH#9690
//// [test.js]
"use strict";
var __1 = require("..");
var _1 = require("../");
__1["default"].a;
_1["default"].a; //aIndex.aIndex;
43 changes: 43 additions & 0 deletions tests/baselines/reference/relativeModuleWithoutSlash.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
=== tests/cases/compiler/a.ts ===

export default { a: 0 };
>a : Symbol(a, Decl(a.ts, 1, 16))

=== tests/cases/compiler/a/index.ts ===
export default { aIndex: 0 };
>aIndex : Symbol(aIndex, Decl(index.ts, 0, 16))

=== tests/cases/compiler/a/test.ts ===
import a from ".";
>a : Symbol(a, Decl(test.ts, 0, 6))

import aIndex from "./";
>aIndex : Symbol(aIndex, Decl(test.ts, 1, 6))

a.a;
>a.a : Symbol(a, Decl(a.ts, 1, 16))
>a : Symbol(a, Decl(test.ts, 0, 6))
>a : Symbol(a, Decl(a.ts, 1, 16))

aIndex.a; //aIndex.aIndex; See GH#9690
>aIndex.a : Symbol(a, Decl(a.ts, 1, 16))
>aIndex : Symbol(aIndex, Decl(test.ts, 1, 6))
>a : Symbol(a, Decl(a.ts, 1, 16))

=== tests/cases/compiler/a/b/test.ts ===
import a from "..";
>a : Symbol(a, Decl(test.ts, 0, 6))

import aIndex from "../";
>aIndex : Symbol(aIndex, Decl(test.ts, 1, 6))

a.a;
>a.a : Symbol(a, Decl(a.ts, 1, 16))
>a : Symbol(a, Decl(test.ts, 0, 6))
>a : Symbol(a, Decl(a.ts, 1, 16))

aIndex.a; //aIndex.aIndex;
>aIndex.a : Symbol(a, Decl(a.ts, 1, 16))
>aIndex : Symbol(aIndex, Decl(test.ts, 1, 6))
>a : Symbol(a, Decl(a.ts, 1, 16))

26 changes: 26 additions & 0 deletions tests/baselines/reference/relativeModuleWithoutSlash.trace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
"======== Resolving module '.' from 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a/test.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module as file / folder, candidate module location 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a'.",
"File 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
"Resolving real path for 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts', result 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'",
"======== Module name '.' was successfully resolved to 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'. ========",
"======== Resolving module './' from 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a/test.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module as file / folder, candidate module location 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a'.",
"File 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
"Resolving real path for 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts', result 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'",
"======== Module name './' was successfully resolved to 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'. ========",
"======== Resolving module '..' from 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a/b/test.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module as file / folder, candidate module location 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a'.",
"File 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
"Resolving real path for 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts', result 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'",
"======== Module name '..' was successfully resolved to 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'. ========",
"======== Resolving module '../' from 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a/b/test.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module as file / folder, candidate module location 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a'.",
"File 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts' exist - use it as a name resolution result.",
"Resolving real path for 'C:/Users/anhans/work/TypeScript/tests/cases/compiler/a.ts', result 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'",
"======== Module name '../' was successfully resolved to 'c:/users/anhans/work/typescript/tests/cases/compiler/a.ts'. ========"
]
47 changes: 47 additions & 0 deletions tests/baselines/reference/relativeModuleWithoutSlash.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== tests/cases/compiler/a.ts ===

export default { a: 0 };
>{ a: 0 } : { a: number; }
>a : number
>0 : number

=== tests/cases/compiler/a/index.ts ===
export default { aIndex: 0 };
>{ aIndex: 0 } : { aIndex: number; }
>aIndex : number
>0 : number

=== tests/cases/compiler/a/test.ts ===
import a from ".";
>a : { a: number; }

import aIndex from "./";
>aIndex : { a: number; }

a.a;
>a.a : number
>a : { a: number; }
>a : number

aIndex.a; //aIndex.aIndex; See GH#9690
>aIndex.a : number
>aIndex : { a: number; }
>a : number

=== tests/cases/compiler/a/b/test.ts ===
import a from "..";
>a : { a: number; }

import aIndex from "../";
>aIndex : { a: number; }

a.a;
>a.a : number
>a : { a: number; }
>a : number

aIndex.a; //aIndex.aIndex;
>aIndex.a : number
>aIndex : { a: number; }
>a : number

20 changes: 20 additions & 0 deletions tests/cases/compiler/relativeModuleWithoutSlash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @traceResolution: true
// @moduleResolution: node

// @Filename: a.ts
export default { a: 0 };

// @Filename: a/index.ts
export default { aIndex: 0 };

// @Filename: a/test.ts
import a from ".";
import aIndex from "./";
a.a;
aIndex.a; //aIndex.aIndex; See GH#9690

// @Filename: a/b/test.ts
import a from "..";
import aIndex from "../";
a.a;
aIndex.a; //aIndex.aIndex;