Skip to content

Commit d1b5443

Browse files
authored
Add extension mapping to configuration file options (#23384)
New `extension` configuration option takes a dictionary mapping custom file extensions (keys) to languages by name (values). Eg, ```toml [tool.ruff] extension = {qmd="markdown"} ``` Issue #23204
1 parent 222574a commit d1b5443

4 files changed

Lines changed: 71 additions & 5 deletions

File tree

crates/ruff/tests/integration_test.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,42 @@ fn stdin_override_parser_py() {
629629
");
630630
}
631631

632+
#[test]
633+
fn stdin_override_parser_py_config() -> Result<()> {
634+
let tempdir = TempDir::new()?;
635+
let pyproject_toml = tempdir.path().join("pyproject.toml");
636+
fs::write(
637+
&pyproject_toml,
638+
r#"
639+
[tool.ruff]
640+
extension = {ipynb="python"}
641+
"#,
642+
)?;
643+
let mut cmd = RuffCheck::default()
644+
.config(&pyproject_toml)
645+
.args(["--stdin-filename", "F401.ipynb"])
646+
.build();
647+
assert_cmd_snapshot!(cmd
648+
.pass_stdin("import os\n"), @"
649+
success: false
650+
exit_code: 1
651+
----- stdout -----
652+
F401 [*] `os` imported but unused
653+
--> F401.ipynb:1:8
654+
|
655+
1 | import os
656+
| ^^
657+
|
658+
help: Remove unused import: `os`
659+
660+
Found 1 error.
661+
[*] 1 fixable with the `--fix` option.
662+
663+
----- stderr -----
664+
");
665+
Ok(())
666+
}
667+
632668
#[test]
633669
fn stdin_fix_when_not_fixable_should_still_print_contents() {
634670
let mut cmd = RuffCheck::default().args(["--fix"]).build();

crates/ruff_workspace/src/configuration.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,7 @@ impl Configuration {
565565
})
566566
.collect()
567567
}),
568-
// `--extension` is a hidden command-line argument that isn't supported in configuration
569-
// files at present.
570-
extension: None,
571-
568+
extension: options.extension.map(ExtensionMapping::from),
572569
lint: LintConfiguration::from_options(lint, project_root)?,
573570
format: FormatConfiguration::from_options(
574571
options.format.unwrap_or_default(),

crates/ruff_workspace/src/options.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use ruff_linter::rules::{
3232
pycodestyle, pydoclint, pydocstyle, pyflakes, pylint, pyupgrade, ruff,
3333
};
3434
use ruff_linter::settings::types::{
35-
IdentifierPattern, OutputFormat, PythonVersion, RequiredVersion,
35+
IdentifierPattern, Language, OutputFormat, PythonVersion, RequiredVersion,
3636
};
3737
use ruff_linter::{RuleSelector, warn_user_once};
3838
use ruff_macros::{CombineOptions, OptionsMetadata};
@@ -282,6 +282,20 @@ pub struct Options {
282282
)]
283283
pub respect_gitignore: Option<bool>,
284284

285+
/// A mapping of custom file extensions to known file types (overridden
286+
/// by the `--extension` command-line flag).
287+
///
288+
/// Supported file types include `python`, `pyi`, `ipynb`, and `markdown`.
289+
#[option(
290+
default = "{}",
291+
value_type = "dict[str, Language]",
292+
example = r#"
293+
# Add a custom file extension mapped to Python
294+
extension = {rpy="python"}
295+
"#
296+
)]
297+
pub extension: Option<FxHashMap<String, Language>>,
298+
285299
// Generic python options
286300
/// A list of builtins to treat as defined references, in addition to the
287301
/// system builtins.

ruff.schema.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)