-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Automattic/add/check-gridicon-format
Add rule `jsx-gridicon-size` for enforcing recommended Gridicon size
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
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
19 changes: 19 additions & 0 deletions
19
packages/eslint-plugin-wpcalypso/docs/rules/jsx-gridicon-size.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,19 @@ | ||
# Enforce recommended Gridicon size attributes | ||
|
||
Gridicon JSX elements must use one of the recommended sizes. | ||
|
||
In previous incarnations, this warning could be subdued by adding a `nonStandardSize` prop to the element, but it's recommended instead to disable the rule using standard ESLint rule options ([documentation](http://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments)). | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
<Gridicon size={ 20 } /> | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
<Gridicon size={ 18 } /> | ||
``` |
39 changes: 39 additions & 0 deletions
39
packages/eslint-plugin-wpcalypso/lib/rules/jsx-gridicon-size.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,39 @@ | ||
/** | ||
* @fileoverview Enforce recommended Gridicon size attributes | ||
* @author Automattic | ||
* @copyright 2016 Automattic. All rights reserved. | ||
* See LICENSE.md file in root directory for full license. | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Constants | ||
//------------------------------------------------------------------------------ | ||
|
||
var VALID_SIZES = [ 12, 18, 24, 36, 48, 54, 72 ]; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = module.exports = function( context ) { | ||
return { | ||
JSXAttribute: function( node ) { | ||
if ( 'size' !== node.name.name || | ||
'JSXOpeningElement' !== node.parent.type || | ||
'Gridicon' !== node.parent.name.name || | ||
'JSXExpressionContainer' !== node.value.type || | ||
'Literal' !== node.value.expression.type ) { | ||
return; | ||
} | ||
|
||
if ( -1 === VALID_SIZES.indexOf( node.value.expression.value ) ) { | ||
context.report( node, rule.ERROR_MESSAGE ); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
rule.ERROR_MESSAGE = 'Gridicon size should be one of recommended sizes: ' + VALID_SIZES.join( ', ' ); | ||
|
||
rule.schema = []; |
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
37 changes: 37 additions & 0 deletions
37
packages/eslint-plugin-wpcalypso/tests/lib/rules/jsx-gridicon-size.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,37 @@ | ||
/** | ||
* @fileoverview Enforce recommended Gridicon size attributes | ||
* @author Automattic | ||
* @copyright 2016 Automattic. All rights reserved. | ||
* See LICENSE.md file in root directory for full license. | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require( '../../../lib/rules/jsx-gridicon-size' ), | ||
RuleTester = require( 'eslint' ).RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
( new RuleTester() ).run( 'jsx-gridicon-size', rule, { | ||
valid: [ | ||
{ | ||
code: '<Gridicon size={ 18 } />', | ||
ecmaFeatures: { jsx: true } | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: '<Gridicon size={ 20 } />', | ||
ecmaFeatures: { jsx: true }, | ||
errors: [ { | ||
message: rule.ERROR_MESSAGE | ||
} ] | ||
} | ||
] | ||
} ); |