-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: ESLint rule to avoid BaseControl component with label without an…
… id. (#14151)
- Loading branch information
1 parent
e5c1b8c
commit 53b012a
Showing
9 changed files
with
177 additions
and
24 deletions.
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.', | ||
} ); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |