Skip to content

Commit

Permalink
feat: start deno lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Aug 18, 2020
1 parent a5c1e4c commit 025abfb
Show file tree
Hide file tree
Showing 20 changed files with 414 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"./packages/bcrypt",
"./packages/crc32",
"./packages/deno-lint",
"./packages/jieba"
]

Expand Down
2 changes: 1 addition & 1 deletion packages/bcrypt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"scripts": {
"bench": "cross-env NODE_ENV=production node benchmark/bcrypt.js",
"build": "napi --release ./bcrypt",
"build:debug": "napi ./bcrypt.debug"
"build:debug": "napi ./bcrypt"
},
"bugs": {
"url": "https://github.com/napi-rs/node-rs/issues"
Expand Down
2 changes: 1 addition & 1 deletion packages/crc32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"scripts": {
"bench": "cross-env NODE_ENV=production node benchmark/crc32.js",
"build": "napi --release ./crc32",
"build:debug": "napi ./index"
"build:debug": "napi ./crc32"
},
"bugs": {
"url": "https://github.com/napi-rs/node-rs/issues"
Expand Down
20 changes: 20 additions & 0 deletions packages/deno-lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "deno-lint"
version = "0.1.0"
authors = ["LongYinan <lynweklm@gmail.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
deno_lint = "0.1"
napi = { version = "0.4" }
napi-derive = { version = "0.4" }
termcolor = "1.1"

[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
jemallocator = { version = "0.3", features = ["disable_initial_exec_tls"] }

[build-dependencies]
napi-build = { version = "0.2" }
10 changes: 10 additions & 0 deletions packages/deno-lint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `@node-rs/deno-lint`

> deno_lint nodejs binding
## Usage

```ts
import { lint } from 'deno-lint'

```
50 changes: 50 additions & 0 deletions packages/deno-lint/benchmark/lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { readFileSync } = require('fs')

const { parseForESLint } = require('@typescript-eslint/parser')
const { Suite } = require('benchmark')
const chalk = require('chalk')
const { Linter, SourceCode } = require('eslint')

const { lint } = require('../index')

const suite = new Suite('Lint benchmark')

const filepath = require.resolve('rxjs/src/internal/Subscriber.ts')

const tsconfigPath = require.resolve('rxjs/src/tsconfig.json')

const sourceCodeBuffer = readFileSync(filepath)
const sourcecode = sourceCodeBuffer.toString('utf-8')

const linter = new Linter()

suite
.add('@node-rs/deno-lint', () => {
lint(filepath, sourceCodeBuffer)
})
.add('eslint', () => {
const parseForESLintResult = parseForESLint(sourcecode, {
filePath: filepath,
sourceType: 'module',
ecmaVersion: 2019,
project: tsconfigPath,
loc: true,
range: true,
tokens: true,
comment: true,
})

const sc = new SourceCode({
text: sourcecode,
...parseForESLintResult,
})

linter.verify(sc, {}, filepath)
})
.on('cycle', function (event) {
console.info(String(event.target))
})
.on('complete', function () {
console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`)
})
.run()
5 changes: 5 additions & 0 deletions packages/deno-lint/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate napi_build;

fn main() {
napi_build::setup();
}
1 change: 1 addition & 0 deletions packages/deno-lint/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function lint(filename: string, source: string | Buffer): string[]
22 changes: 22 additions & 0 deletions packages/deno-lint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { platform } = require('os')

const { loadBinding } = require('@node-rs/helper')

let binding

try {
binding = loadBinding(__dirname, 'deno-lint')
} catch (e) {
try {
binding = require(`@node-rs/deno-lint-${platform()}`)
} catch (e) {
throw new TypeError('Not compatible with your platform. Error message: ' + e.message)
}
}

module.exports = {
lint: function lint(filename, sourcecode) {
const source = Buffer.isBuffer(sourcecode) ? sourcecode : Buffer.from(sourcecode)
return binding.lint(filename, source)
},
}
3 changes: 3 additions & 0 deletions packages/deno-lint/npm/darwin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@node-rs/deno-lint-darwin`

This is the macOS 64-bit binary for `@node-rs/deno-lint`.
24 changes: 24 additions & 0 deletions packages/deno-lint/npm/darwin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@node-rs/deno-lint-darwin",
"version": "0.0.0",
"description": "Deno lint binding for NodeJS",
"keywords": ["Deno", "Lint", "ESLint", "node-rs", "napi", "N-API", "Rust", "napi-rs"],
"author": "LongYinan <lynweklm@gmail.com>",
"homepage": "https://github.com/napi-rs/node-rs",
"license": "MIT",
"main": "deno-lint.darwin.node",
"files": ["deno-lint.darwin.node"],
"os": ["darwin"],
"cpu": ["x64"],
"engines": {
"node": ">= 8.9"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/napi-rs/node-rs.git"
}
}
3 changes: 3 additions & 0 deletions packages/deno-lint/npm/linux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@node-rs/deno-lint-linux`

This is the Linux 64-bit binary for `@node-rs/deno-lint`.
24 changes: 24 additions & 0 deletions packages/deno-lint/npm/linux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@node-rs/deno-lint-linux",
"version": "0.0.0",
"description": "Deno lint binding for NodeJS",
"keywords": ["Deno", "Lint", "ESLint", "node-rs", "napi", "N-API", "Rust", "napi-rs"],
"author": "LongYinan <lynweklm@gmail.com>",
"homepage": "https://github.com/napi-rs/node-rs",
"license": "MIT",
"main": "deno-lint.linux.node",
"files": ["deno-lint.linux.node"],
"os": ["linux"],
"cpu": ["x64"],
"engines": {
"node": ">= 8.9"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/napi-rs/node-rs.git"
}
}
3 changes: 3 additions & 0 deletions packages/deno-lint/npm/win32/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@node-rs/deno-lint-win32`

This is the Windows 64-bit binary for `@node-rs/deno-lint`.
24 changes: 24 additions & 0 deletions packages/deno-lint/npm/win32/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@node-rs/deno-lint-win32",
"version": "0.0.0",
"description": "Deno lint binding for NodeJS",
"keywords": ["Deno", "Lint", "ESLint", "node-rs", "napi", "N-API", "Rust", "napi-rs"],
"author": "LongYinan <lynweklm@gmail.com>",
"homepage": "https://github.com/napi-rs/node-rs",
"license": "MIT",
"main": "deno-lint.win32.node",
"files": ["deno-lint.win32.node"],
"os": ["win32"],
"cpu": ["x64"],
"engines": {
"node": ">= 8.9"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/napi-rs/node-rs.git"
}
}
38 changes: 38 additions & 0 deletions packages/deno-lint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@node-rs/deno-lint",
"version": "0.0.0",
"description": "Deno lint binding for NodeJS",
"keywords": ["Deno", "Lint", "ESLint", "node-rs", "napi", "N-API", "Rust", "napi-rs"],
"author": "LongYinan <lynweklm@gmail.com>",
"homepage": "https://github.com/napi-rs/node-rs",
"license": "MIT",
"main": "index.js",
"typings": "index.d.ts",
"files": ["index.js", "index.d.ts", "LICENSE"],
"os": ["darwin", "linux", "win32"],
"cpu": ["x64"],
"engines": {
"node": ">= 8.9"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/napi-rs/node-rs.git"
},
"scripts": {
"bench": "cross-env NODE_ENV=production node benchmark/lint.js",
"build": "napi --release ./deno-lint",
"build:debug": "napi ./deno-lint",
"prepublishOnly": "node ./publish.js",
"version": "node ./version.js"
},
"bugs": {
"url": "https://github.com/napi-rs/node-rs/issues"
},
"dependencies": {
"@node-rs/helper": "^0.2.1"
}
}
27 changes: 27 additions & 0 deletions packages/deno-lint/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')

const platforms = require('../../scripts/platforms')

const { version } = require('./package.json')
const updatePackageJson = require('./update-package')

updatePackageJson(path.join(__dirname, 'package.json'), {
optionalDependencies: platforms.reduce((acc, cur) => {
acc[`@node-rs/deno-lint-${cur}`] = `^${version}`
return acc
}, {}),
})

for (const name of platforms) {
const pkgDir = path.join(__dirname, 'npm', name)
const filename = `deno-lint.${name}.node`
const bindingFile = fs.readFileSync(path.join(__dirname, `bindings-${name}`, filename))
fs.writeFileSync(path.join(pkgDir, filename), bindingFile)
execSync('npm publish', {
cwd: pkgDir,
env: process.env,
stdio: 'inherit',
})
}
12 changes: 12 additions & 0 deletions packages/deno-lint/simple-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { readFileSync } = require('fs')

const { lint } = require('./index')

const filepath = require.resolve('rxjs/src/internal/Subscriber.ts')

const sourceCodeBuffer = readFileSync(filepath)

const result = lint(filepath, sourceCodeBuffer)

console.assert(Array.isArray(result))
console.info('deno-lint simple test passed')

0 comments on commit 025abfb

Please sign in to comment.