|
| 1 | +/** |
| 2 | + * @fileoverview Prevents jsx context provider values from taking values that |
| 3 | + * will cause needless rerenders. |
| 4 | + * @author Dylan Oshima |
| 5 | + */ |
| 6 | + |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | +// Requirements |
| 11 | +// ----------------------------------------------------------------------------- |
| 12 | + |
| 13 | +const RuleTester = require('eslint').RuleTester; |
| 14 | +const rule = require('../../../lib/rules/jsx-no-constructed-context-values'); |
| 15 | + |
| 16 | +const parsers = require('../../helpers/parsers'); |
| 17 | + |
| 18 | +const parserOptions = { |
| 19 | + ecmaVersion: 2018, |
| 20 | + sourceType: 'module', |
| 21 | + ecmaFeatures: { |
| 22 | + jsx: true |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +// ----------------------------------------------------------------------------- |
| 27 | +// Tests |
| 28 | +// ----------------------------------------------------------------------------- |
| 29 | + |
| 30 | +const ruleTester = new RuleTester({parserOptions}); |
| 31 | +ruleTester.run('react-no-constructed-context-values', rule, { |
| 32 | + valid: [ |
| 33 | + { |
| 34 | + code: '<Context.Provider value={props}></Context.Provider>' |
| 35 | + }, |
| 36 | + { |
| 37 | + code: '<Context.Provider value={100}></Context.Provider>' |
| 38 | + }, |
| 39 | + { |
| 40 | + code: '<Context.Provider value="Some string"></Context.Provider>' |
| 41 | + }, |
| 42 | + { |
| 43 | + code: 'function Component() { const foo = useMemo(() => { return {} }, []); return (<Context.Provider value={foo}></Context.Provider>)}', |
| 44 | + options: [{allowArrowFunctions: true}] |
| 45 | + }, |
| 46 | + { |
| 47 | + code: ` |
| 48 | + function Component({oneProp, twoProp, redProp, blueProp,}) { |
| 49 | + return ( |
| 50 | + <NewContext.Provider value={twoProp}></NewContext.Provider> |
| 51 | + ); |
| 52 | + } |
| 53 | + ` |
| 54 | + }, |
| 55 | + { |
| 56 | + code: ` |
| 57 | + function Foo(section) { |
| 58 | + const foo = section.section_components?.edges; |
| 59 | +
|
| 60 | + return ( |
| 61 | + <Context.Provider value={foo}></Context.Provider> |
| 62 | + ) |
| 63 | + } |
| 64 | + `, |
| 65 | + parser: parsers.BABEL_ESLINT |
| 66 | + }, |
| 67 | + { |
| 68 | + code: ` |
| 69 | + import foo from 'foo'; |
| 70 | + function innerContext() { |
| 71 | + return ( |
| 72 | + <Context.Provider value={foo.something}></Context.Provider> |
| 73 | + ) |
| 74 | + } |
| 75 | + ` |
| 76 | + }, |
| 77 | + { |
| 78 | + code: ` |
| 79 | + // Passes because the lint rule doesn't handle JSX spread attributes |
| 80 | + function innerContext() { |
| 81 | + const foo = {value: 'something'} |
| 82 | + return ( |
| 83 | + <Context.Provider {...foo}></Context.Provider> |
| 84 | + ) |
| 85 | + } |
| 86 | + ` |
| 87 | + }, |
| 88 | + { |
| 89 | + code: ` |
| 90 | + // Passes because the lint rule doesn't handle JSX spread attributes |
| 91 | + function innerContext() { |
| 92 | + const foo = useMemo(() => { |
| 93 | + return bar; |
| 94 | + }) |
| 95 | + return ( |
| 96 | + <Context.Provider value={foo}></Context.Provider> |
| 97 | + ) |
| 98 | + } |
| 99 | + ` |
| 100 | + }, |
| 101 | + { |
| 102 | + code: ` |
| 103 | + // Passes because we can't statically check if it's using the default value |
| 104 | + function Component({ a = {} }) { |
| 105 | + return (<Context.Provider value={a}></Context.Provider>); |
| 106 | + } |
| 107 | + ` |
| 108 | + } |
| 109 | + ], |
| 110 | + invalid: [ |
| 111 | + { |
| 112 | + // Invalid because object construction creates a new identity |
| 113 | + code: |
| 114 | + 'function Component() { const foo = {}; return (<Context.Provider value={foo}></Context.Provider>) }', |
| 115 | + errors: [{ |
| 116 | + messageId: 'withIdentifierMsg', |
| 117 | + data: { |
| 118 | + variableName: 'foo', |
| 119 | + type: 'object', |
| 120 | + nodeLine: '1', |
| 121 | + usageLine: '1' |
| 122 | + } |
| 123 | + }] |
| 124 | + }, |
| 125 | + { |
| 126 | + // Invalid because array construction creates a new identity |
| 127 | + code: |
| 128 | + 'function Component() { const foo = []; return (<Context.Provider value={foo}></Context.Provider>) }', |
| 129 | + errors: [{ |
| 130 | + messageId: 'withIdentifierMsg', |
| 131 | + data: { |
| 132 | + variableName: 'foo', |
| 133 | + type: 'array', |
| 134 | + nodeLine: '1', |
| 135 | + usageLine: '1' |
| 136 | + } |
| 137 | + }] |
| 138 | + }, |
| 139 | + { |
| 140 | + // Invalid because arrow Function creates a new identity |
| 141 | + code: |
| 142 | + 'function Component() { const foo = () => {}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 143 | + errors: [{ |
| 144 | + messageId: 'withIdentifierMsgFunc', |
| 145 | + data: { |
| 146 | + variableName: 'foo', |
| 147 | + type: 'function expression', |
| 148 | + nodeLine: '1', |
| 149 | + usageLine: '1' |
| 150 | + } |
| 151 | + }] |
| 152 | + }, |
| 153 | + { |
| 154 | + // Invalid because function expression creates a new identity |
| 155 | + code: |
| 156 | + 'function Component() { const foo = function bar(){}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 157 | + errors: [{ |
| 158 | + messageId: 'withIdentifierMsgFunc', |
| 159 | + data: { |
| 160 | + variableName: 'foo', |
| 161 | + type: 'function expression', |
| 162 | + nodeLine: '1', |
| 163 | + usageLine: '1' |
| 164 | + } |
| 165 | + }] |
| 166 | + }, |
| 167 | + { |
| 168 | + // Invalid because class expression creates a new identity |
| 169 | + code: |
| 170 | + 'function Component() { const foo = class SomeClass{}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 171 | + errors: [{ |
| 172 | + messageId: 'withIdentifierMsg', |
| 173 | + data: { |
| 174 | + variableName: 'foo', |
| 175 | + type: 'class expression', |
| 176 | + nodeLine: '1', |
| 177 | + usageLine: '1' |
| 178 | + } |
| 179 | + }] |
| 180 | + }, |
| 181 | + { |
| 182 | + // Invalid because new expression creates a new identity |
| 183 | + code: |
| 184 | + 'function Component() { const foo = new SomeClass(); return (<Context.Provider value={foo}></Context.Provider>)}', |
| 185 | + errors: [{ |
| 186 | + messageId: 'withIdentifierMsg', |
| 187 | + data: { |
| 188 | + variableName: 'foo', |
| 189 | + type: 'new expression', |
| 190 | + nodeLine: '1', |
| 191 | + usageLine: '1' |
| 192 | + } |
| 193 | + }] |
| 194 | + }, |
| 195 | + { |
| 196 | + // // Invalid because function declaration creates a new identity |
| 197 | + code: |
| 198 | + 'function Component() { function foo() {}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 199 | + errors: [{ |
| 200 | + messageId: 'withIdentifierMsgFunc', |
| 201 | + data: { |
| 202 | + variableName: 'foo', |
| 203 | + type: 'function declaration', |
| 204 | + nodeLine: '1', |
| 205 | + usageLine: '1' |
| 206 | + } |
| 207 | + }] |
| 208 | + }, |
| 209 | + { |
| 210 | + // Invalid because the object value of the ternrary will create a new identity |
| 211 | + code: |
| 212 | + 'function Component() { const foo = true ? {} : "fine"; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 213 | + errors: [{ |
| 214 | + messageId: 'withIdentifierMsg', |
| 215 | + data: { |
| 216 | + variableName: 'foo', |
| 217 | + type: 'object', |
| 218 | + nodeLine: '1', |
| 219 | + usageLine: '1' |
| 220 | + } |
| 221 | + }] |
| 222 | + }, |
| 223 | + { |
| 224 | + // Invalid because the object value of the logical OR will create a new identity |
| 225 | + code: |
| 226 | + 'function Component() { const foo = bar || {}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 227 | + errors: [{ |
| 228 | + messageId: 'withIdentifierMsg', |
| 229 | + data: { |
| 230 | + variableName: 'foo', |
| 231 | + type: 'object', |
| 232 | + nodeLine: '1', |
| 233 | + usageLine: '1' |
| 234 | + } |
| 235 | + }] |
| 236 | + }, |
| 237 | + { |
| 238 | + // Invalid because the object value of the logical AND will create a new identity |
| 239 | + code: |
| 240 | + 'function Component() { const foo = bar && {}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 241 | + errors: [{ |
| 242 | + messageId: 'withIdentifierMsg', |
| 243 | + data: { |
| 244 | + variableName: 'foo', |
| 245 | + type: 'object', |
| 246 | + nodeLine: '1', |
| 247 | + usageLine: '1' |
| 248 | + } |
| 249 | + }] |
| 250 | + }, |
| 251 | + { |
| 252 | + // Invalid because the object value of the nested ternary will create a new identity |
| 253 | + code: |
| 254 | + 'function Component() { const foo = bar ? baz ? {} : null : null; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 255 | + errors: [{ |
| 256 | + messageId: 'withIdentifierMsg', |
| 257 | + data: { |
| 258 | + variableName: 'foo', |
| 259 | + type: 'object', |
| 260 | + nodeLine: '1', |
| 261 | + usageLine: '1' |
| 262 | + } |
| 263 | + }] |
| 264 | + }, |
| 265 | + { |
| 266 | + // Invalid because the object value will create a new identity |
| 267 | + code: |
| 268 | + 'function Component() { let foo = {}; return (<Context.Provider value={foo}></Context.Provider>) }', |
| 269 | + errors: [{ |
| 270 | + messageId: 'withIdentifierMsg', |
| 271 | + data: { |
| 272 | + variableName: 'foo', |
| 273 | + type: 'object', |
| 274 | + nodeLine: '1', |
| 275 | + usageLine: '1' |
| 276 | + } |
| 277 | + }] |
| 278 | + }, |
| 279 | + { |
| 280 | + // Invalid because the object value will create a new identity |
| 281 | + code: |
| 282 | + 'function Component() { var foo = {}; return (<Context.Provider value={foo}></Context.Provider>)}', |
| 283 | + errors: [{ |
| 284 | + messageId: 'withIdentifierMsg', |
| 285 | + data: { |
| 286 | + variableName: 'foo', |
| 287 | + type: 'object', |
| 288 | + nodeLine: '1', |
| 289 | + usageLine: '1' |
| 290 | + } |
| 291 | + }] |
| 292 | + }, |
| 293 | + { |
| 294 | + // Valid, but currently not handled at the moment. |
| 295 | + code: ` |
| 296 | + function Component() { |
| 297 | + let a = {}; |
| 298 | + a = 10; |
| 299 | + return (<Context.Provider value={a}></Context.Provider>); |
| 300 | + } |
| 301 | + `, |
| 302 | + errors: [{ |
| 303 | + messageId: 'withIdentifierMsg', |
| 304 | + data: { |
| 305 | + variableName: 'a', |
| 306 | + type: 'object', |
| 307 | + nodeLine: '3', |
| 308 | + usageLine: '5' |
| 309 | + } |
| 310 | + }] |
| 311 | + }, |
| 312 | + { |
| 313 | + // Invalid variable reassignment from parameter because bar is an object identity |
| 314 | + code: ` |
| 315 | + function Component() { |
| 316 | + const foo = {}; |
| 317 | + const bar = foo; |
| 318 | + return (<Context.Provider value={bar}></Context.Provider>); |
| 319 | + } |
| 320 | + `, |
| 321 | + errors: [{ |
| 322 | + messageId: 'withIdentifierMsg', |
| 323 | + data: { |
| 324 | + variableName: 'bar', |
| 325 | + type: 'object', |
| 326 | + nodeLine: '3', |
| 327 | + usageLine: '5' |
| 328 | + } |
| 329 | + }] |
| 330 | + }, |
| 331 | + { |
| 332 | + // Invalid because the object expression possibly returned from the ternary will create a new identity |
| 333 | + code: ` |
| 334 | + function Component(foo) { |
| 335 | + let bar = true ? foo : {}; |
| 336 | + return (<Context.Provider value={bar}></Context.Provider>); |
| 337 | + } |
| 338 | + `, |
| 339 | + errors: [{ |
| 340 | + messageId: 'withIdentifierMsg', |
| 341 | + data: { |
| 342 | + variableName: 'bar', |
| 343 | + type: 'object', |
| 344 | + nodeLine: '3', |
| 345 | + usageLine: '4' |
| 346 | + } |
| 347 | + }], |
| 348 | + parser: parsers.BABEL_ESLINT |
| 349 | + }, |
| 350 | + { |
| 351 | + // Invalid because inline object construction will create a new identity |
| 352 | + code: 'function Component() { return (<Context.Provider value={{foo: "bar"}}></Context.Provider>);}', |
| 353 | + errors: [{ |
| 354 | + messageId: 'defaultMsg', |
| 355 | + data: { |
| 356 | + type: 'object', |
| 357 | + nodeLine: '1', |
| 358 | + usageLine: '1' |
| 359 | + } |
| 360 | + }] |
| 361 | + }, |
| 362 | + { |
| 363 | + // Invalid because Wrapper returns JSX which has a new identity |
| 364 | + code: 'function Component() { const Wrapper = (<SomeComp />); return (<Context.Provider value={Wrapper}></Context.Provider>);}', |
| 365 | + errors: [{ |
| 366 | + messageId: 'withIdentifierMsg', |
| 367 | + data: { |
| 368 | + variableName: 'Wrapper', |
| 369 | + type: 'JSX element', |
| 370 | + nodeLine: '1', |
| 371 | + usageLine: '1' |
| 372 | + } |
| 373 | + }] |
| 374 | + }, |
| 375 | + { |
| 376 | + // Invalid because RegEx returns a new object which has will be a new identity |
| 377 | + code: 'function Component() { const someRegex = /HelloWorld/; return (<Context.Provider value={someRegex}></Context.Provider>);}', |
| 378 | + errors: [{ |
| 379 | + messageId: 'withIdentifierMsg', |
| 380 | + data: { |
| 381 | + variableName: 'someRegex', |
| 382 | + type: 'regular expression', |
| 383 | + nodeLine: '1', |
| 384 | + usageLine: '1' |
| 385 | + } |
| 386 | + }] |
| 387 | + }, |
| 388 | + { |
| 389 | + // Invalid because the right hand side of the assignment expression contains a function which will create a new identity |
| 390 | + code: ` |
| 391 | + function Component() { |
| 392 | + let foo = null; |
| 393 | + let bar = x = () => {}; |
| 394 | + return (<Context.Provider value={bar}></Context.Provider>); |
| 395 | + } |
| 396 | + `, |
| 397 | + errors: [{ |
| 398 | + messageId: 'withIdentifierMsg', |
| 399 | + data: { |
| 400 | + variableName: 'bar', |
| 401 | + type: 'assignment expression', |
| 402 | + nodeLine: '4', |
| 403 | + usageLine: '5' |
| 404 | + } |
| 405 | + }] |
| 406 | + } |
| 407 | + ] |
| 408 | +}); |
0 commit comments