Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid code comment detection in PEP 723 script tags #10464

Merged
merged 1 commit into from Mar 18, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/eradicate/ERA001.py
Expand Up @@ -36,3 +36,32 @@ class A():
# except:
# except Foo:
# except Exception as e: print(e)


# Script tag without an opening tag (Error)

# requires-python = ">=3.11"
# dependencies = [
# "requests<3",
# "rich",
# ]
# ///

# Script tag (OK)

# /// script
# requires-python = ">=3.11"
# dependencies = [
# "requests<3",
# "rich",
# ]
# ///

# Script tag without a closing tag (OK)

# /// script
# requires-python = ">=3.11"
# dependencies = [
# "requests<3",
# "rich",
# ]
63 changes: 49 additions & 14 deletions crates/ruff_linter/src/rules/eradicate/rules/commented_out_code.rs
Expand Up @@ -43,30 +43,38 @@ impl Violation for CommentedOutCode {
}
}

fn is_standalone_comment(line: &str) -> bool {
for char in line.chars() {
if char == '#' {
return true;
}
if !char.is_whitespace() {
return false;
}
}
unreachable!("Comment should contain '#' character")
}

/// ERA001
pub(crate) fn commented_out_code(
diagnostics: &mut Vec<Diagnostic>,
locator: &Locator,
indexer: &Indexer,
settings: &LinterSettings,
) {
// Skip comments within `/// script` tags.
let mut in_script_tag = false;

// Iterate over all comments in the document.
for range in indexer.comment_ranges() {
let line = locator.full_lines(*range);
let line = locator.lines(*range);

// Detect `/// script` tags.
if in_script_tag {
if is_script_tag_end(line) {
in_script_tag = false;
}
} else {
if is_script_tag_start(line) {
in_script_tag = true;
}
}

// Skip comments within `/// script` tags.
if in_script_tag {
continue;
}

// Verify that the comment is on its own line, and that it contains code.
if is_standalone_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
if is_own_line_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
let mut diagnostic = Diagnostic::new(CommentedOutCode, *range);
diagnostic.set_fix(Fix::display_only_edit(Edit::range_deletion(
locator.full_lines_range(*range),
Expand All @@ -75,3 +83,30 @@ pub(crate) fn commented_out_code(
}
}
}

/// Returns `true` if line contains an own-line comment.
fn is_own_line_comment(line: &str) -> bool {
for char in line.chars() {
if char == '#' {
return true;
}
if !char.is_whitespace() {
return false;
}
}
unreachable!("Comment should contain '#' character")
}

/// Returns `true` if the line appears to start a script tag.
///
/// See: <https://peps.python.org/pep-0723/>
fn is_script_tag_start(line: &str) -> bool {
line == "# /// script"
}

/// Returns `true` if the line appears to start a script tag.
///
/// See: <https://peps.python.org/pep-0723/>
fn is_script_tag_end(line: &str) -> bool {
line == "# ///"
}
Expand Up @@ -245,6 +245,7 @@ ERA001.py:36:1: ERA001 Found commented-out code
36 |-# except:
37 36 | # except Foo:
38 37 | # except Exception as e: print(e)
39 38 |

ERA001.py:37:1: ERA001 Found commented-out code
|
Expand All @@ -262,6 +263,8 @@ ERA001.py:37:1: ERA001 Found commented-out code
36 36 | # except:
37 |-# except Foo:
38 37 | # except Exception as e: print(e)
39 38 |
40 39 |

ERA001.py:38:1: ERA001 Found commented-out code
|
Expand All @@ -277,3 +280,44 @@ ERA001.py:38:1: ERA001 Found commented-out code
36 36 | # except:
37 37 | # except Foo:
38 |-# except Exception as e: print(e)
39 38 |
40 39 |
41 40 | # Script tag without an opening tag (Error)

ERA001.py:44:1: ERA001 Found commented-out code
|
43 | # requires-python = ">=3.11"
44 | # dependencies = [
| ^^^^^^^^^^^^^^^^^^ ERA001
45 | # "requests<3",
46 | # "rich",
|
= help: Remove commented-out code

ℹ Display-only fix
41 41 | # Script tag without an opening tag (Error)
42 42 |
43 43 | # requires-python = ">=3.11"
44 |-# dependencies = [
45 44 | # "requests<3",
46 45 | # "rich",
47 46 | # ]

ERA001.py:47:1: ERA001 Found commented-out code
|
45 | # "requests<3",
46 | # "rich",
47 | # ]
| ^^^ ERA001
48 | # ///
|
= help: Remove commented-out code

ℹ Display-only fix
44 44 | # dependencies = [
45 45 | # "requests<3",
46 46 | # "rich",
47 |-# ]
48 47 | # ///
49 48 |
50 49 | # Script tag (OK)