|
1 | 1 | use semver_parser::range::parse as parse_request;
|
2 | 2 | use semver_parser::version::parse as parse_version;
|
3 | 3 | use semver_parser::version::Version;
|
4 |
| -use syn; |
| 4 | +use syn::spanned::Spanned; |
5 | 5 | use url::Url;
|
6 | 6 |
|
7 | 7 | 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
|
114 | 114 | _ => continue,
|
115 | 115 | };
|
116 | 116 |
|
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); |
129 | 127 | match check_result {
|
130 | 128 | Ok(()) => {
|
131 |
| - println!("{} (line {}) ... ok", path, line_no + 1); |
| 129 | + println!("{} (line {}) ... ok", path, first_line); |
132 | 130 | return Ok(());
|
133 | 131 | }
|
134 | 132 | 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 | + } |
137 | 137 | return Err(format!("html_root_url errors in {}", path));
|
138 | 138 | }
|
139 | 139 | }
|
|
0 commit comments