Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions crates/codegraph-cli/tests/sync_convergence/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10384,6 +10384,75 @@ pub fn typescript_tsconfig_paths_alias_disappears() -> Case {
.remove("tsconfig.json")
}

/// A `tsconfig.json` whose one `paths` key has text on both sides of its `*`
/// (upstream 77ee3484, #2969), and `src/main.ts` without and with an import
/// of `@x/index`, which starts with `@x/` and ends with `/index` only by
/// counting its one `/` twice.
const OVERLAP_TSCONFIG: &str = "{\"compilerOptions\":{\"baseUrl\":\"./src\",\"paths\":{\"@x/*/index\":[\"widgets/*/index\"]}}}\n";
const OVERLAP_MAIN: &str = "import { press } from '@x/button/index';\n\nexport function go(): number {\n return press();\n}\n";
const OVERLAP_MAIN_IMPORT: &str = "import { trap as caught } from '@x/index';\nimport { press } from '@x/button/index';\n\nexport function go(): number {\n return caught() + press();\n}\n";

/// The overlap project, its `src/main.ts` written `main`: a `trap` in
/// `src/widgets/index.ts`, the file the empty capture would name, and a
/// namesake in `src/index.ts`.
fn overlapping_wildcard_project(main: &'static str) -> Case {
Case::new(Change::BodyEdit, &[Language::TypeScript])
.file("tsconfig.json", OVERLAP_TSCONFIG)
.file(
"src/index.ts",
"export function trap(): number {\n return 1;\n}\n",
)
.file(
"src/widgets/index.ts",
"export function trap(): number {\n return 0;\n}\n",
)
.file(
"src/widgets/button/index.ts",
"export function press(): number {\n return 2;\n}\n",
)
.file("src/main.ts", main)
}

/// A10 (upstream 77ee3484, #2969): `src/main.ts` comes to import `@x/index`
/// under the `paths` key `@x/*/index`, which leaves nothing for its `*` there
/// and so does not match it, as tsc's `isPatternMatch` rules. The sync no
/// longer panics, links neither the import nor the call through it to
/// `src/widgets/index.ts`, and equals a fresh index.
pub fn typescript_tsconfig_overlapping_wildcard_import_appears() -> Case {
overlapping_wildcard_project(OVERLAP_MAIN)
.write("src/main.ts", OVERLAP_MAIN_IMPORT)
.edge_throughout(
"file:src/main.ts",
"imports",
"file:src/widgets/button/index.ts",
)
.named_after("import", "@x/index")
.no_edge_after("file:src/main.ts", "imports", "file:src/widgets/index.ts")
.no_edge_after(
"file:src/main.ts",
"imports",
"function trap @src/widgets/index.ts",
)
.no_edge_after(
"function go",
"calls",
"function trap @src/widgets/index.ts",
)
}

/// A10: the import of `@x/index` goes again. The index that held it
/// initializes, and the sync that drops it equals a fresh index.
pub fn typescript_tsconfig_overlapping_wildcard_import_disappears() -> Case {
overlapping_wildcard_project(OVERLAP_MAIN_IMPORT)
.write("src/main.ts", OVERLAP_MAIN)
.named_before("import", "@x/index")
.edge_throughout(
"file:src/main.ts",
"imports",
"file:src/widgets/button/index.ts",
)
}

/// W5-06: the tsconfig `extends` a file that does not exist yet; once it is
/// created with `paths`, the alias import resolves (the missing target is a
/// recorded input).
Expand Down
2 changes: 2 additions & 0 deletions crates/codegraph-cli/tests/sync_convergence/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,8 @@ matrix! {
typescript_react_dependency_appears_in_package_json,
javascript_jsconfig_paths_alias_appears,
typescript_tsconfig_paths_alias_disappears,
typescript_tsconfig_overlapping_wildcard_import_appears,
typescript_tsconfig_overlapping_wildcard_import_disappears,
typescript_tsconfig_extends_target_appears,
typescript_paths_retarget_moves_a_barrel_source,
typescript_app_tsconfig_paths_appear,
Expand Down
112 changes: 112 additions & 0 deletions crates/codegraph-resolve/src/path_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ pub fn apply_aliases(import_path: &str, aliases: &AliasMap, project_root: &str)
}

let captured = if pat.has_wildcard {
// The prefix and the suffix may not share characters: `@x/*/index`
// starts and ends `@x/index` only by counting its one `/` twice,
// which leaves nothing for `*`. tsc's `isPatternMatch` wants the
// import at least as long as the two together, so the pattern
// does not match and a less specific one may (upstream #2969).
// Both are byte-wise prefix and suffix of the import, so byte
// lengths decide overlap as tsc's UTF-16 lengths do.
if import_path.len() < pat.prefix.len() + pat.suffix.len() {
continue;
}
import_path[pat.prefix.len()..import_path.len() - pat.suffix.len()].to_string()
} else if import_path != pat.prefix {
// Literal pattern must match exactly.
Expand Down Expand Up @@ -1058,6 +1068,108 @@ mod tests {
);
}

/// A project whose `tsconfig.json` maps `paths` from `./src`, as
/// upstream's `path-alias-overlap.test.ts` (77ee3484, #2969) writes it.
fn overlap_project(tag: &str, paths: Value) -> (PathBuf, AliasMap) {
let root = temp_dir(tag);
write_json(
root.join("tsconfig.json"),
serde_json::json!({ "compilerOptions": { "baseUrl": "./src", "paths": paths } }),
);
let map = load_project_aliases(root.to_str().unwrap()).expect("aliases");
(root, map)
}

/// `@x/*/index` splits into `@x/` and `/index`. `@x/index` starts with
/// the one and ends with the other only by counting its one `/` twice,
/// which leaves nothing for `*`: the pattern does not match there.
#[test]
fn apply_aliases_skips_a_wildcard_whose_prefix_and_suffix_overlap() {
let (root, map) = overlap_project(
"overlap",
serde_json::json!({ "@x/*/index": ["widgets/*/index"] }),
);
let project = root.to_str().unwrap();
assert!(apply_aliases("@x/index", &map, project).is_empty());
assert_eq!(
apply_aliases("@x/button/index", &map, project),
vec!["src/widgets/button/index"]
);
std::fs::remove_dir_all(&root).ok();
}

/// Skipping an overlapping pattern does not end the search. Both
/// prefixes are `@x/`, so tsc tries the patterns in the order the config
/// declares them; this map lists them in that order, the overlapping one
/// first.
#[test]
fn apply_aliases_falls_through_past_an_overlapping_wildcard() {
let map = AliasMap {
base_url: "/proj/src".to_string(),
patterns: vec![
AliasPattern {
prefix: "@x/".to_string(),
suffix: "/index".to_string(),
has_wildcard: true,
replacements: vec!["widgets/*/index".to_string()],
},
AliasPattern {
prefix: "@x/".to_string(),
suffix: String::new(),
has_wildcard: true,
replacements: vec!["lib/*".to_string()],
},
],
};
assert_eq!(
apply_aliases("@x/index", &map, "/proj"),
vec!["src/lib/index"]
);
assert_eq!(
apply_aliases("@x/button/index", &map, "/proj"),
vec!["src/widgets/button/index"]
);
}

/// The specificity sort tries the longer prefix `@app/x/` first; where
/// it overlaps, the less specific `@app/*` answers.
#[test]
fn apply_aliases_falls_through_to_a_shorter_prefix_past_an_overlap() {
let (root, map) = overlap_project(
"overlap-shorter",
serde_json::json!({
"@app/*": ["lib/*"],
"@app/x/*/index": ["widgets/*/index"]
}),
);
let project = root.to_str().unwrap();
assert_eq!(map.patterns[0].prefix, "@app/x/");
assert_eq!(
apply_aliases("@app/x/index", &map, project),
vec!["src/lib/x/index"]
);
assert_eq!(
apply_aliases("@app/x/button/index", &map, project),
vec!["src/widgets/button/index"]
);
std::fs::remove_dir_all(&root).ok();
}

/// tsc's bound is `>=`: at exactly the prefix plus the suffix the two
/// meet without sharing a character, and `*` captures the empty string.
#[test]
fn apply_aliases_accepts_an_empty_capture_at_exactly_prefix_plus_suffix() {
let (root, map) = overlap_project(
"overlap-exact",
serde_json::json!({ "@x/*/index": ["widgets/*/index"] }),
);
assert_eq!(
apply_aliases("@x//index", &map, root.to_str().unwrap()),
vec!["src/widgets/index"]
);
std::fs::remove_dir_all(&root).ok();
}

#[test]
fn alias_types_derive_debug_clone_eq() {
let m = wildcard_map();
Expand Down
Loading