Always use forward slash on import paths (#346)

This commit is contained in:
Gustavo Shigueo
2024-08-09 08:43:22 -03:00
committed by GitHub
parent 5cbc7405d8
commit 78591a2831
3 changed files with 36 additions and 3 deletions

View File

@@ -163,3 +163,27 @@ jobs:
shopt -s globstar
tsc bindings/**/*.ts --noEmit --noUnusedLocals
rm -rf bindings
test-windows:
name: Test ts-rs with --all-features on Windows
runs-on: windows-latest
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
steps:
- uses: actions/checkout@v4
- uses: rui314/setup-mold@v1
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
# Create empty tsconfig and cd into bindings to make tsc
# compile every ts file in the directory
run: |
cargo test --all-features
"{}" | Out-File bindings/tsconfig.json
cd bindings
npm i -g typescript
tsc --noEmit --noUnusedLocals --strict
cd ..
rm -r -fo bindings

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
target/
/.idea
*.ts
tsconfig.json
/ts-rs/tests-out

View File

@@ -323,7 +323,7 @@ fn generate_imports<T: TS + ?Sized + 'static>(
writeln!(
out,
"import type {{ {} }} from {:?};",
r#"import type {{ {} }} from "{}";"#,
&dep.ts_name, rel_path
)?;
}
@@ -334,11 +334,19 @@ fn generate_imports<T: TS + ?Sized + 'static>(
/// Returns the required import path for importing `import` from the file `from`
fn import_path(from: &Path, import: &Path) -> Result<String, ExportError> {
let rel_path = diff_paths(import, from.parent().unwrap())?;
let path = match rel_path.components().next() {
Some(Component::Normal(_)) => format!("./{}", rel_path.to_string_lossy()),
let str_path = match rel_path.components().next() {
Some(Component::Normal(_)) => {
format!("./{}", rel_path.to_string_lossy())
}
_ => rel_path.to_string_lossy().into(),
};
let path = if cfg!(target_os = "windows") {
str_path.replace('\\', "/")
} else {
str_path
};
let path_without_extension = path.trim_end_matches(".ts");
Ok(if cfg!(feature = "import-esm") {