-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 #406 from ryym/jsx-equals-spacing
Add jsx-equals-spacing rule
- Loading branch information
Showing
5 changed files
with
288 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Enforce or disallow spaces around equal signs in JSX attributes. (jsx-equals-spacing) | ||
|
||
Some style guides require or disallow spaces around equal signs. | ||
|
||
## Rule Details | ||
|
||
This rule will enforce consistency of spacing around equal signs in JSX attributes, by requiring or disallowing one or more spaces before and after `=`. | ||
|
||
### Options | ||
|
||
There are two options for the rule: | ||
|
||
* `"always"` enforces spaces around the equal sign | ||
* `"never"` disallows spaces around the equal sign (default) | ||
|
||
Depending on your coding conventions, you can choose either option by specifying it in your configuration: | ||
|
||
```json | ||
"jsx-equals-spacing": [2, "always"] | ||
``` | ||
|
||
#### never | ||
|
||
When `"never"` is set, the following patterns are considered warnings: | ||
|
||
```js | ||
<Hello name = {firstname} />; | ||
<Hello name ={firstname} />; | ||
<Hello name= {firstname} />; | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
<Hello name={firstname} />; | ||
<Hello name />; | ||
<Hello {...props} />; | ||
``` | ||
|
||
#### always | ||
|
||
When `"always"` is used, the following patterns are considered warnings: | ||
|
||
```js | ||
<Hello name={firstname} />; | ||
<Hello name ={firstname} />; | ||
<Hello name= {firstname} />; | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
<Hello name = {firstname} />; | ||
<Hello name />; | ||
<Hello {...props} />; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
You can turn this rule off if you are not concerned with the consistency of spacing around equal signs in JSX attributes. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @fileoverview Disallow or enforce spaces around equal signs in JSX attributes. | ||
* @author ryym | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
var config = context.options[0]; | ||
var sourceCode = context.getSourceCode(); | ||
|
||
/** | ||
* Determines a given attribute node has an equal sign. | ||
* @param {ASTNode} attrNode - The attribute node. | ||
* @returns {boolean} Whether or not the attriute node has an equal sign. | ||
*/ | ||
function hasEqual(attrNode) { | ||
return attrNode.type !== 'JSXSpreadAttribute' && attrNode.value !== null; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
JSXOpeningElement: function(node) { | ||
node.attributes.forEach(function(attrNode) { | ||
if (!hasEqual(attrNode)) { | ||
return; | ||
} | ||
|
||
var equalToken = sourceCode.getTokenAfter(attrNode.name); | ||
var spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken); | ||
var spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value); | ||
|
||
switch (config) { | ||
default: | ||
case 'never': | ||
if (spacedBefore) { | ||
context.report(attrNode, equalToken.loc.start, | ||
'There should be no space before \'=\''); | ||
} | ||
if (spacedAfter) { | ||
context.report(attrNode, equalToken.loc.start, | ||
'There should be no space after \'=\''); | ||
} | ||
break; | ||
case 'always': | ||
if (!spacedBefore) { | ||
context.report(attrNode, equalToken.loc.start, | ||
'A space is required before \'=\''); | ||
} | ||
if (!spacedAfter) { | ||
context.report(attrNode, equalToken.loc.start, | ||
'A space is required after \'=\''); | ||
} | ||
break; | ||
} | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports.schema = [{ | ||
enum: ['always', 'never'] | ||
}]; |
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,154 @@ | ||
/** | ||
* @fileoverview Disallow or enforce spaces around equal signs in JSX attributes. | ||
* @author ryym | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/jsx-equals-spacing'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('jsx-equals-spacing', rule, { | ||
valid: [{ | ||
code: '<App />', | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo />', | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo="bar" />', | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo={e => bar(e)} />', | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App {...props} />', | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App />', | ||
options: ['never'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo />', | ||
options: ['never'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo="bar" />', | ||
options: ['never'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo={e => bar(e)} />', | ||
options: ['never'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App {...props} />', | ||
options: ['never'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App />', | ||
options: ['always'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo />', | ||
options: ['always'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo = "bar" />', | ||
options: ['always'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App foo = {e => bar(e)} />', | ||
options: ['always'], | ||
parserOptions: parserOptions | ||
}, { | ||
code: '<App {...props} />', | ||
options: ['always'], | ||
parserOptions: parserOptions | ||
}], | ||
|
||
invalid: [{ | ||
code: '<App foo = {bar} />', | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'There should be no space before \'=\''}, | ||
{message: 'There should be no space after \'=\''} | ||
] | ||
}, { | ||
code: '<App foo = {bar} />', | ||
options: ['never'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'There should be no space before \'=\''}, | ||
{message: 'There should be no space after \'=\''} | ||
] | ||
}, { | ||
code: '<App foo ={bar} />', | ||
options: ['never'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'There should be no space before \'=\''} | ||
] | ||
}, { | ||
code: '<App foo= {bar} />', | ||
options: ['never'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'There should be no space after \'=\''} | ||
] | ||
}, { | ||
code: '<App foo= {bar} bar = {baz} />', | ||
options: ['never'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'There should be no space after \'=\''}, | ||
{message: 'There should be no space before \'=\''}, | ||
{message: 'There should be no space after \'=\''} | ||
] | ||
}, { | ||
code: '<App foo={bar} />', | ||
options: ['always'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'A space is required before \'=\''}, | ||
{message: 'A space is required after \'=\''} | ||
] | ||
}, { | ||
code: '<App foo ={bar} />', | ||
options: ['always'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'A space is required after \'=\''} | ||
] | ||
}, { | ||
code: '<App foo= {bar} />', | ||
options: ['always'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'A space is required before \'=\''} | ||
] | ||
}, { | ||
code: '<App foo={bar} bar ={baz} />', | ||
options: ['always'], | ||
parserOptions: parserOptions, | ||
errors: [ | ||
{message: 'A space is required before \'=\''}, | ||
{message: 'A space is required after \'=\''}, | ||
{message: 'A space is required after \'=\''} | ||
] | ||
}] | ||
}); |