Skip to content

Commit 1387aaa

Browse files
committed
test(linter/plugins): test createOnce returning no visitor functions (#14279)
Add a test to make sure `createOnce` can return an object with only `before` and `after` hooks (no visitor functions) and that both hooks are called for each file in this case.
1 parent f3b61d8 commit 1387aaa

File tree

15 files changed

+189
-9
lines changed

15 files changed

+189
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"define-plugin-plugin/create-once-before-false": "error",
88
"define-plugin-plugin/create-once-before-only": "error",
99
"define-plugin-plugin/create-once-after-only": "error",
10+
"define-plugin-plugin/create-once-hooks-only": "error",
1011
"define-plugin-plugin/create-once-no-hooks": "error"
1112
}
1213
}

apps/oxlint/test/fixtures/definePlugin/eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default [
1212
'define-plugin-plugin/create-once-before-false': 'error',
1313
'define-plugin-plugin/create-once-before-only': 'error',
1414
'define-plugin-plugin/create-once-after-only': 'error',
15+
'define-plugin-plugin/create-once-hooks-only': 'error',
1516
'define-plugin-plugin/create-once-no-hooks': 'error',
1617
},
1718
},

apps/oxlint/test/fixtures/definePlugin/eslint.snap.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ filename: files/1.js define-plugin-plugin/create-once
1414
filename: files/1.js define-plugin-plugin/create-once-before-false
1515
0:1 error before hook:
1616
filename: files/1.js define-plugin-plugin/create-once-before-only
17+
0:1 error before hook:
18+
filename: files/1.js define-plugin-plugin/create-once-hooks-only
1719
0:1 error after hook:
1820
identNum: 2
1921
filename: files/1.js define-plugin-plugin/create-once
2022
0:1 error after hook:
2123
filename: files/1.js define-plugin-plugin/create-once-after-only
24+
0:1 error after hook:
25+
filename: files/1.js define-plugin-plugin/create-once-hooks-only
2226
1:5 error ident visit fn "a":
2327
filename: files/1.js define-plugin-plugin/create
2428
1:5 error ident visit fn "a":
@@ -53,13 +57,17 @@ filename: files/2.js define-plugin-plugin/create-once
5357
filename: files/2.js define-plugin-plugin/create-once-before-false
5458
0:1 error before hook:
5559
filename: files/2.js define-plugin-plugin/create-once-before-only
60+
0:1 error before hook:
61+
filename: files/2.js define-plugin-plugin/create-once-hooks-only
5662
0:1 error after hook:
5763
identNum: 2
5864
filename: files/2.js define-plugin-plugin/create-once
5965
0:1 error after hook:
6066
filename: files/2.js define-plugin-plugin/create-once-before-false
6167
0:1 error after hook:
6268
filename: files/2.js define-plugin-plugin/create-once-after-only
69+
0:1 error after hook:
70+
filename: files/2.js define-plugin-plugin/create-once-hooks-only
6371
1:5 error ident visit fn "c":
6472
filename: files/2.js define-plugin-plugin/create
6573
1:5 error ident visit fn "c":
@@ -87,7 +95,7 @@ filename: files/2.js define-plugin-plugin
8795
1:8 error ident visit fn "d":
8896
filename: files/2.js define-plugin-plugin/create-once-no-hooks
8997
90-
35 problems (35 errors, 0 warnings)
98+
39 problems (39 errors, 0 warnings)
9199
```
92100

93101
# stderr

apps/oxlint/test/fixtures/definePlugin/output.snap.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
: ^
4949
`----
5050
51+
x define-plugin-plugin(create-once-hooks-only): before hook:
52+
| filename: files/1.js
53+
,-[files/1.js:1:1]
54+
1 | let a, b;
55+
: ^
56+
`----
57+
58+
x define-plugin-plugin(create-once-hooks-only): after hook:
59+
| filename: files/1.js
60+
,-[files/1.js:1:1]
61+
1 | let a, b;
62+
: ^
63+
`----
64+
5165
x define-plugin-plugin(create): ident visit fn "a":
5266
| filename: files/1.js
5367
,-[files/1.js:1:5]
@@ -172,6 +186,20 @@
172186
: ^
173187
`----
174188
189+
x define-plugin-plugin(create-once-hooks-only): before hook:
190+
| filename: files/2.js
191+
,-[files/2.js:1:1]
192+
1 | let c, d;
193+
: ^
194+
`----
195+
196+
x define-plugin-plugin(create-once-hooks-only): after hook:
197+
| filename: files/2.js
198+
,-[files/2.js:1:1]
199+
1 | let c, d;
200+
: ^
201+
`----
202+
175203
x define-plugin-plugin(create): ident visit fn "c":
176204
| filename: files/2.js
177205
,-[files/2.js:1:5]
@@ -258,7 +286,7 @@
258286
: ^
259287
`----
260288
261-
Found 0 warnings and 35 errors.
289+
Found 0 warnings and 39 errors.
262290
Finished in Xms on 2 files using X threads.
263291
```
264292

apps/oxlint/test/fixtures/definePlugin/plugin.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const createOnceBeforeFalseRule: Rule = {
139139
},
140140
};
141141

142-
// These 3 rules test that `createOnce` without `before` and `after` hooks works correctly.
142+
// These 4 rules test that `createOnce` without `before` and `after` hooks works correctly.
143143

144144
const createOnceBeforeOnlyRule: Rule = {
145145
createOnce(context) {
@@ -183,6 +183,27 @@ const createOnceAfterOnlyRule: Rule = {
183183
},
184184
};
185185

186+
const createOnceHooksOnlyRule: Rule = {
187+
createOnce(context) {
188+
return {
189+
before() {
190+
context.report({
191+
message: 'before hook:\n' +
192+
`filename: ${relativePath(context.filename)}`,
193+
node: SPAN,
194+
});
195+
},
196+
after() {
197+
context.report({
198+
message: 'after hook:\n' +
199+
`filename: ${relativePath(context.filename)}`,
200+
node: SPAN,
201+
});
202+
},
203+
};
204+
},
205+
};
206+
186207
const createOnceNoHooksRule: Rule = {
187208
createOnce(context) {
188209
return {
@@ -207,6 +228,7 @@ export default definePlugin({
207228
'create-once-before-false': createOnceBeforeFalseRule,
208229
'create-once-before-only': createOnceBeforeOnlyRule,
209230
'create-once-after-only': createOnceAfterOnlyRule,
231+
'create-once-hooks-only': createOnceHooksOnlyRule,
210232
'create-once-no-hooks': createOnceNoHooksRule,
211233
},
212234
});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"define-plugin-and-rule-plugin/create-once-before-false": "error",
88
"define-plugin-and-rule-plugin/create-once-before-only": "error",
99
"define-plugin-and-rule-plugin/create-once-after-only": "error",
10+
"define-plugin-and-rule-plugin/create-once-hooks-only": "error",
1011
"define-plugin-and-rule-plugin/create-once-no-hooks": "error"
1112
}
1213
}

apps/oxlint/test/fixtures/definePlugin_and_defineRule/eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default [
1212
'define-plugin-and-rule-plugin/create-once-before-false': 'error',
1313
'define-plugin-and-rule-plugin/create-once-before-only': 'error',
1414
'define-plugin-and-rule-plugin/create-once-after-only': 'error',
15+
'define-plugin-and-rule-plugin/create-once-hooks-only': 'error',
1516
'define-plugin-and-rule-plugin/create-once-no-hooks': 'error',
1617
},
1718
},

apps/oxlint/test/fixtures/definePlugin_and_defineRule/eslint.snap.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ filename: files/1.js define-plugin-and-rule-plugin/create-once
1414
filename: files/1.js define-plugin-and-rule-plugin/create-once-before-false
1515
0:1 error before hook:
1616
filename: files/1.js define-plugin-and-rule-plugin/create-once-before-only
17+
0:1 error before hook:
18+
filename: files/1.js define-plugin-and-rule-plugin/create-once-hooks-only
1719
0:1 error after hook:
1820
identNum: 2
1921
filename: files/1.js define-plugin-and-rule-plugin/create-once
2022
0:1 error after hook:
2123
filename: files/1.js define-plugin-and-rule-plugin/create-once-after-only
24+
0:1 error after hook:
25+
filename: files/1.js define-plugin-and-rule-plugin/create-once-hooks-only
2226
1:5 error ident visit fn "a":
2327
filename: files/1.js define-plugin-and-rule-plugin/create
2428
1:5 error ident visit fn "a":
@@ -53,13 +57,17 @@ filename: files/2.js define-plugin-and-rule-plugin/create-once
5357
filename: files/2.js define-plugin-and-rule-plugin/create-once-before-false
5458
0:1 error before hook:
5559
filename: files/2.js define-plugin-and-rule-plugin/create-once-before-only
60+
0:1 error before hook:
61+
filename: files/2.js define-plugin-and-rule-plugin/create-once-hooks-only
5662
0:1 error after hook:
5763
identNum: 2
5864
filename: files/2.js define-plugin-and-rule-plugin/create-once
5965
0:1 error after hook:
6066
filename: files/2.js define-plugin-and-rule-plugin/create-once-before-false
6167
0:1 error after hook:
6268
filename: files/2.js define-plugin-and-rule-plugin/create-once-after-only
69+
0:1 error after hook:
70+
filename: files/2.js define-plugin-and-rule-plugin/create-once-hooks-only
6371
1:5 error ident visit fn "c":
6472
filename: files/2.js define-plugin-and-rule-plugin/create
6573
1:5 error ident visit fn "c":
@@ -87,7 +95,7 @@ filename: files/2.js define-plugin-and-ru
8795
1:8 error ident visit fn "d":
8896
filename: files/2.js define-plugin-and-rule-plugin/create-once-no-hooks
8997
90-
35 problems (35 errors, 0 warnings)
98+
39 problems (39 errors, 0 warnings)
9199
```
92100

93101
# stderr

apps/oxlint/test/fixtures/definePlugin_and_defineRule/output.snap.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
: ^
4949
`----
5050
51+
x define-plugin-and-rule-plugin(create-once-hooks-only): before hook:
52+
| filename: files/1.js
53+
,-[files/1.js:1:1]
54+
1 | let a, b;
55+
: ^
56+
`----
57+
58+
x define-plugin-and-rule-plugin(create-once-hooks-only): after hook:
59+
| filename: files/1.js
60+
,-[files/1.js:1:1]
61+
1 | let a, b;
62+
: ^
63+
`----
64+
5165
x define-plugin-and-rule-plugin(create): ident visit fn "a":
5266
| filename: files/1.js
5367
,-[files/1.js:1:5]
@@ -172,6 +186,20 @@
172186
: ^
173187
`----
174188
189+
x define-plugin-and-rule-plugin(create-once-hooks-only): before hook:
190+
| filename: files/2.js
191+
,-[files/2.js:1:1]
192+
1 | let c, d;
193+
: ^
194+
`----
195+
196+
x define-plugin-and-rule-plugin(create-once-hooks-only): after hook:
197+
| filename: files/2.js
198+
,-[files/2.js:1:1]
199+
1 | let c, d;
200+
: ^
201+
`----
202+
175203
x define-plugin-and-rule-plugin(create): ident visit fn "c":
176204
| filename: files/2.js
177205
,-[files/2.js:1:5]
@@ -258,7 +286,7 @@
258286
: ^
259287
`----
260288
261-
Found 0 warnings and 35 errors.
289+
Found 0 warnings and 39 errors.
262290
Finished in Xms on 2 files using X threads.
263291
```
264292

apps/oxlint/test/fixtures/definePlugin_and_defineRule/plugin.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const createOnceBeforeFalseRule = defineRule({
137137
},
138138
});
139139

140-
// These 3 rules test that `createOnce` without `before` and `after` hooks works correctly.
140+
// These 4 rules test that `createOnce` without `before` and `after` hooks works correctly.
141141

142142
const createOnceBeforeOnlyRule = defineRule({
143143
createOnce(context) {
@@ -181,6 +181,27 @@ const createOnceAfterOnlyRule = defineRule({
181181
},
182182
});
183183

184+
const createOnceHooksOnlyRule = defineRule({
185+
createOnce(context) {
186+
return {
187+
before() {
188+
context.report({
189+
message: 'before hook:\n' +
190+
`filename: ${relativePath(context.filename)}`,
191+
node: SPAN,
192+
});
193+
},
194+
after() {
195+
context.report({
196+
message: 'after hook:\n' +
197+
`filename: ${relativePath(context.filename)}`,
198+
node: SPAN,
199+
});
200+
},
201+
};
202+
},
203+
});
204+
184205
const createOnceNoHooksRule = defineRule({
185206
createOnce(context) {
186207
return {
@@ -205,6 +226,7 @@ export default definePlugin({
205226
'create-once-before-false': createOnceBeforeFalseRule,
206227
'create-once-before-only': createOnceBeforeOnlyRule,
207228
'create-once-after-only': createOnceAfterOnlyRule,
229+
'create-once-hooks-only': createOnceHooksOnlyRule,
208230
'create-once-no-hooks': createOnceNoHooksRule,
209231
},
210232
});

0 commit comments

Comments
 (0)