Skip to content

Commit 59454e2

Browse files
committed
feat(deno-lint): implment webpack loader and cli
1 parent 9e1c24a commit 59454e2

File tree

12 files changed

+267
-20
lines changed

12 files changed

+267
-20
lines changed

packages/deno-lint/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ edition = "2018"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
deno_lint = "0.1"
11+
deno_lint = "0.1.22"
1212
ignore = "0.4"
1313
napi = { version = "0.4" }
1414
napi-derive = { version = "0.4" }
15+
serde = "1"
16+
serde_json = "1"
1517
termcolor = "1.1"
1618

1719
[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]

packages/deno-lint/README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,93 @@
11
# `@node-rs/deno-lint`
22

3+
![](https://github.com/napi-rs/node-rs/workflows/CI/badge.svg)
4+
![](https://img.shields.io/npm/dm/@node-rs/deno-lint.svg?sanitize=true)
5+
36
> deno_lint nodejs binding
47
8+
## Performance
9+
10+
### Hardware info
11+
12+
```
13+
Model Name: MacBook Pro
14+
Model Identifier: MacBookPro15,1
15+
Processor Name: 6-Core Intel Core i9
16+
Processor Speed: 2.9 GHz
17+
Number of Processors: 1
18+
Total Number of Cores: 6
19+
L2 Cache (per Core): 256 KB
20+
L3 Cache: 12 MB
21+
Hyper-Threading Technology: Enabled
22+
Memory: 32 GB
23+
```
24+
25+
### Benchmark
26+
27+
```
28+
@node-rs/deno-lint x 885 ops/sec ±1.26% (92 runs sampled)
29+
eslint x 118 ops/sec ±4.97% (78 runs sampled)
30+
Lint benchmark bench suite: Fastest is @node-rs/deno-lint
31+
```
32+
533
## Usage
634

735
```ts
8-
import { lint } from 'deno-lint'
36+
import { lint } from '@node-rs/deno-lint'
937

38+
lint(filepath, source, enableAllRules)
1039
```
40+
41+
## webpack-loader
42+
43+
```js
44+
// webpack.config.js
45+
46+
module.exports = {
47+
module: {
48+
rules: [
49+
{
50+
enforce: 'pre',
51+
test: /\.(t|j)s?$/,
52+
loader: '@node-rs/deno-lint/webpack-loader',
53+
exclude: [/node_modules/],
54+
},
55+
],
56+
},
57+
}
58+
```
59+
60+
### Options
61+
62+
You can pass denolint options using standard webpack loader options.
63+
64+
#### `enableAllRules`
65+
66+
- Type: `Boolean`
67+
- Default: `false`
68+
69+
Whether to enable all rules. If false, `denolint` will enable all recommend rules.
70+
71+
#### `failOnError`
72+
73+
- Type: `Boolean`
74+
- Default: `false`
75+
76+
Will cause the module build to fail if there are any errors, if option is set to `true`.
77+
78+
#### `quiet`
79+
80+
- Type: `Boolean`
81+
- Default: `false`
82+
83+
Emit nothing even if there were errors happened.
84+
85+
## `denolint` cli
86+
87+
### usage
88+
89+
`npx denolint`
90+
91+
### `--all`, `-a`
92+
93+
Enable all rules flag, if not present, denolint will run with recommend rules.

packages/deno-lint/bin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
const { binding } = require('@node-rs/deno-lint')
44

5-
const hasError = binding.denolint(__dirname)
5+
const enableAllRules = process.argv.includes('--all') || process.argv.includes('-a')
6+
7+
const hasError = binding.denolint(__dirname, enableAllRules)
68

79
if (hasError) {
810
process.exit(1)

packages/deno-lint/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@ let binding
66

77
try {
88
binding = loadBinding(__dirname, 'deno-lint')
9-
} catch (e) {
9+
// eslint-disable-next-line no-empty
10+
} catch (e) {}
11+
12+
if (!binding) {
13+
const platformName = platform()
1014
try {
11-
binding = require(`@node-rs/deno-lint-${platform()}`)
15+
binding = require(`@node-rs/deno-lint-${platformName}`)
1216
} catch (e) {
13-
throw new TypeError('Not compatible with your platform. Error message: ' + e.message)
17+
if (platformName === 'linux') {
18+
try {
19+
binding = require('@node-rs/deno-lint-linux-musl')
20+
} catch (e) {
21+
throw new TypeError('Error loading native addon: ' + e.message)
22+
}
23+
} else {
24+
throw new TypeError('Not compatible with your platform. Error message: ' + e.message)
25+
}
1426
}
1527
}
1628

1729
module.exports = {
1830
binding,
19-
lint: function lint(filename, sourcecode) {
31+
lint: function lint(path, sourcecode, allRules = false) {
2032
const source = Buffer.isBuffer(sourcecode) ? sourcecode : Buffer.from(sourcecode)
21-
return binding.lint(filename, source)
33+
return binding.lint(path, source, allRules)
2234
},
2335
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@node-rs/deno-lint-linux-musl`
2+
3+
This is the Linux 64-bit `musl` binary for `@node-rs/deno-lint`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@node-rs/deno-lint-linux-musl",
3+
"version": "0.0.0",
4+
"description": "Deno lint binding for NodeJS",
5+
"keywords": ["Deno", "Lint", "ESLint", "node-rs", "napi", "N-API", "Rust", "napi-rs"],
6+
"author": "LongYinan <[email protected]>",
7+
"homepage": "https://github.com/napi-rs/node-rs",
8+
"license": "MIT",
9+
"main": "deno-lint.musl.node",
10+
"files": ["deno-lint.musl.node"],
11+
"os": ["linux"],
12+
"cpu": ["x64"],
13+
"engines": {
14+
"node": ">= 8.9"
15+
},
16+
"publishConfig": {
17+
"registry": "https://registry.npmjs.org/",
18+
"access": "public"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/napi-rs/node-rs.git"
23+
}
24+
}

packages/deno-lint/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"denolint": "./bin.js"
1212
},
1313
"typings": "index.d.ts",
14-
"files": ["index.js", "index.d.ts", "bin.js", "LICENSE"],
14+
"files": ["index.js", "index.d.ts", "bin.js", "webpack-loader.js", "LICENSE"],
1515
"os": ["darwin", "linux", "win32"],
1616
"cpu": ["x64"],
1717
"engines": {
@@ -36,6 +36,10 @@
3636
"url": "https://github.com/napi-rs/node-rs/issues"
3737
},
3838
"dependencies": {
39-
"@node-rs/helper": "^0.2.1"
39+
"@node-rs/helper": "^0.2.1",
40+
"loader-utils": "^2.0.0"
41+
},
42+
"devDependencies": {
43+
"@types/webpack": "^4.41.21"
4044
}
4145
}

packages/deno-lint/platforms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = ['win32', 'darwin', 'linux']

packages/deno-lint/publish.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ const { execSync } = require('child_process')
22
const fs = require('fs')
33
const path = require('path')
44

5-
const platforms = require('../../scripts/platforms')
6-
75
const { version } = require('./package.json')
6+
const platforms = require('./platforms')
87
const updatePackageJson = require('./update-package')
98

109
updatePackageJson(path.join(__dirname, 'package.json'), {
@@ -14,7 +13,7 @@ updatePackageJson(path.join(__dirname, 'package.json'), {
1413
}, {}),
1514
})
1615

17-
for (const name of platforms) {
16+
for (const name of [...platforms, 'musl']) {
1817
const pkgDir = path.join(__dirname, 'npm', name)
1918
const filename = `deno-lint.${name}.node`
2019
const bindingFile = fs.readFileSync(path.join(__dirname, `bindings-${name}`, filename))

packages/deno-lint/src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ extern crate napi;
33
#[macro_use]
44
extern crate napi_derive;
55

6-
use napi::JsBoolean;
76
use std::env;
87
use std::fmt;
98
use std::fs;
@@ -12,10 +11,12 @@ use std::str;
1211

1312
use deno_lint::diagnostic::LintDiagnostic;
1413
use deno_lint::linter::LinterBuilder;
14+
use deno_lint::rules::{get_all_rules, get_recommended_rules};
15+
use deno_lint::swc_util::get_default_ts_config;
1516
use ignore::overrides::OverrideBuilder;
1617
use ignore::types::TypesBuilder;
1718
use ignore::WalkBuilder;
18-
use napi::{CallContext, Error, JsBuffer, JsObject, JsString, Module, Result, Status};
19+
use napi::{CallContext, Error, JsBoolean, JsBuffer, JsObject, JsString, Module, Result, Status};
1920
use termcolor::Color::{Ansi256, Red};
2021
use termcolor::{Ansi, ColorSpec, WriteColor};
2122

@@ -114,11 +115,19 @@ fn init(js_module: &mut Module) -> Result<()> {
114115
Ok(())
115116
}
116117

117-
#[js_function(2)]
118+
#[js_function(3)]
118119
fn lint(ctx: CallContext) -> Result<JsObject> {
119120
let file_name = ctx.get::<JsString>(0)?;
120121
let source_code = ctx.get::<JsBuffer>(1)?;
121-
let mut linter = LinterBuilder::default().build();
122+
let all_rules = ctx.get::<JsBoolean>(2)?;
123+
let mut linter = LinterBuilder::default()
124+
.rules(if all_rules.get_value()? {
125+
get_all_rules()
126+
} else {
127+
get_recommended_rules()
128+
})
129+
.syntax(get_default_ts_config())
130+
.build();
122131

123132
let source_string = str::from_utf8(&source_code).map_err(|e| Error {
124133
status: Status::StringExpected,
@@ -148,9 +157,10 @@ fn lint(ctx: CallContext) -> Result<JsObject> {
148157
Ok(result)
149158
}
150159

151-
#[js_function(1)]
160+
#[js_function(2)]
152161
fn lint_command(ctx: CallContext) -> Result<JsBoolean> {
153162
let __dirname = ctx.get::<JsString>(0)?;
163+
let enable_all_rules = ctx.get::<JsBoolean>(1)?.get_value()?;
154164
let mut has_error = false;
155165
let cwd = env::current_dir().map_err(|e| {
156166
Error::new(
@@ -210,7 +220,14 @@ fn lint_command(ctx: CallContext) -> Result<JsBoolean> {
210220
if !p.is_dir() {
211221
let file_content = fs::read_to_string(&p)
212222
.map_err(|e| Error::from_reason(format!("Read file {:?} failed: {}", p, e)))?;
213-
let mut linter = LinterBuilder::default().build();
223+
let mut linter = LinterBuilder::default()
224+
.rules(if enable_all_rules {
225+
get_all_rules()
226+
} else {
227+
get_recommended_rules()
228+
})
229+
.syntax(get_default_ts_config())
230+
.build();
214231
let file_diagnostics = linter
215232
.lint(
216233
(&p.to_str())
@@ -227,7 +244,7 @@ fn lint_command(ctx: CallContext) -> Result<JsBoolean> {
227244
})?;
228245
for diagnostic in file_diagnostics {
229246
has_error = true;
230-
println!("{:?}", diagnostic);
247+
println!("{}", format_diagnostic(&diagnostic));
231248
}
232249
}
233250
}

0 commit comments

Comments
 (0)