-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: ESLint rule that forbids a BaseControl that includes label prop but omits id #14151
Merged
aduth
merged 1 commit into
master
from
add/add-Eslint-rule-to-avoid-BaseControl-component-with-label-without-an-id
Apr 24, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/eslint-plugin/docs/rules/no-base-control-with-label-without-id.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Disallow the usage of BaseControl component with a label prop set but omitting the id property (no-base-control-with-label-without-id) | ||
|
||
Base control component ideally should be used together with components providing user input. The label the BaseControl component receives, should be associated with some component providing user via an id attribute. | ||
If a label is provided but the id is omitted it means that the developer missed the id prop or that BaseControl is not a good fit for the use case and a div together with a span can provide the same functionality. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<BaseControl | ||
label="ok" | ||
> | ||
<input id="my-id" /> | ||
</BaseControl> | ||
``` | ||
|
||
|
||
```jsx | ||
<BaseControl label="ok" /> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
|
||
```jsx | ||
<BaseControl /> | ||
``` | ||
|
||
```jsx | ||
<BaseControl | ||
id="my-id" | ||
> | ||
<input id="my-id" /> | ||
</BaseControl> | ||
``` | ||
|
||
```jsx | ||
<BaseControl | ||
label="ok" | ||
id="my-id" | ||
> | ||
<input id="my-id" /> | ||
</BaseControl> | ||
``` |
74 changes: 74 additions & 0 deletions
74
packages/eslint-plugin/rules/__tests__/no-base-control-with-label-without-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { RuleTester } from 'eslint'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import rule from '../no-base-control-with-label-without-id'; | ||
|
||
const ruleTester = new RuleTester( { | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
} ); | ||
|
||
ruleTester.run( 'no-base-control-with-label-without-id', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<BaseControl | ||
label="ok" | ||
id="my-id" | ||
/>`, | ||
}, | ||
{ | ||
code: `<BaseControl />`, | ||
}, | ||
{ | ||
code: ` | ||
<BaseControl | ||
label="ok" | ||
id="my-id" | ||
> | ||
<input id="my-id" /> | ||
</BaseControl>`, | ||
}, | ||
{ | ||
code: ` | ||
<BaseControl> | ||
<input id="my-id" /> | ||
</BaseControl>`, | ||
}, | ||
{ | ||
code: ` | ||
<BaseControl | ||
id="my-id" | ||
> | ||
<input id="my-id" /> | ||
</BaseControl>`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<BaseControl | ||
label="ok" | ||
> | ||
<input id="my-id" /> | ||
</BaseControl>`, | ||
errors: [ { message: 'When using BaseControl component if a label property is passed an id property should also be passed.' } ], | ||
}, | ||
{ | ||
code: ` | ||
<BaseControl | ||
label="ok" | ||
/>`, | ||
errors: [ { message: 'When using BaseControl component if a label property is passed an id property should also be passed.' } ], | ||
}, | ||
], | ||
} ); |
26 changes: 26 additions & 0 deletions
26
packages/eslint-plugin/rules/no-base-control-with-label-without-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
create( context ) { | ||
return { | ||
'JSXOpeningElement[name.name=\'BaseControl\']': ( node ) => { | ||
const containsAttribute = ( attrName ) => { | ||
return node.attributes.some( ( attribute ) => { | ||
return attribute.name.name === attrName; | ||
} ); | ||
}; | ||
if ( | ||
containsAttribute( 'label' ) && | ||
! containsAttribute( 'id' ) | ||
) { | ||
context.report( { | ||
node, | ||
message: 'When using BaseControl component if a label property is passed an id property should also be passed.', | ||
} ); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if this is technically "correct" by the condition of the lint rule itself, it's still not a good example in that the
id
should direct to an input that the BaseControl wraps. Rather than<b>Child</b>
, I'd prefer to see something like<input id="my-id" />
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @aduth thank you for the review 👍 Your feedback was addressed.