mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
Simplify ts-compiler's normalizeString (#5072)
This commit is contained in:
parent
92c0591fcb
commit
38ecabf205
2 changed files with 8 additions and 23 deletions
|
@ -1,7 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
import { SYSTEM_LOADER } from "./bootstrap.ts";
|
import { SYSTEM_LOADER } from "./bootstrap.ts";
|
||||||
import { commonPath, normalizeString, CHAR_FORWARD_SLASH } from "./util.ts";
|
import { commonPath, normalizeString } from "./util.ts";
|
||||||
import { assert } from "../util.ts";
|
import { assert } from "../util.ts";
|
||||||
|
|
||||||
let rootExports: string[] | undefined;
|
let rootExports: string[] | undefined;
|
||||||
|
@ -10,12 +10,7 @@ function normalizeUrl(rootName: string): string {
|
||||||
const match = /^(\S+:\/{2,3})(.+)$/.exec(rootName);
|
const match = /^(\S+:\/{2,3})(.+)$/.exec(rootName);
|
||||||
if (match) {
|
if (match) {
|
||||||
const [, protocol, path] = match;
|
const [, protocol, path] = match;
|
||||||
return `${protocol}${normalizeString(
|
return `${protocol}${normalizeString(path)}`;
|
||||||
path,
|
|
||||||
false,
|
|
||||||
"/",
|
|
||||||
(code) => code === CHAR_FORWARD_SLASH
|
|
||||||
)}`;
|
|
||||||
} else {
|
} else {
|
||||||
return rootName;
|
return rootName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -299,12 +299,7 @@ export function processConfigureResponse(
|
||||||
export const CHAR_DOT = 46; /* . */
|
export const CHAR_DOT = 46; /* . */
|
||||||
export const CHAR_FORWARD_SLASH = 47; /* / */
|
export const CHAR_FORWARD_SLASH = 47; /* / */
|
||||||
|
|
||||||
export function normalizeString(
|
export function normalizeString(path: string): string {
|
||||||
path: string,
|
|
||||||
allowAboveRoot: boolean,
|
|
||||||
separator: string,
|
|
||||||
isPathSeparator: (code: number) => boolean
|
|
||||||
): string {
|
|
||||||
let res = "";
|
let res = "";
|
||||||
let lastSegmentLength = 0;
|
let lastSegmentLength = 0;
|
||||||
let lastSlash = -1;
|
let lastSlash = -1;
|
||||||
|
@ -312,10 +307,10 @@ export function normalizeString(
|
||||||
let code: number;
|
let code: number;
|
||||||
for (let i = 0, len = path.length; i <= len; ++i) {
|
for (let i = 0, len = path.length; i <= len; ++i) {
|
||||||
if (i < len) code = path.charCodeAt(i);
|
if (i < len) code = path.charCodeAt(i);
|
||||||
else if (isPathSeparator(code!)) break;
|
else if (code! === CHAR_FORWARD_SLASH) break;
|
||||||
else code = CHAR_FORWARD_SLASH;
|
else code = CHAR_FORWARD_SLASH;
|
||||||
|
|
||||||
if (isPathSeparator(code)) {
|
if (code === CHAR_FORWARD_SLASH) {
|
||||||
if (lastSlash === i - 1 || dots === 1) {
|
if (lastSlash === i - 1 || dots === 1) {
|
||||||
// NOOP
|
// NOOP
|
||||||
} else if (lastSlash !== i - 1 && dots === 2) {
|
} else if (lastSlash !== i - 1 && dots === 2) {
|
||||||
|
@ -326,13 +321,13 @@ export function normalizeString(
|
||||||
res.charCodeAt(res.length - 2) !== CHAR_DOT
|
res.charCodeAt(res.length - 2) !== CHAR_DOT
|
||||||
) {
|
) {
|
||||||
if (res.length > 2) {
|
if (res.length > 2) {
|
||||||
const lastSlashIndex = res.lastIndexOf(separator);
|
const lastSlashIndex = res.lastIndexOf("/");
|
||||||
if (lastSlashIndex === -1) {
|
if (lastSlashIndex === -1) {
|
||||||
res = "";
|
res = "";
|
||||||
lastSegmentLength = 0;
|
lastSegmentLength = 0;
|
||||||
} else {
|
} else {
|
||||||
res = res.slice(0, lastSlashIndex);
|
res = res.slice(0, lastSlashIndex);
|
||||||
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
|
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
||||||
}
|
}
|
||||||
lastSlash = i;
|
lastSlash = i;
|
||||||
dots = 0;
|
dots = 0;
|
||||||
|
@ -345,13 +340,8 @@ export function normalizeString(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (allowAboveRoot) {
|
|
||||||
if (res.length > 0) res += `${separator}..`;
|
|
||||||
else res = "..";
|
|
||||||
lastSegmentLength = 2;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (res.length > 0) res += separator + path.slice(lastSlash + 1, i);
|
if (res.length > 0) res += "/" + path.slice(lastSlash + 1, i);
|
||||||
else res = path.slice(lastSlash + 1, i);
|
else res = path.slice(lastSlash + 1, i);
|
||||||
lastSegmentLength = i - lastSlash - 1;
|
lastSegmentLength = i - lastSlash - 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue