Treat "." and ".." as relative module names

This commit is contained in:
Andy Hanson
2016-07-12 14:54:06 -07:00
parent f9d29370b6
commit c90897ccdd
7 changed files with 102 additions and 11 deletions

View File

@@ -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 stringEndsWith(path, extension);
}
export function fileExtensionIsAny(path: string, extensions: string[]): boolean {
@@ -915,6 +913,17 @@ namespace ts {
return false;
}
// Should act like String.prototype.startsWith
export function stringStartsWith(s: string, start: string): boolean {
return s.length > start.length && s.substr(0, start.length) === start;
}
// Should act like String.prototype.endsWith
export function stringEndsWith(s: string, end: string): boolean {
const sLen = s.length;
const endLen = end.length;
return sLen > endLen && s.substr(sLen - endLen, endLen) === end;
}
// 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

View File

@@ -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 {

View File

@@ -1218,7 +1218,23 @@ 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) === "..\\";
if (moduleName.charCodeAt(0) === CharacterCodes.dot) {
if (moduleName.length === 1) {
return true;
}
switch (moduleName.charCodeAt(1)) {
case CharacterCodes.slash:
case CharacterCodes.backslash:
return true;
case CharacterCodes.dot:
if (moduleName.length === 2) {
return true;
}
const ch2 = moduleName.charCodeAt(2);
return ch2 === CharacterCodes.slash || ch2 === CharacterCodes.backslash;
}
}
return false;
}
export function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean) {