Skip to content

Commit

Permalink
feat(linter): Add support for ignores property within config file
Browse files Browse the repository at this point in the history
  • Loading branch information
nrayburn-tech committed Nov 2, 2024
1 parent 79bf74a commit c4e2d96
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 24 deletions.
3 changes: 2 additions & 1 deletion apps/oxlint/fixtures/print_config/ban_rules/expect.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
"env": {
"builtin": true
},
"globals": {}
"globals": {},
"ignores": []
}
3 changes: 2 additions & 1 deletion apps/oxlint/fixtures/print_config/normal/expect.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"env": {
"builtin": true
},
"globals": {}
"globals": {},
"ignores": []
}
19 changes: 12 additions & 7 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ impl Runner for LintRunner {
.copied()
.collect::<Vec<&'static str>>();

let paths =
Walk::new(&paths, &ignore_options).with_extensions(Extensions(extensions)).paths();

let number_of_files = paths.len();

let cwd = std::env::current_dir().unwrap();

let mut oxlintrc = if let Some(config_path) = basic_options.config.as_ref() {
match Oxlintrc::from_file(config_path) {
Ok(config) => config,
Expand All @@ -115,6 +108,18 @@ impl Runner for LintRunner {
Oxlintrc::default()
};

let cwd = std::env::current_dir().unwrap();

let paths = Walk::new(
&paths,
&ignore_options,
&oxlintrc.ignores.iter().map(|value| cwd.join(value)).collect::<Vec<_>>(),
)
.with_extensions(Extensions(extensions))
.paths();

let number_of_files = paths.len();

enable_plugins.apply_overrides(&mut oxlintrc.plugins);

let oxlintrc_for_print =
Expand Down
17 changes: 15 additions & 2 deletions apps/oxlint/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ impl ignore::ParallelVisitor for WalkCollector {
impl Walk {
/// Will not canonicalize paths.
/// # Panics
pub fn new(paths: &[PathBuf], options: &IgnoreOptions) -> Self {
pub fn new(
paths: &[PathBuf],
options: &IgnoreOptions,
config_ignore_pattern: &[PathBuf],
) -> Self {
assert!(!paths.is_empty(), "At least one path must be provided to Walk::new");

let mut inner = ignore::WalkBuilder::new(
Expand Down Expand Up @@ -100,6 +104,15 @@ impl Walk {
let overrides = override_builder.build().unwrap();
inner.overrides(overrides);
}

if !config_ignore_pattern.is_empty() {
let mut override_builder = OverrideBuilder::new(Path::new("/"));
for pattern in config_ignore_pattern {
override_builder.add(pattern.to_str().unwrap()).unwrap();
}
let overrides = override_builder.build().unwrap();
inner.overrides(overrides);
}
}
// Turning off `follow_links` because:
// * following symlinks is a really slow syscall
Expand Down Expand Up @@ -155,7 +168,7 @@ mod test {
symlinks: false,
};

let mut paths = Walk::new(&fixtures, &ignore_options)
let mut paths = Walk::new(&fixtures, &ignore_options, &[])
.with_extensions(Extensions(["js", "vue"].to_vec()))
.paths()
.into_iter()
Expand Down
27 changes: 16 additions & 11 deletions crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod linter;

use std::{fmt::Debug, path::PathBuf, str::FromStr};
use std::{env, fmt::Debug, path::PathBuf, str::FromStr};

use dashmap::DashMap;
use futures::future::join_all;
use globset::Glob;
use ignore::gitignore::Gitignore;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use log::{debug, error, info};
use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -353,16 +353,21 @@ impl Backend {
}
if let Some(config_path) = config_path {
let mut linter = self.server_linter.write().await;
let oxlintrc = Oxlintrc::from_file(&config_path)
.expect("should have initialized linter with new options");
let mut gitignore_builder = GitignoreBuilder::new(config_path.parent().unwrap());
for path in &oxlintrc.ignores {
gitignore_builder
.add_line(None, path)
.expect("Invalid glob in config \"ignores\" property");
}
self.gitignore_glob.lock().await.push(gitignore_builder.build().unwrap());
*linter = ServerLinter::new_with_linter(
LinterBuilder::from_oxlintrc(
true,
Oxlintrc::from_file(&config_path)
.expect("should have initialized linter with new options"),
)
// FIXME: Handle this error more gracefully and report it properly
.expect("failed to build linter from oxlint config")
.with_fix(FixKind::SafeFix)
.build(),
LinterBuilder::from_oxlintrc(true, oxlintrc)
// FIXME: Handle this error more gracefully and report it properly
.expect("failed to build linter from oxlint config")
.with_fix(FixKind::SafeFix)
.build(),
);
}
}
Expand Down
11 changes: 9 additions & 2 deletions crates/oxc_linter/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ impl LinterBuilder {
oxlintrc: Oxlintrc,
) -> Result<Self, LinterBuilderError> {
// TODO: monorepo config merging, plugin-based extends, etc.
let Oxlintrc { plugins, settings, env, globals, categories, rules: mut oxlintrc_rules } =
oxlintrc;
let Oxlintrc {
plugins,
settings,
env,
globals,
categories,
rules: mut oxlintrc_rules,
ignores: _,
} = oxlintrc;

let config = LintConfig { plugins, settings, env, globals };
let options = LintOptions::default();
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/config/oxlintrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub struct Oxlintrc {
pub env: OxlintEnv,
/// Enabled or disabled specific global variables.
pub globals: OxlintGlobals,
/// Globs to ignore during linting.
pub ignores: Vec<String>,
}

impl Oxlintrc {
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_linter/src/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ expression: json
}
]
},
"ignores": {
"description": "Globs to ignore during linting.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"plugins": {
"default": [
"react",
Expand Down
8 changes: 8 additions & 0 deletions npm/oxlint/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
}
]
},
"ignores": {
"description": "Globs to ignore during linting.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"plugins": {
"default": [
"react",
Expand Down
9 changes: 9 additions & 0 deletions tasks/website/src/linter/snapshots/schema_markdown.snap
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ Globals can be disabled by setting their value to `"off"`. For example, in an en
You may also use `"readable"` or `false` to represent `"readonly"`, and `"writeable"` or `true` to represent `"writable"`.


## ignores

type: `string[]`

default: `[]`

Globs to ignore during linting.


## plugins

type: `string[]`
Expand Down

0 comments on commit c4e2d96

Please sign in to comment.