Skip to content

Commit

Permalink
Make wrap-multilines more granular
Browse files Browse the repository at this point in the history
This adds an option for enabling/disabling wrap-multilines in the
context of the enclosing expression. In facebook/react, we only care
about jsx expressions in return statements:

facebook/react#4007 (comment)
  • Loading branch information
bgw committed Jun 2, 2015
1 parent 92f33bc commit 4d92237
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 83 deletions.
39 changes: 36 additions & 3 deletions lib/rules/wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
*/
'use strict';

// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------

var DEFAULTS = {
declaration: true,
assignment: true,
return: true
};

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -33,23 +43,46 @@ module.exports = function(context) {
}
}

function isEnabled(type) {
var userOptions = context.options[0] || {};
if (({}).hasOwnProperty.call(userOptions, type)) {
return userOptions[type];
}
return DEFAULTS[type];
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

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'}
}
}];
191 changes: 111 additions & 80 deletions tests/lib/rules/wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p>Hello {this.props.name}</p>;\
}\
});';

var RETURN_PAREN = '\
var Hello = React.createClass({\
render: function() {\
return (\n\
<div>\n\
<p>Hello {this.props.name}</p>\n\
</div>\n\
);\
}\
});';

var RETURN_NO_PAREN = '\
var Hello = React.createClass({\
render: function() {\
return <div>\n\
<p>Hello {this.props.name}</p>\n\
</div>;\
}\
});';

var DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;';

var DECLARATION_PAREN = '\
var hello = (\n\
<div>\n\
<p>Hello</p>\n\
</div>\n\
);';

var DECLARATION_NO_PAREN = '\
var hello = <div>\n\
<p>Hello</p>\n\
</div>;';

var ASSIGNMENT_SINGLE_LINE = 'var hello; hello = <p>Hello</p>;';

var ASSIGNMENT_PAREN = '\
var hello;\
hello = (\n\
<div>\n\
<p>Hello</p>\n\
</div>\n\
);';

var ASSIGNMENT_NO_PAREN = '\
var hello;\
hello = <div>\n\
<p>Hello</p>\n\
</div>;';

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand All @@ -20,97 +81,67 @@ eslintTester.addRuleTest('lib/rules/wrap-multilines', {

valid: [
{
code: '\
var Hello = React.createClass({\
render: function() {\
return <p>Hello {this.props.name}</p>;\
}\
});',
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\
<div>\n\
<p>Hello {this.props.name}</p>\n\
</div>\n\
);\
}\
});',
ecmaFeatures: {
jsx: true
}
code: DECLARATION_PAREN,
ecmaFeatures: {jsx: true}
}, {
code: 'var hello = <p>Hello</p>;',
ecmaFeatures: {
jsx: true
}
code: DECLARATION_NO_PAREN,
args: [1, {declaration: false}],
ecmaFeatures: {jsx: true}
}, {
code: '\
var hello = (\n\
<div>\n\
<p>Hello</p>\n\
</div>\n\
);',
ecmaFeatures: {
jsx: true
}
code: ASSIGNMENT_SINGLE_LINE,
args: [1, {declaration: false}],
ecmaFeatures: {jsx: true}
}, {
code: '\
var hello;\
hello = (\n\
<div>\n\
<p>Hello</p>\n\
</div>\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 <div>\n\
<p>Hello {this.props.name}</p>\n\
</div>;\
}\
});',
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 = <div>\n\
<p>Hello</p>\n\
</div>;',
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 = <div>\n\
<p>Hello</p>\n\
</div>;',
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'}]
}
]
});

0 comments on commit 4d92237

Please sign in to comment.