@eslint-react/jsx 0.10.2 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -12,9 +12,7 @@ const RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
12
12
  function getFragmentFromContext(context) {
13
13
  const settings = shared.parseSchema(shared.ESLintSettingsSchema, context.settings);
14
14
  const fragment = settings.eslintReact?.jsx?.fragment;
15
- if (tools._.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) {
16
- return fragment;
17
- }
15
+ if (tools._.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) return fragment;
18
16
  return "Fragment";
19
17
  }
20
18
  const getPragmaFromContext = memo((context)=>{
@@ -31,9 +29,7 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
31
29
  const variables = ast.getVariablesUpToGlobal(initialScope);
32
30
  const maybeVariable = ast.findVariableByName(variableName)(variables);
33
31
  const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
34
- if (tools.O.isNone(maybeLatestDef)) {
35
- return false;
36
- }
32
+ if (tools.O.isNone(maybeLatestDef)) return false;
37
33
  const latestDef = maybeLatestDef.value;
38
34
  const { node, parent } = latestDef;
39
35
  if (node.type === ast.NodeType.VariableDeclarator && node.init) {
@@ -45,16 +41,12 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
45
41
  type: "Identifier",
46
42
  name: pragma
47
43
  }
48
- }, init)) {
49
- return true;
50
- }
44
+ }, init)) return true;
51
45
  // check for: `{ variable } = pragma`
52
46
  if (a({
53
47
  type: "Identifier",
54
48
  name: pragma
55
- }, init)) {
56
- return true;
57
- }
49
+ }, init)) return true;
58
50
  // check if from a require call: `require("react")`
59
51
  const maybeRequireExpression = N(init).with({
60
52
  type: ast.NodeType.CallExpression,
@@ -72,14 +64,10 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
72
64
  }
73
65
  }
74
66
  }, ({ object })=>tools.O.some(object)).otherwise(tools.O.none);
75
- if (tools.O.isNone(maybeRequireExpression)) {
76
- return false;
77
- }
67
+ if (tools.O.isNone(maybeRequireExpression)) return false;
78
68
  const requireExpression = maybeRequireExpression.value;
79
69
  const [firstArg] = requireExpression.arguments;
80
- if (firstArg?.type !== ast.NodeType.Literal) {
81
- return false;
82
- }
70
+ if (firstArg?.type !== ast.NodeType.Literal) return false;
83
71
  return firstArg.value === pragma.toLowerCase();
84
72
  }
85
73
  // latest definition is an import declaration: import { variable } from 'react'
@@ -110,12 +98,8 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
110
98
  */ function isFromPragma(name) {
111
99
  return (node, context)=>{
112
100
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
113
- if (node.type === ast.NodeType.MemberExpression) {
114
- return isPropertyOfPragma(name, context)(node);
115
- }
116
- if (node.name === name) {
117
- return isInitializedFromPragma(name, context, initialScope);
118
- }
101
+ if (node.type === ast.NodeType.MemberExpression) return isPropertyOfPragma(name, context)(node);
102
+ if (node.name === name) return isInitializedFromPragma(name, context, initialScope);
119
103
  return false;
120
104
  };
121
105
  }
@@ -127,9 +111,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
127
111
  */ function isFromPragmaMember(pragmaMemberName, name) {
128
112
  return (node, context, pragma = getPragmaFromContext(context))=>{
129
113
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
130
- if (node.property.type !== ast.NodeType.Identifier || node.property.name !== name) {
131
- return false;
132
- }
114
+ if (node.property.type !== ast.NodeType.Identifier || node.property.name !== name) return false;
133
115
  if (node.object.type === ast.NodeType.Identifier && node.object.name === pragmaMemberName) {
134
116
  return isInitializedFromPragma(node.object.name, context, initialScope, pragma);
135
117
  }
@@ -144,17 +126,13 @@ function isCallFromPragma(name) {
144
126
  if (!ast.isOneOf([
145
127
  ast.NodeType.Identifier,
146
128
  ast.NodeType.MemberExpression
147
- ])(node.callee)) {
148
- return false;
149
- }
129
+ ])(node.callee)) return false;
150
130
  return isFromPragma(name)(node.callee, context);
151
131
  };
152
132
  }
153
133
  function isCallFromPragmaMember(pragmaMemberName, name) {
154
134
  return (node, context)=>{
155
- if (!ast.is(ast.NodeType.MemberExpression)(node.callee)) {
156
- return false;
157
- }
135
+ if (!ast.is(ast.NodeType.MemberExpression)(node.callee)) return false;
158
136
  return isFromPragmaMember(pragmaMemberName, name)(node.callee, context);
159
137
  };
160
138
  }
@@ -164,9 +142,7 @@ const isCreateElementCall = (node, context)=>{
164
142
  if (!ast.isOneOf([
165
143
  ast.NodeType.Identifier,
166
144
  ast.NodeType.MemberExpression
167
- ])(node.callee)) {
168
- return false;
169
- }
145
+ ])(node.callee)) return false;
170
146
  return isCreateElement(node.callee, context);
171
147
  };
172
148
  const isCloneElement = isFromPragma("cloneElement");
@@ -174,9 +150,7 @@ const isCloneElementCall = (node, context)=>{
174
150
  if (!ast.isOneOf([
175
151
  ast.NodeType.Identifier,
176
152
  ast.NodeType.MemberExpression
177
- ])(node.callee)) {
178
- return false;
179
- }
153
+ ])(node.callee)) return false;
180
154
  return isCloneElement(node.callee, context);
181
155
  };
182
156
 
@@ -225,9 +199,7 @@ function isChildrenOfCreateElement(node, context) {
225
199
  * @param predicate A predicate to filter the children
226
200
  * @returns `true` if the node has children
227
201
  */ function hasChildren(node, predicate) {
228
- if (typeof predicate === "function") {
229
- return node.children.some(predicate);
230
- }
202
+ if (tools._.isFunction(predicate)) return node.children.some(predicate);
231
203
  return node.children.length > 0;
232
204
  }
233
205
  /**
@@ -253,127 +225,11 @@ function isChildrenOfCreateElement(node, context) {
253
225
  return node.openingElement.name.type === ast.NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
254
226
  }
255
227
 
256
- const hdlAnimation = [
257
- "onAnimationStart",
258
- "onAnimationEnd",
259
- "onAnimationIteration"
260
- ];
261
- const hdlClipboard = [
262
- "onCopy",
263
- "onCut",
264
- "onPaste"
265
- ];
266
- const hdlComposition = [
267
- "onCompositionEnd",
268
- "onCompositionStart",
269
- "onCompositionUpdate"
270
- ];
271
- const hdlFocus = [
272
- "onFocus",
273
- "onBlur"
274
- ];
275
- const hdlForm = [
276
- "onChange",
277
- "onInput",
278
- "onSubmit"
279
- ];
280
- const hdlImage = [
281
- "onLoad",
282
- "onError"
283
- ];
284
- const hdlKeyboard = [
285
- "onKeyDown",
286
- "onKeyPress",
287
- "onKeyUp"
288
- ];
289
- const hdlMedia = [
290
- "onAbort",
291
- "onCanPlay",
292
- "onCanPlayThrough",
293
- "onDurationChange",
294
- "onEmptied",
295
- "onEncrypted",
296
- "onEnded",
297
- "onError",
298
- "onLoadedData",
299
- "onLoadedMetadata",
300
- "onLoadStart",
301
- "onPause",
302
- "onPlay",
303
- "onPlaying",
304
- "onProgress",
305
- "onRateChange",
306
- "onSeeked",
307
- "onSeeking",
308
- "onStalled",
309
- "onSuspend",
310
- "onTimeUpdate",
311
- "onVolumeChange",
312
- "onWaiting"
313
- ];
314
- const hdlMouse = [
315
- "onClick",
316
- "onContextMenu",
317
- "onDblClick",
318
- "onDoubleClick",
319
- "onDrag",
320
- "onDragEnd",
321
- "onDragEnter",
322
- "onDragExit",
323
- "onDragLeave",
324
- "onDragOver",
325
- "onDragStart",
326
- "onDrop",
327
- "onMouseDown",
328
- "onMouseEnter",
329
- "onMouseLeave",
330
- "onMouseMove",
331
- "onMouseOut",
332
- "onMouseOver",
333
- "onMouseUp"
334
- ];
335
- const hdlSelection = [
336
- "onSelect"
337
- ];
338
- const hdlTouch = [
339
- "onTouchCancel",
340
- "onTouchEnd",
341
- "onTouchMove",
342
- "onTouchStart"
343
- ];
344
- const hdlTransition = [
345
- "onTransitionEnd"
346
- ];
347
- const hdlScroll = [
348
- "onScroll"
349
- ];
350
- const hdlWheel = [
351
- "onWheel"
352
- ];
353
- [
354
- ...hdlAnimation,
355
- ...hdlClipboard,
356
- ...hdlComposition,
357
- ...hdlFocus,
358
- ...hdlForm,
359
- ...hdlImage,
360
- ...hdlKeyboard,
361
- ...hdlMedia,
362
- ...hdlMouse,
363
- ...hdlSelection,
364
- ...hdlTouch,
365
- ...hdlTransition,
366
- ...hdlScroll,
367
- ...hdlWheel
368
- ];
369
-
370
228
  const isFragment = (node, pragma, fragment)=>{
371
229
  if (!ast.isOneOf([
372
230
  ast.NodeType.JSXElement,
373
231
  ast.NodeType.JSXFragment
374
- ])(node)) {
375
- return false;
376
- }
232
+ ])(node)) return false;
377
233
  return isFragmentSyntax(node) || isFragmentElement(node, pragma, fragment);
378
234
  };
379
235
  /**
@@ -387,9 +243,7 @@ const isFragment = (node, pragma, fragment)=>{
387
243
  */ function isFragmentElement(node, pragma, fragment) {
388
244
  const { name } = node.openingElement;
389
245
  // <Fragment>
390
- if (name.type === ast.NodeType.JSXIdentifier && name.name === fragment) {
391
- return true;
392
- }
246
+ if (name.type === ast.NodeType.JSXIdentifier && name.name === fragment) return true;
393
247
  // <Pragma.Fragment>
394
248
  return name.type === ast.NodeType.JSXMemberExpression && name.object.type === ast.NodeType.JSXIdentifier && name.object.name === pragma && name.property.name === fragment;
395
249
  }
@@ -426,9 +280,7 @@ const isFragment = (node, pragma, fragment)=>{
426
280
  * @param hint The `JSXValueCheckHint` to use
427
281
  * @returns boolean
428
282
  */ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_CHECK_HINT) {
429
- if (!node) {
430
- return false;
431
- }
283
+ if (!node) return false;
432
284
  return N(node).with({
433
285
  type: ast.NodeType.JSXElement
434
286
  }, tools.F.constTrue).with({
@@ -443,14 +295,10 @@ const isFragment = (node, pragma, fragment)=>{
443
295
  return N(node.value).with(null, ()=>!(hint & JSXValueCheckHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueCheckHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueCheckHint.SkipNumberLiteral)).otherwise(tools.F.constFalse);
444
296
  }).with({
445
297
  type: ast.NodeType.TemplateLiteral
446
- }, ()=>{
447
- return !(hint & JSXValueCheckHint.SkipStringLiteral);
448
- }).with({
298
+ }, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with({
449
299
  type: ast.NodeType.ArrayExpression
450
300
  }, (node)=>{
451
- if (hint & JSXValueCheckHint.StrictArray) {
452
- return node.elements.every((n)=>isJSXValue(n, context, hint));
453
- }
301
+ if (hint & JSXValueCheckHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
454
302
  return node.elements.some((n)=>isJSXValue(n, context, hint));
455
303
  }).with({
456
304
  type: ast.NodeType.ConditionalExpression
@@ -483,20 +331,14 @@ const isFragment = (node, pragma, fragment)=>{
483
331
  }).with({
484
332
  type: ast.NodeType.CallExpression
485
333
  }, (node)=>{
486
- if (hint & JSXValueCheckHint.SkipCreateElement) {
487
- return false;
488
- }
334
+ if (hint & JSXValueCheckHint.SkipCreateElement) return false;
489
335
  return isCreateElementCall(node, context);
490
336
  }).with({
491
337
  type: ast.NodeType.Identifier
492
338
  }, (node)=>{
493
339
  const { name } = node;
494
- if (name === "undefined") {
495
- return !(hint & JSXValueCheckHint.SkipUndefinedLiteral);
496
- }
497
- if (ast.isJSXTagNameExpression(node)) {
498
- return true;
499
- }
340
+ if (name === "undefined") return !(hint & JSXValueCheckHint.SkipUndefinedLiteral);
341
+ if (ast.isJSXTagNameExpression(node)) return true;
500
342
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
501
343
  const maybeVariable = ast.findVariableByNameUpToGlobal(name, initialScope);
502
344
  return tools.F.pipe(maybeVariable, tools.O.flatMap(ast.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint)));
@@ -537,15 +379,9 @@ function getProp(props, propName, context, initialScope) {
537
379
  const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
538
380
  if (attribute.type === ast.NodeType.JSXAttribute && "value" in attribute) {
539
381
  const { value } = attribute;
540
- if (value === null) {
541
- return tools.O.none();
542
- }
543
- if (value.type === ast.NodeType.Literal) {
544
- return tools.O.some(getStaticValue(value, initialScope));
545
- }
546
- if (value.type === ast.NodeType.JSXExpressionContainer) {
547
- return tools.O.some(getStaticValue(value.expression, initialScope));
548
- }
382
+ if (value === null) return tools.O.none();
383
+ if (value.type === ast.NodeType.Literal) return tools.O.some(getStaticValue(value, initialScope));
384
+ if (value.type === ast.NodeType.JSXExpressionContainer) return tools.O.some(getStaticValue(value.expression, initialScope));
549
385
  return tools.O.none();
550
386
  }
551
387
  const { argument } = attribute;
@@ -570,16 +406,10 @@ function getProp(props, propName, context, initialScope) {
570
406
  return N(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
571
407
  const { name } = argument;
572
408
  const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
573
- if (tools.O.isNone(maybeInit)) {
574
- return false;
575
- }
409
+ if (tools.O.isNone(maybeInit)) return false;
576
410
  const init = maybeInit.value;
577
- if (init.type !== ast.NodeType.ObjectExpression) {
578
- return false;
579
- }
580
- if (seenProps.includes(name)) {
581
- return false;
582
- }
411
+ if (init.type !== ast.NodeType.ObjectExpression) return false;
412
+ if (seenProps.includes(name)) return false;
583
413
  return tools.O.isSome(findPropInProperties(init.properties, context, initialScope, [
584
414
  ...seenProps,
585
415
  name
@@ -616,13 +446,9 @@ function getProp(props, propName, context, initialScope) {
616
446
  }, (argument)=>{
617
447
  const { name } = argument;
618
448
  const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
619
- if (tools.O.isNone(maybeInit)) {
620
- return false;
621
- }
449
+ if (tools.O.isNone(maybeInit)) return false;
622
450
  const init = maybeInit.value;
623
- if (!("properties" in init)) {
624
- return false;
625
- }
451
+ if (!("properties" in init)) return false;
626
452
  return tools.O.isSome(findPropInProperties(init.properties, context, initialScope)(propName));
627
453
  }).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
628
454
  return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
@@ -686,9 +512,7 @@ function getProp(props, propName, context, initialScope) {
686
512
  * @param node The AST node to check
687
513
  * @returns `true` if the node is inside a prop's value
688
514
  */ function isInsidePropValue(node) {
689
- if (ast.isStringLiteral(node)) {
690
- return node.parent.type === ast.NodeType.JSXAttribute;
691
- }
515
+ if (ast.isStringLiteral(node)) return node.parent.type === ast.NodeType.JSXAttribute;
692
516
  return tools.O.isSome(traverseUpProp(node, (n)=>n.value?.type === ast.NodeType.JSXExpressionContainer));
693
517
  }
694
518
 
@@ -736,20 +560,6 @@ exports.hasAnyProp = hasAnyProp;
736
560
  exports.hasChildren = hasChildren;
737
561
  exports.hasEveryProp = hasEveryProp;
738
562
  exports.hasProp = hasProp;
739
- exports.hdlAnimation = hdlAnimation;
740
- exports.hdlClipboard = hdlClipboard;
741
- exports.hdlComposition = hdlComposition;
742
- exports.hdlFocus = hdlFocus;
743
- exports.hdlForm = hdlForm;
744
- exports.hdlImage = hdlImage;
745
- exports.hdlKeyboard = hdlKeyboard;
746
- exports.hdlMedia = hdlMedia;
747
- exports.hdlMouse = hdlMouse;
748
- exports.hdlScroll = hdlScroll;
749
- exports.hdlSelection = hdlSelection;
750
- exports.hdlTouch = hdlTouch;
751
- exports.hdlTransition = hdlTransition;
752
- exports.hdlWheel = hdlWheel;
753
563
  exports.isCallFromPragma = isCallFromPragma;
754
564
  exports.isCallFromPragmaMember = isCallFromPragmaMember;
755
565
  exports.isChildOfJSXElement = isChildOfJSXElement;
package/dist/index.d.mts CHANGED
@@ -55,21 +55,6 @@ declare function isJSXElementOfUserDefinedComponent(node: TSESTree.JSXElement):
55
55
  */
56
56
  declare function isJSXElementOfBuiltinComponent(node: TSESTree.JSXElement): boolean;
57
57
 
58
- declare const hdlAnimation: readonly ["onAnimationStart", "onAnimationEnd", "onAnimationIteration"];
59
- declare const hdlClipboard: readonly ["onCopy", "onCut", "onPaste"];
60
- declare const hdlComposition: readonly ["onCompositionEnd", "onCompositionStart", "onCompositionUpdate"];
61
- declare const hdlFocus: readonly ["onFocus", "onBlur"];
62
- declare const hdlForm: readonly ["onChange", "onInput", "onSubmit"];
63
- declare const hdlImage: readonly ["onLoad", "onError"];
64
- declare const hdlKeyboard: readonly ["onKeyDown", "onKeyPress", "onKeyUp"];
65
- declare const hdlMedia: readonly ["onAbort", "onCanPlay", "onCanPlayThrough", "onDurationChange", "onEmptied", "onEncrypted", "onEnded", "onError", "onLoadedData", "onLoadedMetadata", "onLoadStart", "onPause", "onPlay", "onPlaying", "onProgress", "onRateChange", "onSeeked", "onSeeking", "onStalled", "onSuspend", "onTimeUpdate", "onVolumeChange", "onWaiting"];
66
- declare const hdlMouse: readonly ["onClick", "onContextMenu", "onDblClick", "onDoubleClick", "onDrag", "onDragEnd", "onDragEnter", "onDragExit", "onDragLeave", "onDragOver", "onDragStart", "onDrop", "onMouseDown", "onMouseEnter", "onMouseLeave", "onMouseMove", "onMouseOut", "onMouseOver", "onMouseUp"];
67
- declare const hdlSelection: readonly ["onSelect"];
68
- declare const hdlTouch: readonly ["onTouchCancel", "onTouchEnd", "onTouchMove", "onTouchStart"];
69
- declare const hdlTransition: readonly ["onTransitionEnd"];
70
- declare const hdlScroll: readonly ["onScroll"];
71
- declare const hdlWheel: readonly ["onWheel"];
72
-
73
58
  declare const isFragment: (node: TSESTree.Node, pragma: string, fragment: string) => node is TSESTree.JSXElement | TSESTree.JSXFragment;
74
59
  /**
75
60
  * Check if a node is `<></>`
@@ -237,4 +222,4 @@ declare const DEFAULT_JSX_VALUE_CHECK_HINT: bigint;
237
222
  */
238
223
  declare function isJSXValue(node: TSESTree$1.Node | null | undefined, context: RuleContext, hint?: bigint): boolean;
239
224
 
240
- export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
225
+ export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
package/dist/index.d.ts CHANGED
@@ -55,21 +55,6 @@ declare function isJSXElementOfUserDefinedComponent(node: TSESTree.JSXElement):
55
55
  */
56
56
  declare function isJSXElementOfBuiltinComponent(node: TSESTree.JSXElement): boolean;
57
57
 
58
- declare const hdlAnimation: readonly ["onAnimationStart", "onAnimationEnd", "onAnimationIteration"];
59
- declare const hdlClipboard: readonly ["onCopy", "onCut", "onPaste"];
60
- declare const hdlComposition: readonly ["onCompositionEnd", "onCompositionStart", "onCompositionUpdate"];
61
- declare const hdlFocus: readonly ["onFocus", "onBlur"];
62
- declare const hdlForm: readonly ["onChange", "onInput", "onSubmit"];
63
- declare const hdlImage: readonly ["onLoad", "onError"];
64
- declare const hdlKeyboard: readonly ["onKeyDown", "onKeyPress", "onKeyUp"];
65
- declare const hdlMedia: readonly ["onAbort", "onCanPlay", "onCanPlayThrough", "onDurationChange", "onEmptied", "onEncrypted", "onEnded", "onError", "onLoadedData", "onLoadedMetadata", "onLoadStart", "onPause", "onPlay", "onPlaying", "onProgress", "onRateChange", "onSeeked", "onSeeking", "onStalled", "onSuspend", "onTimeUpdate", "onVolumeChange", "onWaiting"];
66
- declare const hdlMouse: readonly ["onClick", "onContextMenu", "onDblClick", "onDoubleClick", "onDrag", "onDragEnd", "onDragEnter", "onDragExit", "onDragLeave", "onDragOver", "onDragStart", "onDrop", "onMouseDown", "onMouseEnter", "onMouseLeave", "onMouseMove", "onMouseOut", "onMouseOver", "onMouseUp"];
67
- declare const hdlSelection: readonly ["onSelect"];
68
- declare const hdlTouch: readonly ["onTouchCancel", "onTouchEnd", "onTouchMove", "onTouchStart"];
69
- declare const hdlTransition: readonly ["onTransitionEnd"];
70
- declare const hdlScroll: readonly ["onScroll"];
71
- declare const hdlWheel: readonly ["onWheel"];
72
-
73
58
  declare const isFragment: (node: TSESTree.Node, pragma: string, fragment: string) => node is TSESTree.JSXElement | TSESTree.JSXFragment;
74
59
  /**
75
60
  * Check if a node is `<></>`
@@ -237,4 +222,4 @@ declare const DEFAULT_JSX_VALUE_CHECK_HINT: bigint;
237
222
  */
238
223
  declare function isJSXValue(node: TSESTree$1.Node | null | undefined, context: RuleContext, hint?: bigint): boolean;
239
224
 
240
- export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
225
+ export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
package/dist/index.js CHANGED
@@ -12,9 +12,7 @@ const RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
12
12
  function getFragmentFromContext(context) {
13
13
  const settings = shared.parseSchema(shared.ESLintSettingsSchema, context.settings);
14
14
  const fragment = settings.eslintReact?.jsx?.fragment;
15
- if (tools._.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) {
16
- return fragment;
17
- }
15
+ if (tools._.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) return fragment;
18
16
  return "Fragment";
19
17
  }
20
18
  const getPragmaFromContext = memo((context)=>{
@@ -31,9 +29,7 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
31
29
  const variables = ast.getVariablesUpToGlobal(initialScope);
32
30
  const maybeVariable = ast.findVariableByName(variableName)(variables);
33
31
  const maybeLatestDef = tools.O.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
34
- if (tools.O.isNone(maybeLatestDef)) {
35
- return false;
36
- }
32
+ if (tools.O.isNone(maybeLatestDef)) return false;
37
33
  const latestDef = maybeLatestDef.value;
38
34
  const { node, parent } = latestDef;
39
35
  if (node.type === ast.NodeType.VariableDeclarator && node.init) {
@@ -45,16 +41,12 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
45
41
  type: "Identifier",
46
42
  name: pragma
47
43
  }
48
- }, init)) {
49
- return true;
50
- }
44
+ }, init)) return true;
51
45
  // check for: `{ variable } = pragma`
52
46
  if (a({
53
47
  type: "Identifier",
54
48
  name: pragma
55
- }, init)) {
56
- return true;
57
- }
49
+ }, init)) return true;
58
50
  // check if from a require call: `require("react")`
59
51
  const maybeRequireExpression = N(init).with({
60
52
  type: ast.NodeType.CallExpression,
@@ -72,14 +64,10 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
72
64
  }
73
65
  }
74
66
  }, ({ object })=>tools.O.some(object)).otherwise(tools.O.none);
75
- if (tools.O.isNone(maybeRequireExpression)) {
76
- return false;
77
- }
67
+ if (tools.O.isNone(maybeRequireExpression)) return false;
78
68
  const requireExpression = maybeRequireExpression.value;
79
69
  const [firstArg] = requireExpression.arguments;
80
- if (firstArg?.type !== ast.NodeType.Literal) {
81
- return false;
82
- }
70
+ if (firstArg?.type !== ast.NodeType.Literal) return false;
83
71
  return firstArg.value === pragma.toLowerCase();
84
72
  }
85
73
  // latest definition is an import declaration: import { variable } from 'react'
@@ -110,12 +98,8 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
110
98
  */ function isFromPragma(name) {
111
99
  return (node, context)=>{
112
100
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
113
- if (node.type === ast.NodeType.MemberExpression) {
114
- return isPropertyOfPragma(name, context)(node);
115
- }
116
- if (node.name === name) {
117
- return isInitializedFromPragma(name, context, initialScope);
118
- }
101
+ if (node.type === ast.NodeType.MemberExpression) return isPropertyOfPragma(name, context)(node);
102
+ if (node.name === name) return isInitializedFromPragma(name, context, initialScope);
119
103
  return false;
120
104
  };
121
105
  }
@@ -127,9 +111,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
127
111
  */ function isFromPragmaMember(pragmaMemberName, name) {
128
112
  return (node, context, pragma = getPragmaFromContext(context))=>{
129
113
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
130
- if (node.property.type !== ast.NodeType.Identifier || node.property.name !== name) {
131
- return false;
132
- }
114
+ if (node.property.type !== ast.NodeType.Identifier || node.property.name !== name) return false;
133
115
  if (node.object.type === ast.NodeType.Identifier && node.object.name === pragmaMemberName) {
134
116
  return isInitializedFromPragma(node.object.name, context, initialScope, pragma);
135
117
  }
@@ -144,17 +126,13 @@ function isCallFromPragma(name) {
144
126
  if (!ast.isOneOf([
145
127
  ast.NodeType.Identifier,
146
128
  ast.NodeType.MemberExpression
147
- ])(node.callee)) {
148
- return false;
149
- }
129
+ ])(node.callee)) return false;
150
130
  return isFromPragma(name)(node.callee, context);
151
131
  };
152
132
  }
153
133
  function isCallFromPragmaMember(pragmaMemberName, name) {
154
134
  return (node, context)=>{
155
- if (!ast.is(ast.NodeType.MemberExpression)(node.callee)) {
156
- return false;
157
- }
135
+ if (!ast.is(ast.NodeType.MemberExpression)(node.callee)) return false;
158
136
  return isFromPragmaMember(pragmaMemberName, name)(node.callee, context);
159
137
  };
160
138
  }
@@ -164,9 +142,7 @@ const isCreateElementCall = (node, context)=>{
164
142
  if (!ast.isOneOf([
165
143
  ast.NodeType.Identifier,
166
144
  ast.NodeType.MemberExpression
167
- ])(node.callee)) {
168
- return false;
169
- }
145
+ ])(node.callee)) return false;
170
146
  return isCreateElement(node.callee, context);
171
147
  };
172
148
  const isCloneElement = isFromPragma("cloneElement");
@@ -174,9 +150,7 @@ const isCloneElementCall = (node, context)=>{
174
150
  if (!ast.isOneOf([
175
151
  ast.NodeType.Identifier,
176
152
  ast.NodeType.MemberExpression
177
- ])(node.callee)) {
178
- return false;
179
- }
153
+ ])(node.callee)) return false;
180
154
  return isCloneElement(node.callee, context);
181
155
  };
182
156
 
@@ -225,9 +199,7 @@ function isChildrenOfCreateElement(node, context) {
225
199
  * @param predicate A predicate to filter the children
226
200
  * @returns `true` if the node has children
227
201
  */ function hasChildren(node, predicate) {
228
- if (typeof predicate === "function") {
229
- return node.children.some(predicate);
230
- }
202
+ if (tools._.isFunction(predicate)) return node.children.some(predicate);
231
203
  return node.children.length > 0;
232
204
  }
233
205
  /**
@@ -253,127 +225,11 @@ function isChildrenOfCreateElement(node, context) {
253
225
  return node.openingElement.name.type === ast.NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
254
226
  }
255
227
 
256
- const hdlAnimation = [
257
- "onAnimationStart",
258
- "onAnimationEnd",
259
- "onAnimationIteration"
260
- ];
261
- const hdlClipboard = [
262
- "onCopy",
263
- "onCut",
264
- "onPaste"
265
- ];
266
- const hdlComposition = [
267
- "onCompositionEnd",
268
- "onCompositionStart",
269
- "onCompositionUpdate"
270
- ];
271
- const hdlFocus = [
272
- "onFocus",
273
- "onBlur"
274
- ];
275
- const hdlForm = [
276
- "onChange",
277
- "onInput",
278
- "onSubmit"
279
- ];
280
- const hdlImage = [
281
- "onLoad",
282
- "onError"
283
- ];
284
- const hdlKeyboard = [
285
- "onKeyDown",
286
- "onKeyPress",
287
- "onKeyUp"
288
- ];
289
- const hdlMedia = [
290
- "onAbort",
291
- "onCanPlay",
292
- "onCanPlayThrough",
293
- "onDurationChange",
294
- "onEmptied",
295
- "onEncrypted",
296
- "onEnded",
297
- "onError",
298
- "onLoadedData",
299
- "onLoadedMetadata",
300
- "onLoadStart",
301
- "onPause",
302
- "onPlay",
303
- "onPlaying",
304
- "onProgress",
305
- "onRateChange",
306
- "onSeeked",
307
- "onSeeking",
308
- "onStalled",
309
- "onSuspend",
310
- "onTimeUpdate",
311
- "onVolumeChange",
312
- "onWaiting"
313
- ];
314
- const hdlMouse = [
315
- "onClick",
316
- "onContextMenu",
317
- "onDblClick",
318
- "onDoubleClick",
319
- "onDrag",
320
- "onDragEnd",
321
- "onDragEnter",
322
- "onDragExit",
323
- "onDragLeave",
324
- "onDragOver",
325
- "onDragStart",
326
- "onDrop",
327
- "onMouseDown",
328
- "onMouseEnter",
329
- "onMouseLeave",
330
- "onMouseMove",
331
- "onMouseOut",
332
- "onMouseOver",
333
- "onMouseUp"
334
- ];
335
- const hdlSelection = [
336
- "onSelect"
337
- ];
338
- const hdlTouch = [
339
- "onTouchCancel",
340
- "onTouchEnd",
341
- "onTouchMove",
342
- "onTouchStart"
343
- ];
344
- const hdlTransition = [
345
- "onTransitionEnd"
346
- ];
347
- const hdlScroll = [
348
- "onScroll"
349
- ];
350
- const hdlWheel = [
351
- "onWheel"
352
- ];
353
- [
354
- ...hdlAnimation,
355
- ...hdlClipboard,
356
- ...hdlComposition,
357
- ...hdlFocus,
358
- ...hdlForm,
359
- ...hdlImage,
360
- ...hdlKeyboard,
361
- ...hdlMedia,
362
- ...hdlMouse,
363
- ...hdlSelection,
364
- ...hdlTouch,
365
- ...hdlTransition,
366
- ...hdlScroll,
367
- ...hdlWheel
368
- ];
369
-
370
228
  const isFragment = (node, pragma, fragment)=>{
371
229
  if (!ast.isOneOf([
372
230
  ast.NodeType.JSXElement,
373
231
  ast.NodeType.JSXFragment
374
- ])(node)) {
375
- return false;
376
- }
232
+ ])(node)) return false;
377
233
  return isFragmentSyntax(node) || isFragmentElement(node, pragma, fragment);
378
234
  };
379
235
  /**
@@ -387,9 +243,7 @@ const isFragment = (node, pragma, fragment)=>{
387
243
  */ function isFragmentElement(node, pragma, fragment) {
388
244
  const { name } = node.openingElement;
389
245
  // <Fragment>
390
- if (name.type === ast.NodeType.JSXIdentifier && name.name === fragment) {
391
- return true;
392
- }
246
+ if (name.type === ast.NodeType.JSXIdentifier && name.name === fragment) return true;
393
247
  // <Pragma.Fragment>
394
248
  return name.type === ast.NodeType.JSXMemberExpression && name.object.type === ast.NodeType.JSXIdentifier && name.object.name === pragma && name.property.name === fragment;
395
249
  }
@@ -426,9 +280,7 @@ const isFragment = (node, pragma, fragment)=>{
426
280
  * @param hint The `JSXValueCheckHint` to use
427
281
  * @returns boolean
428
282
  */ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_CHECK_HINT) {
429
- if (!node) {
430
- return false;
431
- }
283
+ if (!node) return false;
432
284
  return N(node).with({
433
285
  type: ast.NodeType.JSXElement
434
286
  }, tools.F.constTrue).with({
@@ -443,14 +295,10 @@ const isFragment = (node, pragma, fragment)=>{
443
295
  return N(node.value).with(null, ()=>!(hint & JSXValueCheckHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueCheckHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueCheckHint.SkipNumberLiteral)).otherwise(tools.F.constFalse);
444
296
  }).with({
445
297
  type: ast.NodeType.TemplateLiteral
446
- }, ()=>{
447
- return !(hint & JSXValueCheckHint.SkipStringLiteral);
448
- }).with({
298
+ }, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with({
449
299
  type: ast.NodeType.ArrayExpression
450
300
  }, (node)=>{
451
- if (hint & JSXValueCheckHint.StrictArray) {
452
- return node.elements.every((n)=>isJSXValue(n, context, hint));
453
- }
301
+ if (hint & JSXValueCheckHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
454
302
  return node.elements.some((n)=>isJSXValue(n, context, hint));
455
303
  }).with({
456
304
  type: ast.NodeType.ConditionalExpression
@@ -483,20 +331,14 @@ const isFragment = (node, pragma, fragment)=>{
483
331
  }).with({
484
332
  type: ast.NodeType.CallExpression
485
333
  }, (node)=>{
486
- if (hint & JSXValueCheckHint.SkipCreateElement) {
487
- return false;
488
- }
334
+ if (hint & JSXValueCheckHint.SkipCreateElement) return false;
489
335
  return isCreateElementCall(node, context);
490
336
  }).with({
491
337
  type: ast.NodeType.Identifier
492
338
  }, (node)=>{
493
339
  const { name } = node;
494
- if (name === "undefined") {
495
- return !(hint & JSXValueCheckHint.SkipUndefinedLiteral);
496
- }
497
- if (ast.isJSXTagNameExpression(node)) {
498
- return true;
499
- }
340
+ if (name === "undefined") return !(hint & JSXValueCheckHint.SkipUndefinedLiteral);
341
+ if (ast.isJSXTagNameExpression(node)) return true;
500
342
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
501
343
  const maybeVariable = ast.findVariableByNameUpToGlobal(name, initialScope);
502
344
  return tools.F.pipe(maybeVariable, tools.O.flatMap(ast.getVariableInit(0)), tools.O.exists((n)=>isJSXValue(n, context, hint)));
@@ -537,15 +379,9 @@ function getProp(props, propName, context, initialScope) {
537
379
  const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
538
380
  if (attribute.type === ast.NodeType.JSXAttribute && "value" in attribute) {
539
381
  const { value } = attribute;
540
- if (value === null) {
541
- return tools.O.none();
542
- }
543
- if (value.type === ast.NodeType.Literal) {
544
- return tools.O.some(getStaticValue(value, initialScope));
545
- }
546
- if (value.type === ast.NodeType.JSXExpressionContainer) {
547
- return tools.O.some(getStaticValue(value.expression, initialScope));
548
- }
382
+ if (value === null) return tools.O.none();
383
+ if (value.type === ast.NodeType.Literal) return tools.O.some(getStaticValue(value, initialScope));
384
+ if (value.type === ast.NodeType.JSXExpressionContainer) return tools.O.some(getStaticValue(value.expression, initialScope));
549
385
  return tools.O.none();
550
386
  }
551
387
  const { argument } = attribute;
@@ -570,16 +406,10 @@ function getProp(props, propName, context, initialScope) {
570
406
  return N(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
571
407
  const { name } = argument;
572
408
  const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
573
- if (tools.O.isNone(maybeInit)) {
574
- return false;
575
- }
409
+ if (tools.O.isNone(maybeInit)) return false;
576
410
  const init = maybeInit.value;
577
- if (init.type !== ast.NodeType.ObjectExpression) {
578
- return false;
579
- }
580
- if (seenProps.includes(name)) {
581
- return false;
582
- }
411
+ if (init.type !== ast.NodeType.ObjectExpression) return false;
412
+ if (seenProps.includes(name)) return false;
583
413
  return tools.O.isSome(findPropInProperties(init.properties, context, initialScope, [
584
414
  ...seenProps,
585
415
  name
@@ -616,13 +446,9 @@ function getProp(props, propName, context, initialScope) {
616
446
  }, (argument)=>{
617
447
  const { name } = argument;
618
448
  const maybeInit = tools.O.flatMap(ast.findVariableByNameUpToGlobal(name, initialScope), ast.getVariableInit(0));
619
- if (tools.O.isNone(maybeInit)) {
620
- return false;
621
- }
449
+ if (tools.O.isNone(maybeInit)) return false;
622
450
  const init = maybeInit.value;
623
- if (!("properties" in init)) {
624
- return false;
625
- }
451
+ if (!("properties" in init)) return false;
626
452
  return tools.O.isSome(findPropInProperties(init.properties, context, initialScope)(propName));
627
453
  }).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
628
454
  return tools.O.isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
@@ -686,9 +512,7 @@ function getProp(props, propName, context, initialScope) {
686
512
  * @param node The AST node to check
687
513
  * @returns `true` if the node is inside a prop's value
688
514
  */ function isInsidePropValue(node) {
689
- if (ast.isStringLiteral(node)) {
690
- return node.parent.type === ast.NodeType.JSXAttribute;
691
- }
515
+ if (ast.isStringLiteral(node)) return node.parent.type === ast.NodeType.JSXAttribute;
692
516
  return tools.O.isSome(traverseUpProp(node, (n)=>n.value?.type === ast.NodeType.JSXExpressionContainer));
693
517
  }
694
518
 
@@ -736,20 +560,6 @@ exports.hasAnyProp = hasAnyProp;
736
560
  exports.hasChildren = hasChildren;
737
561
  exports.hasEveryProp = hasEveryProp;
738
562
  exports.hasProp = hasProp;
739
- exports.hdlAnimation = hdlAnimation;
740
- exports.hdlClipboard = hdlClipboard;
741
- exports.hdlComposition = hdlComposition;
742
- exports.hdlFocus = hdlFocus;
743
- exports.hdlForm = hdlForm;
744
- exports.hdlImage = hdlImage;
745
- exports.hdlKeyboard = hdlKeyboard;
746
- exports.hdlMedia = hdlMedia;
747
- exports.hdlMouse = hdlMouse;
748
- exports.hdlScroll = hdlScroll;
749
- exports.hdlSelection = hdlSelection;
750
- exports.hdlTouch = hdlTouch;
751
- exports.hdlTransition = hdlTransition;
752
- exports.hdlWheel = hdlWheel;
753
563
  exports.isCallFromPragma = isCallFromPragma;
754
564
  exports.isCallFromPragmaMember = isCallFromPragmaMember;
755
565
  exports.isChildOfJSXElement = isChildOfJSXElement;
package/dist/index.mjs CHANGED
@@ -10,9 +10,7 @@ const RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
10
10
  function getFragmentFromContext(context) {
11
11
  const settings = parseSchema(ESLintSettingsSchema, context.settings);
12
12
  const fragment = settings.eslintReact?.jsx?.fragment;
13
- if (_$1.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) {
14
- return fragment;
15
- }
13
+ if (_$1.isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) return fragment;
16
14
  return "Fragment";
17
15
  }
18
16
  const getPragmaFromContext = memo((context)=>{
@@ -29,9 +27,7 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
29
27
  const variables = getVariablesUpToGlobal(initialScope);
30
28
  const maybeVariable = findVariableByName(variableName)(variables);
31
29
  const maybeLatestDef = O$1.flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
32
- if (O$1.isNone(maybeLatestDef)) {
33
- return false;
34
- }
30
+ if (O$1.isNone(maybeLatestDef)) return false;
35
31
  const latestDef = maybeLatestDef.value;
36
32
  const { node, parent } = latestDef;
37
33
  if (node.type === NodeType.VariableDeclarator && node.init) {
@@ -43,16 +39,12 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
43
39
  type: "Identifier",
44
40
  name: pragma
45
41
  }
46
- }, init)) {
47
- return true;
48
- }
42
+ }, init)) return true;
49
43
  // check for: `{ variable } = pragma`
50
44
  if (a({
51
45
  type: "Identifier",
52
46
  name: pragma
53
- }, init)) {
54
- return true;
55
- }
47
+ }, init)) return true;
56
48
  // check if from a require call: `require("react")`
57
49
  const maybeRequireExpression = N(init).with({
58
50
  type: NodeType.CallExpression,
@@ -70,14 +62,10 @@ function isInitializedFromPragma(variableName, context, initialScope, pragma = g
70
62
  }
71
63
  }
72
64
  }, ({ object })=>O$1.some(object)).otherwise(O$1.none);
73
- if (O$1.isNone(maybeRequireExpression)) {
74
- return false;
75
- }
65
+ if (O$1.isNone(maybeRequireExpression)) return false;
76
66
  const requireExpression = maybeRequireExpression.value;
77
67
  const [firstArg] = requireExpression.arguments;
78
- if (firstArg?.type !== NodeType.Literal) {
79
- return false;
80
- }
68
+ if (firstArg?.type !== NodeType.Literal) return false;
81
69
  return firstArg.value === pragma.toLowerCase();
82
70
  }
83
71
  // latest definition is an import declaration: import { variable } from 'react'
@@ -108,12 +96,8 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
108
96
  */ function isFromPragma(name) {
109
97
  return (node, context)=>{
110
98
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
111
- if (node.type === NodeType.MemberExpression) {
112
- return isPropertyOfPragma(name, context)(node);
113
- }
114
- if (node.name === name) {
115
- return isInitializedFromPragma(name, context, initialScope);
116
- }
99
+ if (node.type === NodeType.MemberExpression) return isPropertyOfPragma(name, context)(node);
100
+ if (node.name === name) return isInitializedFromPragma(name, context, initialScope);
117
101
  return false;
118
102
  };
119
103
  }
@@ -125,9 +109,7 @@ function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context
125
109
  */ function isFromPragmaMember(pragmaMemberName, name) {
126
110
  return (node, context, pragma = getPragmaFromContext(context))=>{
127
111
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
128
- if (node.property.type !== NodeType.Identifier || node.property.name !== name) {
129
- return false;
130
- }
112
+ if (node.property.type !== NodeType.Identifier || node.property.name !== name) return false;
131
113
  if (node.object.type === NodeType.Identifier && node.object.name === pragmaMemberName) {
132
114
  return isInitializedFromPragma(node.object.name, context, initialScope, pragma);
133
115
  }
@@ -142,17 +124,13 @@ function isCallFromPragma(name) {
142
124
  if (!isOneOf([
143
125
  NodeType.Identifier,
144
126
  NodeType.MemberExpression
145
- ])(node.callee)) {
146
- return false;
147
- }
127
+ ])(node.callee)) return false;
148
128
  return isFromPragma(name)(node.callee, context);
149
129
  };
150
130
  }
151
131
  function isCallFromPragmaMember(pragmaMemberName, name) {
152
132
  return (node, context)=>{
153
- if (!is(NodeType.MemberExpression)(node.callee)) {
154
- return false;
155
- }
133
+ if (!is(NodeType.MemberExpression)(node.callee)) return false;
156
134
  return isFromPragmaMember(pragmaMemberName, name)(node.callee, context);
157
135
  };
158
136
  }
@@ -162,9 +140,7 @@ const isCreateElementCall = (node, context)=>{
162
140
  if (!isOneOf([
163
141
  NodeType.Identifier,
164
142
  NodeType.MemberExpression
165
- ])(node.callee)) {
166
- return false;
167
- }
143
+ ])(node.callee)) return false;
168
144
  return isCreateElement(node.callee, context);
169
145
  };
170
146
  const isCloneElement = isFromPragma("cloneElement");
@@ -172,9 +148,7 @@ const isCloneElementCall = (node, context)=>{
172
148
  if (!isOneOf([
173
149
  NodeType.Identifier,
174
150
  NodeType.MemberExpression
175
- ])(node.callee)) {
176
- return false;
177
- }
151
+ ])(node.callee)) return false;
178
152
  return isCloneElement(node.callee, context);
179
153
  };
180
154
 
@@ -223,9 +197,7 @@ function isChildrenOfCreateElement(node, context) {
223
197
  * @param predicate A predicate to filter the children
224
198
  * @returns `true` if the node has children
225
199
  */ function hasChildren(node, predicate) {
226
- if (typeof predicate === "function") {
227
- return node.children.some(predicate);
228
- }
200
+ if (_$1.isFunction(predicate)) return node.children.some(predicate);
229
201
  return node.children.length > 0;
230
202
  }
231
203
  /**
@@ -251,127 +223,11 @@ function isChildrenOfCreateElement(node, context) {
251
223
  return node.openingElement.name.type === NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
252
224
  }
253
225
 
254
- const hdlAnimation = [
255
- "onAnimationStart",
256
- "onAnimationEnd",
257
- "onAnimationIteration"
258
- ];
259
- const hdlClipboard = [
260
- "onCopy",
261
- "onCut",
262
- "onPaste"
263
- ];
264
- const hdlComposition = [
265
- "onCompositionEnd",
266
- "onCompositionStart",
267
- "onCompositionUpdate"
268
- ];
269
- const hdlFocus = [
270
- "onFocus",
271
- "onBlur"
272
- ];
273
- const hdlForm = [
274
- "onChange",
275
- "onInput",
276
- "onSubmit"
277
- ];
278
- const hdlImage = [
279
- "onLoad",
280
- "onError"
281
- ];
282
- const hdlKeyboard = [
283
- "onKeyDown",
284
- "onKeyPress",
285
- "onKeyUp"
286
- ];
287
- const hdlMedia = [
288
- "onAbort",
289
- "onCanPlay",
290
- "onCanPlayThrough",
291
- "onDurationChange",
292
- "onEmptied",
293
- "onEncrypted",
294
- "onEnded",
295
- "onError",
296
- "onLoadedData",
297
- "onLoadedMetadata",
298
- "onLoadStart",
299
- "onPause",
300
- "onPlay",
301
- "onPlaying",
302
- "onProgress",
303
- "onRateChange",
304
- "onSeeked",
305
- "onSeeking",
306
- "onStalled",
307
- "onSuspend",
308
- "onTimeUpdate",
309
- "onVolumeChange",
310
- "onWaiting"
311
- ];
312
- const hdlMouse = [
313
- "onClick",
314
- "onContextMenu",
315
- "onDblClick",
316
- "onDoubleClick",
317
- "onDrag",
318
- "onDragEnd",
319
- "onDragEnter",
320
- "onDragExit",
321
- "onDragLeave",
322
- "onDragOver",
323
- "onDragStart",
324
- "onDrop",
325
- "onMouseDown",
326
- "onMouseEnter",
327
- "onMouseLeave",
328
- "onMouseMove",
329
- "onMouseOut",
330
- "onMouseOver",
331
- "onMouseUp"
332
- ];
333
- const hdlSelection = [
334
- "onSelect"
335
- ];
336
- const hdlTouch = [
337
- "onTouchCancel",
338
- "onTouchEnd",
339
- "onTouchMove",
340
- "onTouchStart"
341
- ];
342
- const hdlTransition = [
343
- "onTransitionEnd"
344
- ];
345
- const hdlScroll = [
346
- "onScroll"
347
- ];
348
- const hdlWheel = [
349
- "onWheel"
350
- ];
351
- [
352
- ...hdlAnimation,
353
- ...hdlClipboard,
354
- ...hdlComposition,
355
- ...hdlFocus,
356
- ...hdlForm,
357
- ...hdlImage,
358
- ...hdlKeyboard,
359
- ...hdlMedia,
360
- ...hdlMouse,
361
- ...hdlSelection,
362
- ...hdlTouch,
363
- ...hdlTransition,
364
- ...hdlScroll,
365
- ...hdlWheel
366
- ];
367
-
368
226
  const isFragment = (node, pragma, fragment)=>{
369
227
  if (!isOneOf([
370
228
  NodeType.JSXElement,
371
229
  NodeType.JSXFragment
372
- ])(node)) {
373
- return false;
374
- }
230
+ ])(node)) return false;
375
231
  return isFragmentSyntax(node) || isFragmentElement(node, pragma, fragment);
376
232
  };
377
233
  /**
@@ -385,9 +241,7 @@ const isFragment = (node, pragma, fragment)=>{
385
241
  */ function isFragmentElement(node, pragma, fragment) {
386
242
  const { name } = node.openingElement;
387
243
  // <Fragment>
388
- if (name.type === NodeType.JSXIdentifier && name.name === fragment) {
389
- return true;
390
- }
244
+ if (name.type === NodeType.JSXIdentifier && name.name === fragment) return true;
391
245
  // <Pragma.Fragment>
392
246
  return name.type === NodeType.JSXMemberExpression && name.object.type === NodeType.JSXIdentifier && name.object.name === pragma && name.property.name === fragment;
393
247
  }
@@ -424,9 +278,7 @@ const isFragment = (node, pragma, fragment)=>{
424
278
  * @param hint The `JSXValueCheckHint` to use
425
279
  * @returns boolean
426
280
  */ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_CHECK_HINT) {
427
- if (!node) {
428
- return false;
429
- }
281
+ if (!node) return false;
430
282
  return N(node).with({
431
283
  type: NodeType.JSXElement
432
284
  }, F.constTrue).with({
@@ -441,14 +293,10 @@ const isFragment = (node, pragma, fragment)=>{
441
293
  return N(node.value).with(null, ()=>!(hint & JSXValueCheckHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueCheckHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueCheckHint.SkipNumberLiteral)).otherwise(F.constFalse);
442
294
  }).with({
443
295
  type: NodeType.TemplateLiteral
444
- }, ()=>{
445
- return !(hint & JSXValueCheckHint.SkipStringLiteral);
446
- }).with({
296
+ }, ()=>!(hint & JSXValueCheckHint.SkipStringLiteral)).with({
447
297
  type: NodeType.ArrayExpression
448
298
  }, (node)=>{
449
- if (hint & JSXValueCheckHint.StrictArray) {
450
- return node.elements.every((n)=>isJSXValue(n, context, hint));
451
- }
299
+ if (hint & JSXValueCheckHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
452
300
  return node.elements.some((n)=>isJSXValue(n, context, hint));
453
301
  }).with({
454
302
  type: NodeType.ConditionalExpression
@@ -481,20 +329,14 @@ const isFragment = (node, pragma, fragment)=>{
481
329
  }).with({
482
330
  type: NodeType.CallExpression
483
331
  }, (node)=>{
484
- if (hint & JSXValueCheckHint.SkipCreateElement) {
485
- return false;
486
- }
332
+ if (hint & JSXValueCheckHint.SkipCreateElement) return false;
487
333
  return isCreateElementCall(node, context);
488
334
  }).with({
489
335
  type: NodeType.Identifier
490
336
  }, (node)=>{
491
337
  const { name } = node;
492
- if (name === "undefined") {
493
- return !(hint & JSXValueCheckHint.SkipUndefinedLiteral);
494
- }
495
- if (isJSXTagNameExpression(node)) {
496
- return true;
497
- }
338
+ if (name === "undefined") return !(hint & JSXValueCheckHint.SkipUndefinedLiteral);
339
+ if (isJSXTagNameExpression(node)) return true;
498
340
  const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
499
341
  const maybeVariable = findVariableByNameUpToGlobal(name, initialScope);
500
342
  return F.pipe(maybeVariable, O$1.flatMap(getVariableInit(0)), O$1.exists((n)=>isJSXValue(n, context, hint)));
@@ -535,15 +377,9 @@ function getProp(props, propName, context, initialScope) {
535
377
  const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
536
378
  if (attribute.type === NodeType.JSXAttribute && "value" in attribute) {
537
379
  const { value } = attribute;
538
- if (value === null) {
539
- return O$1.none();
540
- }
541
- if (value.type === NodeType.Literal) {
542
- return O$1.some(getStaticValue(value, initialScope));
543
- }
544
- if (value.type === NodeType.JSXExpressionContainer) {
545
- return O$1.some(getStaticValue(value.expression, initialScope));
546
- }
380
+ if (value === null) return O$1.none();
381
+ if (value.type === NodeType.Literal) return O$1.some(getStaticValue(value, initialScope));
382
+ if (value.type === NodeType.JSXExpressionContainer) return O$1.some(getStaticValue(value.expression, initialScope));
547
383
  return O$1.none();
548
384
  }
549
385
  const { argument } = attribute;
@@ -568,16 +404,10 @@ function getProp(props, propName, context, initialScope) {
568
404
  return N(prop.argument).when(is(NodeType.Identifier), (argument)=>{
569
405
  const { name } = argument;
570
406
  const maybeInit = O$1.flatMap(findVariableByNameUpToGlobal(name, initialScope), getVariableInit(0));
571
- if (O$1.isNone(maybeInit)) {
572
- return false;
573
- }
407
+ if (O$1.isNone(maybeInit)) return false;
574
408
  const init = maybeInit.value;
575
- if (init.type !== NodeType.ObjectExpression) {
576
- return false;
577
- }
578
- if (seenProps.includes(name)) {
579
- return false;
580
- }
409
+ if (init.type !== NodeType.ObjectExpression) return false;
410
+ if (seenProps.includes(name)) return false;
581
411
  return O$1.isSome(findPropInProperties(init.properties, context, initialScope, [
582
412
  ...seenProps,
583
413
  name
@@ -614,13 +444,9 @@ function getProp(props, propName, context, initialScope) {
614
444
  }, (argument)=>{
615
445
  const { name } = argument;
616
446
  const maybeInit = O$1.flatMap(findVariableByNameUpToGlobal(name, initialScope), getVariableInit(0));
617
- if (O$1.isNone(maybeInit)) {
618
- return false;
619
- }
447
+ if (O$1.isNone(maybeInit)) return false;
620
448
  const init = maybeInit.value;
621
- if (!("properties" in init)) {
622
- return false;
623
- }
449
+ if (!("properties" in init)) return false;
624
450
  return O$1.isSome(findPropInProperties(init.properties, context, initialScope)(propName));
625
451
  }).when(is(NodeType.ObjectExpression), (argument)=>{
626
452
  return O$1.isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
@@ -684,9 +510,7 @@ function getProp(props, propName, context, initialScope) {
684
510
  * @param node The AST node to check
685
511
  * @returns `true` if the node is inside a prop's value
686
512
  */ function isInsidePropValue(node) {
687
- if (isStringLiteral(node)) {
688
- return node.parent.type === NodeType.JSXAttribute;
689
- }
513
+ if (isStringLiteral(node)) return node.parent.type === NodeType.JSXAttribute;
690
514
  return O$1.isSome(traverseUpProp(node, (n)=>n.value?.type === NodeType.JSXExpressionContainer));
691
515
  }
692
516
 
@@ -720,4 +544,4 @@ function getProp(props, propName, context, initialScope) {
720
544
  return isLiteral(node) && isWhiteSpace(node) && node.raw.includes("\n");
721
545
  }
722
546
 
723
- export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, hdlAnimation, hdlClipboard, hdlComposition, hdlFocus, hdlForm, hdlImage, hdlKeyboard, hdlMedia, hdlMouse, hdlScroll, hdlSelection, hdlTouch, hdlTransition, hdlWheel, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
547
+ export { DEFAULT_JSX_VALUE_CHECK_HINT, JSXValueCheckHint, elementType, findPropInAttributes, findPropInProperties, getFragmentFromContext, getPragmaFromContext, getProp, getPropName, getPropValue, hasAnyProp, hasChildren, hasEveryProp, hasProp, isCallFromPragma, isCallFromPragmaMember, isChildOfJSXElement, isChildrenOfCreateElement, isCloneElement, isCloneElementCall, isCreateElement, isCreateElementCall, isFragment, isFragmentElement, isFragmentSyntax, isFromPragma, isFromPragmaMember, isFunctionReturningJSXValue, isInitializedFromPragma, isInsideCreateElementProps, isInsidePropValue, isJSXElementOfBuiltinComponent, isJSXElementOfUserDefinedComponent, isJSXValue, isLineBreak, isLiteral, isPaddingSpaces, isPropertyOfPragma, isWhiteSpace, traverseUpProp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/jsx",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "ESLint React's TSESTree AST utility module for static analysis of JSX.",
5
5
  "homepage": "https://github.com/rel1cx/eslint-react",
6
6
  "bugs": {
@@ -35,14 +35,14 @@
35
35
  "./package.json"
36
36
  ],
37
37
  "dependencies": {
38
- "@typescript-eslint/scope-manager": "6.16.0",
39
- "@typescript-eslint/types": "6.16.0",
40
- "@typescript-eslint/utils": "6.16.0",
38
+ "@typescript-eslint/scope-manager": "6.17.0",
39
+ "@typescript-eslint/types": "6.17.0",
40
+ "@typescript-eslint/utils": "6.17.0",
41
41
  "micro-memoize": "4.1.2",
42
- "@eslint-react/ast": "0.10.2",
43
- "@eslint-react/shared": "0.10.2",
44
- "@eslint-react/tools": "0.10.2",
45
- "@eslint-react/types": "0.10.2"
42
+ "@eslint-react/ast": "0.10.3",
43
+ "@eslint-react/shared": "0.10.3",
44
+ "@eslint-react/tools": "0.10.3",
45
+ "@eslint-react/types": "0.10.3"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rollup -c rollup.config.ts --configPlugin swc3 && cp dist/index.d.ts dist/index.d.mts",