Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/oxlint/src-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ export function defineRule(rule: Rule): Rule {
report: { value: dummyReport, enumerable: true, configurable: true },
});

const { before: beforeHook, after: afterHook, ...visitor } = rule.createOnce(context as Context);
let { before: beforeHook, after: afterHook, ...visitor } = rule.createOnce(context as Context);

if (beforeHook === void 0) {
beforeHook = null;
} else if (beforeHook !== null && typeof beforeHook !== 'function') {
throw new Error('`before` property of visitor must be a function if defined');
}

// Add `after` hook to `Program:exit` visit fn
if (afterHook !== null) {
if (afterHook != null) {
if (typeof afterHook !== 'function') throw new Error('`after` property of visitor must be a function if defined');

const programExit = visitor['Program:exit'];
visitor['Program:exit'] = programExit
? (node) => {
Expand Down
200 changes: 198 additions & 2 deletions apps/oxlint/test/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,18 @@ Finished in Xms on 1 file using X threads."

exports[`oxlint CLI > should support \`createOnce\` 1`] = `
"
x create-once-plugin(after-only): after hook: filename: files/1.js
,-[files/1.js:1:1]
1 | let x;
: ^
\`----

x create-once-plugin(after-only): after hook: id: create-once-plugin/after-only
,-[files/1.js:1:1]
1 | let x;
: ^
\`----

x create-once-plugin(always-run): createOnce: filename: Cannot access \`context.filename\` in \`createOnce\`
,-[files/1.js:1:1]
1 | let x;
Expand Down Expand Up @@ -700,6 +712,18 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
: ^
\`----

x create-once-plugin(before-only): before hook: filename: files/1.js
,-[files/1.js:1:1]
1 | let x;
: ^
\`----

x create-once-plugin(before-only): before hook: id: create-once-plugin/before-only
,-[files/1.js:1:1]
1 | let x;
: ^
\`----

x create-once-plugin(skip-run): before hook: filename: files/1.js
,-[files/1.js:1:1]
1 | let x;
Expand All @@ -712,12 +736,42 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
: ^
\`----

x create-once-plugin(after-only): ident visit fn "x": filename: files/1.js
,-[files/1.js:1:5]
1 | let x;
: ^
\`----

x create-once-plugin(always-run): ident visit fn "x": filename: files/1.js
,-[files/1.js:1:5]
1 | let x;
: ^
\`----

x create-once-plugin(before-only): ident visit fn "x": filename: files/1.js
,-[files/1.js:1:5]
1 | let x;
: ^
\`----

x create-once-plugin(no-hooks): ident visit fn "x": filename: files/1.js
,-[files/1.js:1:5]
1 | let x;
: ^
\`----

x create-once-plugin(after-only): after hook: filename: files/2.js
,-[files/2.js:1:1]
1 | let y;
: ^
\`----

x create-once-plugin(after-only): after hook: id: create-once-plugin/after-only
,-[files/2.js:1:1]
1 | let y;
: ^
\`----

x create-once-plugin(always-run): createOnce: filename: Cannot access \`context.filename\` in \`createOnce\`
,-[files/2.js:1:1]
1 | let y;
Expand Down Expand Up @@ -778,6 +832,18 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
: ^
\`----

x create-once-plugin(before-only): before hook: filename: files/2.js
,-[files/2.js:1:1]
1 | let y;
: ^
\`----

x create-once-plugin(before-only): before hook: id: create-once-plugin/before-only
,-[files/2.js:1:1]
1 | let y;
: ^
\`----

x create-once-plugin(skip-run): before hook: filename: files/2.js
,-[files/2.js:1:1]
1 | let y;
Expand All @@ -790,13 +856,31 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
: ^
\`----

x create-once-plugin(after-only): ident visit fn "y": filename: files/2.js
,-[files/2.js:1:5]
1 | let y;
: ^
\`----

x create-once-plugin(always-run): ident visit fn "y": filename: files/2.js
,-[files/2.js:1:5]
1 | let y;
: ^
\`----

Found 0 warnings and 26 errors.
x create-once-plugin(before-only): ident visit fn "y": filename: files/2.js
,-[files/2.js:1:5]
1 | let y;
: ^
\`----

x create-once-plugin(no-hooks): ident visit fn "y": filename: files/2.js
,-[files/2.js:1:5]
1 | let y;
: ^
\`----

Found 0 warnings and 40 errors.
Finished in Xms on 2 files using X threads."
`;

Expand Down Expand Up @@ -825,6 +909,20 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
: ^
\`----

x define-rule-plugin(create-once-after-only): after hook:
| filename: files/1.js
,-[files/1.js:1:1]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create-once-before-only): before hook:
| filename: files/1.js
,-[files/1.js:1:1]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create): ident visit fn "a":
| filename: files/1.js
,-[files/1.js:1:5]
Expand All @@ -840,6 +938,27 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
: ^
\`----

x define-rule-plugin(create-once-after-only): ident visit fn "a":
| filename: files/1.js
,-[files/1.js:1:5]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create-once-before-only): ident visit fn "a":
| filename: files/1.js
,-[files/1.js:1:5]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create-once-no-hooks): ident visit fn "a":
| filename: files/1.js
,-[files/1.js:1:5]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create): ident visit fn "b":
| filename: files/1.js
,-[files/1.js:1:8]
Expand All @@ -855,6 +974,27 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
: ^
\`----

x define-rule-plugin(create-once-after-only): ident visit fn "b":
| filename: files/1.js
,-[files/1.js:1:8]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create-once-before-only): ident visit fn "b":
| filename: files/1.js
,-[files/1.js:1:8]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create-once-no-hooks): ident visit fn "b":
| filename: files/1.js
,-[files/1.js:1:8]
1 | let a, b;
: ^
\`----

x define-rule-plugin(create): create body:
| this === rule: true
,-[files/2.js:1:1]
Expand All @@ -878,6 +1018,20 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
: ^
\`----

x define-rule-plugin(create-once-after-only): after hook:
| filename: files/2.js
,-[files/2.js:1:1]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create-once-before-only): before hook:
| filename: files/2.js
,-[files/2.js:1:1]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create): ident visit fn "c":
| filename: files/2.js
,-[files/2.js:1:5]
Expand All @@ -893,6 +1047,27 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
: ^
\`----

x define-rule-plugin(create-once-after-only): ident visit fn "c":
| filename: files/2.js
,-[files/2.js:1:5]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create-once-before-only): ident visit fn "c":
| filename: files/2.js
,-[files/2.js:1:5]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create-once-no-hooks): ident visit fn "c":
| filename: files/2.js
,-[files/2.js:1:5]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create): ident visit fn "d":
| filename: files/2.js
,-[files/2.js:1:8]
Expand All @@ -908,7 +1083,28 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
: ^
\`----

Found 0 warnings and 14 errors.
x define-rule-plugin(create-once-after-only): ident visit fn "d":
| filename: files/2.js
,-[files/2.js:1:8]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create-once-before-only): ident visit fn "d":
| filename: files/2.js
,-[files/2.js:1:8]
1 | let c, d;
: ^
\`----

x define-rule-plugin(create-once-no-hooks): ident visit fn "d":
| filename: files/2.js
,-[files/2.js:1:8]
1 | let c, d;
: ^
\`----

Found 0 warnings and 30 errors.
Finished in Xms on 2 files using X threads."
`;

Expand Down
34 changes: 33 additions & 1 deletion apps/oxlint/test/__snapshots__/eslint-compat.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,72 @@ this === rule: true define-rule-plugin/create
0:1 error before hook:
this === rule: true
filename: files/1.js define-rule-plugin/create-once
0:1 error before hook:
filename: files/1.js define-rule-plugin/create-once-before-only
0:1 error after hook:
identNum: 2
filename: files/1.js define-rule-plugin/create-once
0:1 error after hook:
filename: files/1.js define-rule-plugin/create-once-after-only
1:5 error ident visit fn "a":
filename: files/1.js define-rule-plugin/create
1:5 error ident visit fn "a":
identNum: 1
filename: files/1.js define-rule-plugin/create-once
1:5 error ident visit fn "a":
filename: files/1.js define-rule-plugin/create-once-before-only
1:5 error ident visit fn "a":
filename: files/1.js define-rule-plugin/create-once-after-only
1:5 error ident visit fn "a":
filename: files/1.js define-rule-plugin/create-once-no-hooks
1:8 error ident visit fn "b":
filename: files/1.js define-rule-plugin/create
1:8 error ident visit fn "b":
identNum: 2
filename: files/1.js define-rule-plugin/create-once
1:8 error ident visit fn "b":
filename: files/1.js define-rule-plugin/create-once-before-only
1:8 error ident visit fn "b":
filename: files/1.js define-rule-plugin/create-once-after-only
1:8 error ident visit fn "b":
filename: files/1.js define-rule-plugin/create-once-no-hooks

<root>/apps/oxlint/test/fixtures/define/files/2.js
0:1 error create body:
this === rule: true define-rule-plugin/create
0:1 error before hook:
this === rule: true
filename: files/2.js define-rule-plugin/create-once
0:1 error before hook:
filename: files/2.js define-rule-plugin/create-once-before-only
0:1 error after hook:
identNum: 2
filename: files/2.js define-rule-plugin/create-once
0:1 error after hook:
filename: files/2.js define-rule-plugin/create-once-after-only
1:5 error ident visit fn "c":
filename: files/2.js define-rule-plugin/create
1:5 error ident visit fn "c":
identNum: 1
filename: files/2.js define-rule-plugin/create-once
1:5 error ident visit fn "c":
filename: files/2.js define-rule-plugin/create-once-before-only
1:5 error ident visit fn "c":
filename: files/2.js define-rule-plugin/create-once-after-only
1:5 error ident visit fn "c":
filename: files/2.js define-rule-plugin/create-once-no-hooks
1:8 error ident visit fn "d":
filename: files/2.js define-rule-plugin/create
1:8 error ident visit fn "d":
identNum: 2
filename: files/2.js define-rule-plugin/create-once
1:8 error ident visit fn "d":
filename: files/2.js define-rule-plugin/create-once-before-only
1:8 error ident visit fn "d":
filename: files/2.js define-rule-plugin/create-once-after-only
1:8 error ident visit fn "d":
filename: files/2.js define-rule-plugin/create-once-no-hooks

14 problems (14 errors, 0 warnings)
30 problems (30 errors, 0 warnings)
"
`;
5 changes: 4 additions & 1 deletion apps/oxlint/test/fixtures/createOnce/.oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"categories": {"correctness": "off"},
"rules": {
"create-once-plugin/always-run": "error",
"create-once-plugin/skip-run": "error"
"create-once-plugin/skip-run": "error",
"create-once-plugin/before-only": "error",
"create-once-plugin/after-only": "error",
"create-once-plugin/no-hooks": "error"
},
"ignorePatterns": ["test_plugin/**"]
}
Loading
Loading