Skip to content

Commit 01a122b

Browse files
committed
fix(linter/plugins): defineRule accept visitor with no before / after hooks
1 parent b109419 commit 01a122b

File tree

8 files changed

+363
-7
lines changed

8 files changed

+363
-7
lines changed

apps/oxlint/src-js/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ export function defineRule(rule: Rule): Rule {
2727
report: { value: dummyReport, enumerable: true, configurable: true },
2828
});
2929

30-
const { before: beforeHook, after: afterHook, ...visitor } = rule.createOnce(context as Context);
30+
let { before: beforeHook, after: afterHook, ...visitor } = rule.createOnce(context as Context);
31+
32+
if (beforeHook === void 0) {
33+
beforeHook = null;
34+
} else if (beforeHook !== null && typeof beforeHook !== 'function') {
35+
throw new Error('`before` property of visitor must be a function if defined');
36+
}
3137

3238
// Add `after` hook to `Program:exit` visit fn
33-
if (afterHook !== null) {
39+
if (afterHook != null) {
40+
if (typeof afterHook !== 'function') throw new Error('`after` property of visitor must be a function if defined');
41+
3442
const programExit = visitor['Program:exit'];
3543
visitor['Program:exit'] = programExit
3644
? (node) => {

apps/oxlint/test/__snapshots__/e2e.test.ts.snap

Lines changed: 198 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,18 @@ Finished in Xms on 1 file using X threads."
640640
641641
exports[`oxlint CLI > should support \`createOnce\` 1`] = `
642642
"
643+
x create-once-plugin(after-only): after hook: filename: files/1.js
644+
,-[files/1.js:1:1]
645+
1 | let x;
646+
: ^
647+
\`----
648+
649+
x create-once-plugin(after-only): after hook: id: create-once-plugin/after-only
650+
,-[files/1.js:1:1]
651+
1 | let x;
652+
: ^
653+
\`----
654+
643655
x create-once-plugin(always-run): createOnce: filename: Cannot access \`context.filename\` in \`createOnce\`
644656
,-[files/1.js:1:1]
645657
1 | let x;
@@ -700,6 +712,18 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
700712
: ^
701713
\`----
702714
715+
x create-once-plugin(before-only): before hook: filename: files/1.js
716+
,-[files/1.js:1:1]
717+
1 | let x;
718+
: ^
719+
\`----
720+
721+
x create-once-plugin(before-only): before hook: id: create-once-plugin/before-only
722+
,-[files/1.js:1:1]
723+
1 | let x;
724+
: ^
725+
\`----
726+
703727
x create-once-plugin(skip-run): before hook: filename: files/1.js
704728
,-[files/1.js:1:1]
705729
1 | let x;
@@ -712,12 +736,42 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
712736
: ^
713737
\`----
714738
739+
x create-once-plugin(after-only): ident visit fn "x": filename: files/1.js
740+
,-[files/1.js:1:5]
741+
1 | let x;
742+
: ^
743+
\`----
744+
715745
x create-once-plugin(always-run): ident visit fn "x": filename: files/1.js
716746
,-[files/1.js:1:5]
717747
1 | let x;
718748
: ^
719749
\`----
720750
751+
x create-once-plugin(before-only): ident visit fn "x": filename: files/1.js
752+
,-[files/1.js:1:5]
753+
1 | let x;
754+
: ^
755+
\`----
756+
757+
x create-once-plugin(no-hooks): ident visit fn "x": filename: files/1.js
758+
,-[files/1.js:1:5]
759+
1 | let x;
760+
: ^
761+
\`----
762+
763+
x create-once-plugin(after-only): after hook: filename: files/2.js
764+
,-[files/2.js:1:1]
765+
1 | let y;
766+
: ^
767+
\`----
768+
769+
x create-once-plugin(after-only): after hook: id: create-once-plugin/after-only
770+
,-[files/2.js:1:1]
771+
1 | let y;
772+
: ^
773+
\`----
774+
721775
x create-once-plugin(always-run): createOnce: filename: Cannot access \`context.filename\` in \`createOnce\`
722776
,-[files/2.js:1:1]
723777
1 | let y;
@@ -778,6 +832,18 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
778832
: ^
779833
\`----
780834
835+
x create-once-plugin(before-only): before hook: filename: files/2.js
836+
,-[files/2.js:1:1]
837+
1 | let y;
838+
: ^
839+
\`----
840+
841+
x create-once-plugin(before-only): before hook: id: create-once-plugin/before-only
842+
,-[files/2.js:1:1]
843+
1 | let y;
844+
: ^
845+
\`----
846+
781847
x create-once-plugin(skip-run): before hook: filename: files/2.js
782848
,-[files/2.js:1:1]
783849
1 | let y;
@@ -790,13 +856,31 @@ exports[`oxlint CLI > should support \`createOnce\` 1`] = `
790856
: ^
791857
\`----
792858
859+
x create-once-plugin(after-only): ident visit fn "y": filename: files/2.js
860+
,-[files/2.js:1:5]
861+
1 | let y;
862+
: ^
863+
\`----
864+
793865
x create-once-plugin(always-run): ident visit fn "y": filename: files/2.js
794866
,-[files/2.js:1:5]
795867
1 | let y;
796868
: ^
797869
\`----
798870
799-
Found 0 warnings and 26 errors.
871+
x create-once-plugin(before-only): ident visit fn "y": filename: files/2.js
872+
,-[files/2.js:1:5]
873+
1 | let y;
874+
: ^
875+
\`----
876+
877+
x create-once-plugin(no-hooks): ident visit fn "y": filename: files/2.js
878+
,-[files/2.js:1:5]
879+
1 | let y;
880+
: ^
881+
\`----
882+
883+
Found 0 warnings and 40 errors.
800884
Finished in Xms on 2 files using X threads."
801885
`;
802886
@@ -825,6 +909,20 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
825909
: ^
826910
\`----
827911
912+
x define-rule-plugin(create-once-after-only): after hook:
913+
| filename: files/1.js
914+
,-[files/1.js:1:1]
915+
1 | let a, b;
916+
: ^
917+
\`----
918+
919+
x define-rule-plugin(create-once-before-only): before hook:
920+
| filename: files/1.js
921+
,-[files/1.js:1:1]
922+
1 | let a, b;
923+
: ^
924+
\`----
925+
828926
x define-rule-plugin(create): ident visit fn "a":
829927
| filename: files/1.js
830928
,-[files/1.js:1:5]
@@ -840,6 +938,27 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
840938
: ^
841939
\`----
842940
941+
x define-rule-plugin(create-once-after-only): ident visit fn "a":
942+
| filename: files/1.js
943+
,-[files/1.js:1:5]
944+
1 | let a, b;
945+
: ^
946+
\`----
947+
948+
x define-rule-plugin(create-once-before-only): ident visit fn "a":
949+
| filename: files/1.js
950+
,-[files/1.js:1:5]
951+
1 | let a, b;
952+
: ^
953+
\`----
954+
955+
x define-rule-plugin(create-once-no-hooks): ident visit fn "a":
956+
| filename: files/1.js
957+
,-[files/1.js:1:5]
958+
1 | let a, b;
959+
: ^
960+
\`----
961+
843962
x define-rule-plugin(create): ident visit fn "b":
844963
| filename: files/1.js
845964
,-[files/1.js:1:8]
@@ -855,6 +974,27 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
855974
: ^
856975
\`----
857976
977+
x define-rule-plugin(create-once-after-only): ident visit fn "b":
978+
| filename: files/1.js
979+
,-[files/1.js:1:8]
980+
1 | let a, b;
981+
: ^
982+
\`----
983+
984+
x define-rule-plugin(create-once-before-only): ident visit fn "b":
985+
| filename: files/1.js
986+
,-[files/1.js:1:8]
987+
1 | let a, b;
988+
: ^
989+
\`----
990+
991+
x define-rule-plugin(create-once-no-hooks): ident visit fn "b":
992+
| filename: files/1.js
993+
,-[files/1.js:1:8]
994+
1 | let a, b;
995+
: ^
996+
\`----
997+
858998
x define-rule-plugin(create): create body:
859999
| this === rule: true
8601000
,-[files/2.js:1:1]
@@ -878,6 +1018,20 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
8781018
: ^
8791019
\`----
8801020
1021+
x define-rule-plugin(create-once-after-only): after hook:
1022+
| filename: files/2.js
1023+
,-[files/2.js:1:1]
1024+
1 | let c, d;
1025+
: ^
1026+
\`----
1027+
1028+
x define-rule-plugin(create-once-before-only): before hook:
1029+
| filename: files/2.js
1030+
,-[files/2.js:1:1]
1031+
1 | let c, d;
1032+
: ^
1033+
\`----
1034+
8811035
x define-rule-plugin(create): ident visit fn "c":
8821036
| filename: files/2.js
8831037
,-[files/2.js:1:5]
@@ -893,6 +1047,27 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
8931047
: ^
8941048
\`----
8951049
1050+
x define-rule-plugin(create-once-after-only): ident visit fn "c":
1051+
| filename: files/2.js
1052+
,-[files/2.js:1:5]
1053+
1 | let c, d;
1054+
: ^
1055+
\`----
1056+
1057+
x define-rule-plugin(create-once-before-only): ident visit fn "c":
1058+
| filename: files/2.js
1059+
,-[files/2.js:1:5]
1060+
1 | let c, d;
1061+
: ^
1062+
\`----
1063+
1064+
x define-rule-plugin(create-once-no-hooks): ident visit fn "c":
1065+
| filename: files/2.js
1066+
,-[files/2.js:1:5]
1067+
1 | let c, d;
1068+
: ^
1069+
\`----
1070+
8961071
x define-rule-plugin(create): ident visit fn "d":
8971072
| filename: files/2.js
8981073
,-[files/2.js:1:8]
@@ -908,7 +1083,28 @@ exports[`oxlint CLI > should support \`defineRule\` + \`definePlugin\` 1`] = `
9081083
: ^
9091084
\`----
9101085
911-
Found 0 warnings and 14 errors.
1086+
x define-rule-plugin(create-once-after-only): ident visit fn "d":
1087+
| filename: files/2.js
1088+
,-[files/2.js:1:8]
1089+
1 | let c, d;
1090+
: ^
1091+
\`----
1092+
1093+
x define-rule-plugin(create-once-before-only): ident visit fn "d":
1094+
| filename: files/2.js
1095+
,-[files/2.js:1:8]
1096+
1 | let c, d;
1097+
: ^
1098+
\`----
1099+
1100+
x define-rule-plugin(create-once-no-hooks): ident visit fn "d":
1101+
| filename: files/2.js
1102+
,-[files/2.js:1:8]
1103+
1 | let c, d;
1104+
: ^
1105+
\`----
1106+
1107+
Found 0 warnings and 30 errors.
9121108
Finished in Xms on 2 files using X threads."
9131109
`;
9141110

apps/oxlint/test/__snapshots__/eslint-compat.test.ts.snap

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,72 @@ this === rule: true define-rule-plugin/create
88
0:1 error before hook:
99
this === rule: true
1010
filename: files/1.js define-rule-plugin/create-once
11+
0:1 error before hook:
12+
filename: files/1.js define-rule-plugin/create-once-before-only
1113
0:1 error after hook:
1214
identNum: 2
1315
filename: files/1.js define-rule-plugin/create-once
16+
0:1 error after hook:
17+
filename: files/1.js define-rule-plugin/create-once-after-only
1418
1:5 error ident visit fn "a":
1519
filename: files/1.js define-rule-plugin/create
1620
1:5 error ident visit fn "a":
1721
identNum: 1
1822
filename: files/1.js define-rule-plugin/create-once
23+
1:5 error ident visit fn "a":
24+
filename: files/1.js define-rule-plugin/create-once-before-only
25+
1:5 error ident visit fn "a":
26+
filename: files/1.js define-rule-plugin/create-once-after-only
27+
1:5 error ident visit fn "a":
28+
filename: files/1.js define-rule-plugin/create-once-no-hooks
1929
1:8 error ident visit fn "b":
2030
filename: files/1.js define-rule-plugin/create
2131
1:8 error ident visit fn "b":
2232
identNum: 2
2333
filename: files/1.js define-rule-plugin/create-once
34+
1:8 error ident visit fn "b":
35+
filename: files/1.js define-rule-plugin/create-once-before-only
36+
1:8 error ident visit fn "b":
37+
filename: files/1.js define-rule-plugin/create-once-after-only
38+
1:8 error ident visit fn "b":
39+
filename: files/1.js define-rule-plugin/create-once-no-hooks
2440
2541
<root>/apps/oxlint/test/fixtures/define/files/2.js
2642
0:1 error create body:
2743
this === rule: true define-rule-plugin/create
2844
0:1 error before hook:
2945
this === rule: true
3046
filename: files/2.js define-rule-plugin/create-once
47+
0:1 error before hook:
48+
filename: files/2.js define-rule-plugin/create-once-before-only
3149
0:1 error after hook:
3250
identNum: 2
3351
filename: files/2.js define-rule-plugin/create-once
52+
0:1 error after hook:
53+
filename: files/2.js define-rule-plugin/create-once-after-only
3454
1:5 error ident visit fn "c":
3555
filename: files/2.js define-rule-plugin/create
3656
1:5 error ident visit fn "c":
3757
identNum: 1
3858
filename: files/2.js define-rule-plugin/create-once
59+
1:5 error ident visit fn "c":
60+
filename: files/2.js define-rule-plugin/create-once-before-only
61+
1:5 error ident visit fn "c":
62+
filename: files/2.js define-rule-plugin/create-once-after-only
63+
1:5 error ident visit fn "c":
64+
filename: files/2.js define-rule-plugin/create-once-no-hooks
3965
1:8 error ident visit fn "d":
4066
filename: files/2.js define-rule-plugin/create
4167
1:8 error ident visit fn "d":
4268
identNum: 2
4369
filename: files/2.js define-rule-plugin/create-once
70+
1:8 error ident visit fn "d":
71+
filename: files/2.js define-rule-plugin/create-once-before-only
72+
1:8 error ident visit fn "d":
73+
filename: files/2.js define-rule-plugin/create-once-after-only
74+
1:8 error ident visit fn "d":
75+
filename: files/2.js define-rule-plugin/create-once-no-hooks
4476
45-
14 problems (14 errors, 0 warnings)
77+
30 problems (30 errors, 0 warnings)
4678
"
4779
`;

apps/oxlint/test/fixtures/createOnce/.oxlintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"categories": {"correctness": "off"},
44
"rules": {
55
"create-once-plugin/always-run": "error",
6-
"create-once-plugin/skip-run": "error"
6+
"create-once-plugin/skip-run": "error",
7+
"create-once-plugin/before-only": "error",
8+
"create-once-plugin/after-only": "error",
9+
"create-once-plugin/no-hooks": "error"
710
},
811
"ignorePatterns": ["test_plugin/**"]
912
}

0 commit comments

Comments
 (0)