[lint] move use lint to non-experimental (#27768)
`use` is being stabilized, so let's make sure the lint is updated for the next release.
Jan Kassens committed
Nov 30, 2023 at 17:39 UTC
b8be034f07e1abc59863742063f5baeff20e33fe
2 files changed
+93
-99
packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js
+91
-91
@@ -479,6 +479,45 @@ const tests = {
479
}
480
`,
481
},
482
+ {
483
+ code: normalizeIndent`
484
+ function App() {
485
+ const text = use(Promise.resolve('A'));
486
+ return <Text text={text} />
487
+ }
488
+ `,
489
+ },
490
+ {
491
+ code: normalizeIndent`
492
+ function App() {
493
+ if (shouldShowText) {
494
+ const text = use(query);
495
+ return <Text text={text} />
496
+ }
497
+ return <Text text={shouldFetchBackupText ? use(backupQuery) : "Nothing to see here"} />
498
+ }
499
+ `,
500
+ },
501
+ {
502
+ code: normalizeIndent`
503
+ function App() {
504
+ let data = [];
505
+ for (const query of queries) {
506
+ const text = use(item);
507
+ data.push(text);
508
+ }
509
+ return <Child data={data} />
510
+ }
511
+ `,
512
+ },
513
+ {
514
+ code: normalizeIndent`
515
+ function App() {
516
+ const data = someCallback((x) => use(x));
517
+ return <Child data={data} />
518
+ }
519
+ `,
520
+ },
521
],
522
invalid: [
523
{
@@ -1058,6 +1097,58 @@ const tests = {
1097
`,
1098
errors: [asyncComponentHookError('useState')],
1099
},
1100
+ {
1101
+ code: normalizeIndent`
1102
+ Hook.use();
1103
+ Hook._use();
1104
+ Hook.useState();
1105
+ Hook._useState();
1106
+ Hook.use42();
1107
+ Hook.useHook();
1108
+ Hook.use_hook();
1109
+ `,
1110
+ errors: [
1111
+ topLevelError('Hook.use'),
1112
+ topLevelError('Hook.useState'),
1113
+ topLevelError('Hook.use42'),
1114
+ topLevelError('Hook.useHook'),
1115
+ ],
1116
+ },
1117
+ {
1118
+ code: normalizeIndent`
1119
+ function notAComponent() {
1120
+ use(promise);
1121
+ }
1122
+ `,
1123
+ errors: [functionError('use', 'notAComponent')],
1124
+ },
1125
+ {
1126
+ code: normalizeIndent`
1127
+ const text = use(promise);
1128
+ function App() {
1129
+ return <Text text={text} />
1130
+ }
1131
+ `,
1132
+ errors: [topLevelError('use')],
1133
+ },
1134
+ {
1135
+ code: normalizeIndent`
1136
+ class C {
1137
+ m() {
1138
+ use(promise);
1139
+ }
1140
+ }
1141
+ `,
1142
+ errors: [classError('use')],
1143
+ },
1144
+ {
1145
+ code: normalizeIndent`
1146
+ async function AsyncComponent() {
1147
+ use();
1148
+ }
1149
+ `,
1150
+ errors: [asyncComponentHookError('use')],
1151
+ },
1152
],
1153
};
1154
@@ -1159,45 +1250,6 @@ if (__EXPERIMENTAL__) {
1250
}
1251
`,
1252
},
1162
- {
1163
- code: normalizeIndent`
1164
- function App() {
1165
- const text = use(Promise.resolve('A'));
1166
- return <Text text={text} />
1167
- }
1168
- `,
1169
- },
1170
- {
1171
- code: normalizeIndent`
1172
- function App() {
1173
- if (shouldShowText) {
1174
- const text = use(query);
1175
- return <Text text={text} />
1176
- }
1177
- return <Text text={shouldFetchBackupText ? use(backupQuery) : "Nothing to see here"} />
1178
- }
1179
- `,
1180
- },
1181
- {
1182
- code: normalizeIndent`
1183
- function App() {
1184
- let data = [];
1185
- for (const query of queries) {
1186
- const text = use(item);
1187
- data.push(text);
1188
- }
1189
- return <Child data={data} />
1190
- }
1191
- `,
1192
- },
1193
- {
1194
- code: normalizeIndent`
1195
- function App() {
1196
- const data = someCallback((x) => use(x));
1197
- return <Child data={data} />
1198
- }
1199
- `,
1200
- },
1253
];
1254
tests.invalid = [
1255
...tests.invalid,
@@ -1272,58 +1324,6 @@ if (__EXPERIMENTAL__) {
1324
`,
1325
errors: [useEffectEventError('onClick')],
1326
},
1275
- {
1276
- code: normalizeIndent`
1277
- Hook.use();
1278
- Hook._use();
1279
- Hook.useState();
1280
- Hook._useState();
1281
- Hook.use42();
1282
- Hook.useHook();
1283
- Hook.use_hook();
1284
- `,
1285
- errors: [
1286
- topLevelError('Hook.use'),
1287
- topLevelError('Hook.useState'),
1288
- topLevelError('Hook.use42'),
1289
- topLevelError('Hook.useHook'),
1290
- ],
1291
- },
1292
- {
1293
- code: normalizeIndent`
1294
- function notAComponent() {
1295
- use(promise);
1296
- }
1297
- `,
1298
- errors: [functionError('use', 'notAComponent')],
1299
- },
1300
- {
1301
- code: normalizeIndent`
1302
- const text = use(promise);
1303
- function App() {
1304
- return <Text text={text} />
1305
- }
1306
- `,
1307
- errors: [topLevelError('use')],
1308
- },
1309
- {
1310
- code: normalizeIndent`
1311
- class C {
1312
- m() {
1313
- use(promise);
1314
- }
1315
- }
1316
- `,
1317
- errors: [classError('use')],
1318
- },
1319
- {
1320
- code: normalizeIndent`
1321
- async function AsyncComponent() {
1322
- use();
1323
- }
1324
- `,
1325
- errors: [asyncComponentHookError('use')],
1326
- },
1327
];
1328
}
1329
packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
+2
-8
@@ -16,10 +16,7 @@
16
*/
17
18
function isHookName(s) {
19
- if (__EXPERIMENTAL__) {
20
- return s === 'use' || /^use[A-Z0-9]/.test(s);
21
- }
22
- return /^use[A-Z0-9]/.test(s);
19
+ return s === 'use' || /^use[A-Z0-9]/.test(s);
20
}
21
22
/**
@@ -111,10 +108,7 @@ function isUseEffectEventIdentifier(node) {
108
}
109
110
function isUseIdentifier(node) {
114
- if (__EXPERIMENTAL__) {
115
- return node.type === 'Identifier' && node.name === 'use';
116
- }
117
- return false;
111
+ return node.type === 'Identifier' && node.name === 'use';
112
}
113
114
export default {