|
| 1 | +/** |
| 2 | + * @fileoverview Prevent missing parentheses around multilines JSX |
| 3 | + * @author Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/wrap-multilines'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +var parserOptions = { |
| 15 | + ecmaVersion: 6, |
| 16 | + ecmaFeatures: { |
| 17 | + jsx: true |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | +// Code Snippets |
| 23 | +// ------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +var RETURN_SINGLE_LINE = '\ |
| 26 | + var Hello = React.createClass({\ |
| 27 | + render: function() {\ |
| 28 | + return <p>Hello {this.props.name}</p>;\ |
| 29 | + }\ |
| 30 | + });'; |
| 31 | + |
| 32 | +var RETURN_PAREN = '\ |
| 33 | + var Hello = React.createClass({\ |
| 34 | + render: function() {\ |
| 35 | + return (<div>\n\ |
| 36 | + <p>Hello {this.props.name}</p>\n\ |
| 37 | + </div>);\ |
| 38 | + }\ |
| 39 | + });'; |
| 40 | + |
| 41 | +var RETURN_NO_PAREN = '\ |
| 42 | + var Hello = React.createClass({\ |
| 43 | + render: function() {\ |
| 44 | + return <div>\n\ |
| 45 | + <p>Hello {this.props.name}</p>\n\ |
| 46 | + </div>;\ |
| 47 | + }\ |
| 48 | + });'; |
| 49 | + |
| 50 | +var DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;'; |
| 51 | + |
| 52 | +var DECLARATION_PAREN = '\ |
| 53 | + var hello = (<div>\n\ |
| 54 | + <p>Hello</p>\n\ |
| 55 | + </div>);'; |
| 56 | + |
| 57 | +var DECLARATION_NO_PAREN = '\ |
| 58 | + var hello = <div>\n\ |
| 59 | + <p>Hello</p>\n\ |
| 60 | + </div>;'; |
| 61 | + |
| 62 | +var ASSIGNMENT_SINGLE_LINE = 'var hello; hello = <p>Hello</p>;'; |
| 63 | + |
| 64 | +var ASSIGNMENT_PAREN = '\ |
| 65 | + var hello;\ |
| 66 | + hello = (<div>\n\ |
| 67 | + <p>Hello</p>\n\ |
| 68 | + </div>);'; |
| 69 | + |
| 70 | +var ASSIGNMENT_NO_PAREN = '\ |
| 71 | + var hello;\ |
| 72 | + hello = <div>\n\ |
| 73 | + <p>Hello</p>\n\ |
| 74 | + </div>;'; |
| 75 | + |
| 76 | +// ------------------------------------------------------------------------------ |
| 77 | +// Tests |
| 78 | +// ------------------------------------------------------------------------------ |
| 79 | + |
| 80 | +var ruleTester = new RuleTester(); |
| 81 | +ruleTester.run('wrap-multilines', rule, { |
| 82 | + |
| 83 | + valid: [ |
| 84 | + { |
| 85 | + code: RETURN_SINGLE_LINE, |
| 86 | + parserOptions: parserOptions |
| 87 | + }, { |
| 88 | + code: RETURN_PAREN, |
| 89 | + parserOptions: parserOptions |
| 90 | + }, { |
| 91 | + code: RETURN_NO_PAREN, |
| 92 | + options: [{return: false}], |
| 93 | + parserOptions: parserOptions |
| 94 | + }, { |
| 95 | + code: DECLARATION_SINGLE_LINE, |
| 96 | + parserOptions: parserOptions |
| 97 | + }, { |
| 98 | + code: DECLARATION_PAREN, |
| 99 | + parserOptions: parserOptions |
| 100 | + }, { |
| 101 | + code: DECLARATION_NO_PAREN, |
| 102 | + options: [{declaration: false}], |
| 103 | + parserOptions: parserOptions |
| 104 | + }, { |
| 105 | + code: ASSIGNMENT_SINGLE_LINE, |
| 106 | + options: [{declaration: false}], |
| 107 | + parserOptions: parserOptions |
| 108 | + }, { |
| 109 | + code: ASSIGNMENT_PAREN, |
| 110 | + parserOptions: parserOptions |
| 111 | + }, { |
| 112 | + code: ASSIGNMENT_NO_PAREN, |
| 113 | + options: [{assignment: false}], |
| 114 | + parserOptions: parserOptions |
| 115 | + } |
| 116 | + ], |
| 117 | + |
| 118 | + invalid: [ |
| 119 | + { |
| 120 | + code: RETURN_NO_PAREN, |
| 121 | + output: RETURN_PAREN, |
| 122 | + parserOptions: parserOptions, |
| 123 | + errors: [{message: 'Missing parentheses around multilines JSX'}] |
| 124 | + }, { |
| 125 | + code: RETURN_NO_PAREN, |
| 126 | + output: RETURN_PAREN, |
| 127 | + parserOptions: parserOptions, |
| 128 | + options: [{return: true}], |
| 129 | + errors: [{message: 'Missing parentheses around multilines JSX'}] |
| 130 | + }, { |
| 131 | + code: DECLARATION_NO_PAREN, |
| 132 | + output: DECLARATION_PAREN, |
| 133 | + parserOptions: parserOptions, |
| 134 | + errors: [{message: 'Missing parentheses around multilines JSX'}] |
| 135 | + }, { |
| 136 | + code: DECLARATION_NO_PAREN, |
| 137 | + output: DECLARATION_PAREN, |
| 138 | + parserOptions: parserOptions, |
| 139 | + options: [{declaration: true}], |
| 140 | + errors: [{message: 'Missing parentheses around multilines JSX'}] |
| 141 | + }, { |
| 142 | + code: ASSIGNMENT_NO_PAREN, |
| 143 | + output: ASSIGNMENT_PAREN, |
| 144 | + parserOptions: parserOptions, |
| 145 | + errors: [{message: 'Missing parentheses around multilines JSX'}] |
| 146 | + }, { |
| 147 | + code: ASSIGNMENT_NO_PAREN, |
| 148 | + output: ASSIGNMENT_PAREN, |
| 149 | + parserOptions: parserOptions, |
| 150 | + options: [{assignment: true}], |
| 151 | + errors: [{message: 'Missing parentheses around multilines JSX'}] |
| 152 | + } |
| 153 | + ] |
| 154 | +}); |
0 commit comments