Skip to content

Commit

Permalink
[Fix] checked-requires-onchange-or-readonly: correct options that wer…
Browse files Browse the repository at this point in the history
…e behaving opposite
  • Loading branch information
jaesoekjjang committed Mar 15, 2024
1 parent da1013c commit 14b0175
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/rules/checked-requires-onchange-or-readonly.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const messages = {
const targetPropSet = new Set(['checked', 'onChange', 'readOnly', 'defaultChecked']);

const defaultOptions = {
ignoreMissingProperties: true,
ignoreExclusiveCheckedAttribute: true,
ignoreMissingProperties: false,
ignoreExclusiveCheckedAttribute: false,
};

/**
Expand Down Expand Up @@ -93,12 +93,12 @@ module.exports = {
return;
}

if (options.ignoreExclusiveCheckedAttribute && propSet.has('defaultChecked')) {
if (!options.ignoreExclusiveCheckedAttribute && propSet.has('defaultChecked')) {
reportExclusiveCheckedAttribute(node);
}

if (
options.ignoreMissingProperties
!options.ignoreMissingProperties
&& !(propSet.has('onChange') || propSet.has('readOnly'))
) {
reportMissingProperty(node);
Expand Down
24 changes: 18 additions & 6 deletions tests/lib/rules/checked-requires-onchange-or-readonly.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@ ruleTester.run('checked-requires-onchange-or-readonly', rule, {
"React.createElement('input', { checked: foo, onChange: noop, readOnly: true })",
{
code: '<input type="checkbox" checked />',
options: [{ ignoreMissingProperties: false }],
options: [{ ignoreMissingProperties: true }],
},
{
code: '<input type="checkbox" checked={true} />',
options: [{ ignoreMissingProperties: false }],
options: [{ ignoreMissingProperties: true }],
},
{
code: '<input type="checkbox" onChange={noop} checked defaultChecked />',
options: [{ ignoreExclusiveCheckedAttribute: false }],
options: [{ ignoreExclusiveCheckedAttribute: true }],
},
{
code: '<input type="checkbox" onChange={noop} checked={true} defaultChecked />',
options: [{ ignoreExclusiveCheckedAttribute: false }],
options: [{ ignoreExclusiveCheckedAttribute: true }],
},
{
code: '<input type="checkbox" onChange={noop} checked defaultChecked />',
options: [{ ignoreMissingProperties: true, ignoreExclusiveCheckedAttribute: true }],
},
'<span/>',
"React.createElement('span')",
Expand Down Expand Up @@ -99,13 +103,21 @@ ruleTester.run('checked-requires-onchange-or-readonly', rule, {
},
{
code: '<input type="checkbox" checked defaultChecked />',
options: [{ ignoreMissingProperties: false }],
options: [{ ignoreMissingProperties: true }],
errors: [{ messageId: 'exclusiveCheckedAttribute' }],
},
{
code: '<input type="checkbox" checked defaultChecked />',
options: [{ ignoreExclusiveCheckedAttribute: false }],
options: [{ ignoreExclusiveCheckedAttribute: true }],
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="checkbox" checked defaultChecked />',
options: [{ ignoreMissingProperties: false, ignoreExclusiveCheckedAttribute: false }],
errors: [
{ messageId: 'exclusiveCheckedAttribute' },
{ messageId: 'missingProperty' },
],
},
]),
});

0 comments on commit 14b0175

Please sign in to comment.