Skip to content

Commit

Permalink
feat(deno-lint): Support config content in lint and webpack (#644)
Browse files Browse the repository at this point in the history
While enableAllRules serves as "tags": ["recommended"], there is no
way to specify the whole config as known from .denolint.json.
Passing RulesConfig would be better, but it would need to make it
serialisable.

Co-authored-by: LongYinan <lynweklm@gmail.com>
  • Loading branch information
prantlf and Brooooooklyn committed Jan 3, 2023
1 parent a785bec commit 82091a5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
16 changes: 15 additions & 1 deletion packages/deno-lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ Lint benchmark bench suite: Fastest is @node-rs/deno-lint

## Usage

Pass a boolean `enableAllRules` to use the recommended (`true`, default) or all rules (`false`):

```ts
import { lint } from '@node-rs/deno-lint'

lint(filepath, source, enableAllRules)
```

Pass the config file content (as string), to be able to specify what rules should be included or excluded:

```ts
import { readFile } from 'fs/promises'
import { lint } from '@node-rs/deno-lint'

const config = await readFile('.denolint.json', 'utf8')
lint(filepath, source, config)
```

## webpack-loader

```js
Expand All @@ -81,11 +93,13 @@ You can pass denolint options using standard webpack loader options.

#### `enableAllRules`

- Type: `Boolean`
- Type: `Boolean | String`
- Default: `false`

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

Instead of the boolean, a string with the content of `.denolint.json` can be passed as a value.

#### `failOnError`

- Type: `Boolean`
Expand Down
2 changes: 1 addition & 1 deletion packages/deno-lint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
export function lint(
fileName: string,
sourceCode: string | Buffer,
allRules?: boolean | undefined | null,
allRules?: boolean | string | undefined | null
): Array<string>
export function denolint(dirname: string, configPath: string): boolean
5 changes: 5 additions & 0 deletions packages/deno-lint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub fn load_from_json(config_path: &Path) -> Result<Config, std::io::Error> {
Ok(config)
}

pub fn load_from_string(json_str: String) -> Result<Config, std::io::Error> {
let config: Config = serde_json::from_str(&json_str)?;
Ok(config)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
22 changes: 16 additions & 6 deletions packages/deno-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ fn get_media_type(p: &Path) -> MediaType {
fn lint(
file_name: String,
source_code: Either<String, Buffer>,
all_rules: Option<bool>,
all_rules: Option<Either<bool, 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(match all_rules {
Some(r) => match r {
Either::A(a) => {
if a {
get_all_rules()
} else {
get_recommended_rules()
}
}
Either::B(b) => {
let cfg = config::load_from_string(b)?;
cfg.get_rules()
}
},
None => get_recommended_rules(),
})
.media_type(get_media_type(Path::new(file_name.as_str())))
.ignore_diagnostic_directive("eslint-disable-next-line")
Expand Down

0 comments on commit 82091a5

Please sign in to comment.