Skip to content

Commit

Permalink
Improve paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 19, 2023
1 parent 6b19fe1 commit e4b6857
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 14 deletions.
24 changes: 12 additions & 12 deletions crates/typescript/platform/src/sync_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,21 @@ impl<'app> TypeScriptSyncer<'app> {
)?],
);

tsconfig_compiler_paths.insert(
format!("{dep_package_name}/*"),
vec![to_relative_virtual_string(
ref_path.join(if index.starts_with("src") {
"src/*"
} else {
"*"
}),
&self.project.root,
)?],
);

break;
}
}

tsconfig_compiler_paths.insert(
format!("{dep_package_name}/*"),
vec![to_relative_virtual_string(
ref_path.join(if ref_path.join("src").exists() {
"src/*"
} else {
"*"
}),
&self.project.root,
)?],
);
}
}
}
Expand Down
77 changes: 77 additions & 0 deletions crates/typescript/platform/tests/sync_project_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use moon_typescript_lang::TsConfigJson;
use moon_typescript_platform::TypeScriptSyncer;
use moon_utils::string_vec;
use rustc_hash::FxHashSet;
use std::collections::BTreeMap;
use std::path::Path;

mod missing_tsconfig {
use super::*;
Expand Down Expand Up @@ -633,4 +635,79 @@ mod sync_config {
assert_eq!(tsconfig.references, None);
}
}

mod paths {
use super::*;

fn run_for_a(root: &Path) -> TsConfigJson {
let project = Project {
id: Id::raw("project"),
root: root.join("packages/a"),
..Project::default()
};

let config = TypeScriptConfig {
create_missing_config: true,
sync_project_references: true,
sync_project_references_to_paths: true,
..TypeScriptConfig::default()
};

TypeScriptSyncer::new(&project, &config, root)
.sync_project_tsconfig(FxHashSet::from_iter([
root.join("packages/b"),
root.join("common/c"),
]))
.unwrap();

TsConfigJson::read_with_name(project.root, "tsconfig.json")
.unwrap()
.unwrap()
}

#[test]
fn adds_wildcards() {
let sandbox = create_sandbox("empty");
sandbox.create_file("tsconfig.json", "{}");
sandbox.create_file("packages/a/tsconfig.json", "{}");
sandbox.create_file("packages/a/package.json", r#"{ "name": "a" }"#);
sandbox.create_file("packages/b/package.json", r#"{ "name": "b" }"#);
sandbox.create_file("packages/b/src/file.ts", ""); // Not index on purpose
sandbox.create_file("common/c/package.json", r#"{ "name": "c" }"#);

let tsconfig = run_for_a(sandbox.path());

assert_eq!(
tsconfig.compiler_options.unwrap().paths.unwrap(),
BTreeMap::from_iter([
("b/*".into(), vec!["../b/src/*".into()]),
("c/*".into(), vec!["../../common/c/*".into()]),
])
);
}

#[test]
fn adds_indexes() {
let sandbox = create_sandbox("empty");
sandbox.create_file("tsconfig.json", "{}");
sandbox.create_file("packages/a/tsconfig.json", "{}");
sandbox.create_file("packages/a/package.json", r#"{ "name": "a" }"#);
sandbox.create_file("packages/b/package.json", r#"{ "name": "b" }"#);
sandbox.create_file("packages/b/src/index.ts", "");
sandbox.create_file("common/c/package.json", r#"{ "name": "c" }"#);
sandbox.create_file("common/c/index.ts", "");

let tsconfig = run_for_a(sandbox.path());

assert_eq!(
tsconfig.compiler_options.unwrap().paths.unwrap(),
BTreeMap::from_iter([
("b".into(), vec!["../b/src/index.ts".into()]),
("b/*".into(), vec!["../b/src/*".into()]),
("c".into(), vec!["../../common/c/index.ts".into()]),
("c/*".into(), vec!["../../common/c/*".into()]),
])
);
}
}
}
2 changes: 1 addition & 1 deletion nextgen/config/src/project/overrides_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ cacheable!(
#[derive(Clone, Config, Debug)]
pub struct ProjectToolchainTypeScriptConfig {
pub disabled: bool,
pub include_shared_types: Option<bool>,
pub include_project_reference_sources: Option<bool>,
pub include_shared_types: Option<bool>,
pub route_out_dir_to_cache: Option<bool>,
pub sync_project_references: Option<bool>,
pub sync_project_references_to_paths: Option<bool>,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
- Improved TypeScript support.
- Added a `typescript.includeSharedTypes` setting, for syncing a shared types path to all
project's `include`.
- Updated the `typescript.rootConfigFileName` setting to support directories.
- Updated `typescript.rootConfigFileName` setting to support directories.
- Updated `typescript.syncProjectReferencesToPaths` setting to always include the wildcard, and
not require an index file.
- Improved project reference syncing and edge case handling.

## 1.16.5
Expand Down

0 comments on commit e4b6857

Please sign in to comment.