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.
Rename wrap-multilines to jsx-wrap-multilines
This rule is JSX-specific, not React-specific. Changing its name makes this clearer. I've kept the old one around as deprecated. We can remove it at the next major version bump. Addresses jsx-eslint#686. Fixes jsx-eslint#668.
- Loading branch information
Showing
6 changed files
with
123 additions
and
76 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
2 changes: 1 addition & 1 deletion
2
docs/rules/wrap-multilines.md → docs/rules/jsx-wrap-multilines.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
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,103 @@ | ||
/** | ||
* @fileoverview Prevent missing parentheses around multilines JSX | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var DEFAULTS = { | ||
declaration: true, | ||
assignment: true, | ||
return: true | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
var sourceCode = context.getSourceCode(); | ||
|
||
function isParenthesised(node) { | ||
var previousToken = sourceCode.getTokenBefore(node); | ||
var nextToken = sourceCode.getTokenAfter(node); | ||
|
||
return previousToken && nextToken && | ||
previousToken.value === '(' && previousToken.range[1] <= node.range[0] && | ||
nextToken.value === ')' && nextToken.range[0] >= node.range[1]; | ||
} | ||
|
||
function isMultilines(node) { | ||
return node.loc.start.line !== node.loc.end.line; | ||
} | ||
|
||
function check(node) { | ||
if (!node || node.type !== 'JSXElement') { | ||
return; | ||
} | ||
|
||
if (!isParenthesised(node) && isMultilines(node)) { | ||
context.report({ | ||
node: node, | ||
message: 'Missing parentheses around multilines JSX', | ||
fix: function(fixer) { | ||
return fixer.replaceText(node, '(' + sourceCode.getText(node) + ')'); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function isEnabled(type) { | ||
var userOptions = context.options[0] || {}; | ||
if (({}).hasOwnProperty.call(userOptions, type)) { | ||
return userOptions[type]; | ||
} | ||
return DEFAULTS[type]; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
VariableDeclarator: function(node) { | ||
if (isEnabled('declaration')) { | ||
check(node.init); | ||
} | ||
}, | ||
|
||
AssignmentExpression: function(node) { | ||
if (isEnabled('assignment')) { | ||
check(node.right); | ||
} | ||
}, | ||
|
||
ReturnStatement: function(node) { | ||
if (isEnabled('return')) { | ||
check(node.argument); | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = [{ | ||
type: 'object', | ||
properties: { | ||
declaration: { | ||
type: 'boolean' | ||
}, | ||
assignment: { | ||
type: 'boolean' | ||
}, | ||
return: { | ||
type: 'boolean' | ||
} | ||
}, | ||
additionalProperties: false | ||
}]; |
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