|
| 1 | +/** |
| 2 | + * @fileoverview Enforce style prop value is an object |
| 3 | + * @author David Petersen |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/style-prop-object'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +var parserOptions = { |
| 15 | + ecmaVersion: 6, |
| 16 | + ecmaFeatures: { |
| 17 | + experimentalObjectRestSpread: true, |
| 18 | + jsx: true |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +// ------------------------------------------------------------------------------ |
| 23 | +// Tests |
| 24 | +// ------------------------------------------------------------------------------ |
| 25 | + |
| 26 | +var ruleTester = new RuleTester(); |
| 27 | +ruleTester.run('style-prop-object', rule, { |
| 28 | + valid: [ |
| 29 | + { |
| 30 | + code: '<div style={{ color: "red" }} />', |
| 31 | + parserOptions: parserOptions |
| 32 | + }, |
| 33 | + { |
| 34 | + code: '<Hello style={{ color: "red" }} />', |
| 35 | + parserOptions: parserOptions |
| 36 | + }, |
| 37 | + { |
| 38 | + code: [ |
| 39 | + 'function redDiv() {', |
| 40 | + ' const styles = { color: "red" };', |
| 41 | + ' return <div style={styles} />;', |
| 42 | + '}' |
| 43 | + ].join('\n'), |
| 44 | + parserOptions: parserOptions |
| 45 | + }, |
| 46 | + { |
| 47 | + code: [ |
| 48 | + 'function redDiv() {', |
| 49 | + ' const styles = { color: "red" };', |
| 50 | + ' return <Hello style={styles} />;', |
| 51 | + '}' |
| 52 | + ].join('\n'), |
| 53 | + parserOptions: parserOptions |
| 54 | + }, |
| 55 | + { |
| 56 | + code: [ |
| 57 | + 'const styles = { color: "red" };', |
| 58 | + 'function redDiv() {', |
| 59 | + ' return <div style={styles} />;', |
| 60 | + '}' |
| 61 | + ].join('\n'), |
| 62 | + parserOptions: parserOptions |
| 63 | + }, |
| 64 | + { |
| 65 | + code: [ |
| 66 | + 'function redDiv(props) {', |
| 67 | + ' return <div style={props.styles} />;', |
| 68 | + '}' |
| 69 | + ].join('\n'), |
| 70 | + parserOptions: parserOptions |
| 71 | + }, |
| 72 | + { |
| 73 | + code: [ |
| 74 | + 'import styles from \'./styles\';', |
| 75 | + 'function redDiv() {', |
| 76 | + ' return <div style={styles} />;', |
| 77 | + '}' |
| 78 | + ].join('\n'), |
| 79 | + parserOptions: Object.assign({sourceType: 'module'}, parserOptions) |
| 80 | + }, |
| 81 | + { |
| 82 | + code: [ |
| 83 | + 'import mystyles from \'./styles\';', |
| 84 | + 'const styles = Object.assign({ color: \'red\' }, mystyles);', |
| 85 | + 'function redDiv() {', |
| 86 | + ' return <div style={styles} />;', |
| 87 | + '}' |
| 88 | + ].join('\n'), |
| 89 | + parserOptions: Object.assign({sourceType: 'module'}, parserOptions) |
| 90 | + }, |
| 91 | + { |
| 92 | + code: [ |
| 93 | + 'const otherProps = { style: { color: "red" } };', |
| 94 | + 'const { a, b, ...props } = otherProps;', |
| 95 | + '<div {...props} />' |
| 96 | + ].join('\n'), |
| 97 | + parserOptions: parserOptions |
| 98 | + }, |
| 99 | + { |
| 100 | + code: [ |
| 101 | + 'const styles = Object.assign({ color: \'red\' }, mystyles);', |
| 102 | + 'React.createElement("div", { style: styles });' |
| 103 | + ].join('\n'), |
| 104 | + parserOptions: Object.assign({sourceType: 'module'}, parserOptions) |
| 105 | + } |
| 106 | + ], |
| 107 | + invalid: [ |
| 108 | + { |
| 109 | + code: '<div style="color: \'red\'" />', |
| 110 | + errors: [{ |
| 111 | + message: 'Style prop value must be an object', |
| 112 | + line: 1, |
| 113 | + column: 6, |
| 114 | + type: 'JSXAttribute' |
| 115 | + }], |
| 116 | + parserOptions: parserOptions |
| 117 | + }, |
| 118 | + { |
| 119 | + code: '<Hello style="color: \'red\'" />', |
| 120 | + errors: [{ |
| 121 | + message: 'Style prop value must be an object', |
| 122 | + line: 1, |
| 123 | + column: 8, |
| 124 | + type: 'JSXAttribute' |
| 125 | + }], |
| 126 | + parserOptions: parserOptions |
| 127 | + }, |
| 128 | + { |
| 129 | + code: '<div style={true} />', |
| 130 | + errors: [{ |
| 131 | + message: 'Style prop value must be an object', |
| 132 | + line: 1, |
| 133 | + column: 6, |
| 134 | + type: 'JSXAttribute' |
| 135 | + }], |
| 136 | + parserOptions: parserOptions |
| 137 | + }, |
| 138 | + { |
| 139 | + code: [ |
| 140 | + 'const styles = \'color: "red"\';', |
| 141 | + 'function redDiv2() {', |
| 142 | + ' return <div style={styles} />;', |
| 143 | + '}' |
| 144 | + ].join('\n'), |
| 145 | + errors: [{ |
| 146 | + message: 'Style prop value must be an object', |
| 147 | + line: 3, |
| 148 | + column: 22, |
| 149 | + type: 'Identifier' |
| 150 | + }], |
| 151 | + parserOptions: parserOptions |
| 152 | + }, |
| 153 | + { |
| 154 | + code: [ |
| 155 | + 'const styles = \'color: "red"\';', |
| 156 | + 'function redDiv2() {', |
| 157 | + ' return <Hello style={styles} />;', |
| 158 | + '}' |
| 159 | + ].join('\n'), |
| 160 | + errors: [{ |
| 161 | + message: 'Style prop value must be an object', |
| 162 | + line: 3, |
| 163 | + column: 24, |
| 164 | + type: 'Identifier' |
| 165 | + }], |
| 166 | + parserOptions: parserOptions |
| 167 | + }, |
| 168 | + { |
| 169 | + code: [ |
| 170 | + 'const styles = true;', |
| 171 | + 'function redDiv() {', |
| 172 | + ' return <div style={styles} />;', |
| 173 | + '}' |
| 174 | + ].join('\n'), |
| 175 | + errors: [{ |
| 176 | + message: 'Style prop value must be an object', |
| 177 | + line: 3, |
| 178 | + column: 22, |
| 179 | + type: 'Identifier' |
| 180 | + }], |
| 181 | + parserOptions: parserOptions |
| 182 | + } |
| 183 | + ] |
| 184 | +}); |
0 commit comments