diff --git a/docs/rules/wrap-multilines.md b/docs/rules/wrap-multilines.md index 40b7a71253..12953e369a 100644 --- a/docs/rules/wrap-multilines.md +++ b/docs/rules/wrap-multilines.md @@ -1,6 +1,6 @@ -# Prevent missing parentheses around multilines JSX (wrap-multilines) +# Prevent missing parentheses around multiline JSX (wrap-multilines) -Wrapping multilines JSX in parentheses can improve readability and/or convenience. +Wrapping multiline JSX in parentheses can improve readability and/or convenience. It optionally takes a second parameter in the form of an object, containing places to apply the rule. By default, `"declaration"`, `"assignment"`, and `"return"` syntax is checked, but these can be explicitly disabled. Any syntax type missing in the object will follow the default behavior (become enabled). ## Rule Details @@ -30,4 +30,15 @@ var Hello = React.createClass({ ); } }); + +// When [1, {declaration: false}] +var hello; +hello =
+

Hello

+
+ +// When [1, {declaration: true, assignment: false, return: true}] +var world =
+

World

+
``` diff --git a/lib/rules/wrap-multilines.js b/lib/rules/wrap-multilines.js index 44b8b83c45..193060087c 100644 --- a/lib/rules/wrap-multilines.js +++ b/lib/rules/wrap-multilines.js @@ -4,6 +4,16 @@ */ 'use strict'; +// ------------------------------------------------------------------------------ +// Constants +// ------------------------------------------------------------------------------ + +var DEFAULTS = { + declaration: true, + assignment: true, + return: true +}; + // ------------------------------------------------------------------------------ // Rule Definition // ------------------------------------------------------------------------------ @@ -33,6 +43,14 @@ module.exports = function(context) { } } + function isEnabled(type) { + var userOptions = context.options[0] || {}; + if (({}).hasOwnProperty.call(userOptions, type)) { + return userOptions[type]; + } + return DEFAULTS[type]; + } + // -------------------------------------------------------------------------- // Public // -------------------------------------------------------------------------- @@ -40,16 +58,31 @@ module.exports = function(context) { return { VariableDeclarator: function(node) { - check(node.init); + if (isEnabled('declaration')) { + check(node.init); + } }, AssignmentExpression: function(node) { - check(node.right); + if (isEnabled('assignment')) { + check(node.right); + } }, ReturnStatement: function(node) { - check(node.argument); + if (isEnabled('return')) { + check(node.argument); + } } }; }; + +module.exports.schema = [{ + type: 'object', + properties: { + declaration: {type: 'boolean'}, + assignment: {type: 'boolean'}, + return: {type: 'boolean'} + } +}]; diff --git a/tests/lib/rules/wrap-multilines.js b/tests/lib/rules/wrap-multilines.js index f6da85bcb5..aa32d87769 100644 --- a/tests/lib/rules/wrap-multilines.js +++ b/tests/lib/rules/wrap-multilines.js @@ -11,6 +11,67 @@ var eslint = require('eslint').linter; var ESLintTester = require('eslint-tester'); +// ------------------------------------------------------------------------------ +// Code Snippets +// ------------------------------------------------------------------------------ + +var RETURN_SINGLE_LINE = '\ + var Hello = React.createClass({\ + render: function() {\ + return

Hello {this.props.name}

;\ + }\ + });'; + +var RETURN_PAREN = '\ + var Hello = React.createClass({\ + render: function() {\ + return (\n\ +
\n\ +

Hello {this.props.name}

\n\ +
\n\ + );\ + }\ + });'; + +var RETURN_NO_PAREN = '\ + var Hello = React.createClass({\ + render: function() {\ + return
\n\ +

Hello {this.props.name}

\n\ +
;\ + }\ + });'; + +var DECLARATION_SINGLE_LINE = 'var hello =

Hello

;'; + +var DECLARATION_PAREN = '\ + var hello = (\n\ +
\n\ +

Hello

\n\ +
\n\ + );'; + +var DECLARATION_NO_PAREN = '\ + var hello =
\n\ +

Hello

\n\ +
;'; + +var ASSIGNMENT_SINGLE_LINE = 'var hello; hello =

Hello

;'; + +var ASSIGNMENT_PAREN = '\ + var hello;\ + hello = (\n\ +
\n\ +

Hello

\n\ +
\n\ + );'; + +var ASSIGNMENT_NO_PAREN = '\ + var hello;\ + hello =
\n\ +

Hello

\n\ +
;'; + // ------------------------------------------------------------------------------ // Tests // ------------------------------------------------------------------------------ @@ -20,97 +81,67 @@ eslintTester.addRuleTest('lib/rules/wrap-multilines', { valid: [ { - code: '\ - var Hello = React.createClass({\ - render: function() {\ - return

Hello {this.props.name}

;\ - }\ - });', - ecmaFeatures: { - jsx: true - } + code: RETURN_SINGLE_LINE, + ecmaFeatures: {jsx: true} + }, { + code: RETURN_PAREN, + ecmaFeatures: {jsx: true} + }, { + code: RETURN_NO_PAREN, + args: [1, {return: false}], + ecmaFeatures: {jsx: true} + }, { + code: DECLARATION_SINGLE_LINE, + ecmaFeatures: {jsx: true} }, { - code: '\ - var Hello = React.createClass({\ - render: function() {\ - return (\n\ -
\n\ -

Hello {this.props.name}

\n\ -
\n\ - );\ - }\ - });', - ecmaFeatures: { - jsx: true - } + code: DECLARATION_PAREN, + ecmaFeatures: {jsx: true} }, { - code: 'var hello =

Hello

;', - ecmaFeatures: { - jsx: true - } + code: DECLARATION_NO_PAREN, + args: [1, {declaration: false}], + ecmaFeatures: {jsx: true} }, { - code: '\ - var hello = (\n\ -
\n\ -

Hello

\n\ -
\n\ - );', - ecmaFeatures: { - jsx: true - } + code: ASSIGNMENT_SINGLE_LINE, + args: [1, {declaration: false}], + ecmaFeatures: {jsx: true} }, { - code: '\ - var hello;\ - hello = (\n\ -
\n\ -

Hello

\n\ -
\n\ - );', - ecmaFeatures: { - jsx: true - } + code: ASSIGNMENT_PAREN, + ecmaFeatures: {jsx: true} + }, { + code: ASSIGNMENT_NO_PAREN, + args: [1, {assignment: false}], + ecmaFeatures: {jsx: true} } ], invalid: [ { - code: '\ - var Hello = React.createClass({\ - render: function() {\ - return
\n\ -

Hello {this.props.name}

\n\ -
;\ - }\ - });', - ecmaFeatures: { - jsx: true - }, - errors: [{ - message: 'Missing parentheses around multilines JSX' - }] + code: RETURN_NO_PAREN, + ecmaFeatures: {jsx: true}, + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: RETURN_NO_PAREN, + ecmaFeatures: {jsx: true}, + args: [1, {return: true}], + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: DECLARATION_NO_PAREN, + ecmaFeatures: {jsx: true}, + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: DECLARATION_NO_PAREN, + ecmaFeatures: {jsx: true}, + args: [1, {declaration: true}], + errors: [{message: 'Missing parentheses around multilines JSX'}] }, { - code: '\ - var hello =
\n\ -

Hello

\n\ -
;', - ecmaFeatures: { - jsx: true - }, - errors: [{ - message: 'Missing parentheses around multilines JSX' - }] + code: ASSIGNMENT_NO_PAREN, + ecmaFeatures: {jsx: true}, + errors: [{message: 'Missing parentheses around multilines JSX'}] }, { - code: '\ - var hello;\ - hello =
\n\ -

Hello

\n\ -
;', - ecmaFeatures: { - jsx: true - }, - errors: [{ - message: 'Missing parentheses around multilines JSX' - }] + code: ASSIGNMENT_NO_PAREN, + ecmaFeatures: {jsx: true}, + args: [1, {assignment: true}], + errors: [{message: 'Missing parentheses around multilines JSX'}] } ] });