Skip to content

Commit 42c872d

Browse files
committed
fix: use autocorrect-node under the hood
Fix #3. `autocorrect-wasm` is not offical published. Ref: huacnlee/autocorrect#251
1 parent c3fe904 commit 42c872d

File tree

10 files changed

+338
-210
lines changed

10 files changed

+338
-210
lines changed

.autocorrectignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__tests__
2+
ignored.js

.autocorrectrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
textRules:
2+
"没有error": 0

.github/workflows/test.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
unit-test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
13+
- uses: oven-sh/setup-bun@4bc047ad259df6fc24a6c9b0f9a0cb08cf17fbe5 # v2
14+
with:
15+
bun-version-file: .tool-versions
16+
17+
- run: bun i --frozen-lockfile
18+
- run: bun test

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bun 1.2.2
1+
bun 1.2.7

.vscode/extensions.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"dbaeumer.vscode-eslint",
4+
"oven.bun-vscode"
5+
]
6+
}

__tests__/rule.spec.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
1-
import { test } from 'bun:test';
1+
import path from 'node:path';
2+
import process from 'node:process';
3+
import { it } from 'bun:test';
24
import { RuleTester } from 'eslint';
35
import correct from '../src/rule';
46

7+
const cwd = process.cwd();
58
const tester = new RuleTester();
69

7-
test('rule', () => {
8-
tester.run('rule', correct, {
9-
valid: [{
10-
filename: '.js',
11-
code: 'f("Hello 世界")',
12-
}, {
13-
filename: '.js',
14-
code: 'f("Hello,世界")\nf("。。。")',
15-
}],
16-
invalid: [{
17-
filename: '.js',
18-
code: 'console.log("Hello世界")',
19-
errors: 1,
20-
output: 'console.log("Hello 世界")',
21-
}, {
22-
filename: '.js',
23-
code: 'f("Hello ,世界")\nf("啊 ?")',
24-
errors: 2,
25-
output: 'f("Hello,世界")\nf("啊?")',
26-
}],
27-
});
28-
});
10+
it('should work', () => tester.run('rule', correct, {
11+
valid: [{
12+
filename: '.js',
13+
code: 'f("Hello 世界")',
14+
}, {
15+
filename: '.js',
16+
code: 'f("Hello,世界")\nf("。。。")',
17+
}],
18+
19+
invalid: [{
20+
filename: '.js',
21+
code: 'console.log("Hello世界")',
22+
errors: 1,
23+
output: 'console.log("Hello 世界")',
24+
}, {
25+
filename: '.js',
26+
code: 'f("Hello ,世界")\nf("啊 ?")',
27+
errors: 2,
28+
output: 'f("Hello,世界")\nf("啊?")',
29+
}],
30+
}));
31+
32+
it('should respect .autocorrectrc', () => tester.run('rule', correct, {
33+
valid: [{
34+
filename: path.join(cwd, 'ignored.js'),
35+
code: 'f("没有error")', // see .autocorrectrc
36+
}, {
37+
filename: path.join(cwd, 'ignored.js'),
38+
code: 'f("没有 error")',
39+
}],
40+
41+
invalid: [],
42+
}));
43+
44+
it('should respect .autocorrectignore', () => tester.run('rule', correct, {
45+
valid: [{
46+
filename: path.resolve(cwd, 'ignored.js'),
47+
code: 'f("有error")',
48+
}, {
49+
filename: path.resolve(cwd, 'ignored.js'),
50+
code: 'f("有 error")',
51+
}],
52+
53+
invalid: [],
54+
}));

bun.lock

Lines changed: 223 additions & 151 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828
"eslint": ">= 9.0.0"
2929
},
3030
"dependencies": {
31-
"autocorrect-wasm": "^1.0.0"
31+
"autocorrect-node": "^2.13.3"
3232
},
3333
"devDependencies": {
34-
"@antfu/eslint-config": "^4.1.1",
35-
"@commitlint/cli": "^19.7.1",
36-
"@commitlint/config-conventional": "^19.7.1",
34+
"@antfu/eslint-config": "^4.11.0",
35+
"@commitlint/cli": "^19.8.0",
36+
"@commitlint/config-conventional": "^19.8.0",
3737
"@tsconfig/bun": "^1.0.7",
3838
"@typed-sigterm/eslint-config": "^1.4.1",
39-
"@types/bun": "latest",
40-
"eslint": "^9.20.0",
39+
"@types/bun": "^1.2.8",
40+
"eslint": "^9.23.0",
4141
"eslint-plugin-eslint-plugin": "^6.4.0",
4242
"husky": "^9.1.7",
43-
"tsup": "^8.3.6",
44-
"typescript": "^5.7.3"
43+
"tsup": "^8.4.0",
44+
"typescript": "^5.8.2"
45+
},
46+
"publishConfig": {
47+
"provenance": true
4548
}
4649
}

src/config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { existsSync, readFileSync } from 'node:fs';
2+
import path from 'node:path';
3+
import process from 'node:process';
4+
import { Ignorer } from 'autocorrect-node';
5+
6+
const cwd = process.cwd();
7+
8+
export function getConfig() {
9+
// TODO: handle errors
10+
const p = path.resolve(cwd, '.autocorrectrc');
11+
if (!existsSync(p))
12+
return '';
13+
return readFileSync(p, 'utf8');
14+
}
15+
16+
export const getIgnorer = () => new Ignorer(cwd);

src/rule.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,38 @@
11
import type { ESLint, SourceCode } from 'eslint';
2-
import { readFileSync } from 'node:fs';
3-
import { fileURLToPath } from 'node:url';
4-
import { initSync, lintFor } from 'autocorrect-wasm';
2+
import { lintFor, loadConfig } from 'autocorrect-node';
3+
import { getConfig, getIgnorer } from './config';
54

65
type Rule = Exclude<ESLint.Plugin['rules'], undefined>[string];
76

8-
interface LintResult {
9-
filepath: string
10-
lines: Array<{
11-
l: number
12-
c: number
13-
old: string
14-
new: string
15-
serverity: Severity
16-
}>
17-
error: string
18-
}
7+
loadConfig(getConfig());
198

20-
enum Severity {
21-
Pass = 0,
22-
Error = 1,
23-
Warning = 2,
24-
}
25-
26-
const wasmUrl = import.meta.resolve('autocorrect-wasm/autocorrect_wasm_bg.wasm');
27-
const wasm = readFileSync(fileURLToPath(wasmUrl));
28-
initSync(wasm);
9+
const ignorer = getIgnorer();
2910

3011
const rule: Rule = {
3112
meta: {
3213
type: 'layout',
3314
fixable: 'whitespace',
3415
schema: [],
3516
messages: {
36-
error: 'Autocorrect error detected',
37-
warning: 'Autocorrect warning detected',
17+
issue: 'Autocorrect issue detected',
3818
},
3919
docs: {
4020
recommended: true,
4121
},
4222
},
4323
create(ctx) {
4424
const sourceCode = ctx.sourceCode as SourceCode;
45-
const res: LintResult = lintFor(sourceCode.getText(), ctx.filename ?? '');
25+
if (!('getText' in sourceCode) || !('getIndexFromLoc' in sourceCode))
26+
return {};
27+
if (ignorer.isIgnored(ctx.filename))
28+
return {};
29+
const res = lintFor(sourceCode.getText(), ctx.filename);
4630

4731
for (const line of res.lines) {
4832
const start = { line: line.l, column: line.c - 1 };
4933
const end = { line: line.l, column: line.c - 1 + line.old.length };
5034
ctx.report({
51-
messageId: line.serverity === Severity.Error ? 'error' : 'warning',
35+
messageId: 'issue',
5236
loc: {
5337
start: { line: line.l, column: 0 },
5438
end: { line: line.l, column: line.old.length },

0 commit comments

Comments
 (0)