Skip to content

Commit 5c01401

Browse files
authored
Merge pull request #61 from mgeisler/span-locations
check_html_root_url: report accurate line numbers
2 parents e2b1c04 + 6c260be commit 5c01401

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ codecov = { repository = "mgeisler/version-sync" }
2424
pulldown-cmark = { version = "0.4", default-features = false }
2525
semver-parser = "0.9"
2626
syn = { version = "0.15", features = ["full"] }
27+
# Enable the span-locations feature in proc-macro2, the exact version
28+
# is determined by the syn crate.
29+
proc-macro2 = { version = "*", features = ["span-locations"] }
2730
toml = "0.5"
2831
url = "1.5.1"
2932
itertools = "0.8"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ This is a changelog describing the most important changes per release.
114114
### Unreleased ###
115115

116116
We now use Rust 2018, which means we reqire Rust version 1.31.0 or
117-
later.
117+
later. The `assert_html_root_url_updated!` macro will again report
118+
accurate line numbers based on span information from the `syn` crate.
118119

119120
### Version 0.7.0 — January 14th, 2019
120121

src/html_root_url.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use semver_parser::range::parse as parse_request;
22
use semver_parser::version::parse as parse_version;
33
use semver_parser::version::Version;
4-
use syn;
4+
use syn::spanned::Spanned;
55
use url::Url;
66

77
use crate::helpers::{indent, read_file, version_matches_request, Result};
@@ -114,26 +114,26 @@ pub fn check_html_root_url(path: &str, pkg_name: &str, pkg_version: &str) -> Res
114114
_ => continue,
115115
};
116116

117-
// FIXME: use line number from the syn crate when it
118-
// preserves span information. Here we simply find the
119-
// first source line that contains "html_root_url".
120-
//
121-
// We know such a line must exist since we would have
122-
// continue the loop above if it wasn't present.
123-
let (line_no, source_line) = code
124-
.lines()
125-
.enumerate()
126-
.find(|&(_, line)| line.contains("html_root_url"))
127-
.expect("html_root_url attribute not present");
128-
117+
// FIXME: the proc-macro2-0.4.27 crate hides accurate span
118+
// information behind a procmacro2_semver_exempt flag: the
119+
// start line is correct, but the end line is always equal
120+
// to the start. Luckily, most html_root_url attributes
121+
// are on a single line, so the code below works okay.
122+
let first_line = attr.span().start().line;
123+
let last_line = attr.span().end().line;
124+
// Getting the source code for a span is tracked upstream:
125+
// https://github.com/alexcrichton/proc-macro2/issues/110.
126+
let source_lines = code.lines().take(last_line).skip(first_line - 1);
129127
match check_result {
130128
Ok(()) => {
131-
println!("{} (line {}) ... ok", path, line_no + 1);
129+
println!("{} (line {}) ... ok", path, first_line);
132130
return Ok(());
133131
}
134132
Err(err) => {
135-
println!("{} (line {}) ... {} in", path, line_no + 1, err);
136-
println!("{}\n", indent(source_line));
133+
println!("{} (line {}) ... {} in", path, first_line, err);
134+
for line in source_lines {
135+
println!("{}", indent(line));
136+
}
137137
return Err(format!("html_root_url errors in {}", path));
138138
}
139139
}

0 commit comments

Comments
 (0)