Skip to content

Commit

Permalink
feat(deno-lint): Support list of rules to exclude in lint and webpack
Browse files Browse the repository at this point in the history
While enableAllRules serves as "tags": ["recommended"], there is no
way to specify "exclude": [] as known from .denolint.json. Adding
excludeRules is not exactly according to the config schema, but it
is the simplest change without breaking the current option schema.
  • Loading branch information
prantlf committed Sep 24, 2022
1 parent d6cf2b6 commit af09e57
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packages/deno-lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Lint benchmark bench suite: Fastest is @node-rs/deno-lint
```ts
import { lint } from '@node-rs/deno-lint'

lint(filepath, source, enableAllRules)
lint(filepath, source, enableAllRules, excludeRules)
```

## webpack-loader
Expand Down Expand Up @@ -86,6 +86,13 @@ You can pass denolint options using standard webpack loader options.

Whether to enable all rules. If false, `denolint` will enable all recommend rules.

#### `excludeRules`

- Type: `String[]`
- Default: `[]`

Rules to exclude from all or recommended ones chosen by `enableAllRules`.

#### `failOnError`

- Type: `Boolean`
Expand Down
1 change: 1 addition & 0 deletions packages/deno-lint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export function lint(
fileName: string,
sourceCode: string | Buffer,
allRules?: boolean | undefined | null,
excludeRules?: Array<string> | undefined | null,
): Array<string>
export function denolint(dirname: string, configPath: string): boolean
22 changes: 21 additions & 1 deletion packages/deno-lint/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2020-2021 the Deno authors. All rights reserved. MIT license.
use deno_lint::rules::{get_filtered_rules, LintRule};
use deno_lint::rules::{get_all_rules, get_filtered_rules, get_recommended_rules, LintRule};
use serde::Deserialize;
use std::path::Path;
use std::sync::Arc;
Expand Down Expand Up @@ -42,6 +42,26 @@ pub fn load_from_json(config_path: &Path) -> Result<Config, std::io::Error> {
Ok(config)
}

pub fn filter_rules(all: bool, exclude: Option<Vec<String>>) -> Vec<Arc<dyn LintRule>> {
match exclude {
Some(exclude) => {
let tags = if all {
vec![]
} else {
vec!["recommended".to_string()]
};
get_filtered_rules(Some(tags), Some(exclude.clone()), Some(vec![]))
}
None => {
if all {
get_all_rules()
} else {
get_recommended_rules()
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
13 changes: 6 additions & 7 deletions packages/deno-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::str;

use deno_ast::MediaType;
use deno_lint::linter::LinterBuilder;
use deno_lint::rules::{get_all_rules, get_recommended_rules};
use deno_lint::rules::get_recommended_rules;
use ignore::types::TypesBuilder;
use ignore::WalkBuilder;
use napi::bindgen_prelude::*;
Expand All @@ -37,14 +37,13 @@ fn lint(
file_name: String,
source_code: Either<String, Buffer>,
all_rules: Option<bool>,
exclude_rules: Option<Vec<String>>,
) -> Result<Vec<String>> {
let all_rules = all_rules.unwrap_or(false);
let linter = LinterBuilder::default()
.rules(if all_rules {
get_all_rules()
} else {
get_recommended_rules()
})
.rules(config::filter_rules(
all_rules.unwrap_or(false),
exclude_rules,
))
.media_type(get_media_type(Path::new(file_name.as_str())))
.ignore_diagnostic_directive("eslint-disable-next-line")
.build();
Expand Down
2 changes: 1 addition & 1 deletion packages/deno-lint/webpack-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { lint } = require('./index')
module.exports = function denoLintLoader(source, sm) {
const callback = this.async()
const options = this.getOptions()
const diagnostics = lint(this.resourcePath, source, options.enableAllRules)
const diagnostics = lint(this.resourcePath, source, options.enableAllRules, options.excludeRules)

if (this.resourcePath.endsWith('diff-size.ts')) {
this.emitWarning(`${this.resourcePath}, ${diagnostics.length}`)
Expand Down

0 comments on commit af09e57

Please sign in to comment.