Skip to content

Commit

Permalink
Merge pull request #8 from Automattic/add/check-gridicon-format
Browse files Browse the repository at this point in the history
Add rule `jsx-gridicon-size` for enforcing recommended Gridicon size
  • Loading branch information
aduth authored and sirreal committed Dec 5, 2018
1 parent 66cc6c6 commit be4aea2
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/eslint-plugin-wpcalypso/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### v1.2.0 (June 9, 2016)

- New rule: [`jsx-gridicon-size`](docs/rules/jsx-gridicon-size.md)

#### v1.1.4 (June 9, 2016)

- Fix: i18n-no-variables should allow ES2015 template literal strings as long as there are no interpolated variables
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-wpcalypso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Then configure the rules you want to use under the rules section.
- [`i18n-no-placeholders-only`](docs/rules/i18n-no-placeholders-only.md): Disallow strings which include only placeholders
- [`i18n-mismatched-placeholders`](docs/rules/i18n-mismatched-placeholders.md): Ensure placeholder counts match between singular and plural strings
- [`i18n-named-placeholders`](docs/rules/i18n-named-placeholders.md): Disallow multiple unnamed placeholders
- [`jsx-gridicon-size`](docs/rules/jsx-gridicon-size.md): Enforce recommended Gridicon size attributes

## License

Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin-wpcalypso/docs/rules/jsx-gridicon-size.md
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 packages/eslint-plugin-wpcalypso/lib/rules/jsx-gridicon-size.js
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 = [];
2 changes: 1 addition & 1 deletion packages/eslint-plugin-wpcalypso/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-wpcalypso",
"version": "1.1.4",
"version": "1.2.0",
"description": "Custom ESLint rules for the WordPress.com Calypso project",
"repository": {
"type": "git",
Expand Down
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
} ]
}
]
} );

0 comments on commit be4aea2

Please sign in to comment.