mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(compile/windows): handle cjs re-export of relative path with parent component (#24795)
Closes https://github.com/denoland/deno/issues/24785
This commit is contained in:
parent
7a3810195d
commit
fe884c557a
7 changed files with 94 additions and 1 deletions
|
@ -13,8 +13,31 @@ pub trait PathClean<T> {
|
|||
|
||||
impl PathClean<PathBuf> for PathBuf {
|
||||
fn clean(&self) -> PathBuf {
|
||||
fn is_clean_path(path: &Path) -> bool {
|
||||
let path = path.to_string_lossy();
|
||||
let mut current_index = 0;
|
||||
while let Some(index) = path[current_index..].find("\\.") {
|
||||
let trailing_index = index + current_index + 2;
|
||||
let mut trailing_chars = path[trailing_index..].chars();
|
||||
match trailing_chars.next() {
|
||||
Some('.') => match trailing_chars.next() {
|
||||
Some('/') | Some('\\') | None => {
|
||||
return false;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Some('/') | Some('\\') => {
|
||||
return false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
current_index = trailing_index;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
let path = path_clean::PathClean::clean(self);
|
||||
if cfg!(windows) && path.to_string_lossy().contains("..\\") {
|
||||
if cfg!(windows) && !is_clean_path(&path) {
|
||||
// temporary workaround because path_clean::PathClean::clean is
|
||||
// not good enough on windows
|
||||
let mut components = Vec::new();
|
||||
|
@ -103,6 +126,20 @@ pub fn strip_unc_prefix(path: PathBuf) -> PathBuf {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn test_path_clean() {
|
||||
use super::*;
|
||||
|
||||
run_test("C:\\test\\./file.txt", "C:\\test\\file.txt");
|
||||
run_test("C:\\test\\../other/file.txt", "C:\\other\\file.txt");
|
||||
run_test("C:\\test\\../other\\file.txt", "C:\\other\\file.txt");
|
||||
|
||||
fn run_test(input: &str, expected: &str) {
|
||||
assert_eq!(PathBuf::from(input).clean(), PathBuf::from(expected));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn test_strip_unc_prefix() {
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
"use strict";
|
||||
class Hello {
|
||||
sayHello() {
|
||||
console.log("Hi.");
|
||||
}
|
||||
}
|
||||
exports.hello = new Hello();
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "@denotest/cjs-reexport-relative-parent",
|
||||
"version": "1.0.0",
|
||||
"main": "./sub_dir/index.js"
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
// collision will occur with __esModule in other_file.js
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const other_file_1 = __importDefault(require("../dir"));
|
||||
__exportStar(require("../dir"), exports);
|
||||
exports.default = other_file_1.default;
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"tempDir": true,
|
||||
"steps": [{
|
||||
"if": "unix",
|
||||
"args": "compile --output main main.ts",
|
||||
"output": "[WILDCARD]"
|
||||
}, {
|
||||
"if": "unix",
|
||||
"commandName": "./main",
|
||||
"args": [],
|
||||
"output": "main.out"
|
||||
}, {
|
||||
"if": "windows",
|
||||
"args": "compile --output main.exe main.ts",
|
||||
"output": "[WILDCARD]"
|
||||
}, {
|
||||
"if": "windows",
|
||||
"commandName": "./main.exe",
|
||||
"args": [],
|
||||
"output": "main.out"
|
||||
}]
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Hi.
|
|
@ -0,0 +1,2 @@
|
|||
import { hello } from "npm:@denotest/cjs-reexport-relative-parent";
|
||||
hello.sayHello();
|
Loading…
Reference in a new issue