Skip to content

Commit

Permalink
feat(deno-lint): Support list of rules to include in lint and webpack
Browse files Browse the repository at this point in the history
Complement the previous commit adding support for rules.exclude.

While enableAllRules serves as "tags": ["recommended"], there is no
way to specify "include": [] as known from .denolint.json.
  • Loading branch information
prantlf committed Sep 24, 2022
1 parent af09e57 commit 87db9b7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 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, excludeRules)
lint(filepath, source, enableAllRules, excludeRules, includeRules)
```

## webpack-loader
Expand Down Expand Up @@ -93,6 +93,13 @@ Whether to enable all rules. If false, `denolint` will enable all recommend rule

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

#### `includeRules`

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

Rules to include in addition to the recommended ones chosen by `enableAllRules` set to `false`.

#### `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 @@ -8,5 +8,6 @@ export function lint(
sourceCode: string | Buffer,
allRules?: boolean | undefined | null,
excludeRules?: Array<string> | undefined | null,
includeRules?: Array<string> | undefined | null,
): Array<string>
export function denolint(dirname: string, configPath: string): boolean
38 changes: 21 additions & 17 deletions packages/deno-lint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,27 @@ 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()
}
}
pub fn filter_rules(
all: bool,
exclude: Option<Vec<String>>,
include: Option<Vec<String>>,
) -> Vec<Arc<dyn LintRule>> {
if exclude.is_some() || include.is_some() {
let tags = if all {
vec![]
} else {
vec!["recommended".to_string()]
};
return get_filtered_rules(
Some(tags),
Some(exclude.unwrap_or(vec![]).clone()),
Some(include.unwrap_or(vec![]).clone()),
);
}
if all {
get_all_rules()
} else {
get_recommended_rules()
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/deno-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ fn lint(
source_code: Either<String, Buffer>,
all_rules: Option<bool>,
exclude_rules: Option<Vec<String>>,
include_rules: Option<Vec<String>>,
) -> Result<Vec<String>> {
let linter = LinterBuilder::default()
.rules(config::filter_rules(
all_rules.unwrap_or(false),
exclude_rules,
include_rules,
))
.media_type(get_media_type(Path::new(file_name.as_str())))
.ignore_diagnostic_directive("eslint-disable-next-line")
Expand Down
8 changes: 7 additions & 1 deletion packages/deno-lint/webpack-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ 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, options.excludeRules)
const diagnostics = lint(
this.resourcePath,
source,
options.enableAllRules,
options.excludeRules,
options.includeRules,
)

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

0 comments on commit 87db9b7

Please sign in to comment.