|
| 1 | +/** |
| 2 | + * @fileoverview Ensure proper position of the first property in JSX |
| 3 | + * @author Joachim Seminck |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Requirements |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/jsx-first-prop-new-line'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +var parserOptions = 'babel-eslint'; |
| 15 | + |
| 16 | +// ----------------------------------------------------------------------------- |
| 17 | +// Tests |
| 18 | +// ----------------------------------------------------------------------------- |
| 19 | + |
| 20 | +var ruleTester = new RuleTester(); |
| 21 | +ruleTester.run('jsx-first-prop-new-line', rule, { |
| 22 | + |
| 23 | + valid: [ |
| 24 | + { |
| 25 | + code: '<Foo />', |
| 26 | + options: ['never'], |
| 27 | + parser: parserOptions |
| 28 | + }, |
| 29 | + { |
| 30 | + code: '<Foo prop="bar" />', |
| 31 | + options: ['never'], |
| 32 | + parser: parserOptions |
| 33 | + }, |
| 34 | + { |
| 35 | + code: '<Foo {...this.props} />', |
| 36 | + options: ['never'], |
| 37 | + parser: parserOptions |
| 38 | + }, |
| 39 | + { |
| 40 | + code: '<Foo a a a />', |
| 41 | + options: ['never'], |
| 42 | + parser: parserOptions |
| 43 | + }, |
| 44 | + { |
| 45 | + code: [ |
| 46 | + '<Foo a', |
| 47 | + ' b ', |
| 48 | + '/>' |
| 49 | + ].join('\n'), |
| 50 | + options: ['never'], |
| 51 | + parser: parserOptions |
| 52 | + }, |
| 53 | + { |
| 54 | + code: '<Foo />', |
| 55 | + options: ['multiline'], |
| 56 | + parser: parserOptions |
| 57 | + }, |
| 58 | + { |
| 59 | + code: '<Foo prop="one" />', |
| 60 | + options: ['multiline'], |
| 61 | + parser: parserOptions |
| 62 | + }, |
| 63 | + { |
| 64 | + code: '<Foo {...this.props} />', |
| 65 | + options: ['multiline'], |
| 66 | + parser: parserOptions |
| 67 | + }, |
| 68 | + { |
| 69 | + code: '<Foo a a a />', |
| 70 | + options: ['multiline'], |
| 71 | + parser: parserOptions |
| 72 | + }, |
| 73 | + { |
| 74 | + code: [ |
| 75 | + '<Foo', |
| 76 | + ' propOne="one"', |
| 77 | + ' propTwo="two"', |
| 78 | + '/>' |
| 79 | + ].join('\n'), |
| 80 | + options: ['multiline'], |
| 81 | + parser: parserOptions |
| 82 | + }, |
| 83 | + { |
| 84 | + code: [ |
| 85 | + '<Foo', |
| 86 | + ' {...this.props}', |
| 87 | + ' propTwo="two"', |
| 88 | + '/>' |
| 89 | + ].join('\n'), |
| 90 | + options: ['multiline'], |
| 91 | + parser: parserOptions |
| 92 | + }, |
| 93 | + { |
| 94 | + code: '<Foo />', |
| 95 | + options: ['always'], |
| 96 | + parser: parserOptions |
| 97 | + }, |
| 98 | + { |
| 99 | + code: [ |
| 100 | + '<Foo', |
| 101 | + ' propOne="one"', |
| 102 | + ' propTwo="two"', |
| 103 | + '/>' |
| 104 | + ].join('\n'), |
| 105 | + options: ['always'], |
| 106 | + parser: parserOptions |
| 107 | + }, |
| 108 | + { |
| 109 | + code: [ |
| 110 | + '<Foo', |
| 111 | + ' {...this.props}', |
| 112 | + ' propTwo="two"', |
| 113 | + '/>' |
| 114 | + ].join('\n'), |
| 115 | + options: ['always'], |
| 116 | + parser: parserOptions |
| 117 | + } |
| 118 | + ], |
| 119 | + |
| 120 | + invalid: [ |
| 121 | + { |
| 122 | + code: '<Foo prop="one" />', |
| 123 | + options: ['always'], |
| 124 | + errors: [{message: 'Property should be placed on a new line'}], |
| 125 | + parser: parserOptions |
| 126 | + }, |
| 127 | + { |
| 128 | + code: [ |
| 129 | + '<Foo propOne="one"', |
| 130 | + ' propTwo="two"', |
| 131 | + '/>' |
| 132 | + ].join('\n'), |
| 133 | + options: ['always'], |
| 134 | + errors: [{message: 'Property should be placed on a new line'}], |
| 135 | + parser: parserOptions |
| 136 | + }, |
| 137 | + { |
| 138 | + code: [ |
| 139 | + '<Foo', |
| 140 | + ' propOne="one"', |
| 141 | + ' propTwo="two"', |
| 142 | + '/>' |
| 143 | + ].join('\n'), |
| 144 | + options: ['never'], |
| 145 | + errors: [{message: 'Property should be placed on the same line as the component declaration'}], |
| 146 | + parser: parserOptions |
| 147 | + } |
| 148 | + ] |
| 149 | +}); |
0 commit comments