Skip to content

Commit c3fe904

Browse files
committed
perf: init autocorrect wasm only once
1 parent ba24c89 commit c3fe904

File tree

3 files changed

+41
-32
lines changed

3 files changed

+41
-32
lines changed

__tests__/rule.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { test } from 'bun:test';
22
import { RuleTester } from 'eslint';
3-
import { rule } from '../src/rule';
3+
import correct from '../src/rule';
44

55
const tester = new RuleTester();
66

77
test('rule', () => {
8-
tester.run('rule', rule, {
8+
tester.run('rule', correct, {
99
valid: [{
1010
filename: '.js',
1111
code: 'f("Hello 世界")',

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import type { ESLint } from 'eslint';
22
import { version } from '../package.json';
33
import correct from './rule';
44

5-
export default {
5+
const plugin: ESLint.Plugin = {
66
meta: {
7-
name: 'eslint-plugin-autocorrect',
7+
name: 'autocorrect',
88
version,
99
},
10-
1110
rules: {
1211
correct,
1312
},
14-
} satisfies ESLint.Plugin;
13+
};
14+
15+
export default plugin;

src/rule.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,55 @@ interface LintResult {
1212
c: number
1313
old: string
1414
new: string
15-
serverity: number
15+
serverity: Severity
1616
}>
1717
error: string
1818
}
1919

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);
29+
2030
const rule: Rule = {
2131
meta: {
2232
type: 'layout',
2333
fixable: 'whitespace',
2434
schema: [],
2535
messages: {
26-
issue: 'Issue detected',
36+
error: 'Autocorrect error detected',
37+
warning: 'Autocorrect warning detected',
38+
},
39+
docs: {
40+
recommended: true,
2741
},
2842
},
2943
create(ctx) {
30-
const wasmUrl = import.meta.resolve('autocorrect-wasm/autocorrect_wasm_bg.wasm');
31-
const wasm = readFileSync(fileURLToPath(wasmUrl));
32-
initSync(wasm);
33-
3444
const sourceCode = ctx.sourceCode as SourceCode;
35-
return {
36-
Program() {
37-
const res: LintResult = lintFor(sourceCode.getText(), ctx.filename ?? '');
38-
for (const line of res.lines) {
39-
const start = { line: line.l, column: line.c - 1 };
40-
const end = { line: line.l, column: line.c - 1 + line.old.length };
41-
42-
ctx.report({
43-
messageId: 'issue',
44-
loc: {
45-
start: { line: line.l, column: 0 },
46-
end: { line: line.l, column: line.old.length },
47-
},
48-
fix: f => f.replaceTextRange(
49-
[sourceCode.getIndexFromLoc(start), sourceCode.getIndexFromLoc(end)],
50-
line.new,
51-
),
52-
});
53-
}
54-
},
45+
const res: LintResult = lintFor(sourceCode.getText(), ctx.filename ?? '');
46+
47+
for (const line of res.lines) {
48+
const start = { line: line.l, column: line.c - 1 };
49+
const end = { line: line.l, column: line.c - 1 + line.old.length };
50+
ctx.report({
51+
messageId: line.serverity === Severity.Error ? 'error' : 'warning',
52+
loc: {
53+
start: { line: line.l, column: 0 },
54+
end: { line: line.l, column: line.old.length },
55+
},
56+
fix: f => f.replaceTextRange(
57+
[sourceCode.getIndexFromLoc(start), sourceCode.getIndexFromLoc(end)],
58+
line.new,
59+
),
60+
});
5561
};
62+
63+
return {};
5664
},
5765
};
5866

0 commit comments

Comments
 (0)