Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support list of rules to exclude and include in lint and webpack #643

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 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, includeRules)
```

## webpack-loader
Expand Down Expand Up @@ -86,6 +86,20 @@ 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`.

#### `includeRules`

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

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

#### `failOnError`

- Type: `Boolean`
Expand Down
2 changes: 2 additions & 0 deletions packages/deno-lint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export function lint(
fileName: string,
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
26 changes: 25 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,30 @@ 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>>,
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_default()),
Some(include.unwrap_or_default()),
);
}
if all {
get_all_rules()
} else {
get_recommended_rules()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 8 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,15 @@ fn lint(
file_name: String,
source_code: Either<String, Buffer>,
all_rules: Option<bool>,
exclude_rules: Option<Vec<String>>,
include_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,
include_rules,
))
.media_type(get_media_type(Path::new(file_name.as_str())))
.ignore_diagnostic_directive("eslint-disable-next-line")
.build();
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)
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