Skip to content

Commit

Permalink
fix: allow blank lines between disable comment and disabled line
Browse files Browse the repository at this point in the history
Prettier will force insertion of blank liines between some nodes, so skip the blank line if it exists.
  • Loading branch information
charislam committed Nov 14, 2024
1 parent dd7482f commit d4ca8ca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap = { version = "4.5.20", features = ["derive"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
exitcode = "1.1.2"
glob = "0.3.1"
itertools = "0.13.0"
log = "0.4.22"
markdown = "1.0.0-alpha.21"
once_cell = "1.20.2"
Expand Down
55 changes: 49 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Result};
use itertools::Itertools;
use log::{debug, warn};
use markdown::{mdast::Node, to_mdast, Constructs, ParseOptions};
use regex::Regex;
Expand Down Expand Up @@ -288,11 +289,13 @@ impl LintDisables {
/// corresponding disables.
fn collect_lint_disables(
ast: &Node,
next_node: Option<&Node>,
) -> Result<HashMap<String, Vec<LintDisable>>, DisableParseError> {
let mut disables = HashMap::<String, Vec<LintDisable>>::new();

fn collect_lint_disables_internal(
ast: &Node,
next_node: Option<&Node>,
disables: &mut HashMap<String, Vec<LintDisable>>,
#[allow(non_snake_case)] ALL_MARKER: &str,
) -> std::result::Result<(), DisableParseError> {
Expand Down Expand Up @@ -369,7 +372,17 @@ impl LintDisables {
return Err(DisableParseError::new("Could not disable all rules because underlying node is missing line number."));
};
let end_line = if next_line_only {
Some(start_line.unwrap() + 2)
match next_node.and_then(|node| node.position()) {
Some(position) => {
let next_node_start = position.start.line;
if next_node_start > start_line.unwrap() {
Some(next_node_start + 1)
} else {
Some(start_line.unwrap() + 2)
}
}
None => Some(start_line.unwrap() + 2),
}
} else {
None
};
Expand Down Expand Up @@ -401,7 +414,17 @@ impl LintDisables {
return Err(DisableParseError::new(format!("Could not disable rule {} because underlying node is missing line number.", rule_name)));
};
let end_line = if next_line_only {
Some(start_line.unwrap() + 2)
match next_node.and_then(|node| node.position()) {
Some(position) => {
let next_node_start = position.start.line;
if next_node_start > start_line.unwrap() {
Some(next_node_start + 1)
} else {
Some(start_line.unwrap() + 2)
}
}
None => Some(start_line.unwrap() + 2),
}
} else {
None
};
Expand All @@ -421,16 +444,18 @@ impl LintDisables {
}
_ => {
if let Some(children) = ast.children() {
for child in children {
collect_lint_disables_internal(child, disables, ALL_MARKER)?;
for node_pair in children.iter().zip_longest(children.iter().skip(1)) {
let child = node_pair.clone().left().unwrap();
let next = node_pair.right();
collect_lint_disables_internal(child, next, disables, ALL_MARKER)?;
}
}

Ok(())
}
}
}
collect_lint_disables_internal(ast, &mut disables, LintDisables::all_marker())?;
collect_lint_disables_internal(ast, next_node, &mut disables, LintDisables::all_marker())?;

for (_, value) in disables.iter_mut() {
value.sort_by_key(|k| k.line_range.start);
Expand Down Expand Up @@ -481,7 +506,7 @@ impl TryFrom<&Node> for LintDisables {
type Error = DisableParseError;

fn try_from(node: &Node) -> Result<Self, DisableParseError> {
let disables = LintDisables::collect_lint_disables(node)?;
let disables = LintDisables::collect_lint_disables(node, None)?;
Ok(Self(disables))
}
}
Expand Down Expand Up @@ -745,4 +770,22 @@ This should error because there was no disable"#;
let parse_result = parse(input).unwrap();
assert!(TryInto::<LintDisables>::try_into(&parse_result.ast).is_err());
}

#[test]
fn test_collect_lint_disables_skip_blank_lines() {
let input = r#"{/* supa-mdx-lint-disable-next-line foo */}
This line is ignored
This line is not ignored"#;

let parse_result = parse(input).unwrap();
let disables: LintDisables = (&parse_result.ast).try_into().unwrap();
debug!("Disables: {:?}", disables);

assert_eq!(disables.0.len(), 1);
assert_eq!(
disables.0["foo"][0].line_range.end,
Some(NonZeroUsize::new(4).unwrap())
);
}
}

0 comments on commit d4ca8ca

Please sign in to comment.