mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
Automatically linkify option references in rule documentation
Previously the rule documentation referenced configuration options via full https:// URLs, which was bad for several reasons: * changing the website would mean you'd have to change all URLs * the links didn't work when building mkdocs locally * the URLs showed up in the `ruff rule` output * broken references weren't detected by our CI This commit solves all of these problems by post-processing the Markdown, recognizing sections such as: ## Options * `flake8-tidy-imports.ban-relative-imports` `cargo dev generate-all` will automatically linkify such references and panic if the referenced option doesn't exist. Note that the option can also be linked in the other Markdown sections via e.g. [`flake8-tidy-imports.ban-relative-imports`] since the post-processing code generates a CommonMark link definition. Resolves #2766.
This commit is contained in:
parent
fc4c927788
commit
28c9263722
13 changed files with 122 additions and 41 deletions
|
@ -24,10 +24,9 @@ define_violation!(
|
|||
/// `__init__.py` file is typically meant to be a regular package, and
|
||||
/// the absence of the `__init__.py` file is probably an oversight.
|
||||
///
|
||||
/// Note that namespace packages can be specified via the
|
||||
/// [`namespace-packages`](https://github.com/charliermarsh/ruff#namespace-packages)
|
||||
/// configuration option. Adding a namespace package to the configuration
|
||||
/// will suppress this violation for a given package.
|
||||
/// ## Options
|
||||
///
|
||||
/// * `namespace-packages`
|
||||
pub struct ImplicitNamespacePackage {
|
||||
pub filename: String,
|
||||
}
|
||||
|
|
|
@ -14,13 +14,16 @@ use crate::violation::AlwaysAutofixableViolation;
|
|||
define_violation!(
|
||||
/// ## What it does
|
||||
/// Checks for inline strings that use single quotes or double quotes,
|
||||
/// depending on the value of the [`inline-quotes`](https://github.com/charliermarsh/ruff#inline-quotes)
|
||||
/// setting.
|
||||
/// depending on the value of the [`flake8-quotes.inline-quotes`] option.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Consistency is good. Use either single or double quotes for inline
|
||||
/// strings, but be consistent.
|
||||
///
|
||||
/// ## Options
|
||||
///
|
||||
/// * `flake8-quotes.inline-quotes`
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// foo = 'bar'
|
||||
|
@ -56,13 +59,17 @@ impl AlwaysAutofixableViolation for BadQuotesInlineString {
|
|||
define_violation!(
|
||||
/// ## What it does
|
||||
/// Checks for multiline strings that use single quotes or double quotes,
|
||||
/// depending on the value of the [`multiline-quotes`](https://github.com/charliermarsh/ruff#multiline-quotes)
|
||||
/// depending on the value of the [`flake8-quotes.multiline-quotes`]
|
||||
/// setting.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Consistency is good. Use either single or double quotes for multiline
|
||||
/// strings, but be consistent.
|
||||
///
|
||||
/// ## Options
|
||||
///
|
||||
/// * `flake8-quotes.multiline-quotes`
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// foo = '''
|
||||
|
@ -101,13 +108,17 @@ impl AlwaysAutofixableViolation for BadQuotesMultilineString {
|
|||
|
||||
define_violation!(
|
||||
/// ## What it does
|
||||
/// Checks for docstrings that use single quotes or double quotes, depending on the value of the [`docstring-quotes`](https://github.com/charliermarsh/ruff#docstring-quotes)
|
||||
/// setting.
|
||||
/// Checks for docstrings that use single quotes or double quotes, depending
|
||||
/// on the value of the [`flake8-quotes.docstring-quotes`] setting.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// Consistency is good. Use either single or double quotes for docstring
|
||||
/// strings, but be consistent.
|
||||
///
|
||||
/// ## Options
|
||||
///
|
||||
/// * `flake8-quotes.docstring-quotes`
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// '''
|
||||
|
|
|
@ -48,11 +48,9 @@ define_violation!(
|
|||
/// > from .sibling import example
|
||||
/// > ```
|
||||
///
|
||||
/// Note that degree of strictness packages can be specified via the
|
||||
/// [`ban-relative-imports`](https://github.com/charliermarsh/ruff#ban-relative-imports)
|
||||
/// configuration option, which allows banning all relative imports
|
||||
/// (`ban-relative-imports = "all"`) or only those that extend into the parent module or beyond
|
||||
/// (`ban-relative-imports = "parents"`, the default).
|
||||
/// ## Options
|
||||
///
|
||||
/// * `flake8-tidy-imports.ban-relative-imports`
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
|
|
|
@ -18,6 +18,10 @@ define_violation!(
|
|||
/// ## Why is this bad?
|
||||
/// Functions with a high complexity are hard to understand and maintain.
|
||||
///
|
||||
/// ## Options
|
||||
///
|
||||
/// * `mccabe.max-complexity`
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// def foo(a, b, c):
|
||||
|
|
|
@ -23,9 +23,13 @@ define_violation!(
|
|||
/// A variable that is defined but not used is likely a mistake, and should be
|
||||
/// removed to avoid confusion.
|
||||
///
|
||||
/// If a variable is intentionally defined-but-not-used, it should be prefixed
|
||||
/// with an underscore, or some other value that adheres to the
|
||||
/// [`dummy-variable-rgx`](https://github.com/charliermarsh/ruff#dummy-variable-rgx) pattern.
|
||||
/// If a variable is intentionally defined-but-not-used, it should be
|
||||
/// prefixed with an underscore, or some other value that adheres to the
|
||||
/// [`dummy-variable-rgx`] pattern.
|
||||
///
|
||||
/// ## Options
|
||||
///
|
||||
/// * `dummy-variable-rgx`
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
|
|
|
@ -5,6 +5,8 @@ use std::fs;
|
|||
|
||||
use anyhow::Result;
|
||||
use ruff::registry::{Linter, Rule, RuleNamespace};
|
||||
use ruff::settings::options::Options;
|
||||
use ruff::settings::options_base::ConfigurationOptions;
|
||||
use ruff::AutofixAvailability;
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
|
@ -37,7 +39,7 @@ pub fn main(args: &Args) -> Result<()> {
|
|||
output.push('\n');
|
||||
}
|
||||
|
||||
output.push_str(explanation.trim());
|
||||
process_documentation(explanation.trim(), &mut output);
|
||||
|
||||
if args.dry_run {
|
||||
println!("{output}");
|
||||
|
@ -49,3 +51,35 @@ pub fn main(args: &Args) -> Result<()> {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_documentation(documentation: &str, out: &mut String) {
|
||||
let mut in_options = false;
|
||||
let mut after = String::new();
|
||||
|
||||
for line in documentation.split_inclusive('\n') {
|
||||
if line.starts_with("## ") {
|
||||
in_options = line == "## Options\n";
|
||||
} else if in_options {
|
||||
if let Some(rest) = line.strip_prefix("* `") {
|
||||
let option = rest.trim_end().trim_end_matches('`');
|
||||
|
||||
assert!(
|
||||
Options::get(Some(option)).is_some(),
|
||||
"unknown option {option}"
|
||||
);
|
||||
|
||||
let anchor = option.rsplit('.').next().unwrap();
|
||||
out.push_str(&format!("* [`{option}`]\n"));
|
||||
after.push_str(&format!("[`{option}`]: ../../settings#{anchor}"));
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str(line);
|
||||
}
|
||||
if !after.is_empty() {
|
||||
out.push_str("\n\n");
|
||||
out.push_str(&after);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue