diff --git a/lib/rules/wrap-multilines.js b/lib/rules/wrap-multilines.js index 31647373ad..2b6900eedd 100644 --- a/lib/rules/wrap-multilines.js +++ b/lib/rules/wrap-multilines.js @@ -20,6 +20,7 @@ module.exports = { category: 'Stylistic Issues', recommended: false }, + fixable: 'code', schema: [{ type: 'object', @@ -39,7 +40,7 @@ module.exports = { }, create: function(context) { - return util._extend(jsxWrapMultilines(context), { + return util._extend(jsxWrapMultilines.create(context), { Program: function() { if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { return; diff --git a/tests/lib/rules/wrap-multilines.js b/tests/lib/rules/wrap-multilines.js new file mode 100644 index 0000000000..e169ff736d --- /dev/null +++ b/tests/lib/rules/wrap-multilines.js @@ -0,0 +1,154 @@ +/** + * @fileoverview Prevent missing parentheses around multilines JSX + * @author Yannick Croissant + */ +'use strict'; + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +var rule = require('../../../lib/rules/wrap-multilines'); +var RuleTester = require('eslint').RuleTester; + +var parserOptions = { + ecmaVersion: 6, + ecmaFeatures: { + jsx: true + } +}; + +// ------------------------------------------------------------------------------ +// 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\ +

Hello {this.props.name}

\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\ +

Hello

\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\ +

Hello

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

Hello

\n\ +
;'; + +// ------------------------------------------------------------------------------ +// Tests +// ------------------------------------------------------------------------------ + +var ruleTester = new RuleTester(); +ruleTester.run('wrap-multilines', rule, { + + valid: [ + { + code: RETURN_SINGLE_LINE, + parserOptions: parserOptions + }, { + code: RETURN_PAREN, + parserOptions: parserOptions + }, { + code: RETURN_NO_PAREN, + options: [{return: false}], + parserOptions: parserOptions + }, { + code: DECLARATION_SINGLE_LINE, + parserOptions: parserOptions + }, { + code: DECLARATION_PAREN, + parserOptions: parserOptions + }, { + code: DECLARATION_NO_PAREN, + options: [{declaration: false}], + parserOptions: parserOptions + }, { + code: ASSIGNMENT_SINGLE_LINE, + options: [{declaration: false}], + parserOptions: parserOptions + }, { + code: ASSIGNMENT_PAREN, + parserOptions: parserOptions + }, { + code: ASSIGNMENT_NO_PAREN, + options: [{assignment: false}], + parserOptions: parserOptions + } + ], + + invalid: [ + { + code: RETURN_NO_PAREN, + output: RETURN_PAREN, + parserOptions: parserOptions, + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: RETURN_NO_PAREN, + output: RETURN_PAREN, + parserOptions: parserOptions, + options: [{return: true}], + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: DECLARATION_NO_PAREN, + output: DECLARATION_PAREN, + parserOptions: parserOptions, + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: DECLARATION_NO_PAREN, + output: DECLARATION_PAREN, + parserOptions: parserOptions, + options: [{declaration: true}], + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: ASSIGNMENT_NO_PAREN, + output: ASSIGNMENT_PAREN, + parserOptions: parserOptions, + errors: [{message: 'Missing parentheses around multilines JSX'}] + }, { + code: ASSIGNMENT_NO_PAREN, + output: ASSIGNMENT_PAREN, + parserOptions: parserOptions, + options: [{assignment: true}], + errors: [{message: 'Missing parentheses around multilines JSX'}] + } + ] +});