Skip to content

Commit 82091a5

Browse files
feat(deno-lint): Support config content in lint and webpack (#644)
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 <[email protected]>
1 parent a785bec commit 82091a5

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

packages/deno-lint/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,24 @@ Lint benchmark bench suite: Fastest is @node-rs/deno-lint
5050

5151
## Usage
5252

53+
Pass a boolean `enableAllRules` to use the recommended (`true`, default) or all rules (`false`):
54+
5355
```ts
5456
import { lint } from '@node-rs/deno-lint'
5557

5658
lint(filepath, source, enableAllRules)
5759
```
5860

61+
Pass the config file content (as string), to be able to specify what rules should be included or excluded:
62+
63+
```ts
64+
import { readFile } from 'fs/promises'
65+
import { lint } from '@node-rs/deno-lint'
66+
67+
const config = await readFile('.denolint.json', 'utf8')
68+
lint(filepath, source, config)
69+
```
70+
5971
## webpack-loader
6072

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

8294
#### `enableAllRules`
8395

84-
- Type: `Boolean`
96+
- Type: `Boolean | String`
8597
- Default: `false`
8698

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

101+
Instead of the boolean, a string with the content of `.denolint.json` can be passed as a value.
102+
89103
#### `failOnError`
90104

91105
- Type: `Boolean`

packages/deno-lint/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
export function lint(
77
fileName: string,
88
sourceCode: string | Buffer,
9-
allRules?: boolean | undefined | null,
9+
allRules?: boolean | string | undefined | null
1010
): Array<string>
1111
export function denolint(dirname: string, configPath: string): boolean

packages/deno-lint/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ pub fn load_from_json(config_path: &Path) -> Result<Config, std::io::Error> {
4242
Ok(config)
4343
}
4444

45+
pub fn load_from_string(json_str: String) -> Result<Config, std::io::Error> {
46+
let config: Config = serde_json::from_str(&json_str)?;
47+
Ok(config)
48+
}
49+
4550
#[cfg(test)]
4651
mod tests {
4752
use super::*;

packages/deno-lint/src/lib.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@ fn get_media_type(p: &Path) -> MediaType {
3737
fn lint(
3838
file_name: String,
3939
source_code: Either<String, Buffer>,
40-
all_rules: Option<bool>,
40+
all_rules: Option<Either<bool, String>>,
4141
) -> Result<Vec<String>> {
42-
let all_rules = all_rules.unwrap_or(false);
4342
let linter = LinterBuilder::default()
44-
.rules(if all_rules {
45-
get_all_rules()
46-
} else {
47-
get_recommended_rules()
43+
.rules(match all_rules {
44+
Some(r) => match r {
45+
Either::A(a) => {
46+
if a {
47+
get_all_rules()
48+
} else {
49+
get_recommended_rules()
50+
}
51+
}
52+
Either::B(b) => {
53+
let cfg = config::load_from_string(b)?;
54+
cfg.get_rules()
55+
}
56+
},
57+
None => get_recommended_rules(),
4858
})
4959
.media_type(get_media_type(Path::new(file_name.as_str())))
5060
.ignore_diagnostic_directive("eslint-disable-next-line")

0 commit comments

Comments
 (0)