requirements.refactor.rationale.paths
Requirements index · View definition
Requirement
In the rationale of each changed record,
habeas refactorMUST rewrite each code span that is exactly a mapped file path, or a path beneath a mapped directory, to its new path.
Rationale
Obligation. In the rationale of each changed record, rewrite each code span that is exactly a mapped file path, or a path beneath a mapped directory, to its new path.
Implementation.
src/refactor.rs#fn[rewrite_record]callsrewrite_rationaleonly after itsif !changedearly return, so only changed records have their rationale rewritten.src/refactor.rs#fn[rewrite_rationale]carries the{impl}reference. It matches whole code spans withCODE_SPAN.- For each span it calls
mappings.address(span), which callsMappings::node, which falls back toMappings::path. Mappings::pathusesbeneath(file, old, '/'). This matches the old path exactly or any path beneath it, and returnsnew + rest.address()returns the bare file path when the item is empty, so a path span is replaced by the new path with the whole span matched.- Spans that only contain an old path are left unchanged; the code lists them as mentions. That behavior is specified by
rationale.untouched, which the same function marks with{impl}.
Tests.
src/refactor/tests.rs#fn[rationales_rewrite_exact_code_spans_and_list_other_mentions]has a directory mappingsrc/old→src/new. It asserts that a spansrc/old/x.rsbecomessrc/new/x.rs, so the path-beneath-a-directory case is covered. It also asserts that spans that are not exact matches (the baresrc/a.rsand a span with a suffix after the path) stay unchanged.tests/refactor.rs#fn[a_moved_file_rewrites_anchors_and_paths]runs the real command with a file mappingdata.txt=info.txt. It asserts that the saved record’s rationale starts with the spaninfo.txt, so the exact-file case is covered end to end.
Both tests carry {test} references to the subject. I found no gaps.
Inputs
Implementation
Input 3: outline src/refactor.rs fn[rewrite_rationale]
- File:
src/refactor.rs - Selector:
fn[rewrite_rationale] - Kind:
fn src/refactor.rs:350–374
/// Rewrites a rationale's code spans that are exactly an old definition
/// name, path, or node address, and lists the other spans that mention an
/// old path.
// {impl}`requirements.refactor.rationale.names`
// {impl}`requirements.refactor.rationale.paths`
// {impl}`requirements.refactor.rationale.addresses`
// {impl}`requirements.refactor.rationale.untouched`
fn rewrite_rationale(mappings: &Mappings, rationale: &str) -> (String, Vec<String>) {
let old_paths = mappings.old_paths();
let mut mentions = Vec::new();
let rewritten = CODE_SPAN.replace_all(rationale, |captures: ®ex::Captures<'_>| {
let span = &captures[1];
let mapped = mappings.name(span).or_else(|| mappings.address(span));
match mapped {
Some(new) => format!("`{new}`"),
None => {
if old_paths.iter().any(|old| span.contains(old)) {
mentions.push(format!("`{span}`"));
}
captures[0].to_owned()
}
}
});
(rewritten.into_owned(), mentions)
}
Tests
Input 2: outline src/refactor/tests.rs fn[rationales_rewrite_exact_code_spans_and_list_other_mentions]
- File:
src/refactor/tests.rs - Selector:
fn[rationales_rewrite_exact_code_spans_and_list_other_mentions] - Kind:
fn src/refactor/tests.rs:189–211
// {test}`requirements.refactor.rationale.names`
// {test}`requirements.refactor.rationale.paths`
// {test}`requirements.refactor.rationale.addresses`
// {test}`requirements.refactor.rationale.untouched`
#[test]
fn rationales_rewrite_exact_code_spans_and_list_other_mentions() {
let map = mappings(&[
("name", "rules.cli", "rules.core"),
("path", "src/old", "src/new"),
("node", "src/a.rs#fn[f]", "src/b.rs#fn[f]"),
]);
let (rewritten, mentions) = rewrite_rationale(
&map,
"`rules.cli.check` holds; `src/old/x.rs` and `src/a.rs#fn[f]` do it. \
`src/a.rs` stays, as does `src/a.rs:3`, rules.cli.check, and `rules.client`.",
);
assert_eq!(
rewritten,
"`rules.core.check` holds; `src/new/x.rs` and `src/b.rs#fn[f]` do it. \
`src/a.rs` stays, as does `src/a.rs:3`, rules.cli.check, and `rules.client`."
);
assert_eq!(mentions, ["`src/a.rs`", "`src/a.rs:3`"]);
}
Input 4: outline tests/refactor.rs fn[a_moved_file_rewrites_anchors_and_paths]
- File:
tests/refactor.rs - Selector:
fn[a_moved_file_rewrites_anchors_and_paths] - Kind:
fn tests/refactor.rs:368–393
// {test}`requirements.refactor.reference_rewrite`
// {test}`requirements.refactor.reference_digest`
// {test}`requirements.refactor.rationale.paths`
// {test}`requirements.refactor.equivalence`
// {test}`requirements.refactor.mappings`
#[test]
fn a_moved_file_rewrites_anchors_and_paths() {
let temp = project();
let root = temp.path();
author(root);
fs::rename(root.join("data.txt"), root.join("info.txt")).unwrap();
let output = refactor(root, &["--map", "data.txt=info.txt"]);
assert!(output.status.success(), "{output:?}");
let book = fs::read_to_string(root.join("md/rules.md")).unwrap();
assert!(book.contains(":impl: info.txt\n"), "{book}");
let record: Value =
serde_json::from_str(&fs::read_to_string(root.join("evidence/rules.check.json")).unwrap())
.unwrap();
assert!(
record["rationale"]
.as_str()
.unwrap()
.starts_with("`info.txt` is checked")
);
assert_eq!(errors(root), Vec::<String>::new());
}
Referenced definitions
Input 6: definition requirements.refactor.mapped_back
requirements.refactor.mapped_back:md/requirements/refactor.md:58–63
:::{definition} mapped_back
:phrase: mapped-back
The *mapped-back* form of current text or a current address is the text or
address with each new side of the mappings replaced by its old side.
:::
Other sources
Input 1: requirements selection requirements.refactor.rationale.paths
requirements.refactor.rationale.paths:md/requirements/refactor.md:138–142
:::{req} rationale.paths
In the rationale of each changed record, `habeas refactor` MUST rewrite each
code span that is exactly a mapped file path, or a path beneath a mapped
directory, to its new path.
:::
Input 5: outline src/refactor.rs impl[Mappings]
- File:
src/refactor.rs - Selector:
impl[Mappings] - Kind:
impl src/refactor.rs:102–205
// {impl}`requirements.refactor.mappings`
impl Mappings {
/// A definition name's new name, if a mapping covers it.
fn name(&self, name: &str) -> Option<String> {
self.names
.iter()
.find_map(|(old, new)| beneath(name, old, '.').map(|rest| format!("{new}{rest}")))
}
/// A current definition name's old name, if a mapping covers it.
fn name_back(&self, name: &str) -> Option<String> {
self.names
.iter()
.find_map(|(old, new)| beneath(name, new, '.').map(|rest| format!("{old}{rest}")))
}
/// A definition selection with a mapped name prefix replaced.
fn selection(&self, selection: &str) -> Option<String> {
self.name(selection)
}
/// A file's new path under a path mapping.
fn path(&self, file: &str) -> Option<String> {
self.paths
.iter()
.find_map(|(old, new)| beneath(file, old, '/').map(|rest| format!("{new}{rest}")))
}
/// A current file's old path under a path mapping.
fn path_back(&self, file: &str) -> Option<String> {
self.paths
.iter()
.find_map(|(old, new)| beneath(file, new, '/').map(|rest| format!("{old}{rest}")))
}
/// A node's new address: under a node mapping covering it, or else its
/// file's path mapping.
fn node(&self, file: &str, item: &str) -> Option<(String, String)> {
self.nodes
.iter()
.find_map(|((old_file, old_item), (new_file, new_item))| {
(file == old_file)
.then(|| beneath_node(item, old_item))
.flatten()
.map(|rest| (new_file.clone(), join_node(new_item, rest)))
})
.or_else(|| self.path(file).map(|file| (file, item.to_owned())))
}
/// A current node's old address.
fn node_back(&self, file: &str, item: &str) -> Option<(String, String)> {
self.nodes
.iter()
.find_map(|((old_file, old_item), (new_file, new_item))| {
(file == new_file)
.then(|| beneath_node(item, new_item))
.flatten()
.map(|rest| (old_file.clone(), join_node(old_item, rest)))
})
.or_else(|| self.path_back(file).map(|file| (file, item.to_owned())))
}
/// An anchor value or rationale address, `file` or `file#selector`,
/// under the mappings.
fn address(&self, value: &str) -> Option<String> {
let (file, item) = value.split_once('#').unwrap_or((value, ""));
self.node(file, item)
.map(|(file, item)| address(&file, &item))
}
/// An address mapped back.
fn address_back(&self, value: &str) -> Option<String> {
let (file, item) = value.split_once('#').unwrap_or((value, ""));
self.node_back(file, item)
.map(|(file, item)| address(&file, &item))
}
/// The old sides of path and node mappings, as paths.
fn old_paths(&self) -> Vec<&str> {
self.paths
.iter()
.map(|(old, _)| old.as_str())
.chain(self.nodes.iter().map(|((file, _), _)| file.as_str()))
.collect()
}
/// The {term}`requirements.refactor.mapped_back` form of text: each new
/// node address, definition name, and path replaced by its old side.
fn text_back(&self, text: &str) -> String {
let mut text = text.to_owned();
for ((old_file, old_item), (new_file, new_item)) in &self.nodes {
if !new_item.is_empty() {
text = text.replace(&address(new_file, new_item), &address(old_file, old_item));
}
}
for (old, new) in &self.names {
text = replace_tokens(&text, new, old, Token::Name);
}
for (old, new) in &self.paths {
text = replace_tokens(&text, new, old, Token::Path);
}
text
}
}
Input 7: outline src/refactor.rs static[CODE_SPAN]
- File:
src/refactor.rs - Selector:
static[CODE_SPAN] - Kind:
static src/refactor.rs:261–263
/// A backtick-delimited code span.
static CODE_SPAN: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"`([^`\n]+)`").expect("code span pattern"));
Input 8: outline src/refactor.rs fn[address]
- File:
src/refactor.rs - Selector:
fn[address] - Kind:
fn src/refactor.rs:83–91
/// A node address, as written in an anchor or rationale: `file#selector`, or
/// `file` for the root.
fn address(file: &str, item: &str) -> String {
if item.is_empty() {
file.to_owned()
} else {
format!("{file}#{item}")
}
}