Skip to content

Commit 7cd4efd

Browse files
committed
Disregard interactive props with null values
1 parent 20cda71 commit 7cd4efd

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

__tests__/src/rules/no-noninteractive-element-interactions-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ ruleTester.run('no-noninteractive-element-handlers', rule, {
103103
{ code: '<div onClick={() => void 0} />;' },
104104
{ code: '<div onClick={() => void 0} role={undefined} />;' },
105105
{ code: '<div onClick={() => void 0} {...props} />;' },
106+
{ code: '<div onClick={null} />;' },
106107
{ code: '<div onKeyUp={() => void 0} aria-hidden={false} />;' },
107108
{ code: '<dl onClick={() => {}} />;' },
108109
{ code: '<em onClick={() => {}} />;' },
@@ -123,6 +124,7 @@ ruleTester.run('no-noninteractive-element-handlers', rule, {
123124
{ code: '<label onClick={() => {}} />;' },
124125
{ code: '<legend onClick={() => {}} />;' },
125126
{ code: '<link onClick={() => {}} />;' },
127+
{ code: '<main onClick={null} />;' },
126128
{ code: '<map onClick={() => {}} />;' },
127129
{ code: '<mark onClick={() => {}} />;' },
128130
{ code: '<marquee onClick={() => {}} />;' },

__tests__/src/rules/no-static-element-interactions-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ ruleTester.run('no-static-element-interactions', rule, {
3535
{ code: '<div className="foo" {...props} />;' },
3636
{ code: '<div onClick={() => void 0} aria-hidden />;' },
3737
{ code: '<div onClick={() => void 0} aria-hidden={true} />;' },
38+
{ code: '<div onClick={null} />;' },
3839
/* All flavors of input */
3940
{ code: '<input onClick={() => void 0} />' },
4041
{ code: '<input type="button" onClick={() => void 0} />' },

src/rules/no-noninteractive-element-interactions.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
import {
1414
elementType,
1515
eventHandlersByType,
16-
hasAnyProp,
16+
getPropValue,
17+
getProp,
18+
hasProp,
1719
} from 'jsx-ast-utils';
1820
import type { JSXOpeningElement } from 'ast-types-flow';
1921
import { generateObjSchema } from '../util/schemas';
@@ -49,7 +51,11 @@ module.exports = {
4951
const attributes = node.attributes;
5052
const type = elementType(node);
5153

52-
const hasInteractiveProps = hasAnyProp(attributes, interactiveProps);
54+
const hasInteractiveProps = interactiveProps
55+
.some(prop => (
56+
hasProp(attributes, prop)
57+
&& getPropValue(getProp(attributes, prop)) != null
58+
));
5359

5460
if (!domElements.includes(type)) {
5561
// Do not test higher level JSX components, as we do not know what

src/rules/no-static-element-interactions.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {
1414
import {
1515
elementType,
1616
eventHandlersByType,
17-
hasAnyProp,
17+
getPropValue,
18+
getProp,
19+
hasProp,
1820
} from 'jsx-ast-utils';
1921
import type { JSXOpeningElement } from 'ast-types-flow';
2022
import { generateObjSchema } from '../util/schemas';
@@ -50,7 +52,11 @@ module.exports = {
5052
const attributes = node.attributes;
5153
const type = elementType(node);
5254

53-
const hasInteractiveProps = hasAnyProp(attributes, interactiveProps);
55+
const hasInteractiveProps = interactiveProps
56+
.some(prop => (
57+
hasProp(attributes, prop)
58+
&& getPropValue(getProp(attributes, prop)) != null
59+
));
5460

5561
if (!domElements.includes(type)) {
5662
// Do not test higher level JSX components, as we do not know what

0 commit comments

Comments
 (0)