forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsx-equals-spacing rule (fixes jsx-eslint#394)
- Loading branch information
Showing
3 changed files
with
225 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,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 \'=\''} | ||
] | ||
}] | ||
}); |