|
| 1 | +const rule = require('../spread-props-first') |
| 2 | +const {RuleTester} = require('eslint') |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + languageOptions: { |
| 6 | + ecmaVersion: 'latest', |
| 7 | + sourceType: 'module', |
| 8 | + parserOptions: { |
| 9 | + ecmaFeatures: { |
| 10 | + jsx: true, |
| 11 | + }, |
| 12 | + }, |
| 13 | + }, |
| 14 | +}) |
| 15 | + |
| 16 | +ruleTester.run('spread-props-first', rule, { |
| 17 | + valid: [ |
| 18 | + // Spread props before named props |
| 19 | + `<Example {...rest} className="foo" />`, |
| 20 | + // Multiple spreads before named props |
| 21 | + `<Example {...rest} {...other} className="foo" id="bar" />`, |
| 22 | + // Only spread props |
| 23 | + `<Example {...rest} />`, |
| 24 | + // Only named props |
| 25 | + `<Example className="foo" id="bar" />`, |
| 26 | + // Empty element |
| 27 | + `<Example />`, |
| 28 | + // Spread first, then named props |
| 29 | + `<Example {...rest} className="foo" onClick={handleClick} />`, |
| 30 | + // Multiple spreads at the beginning |
| 31 | + `<Example {...props1} {...props2} {...props3} className="foo" />`, |
| 32 | + ], |
| 33 | + invalid: [ |
| 34 | + // Named prop before spread |
| 35 | + { |
| 36 | + code: `<Example className="foo" {...rest} />`, |
| 37 | + output: `<Example {...rest} className="foo" />`, |
| 38 | + errors: [ |
| 39 | + { |
| 40 | + messageId: 'spreadPropsFirst', |
| 41 | + data: {spreadProp: '{...rest}', namedProp: 'className'}, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + // Multiple named props before spread |
| 46 | + { |
| 47 | + code: `<Example className="foo" id="bar" {...rest} />`, |
| 48 | + output: `<Example {...rest} className="foo" id="bar" />`, |
| 49 | + errors: [ |
| 50 | + { |
| 51 | + messageId: 'spreadPropsFirst', |
| 52 | + data: {spreadProp: '{...rest}', namedProp: 'id'}, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + // Named prop with expression before spread |
| 57 | + { |
| 58 | + code: `<Example onClick={handleClick} {...rest} />`, |
| 59 | + output: `<Example {...rest} onClick={handleClick} />`, |
| 60 | + errors: [ |
| 61 | + { |
| 62 | + messageId: 'spreadPropsFirst', |
| 63 | + data: {spreadProp: '{...rest}', namedProp: 'onClick'}, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + // Mixed order with multiple spreads |
| 68 | + { |
| 69 | + code: `<Example className="foo" {...rest} id="bar" {...other} />`, |
| 70 | + output: `<Example {...rest} {...other} className="foo" id="bar" />`, |
| 71 | + errors: [ |
| 72 | + { |
| 73 | + messageId: 'spreadPropsFirst', |
| 74 | + data: {spreadProp: '{...rest}', namedProp: 'id'}, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + // Named prop before multiple spreads |
| 79 | + { |
| 80 | + code: `<Example className="foo" {...rest} {...other} />`, |
| 81 | + output: `<Example {...rest} {...other} className="foo" />`, |
| 82 | + errors: [ |
| 83 | + { |
| 84 | + messageId: 'spreadPropsFirst', |
| 85 | + data: {spreadProp: '{...rest}', namedProp: 'className'}, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + // Complex example with many props |
| 90 | + { |
| 91 | + code: `<Example className="foo" id="bar" onClick={handleClick} {...rest} disabled />`, |
| 92 | + output: `<Example {...rest} className="foo" id="bar" onClick={handleClick} disabled />`, |
| 93 | + errors: [ |
| 94 | + { |
| 95 | + messageId: 'spreadPropsFirst', |
| 96 | + data: {spreadProp: '{...rest}', namedProp: 'disabled'}, |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + // Boolean prop before spread |
| 101 | + { |
| 102 | + code: `<Example disabled {...rest} />`, |
| 103 | + output: `<Example {...rest} disabled />`, |
| 104 | + errors: [ |
| 105 | + { |
| 106 | + messageId: 'spreadPropsFirst', |
| 107 | + data: {spreadProp: '{...rest}', namedProp: 'disabled'}, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + // Spread in the middle |
| 112 | + { |
| 113 | + code: `<Example className="foo" {...rest} id="bar" />`, |
| 114 | + output: `<Example {...rest} className="foo" id="bar" />`, |
| 115 | + errors: [ |
| 116 | + { |
| 117 | + messageId: 'spreadPropsFirst', |
| 118 | + data: {spreadProp: '{...rest}', namedProp: 'id'}, |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + ], |
| 123 | +}) |
0 commit comments