|
| 1 | +/** |
| 2 | + * @fileoverview Prevents usage of && condition in JSX Embeds. |
| 3 | + * @author Anees Iqbal <[email protected]> |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const RuleTester = require('eslint').RuleTester; |
| 13 | +const rule = require('../../../lib/rules/jsx-embed-condition'); |
| 14 | +const parsers = require('../../helpers/parsers'); |
| 15 | + |
| 16 | +const parserOptions = { |
| 17 | + ecmaVersion: 2018, |
| 18 | + sourceType: 'module', |
| 19 | + ecmaFeatures: { |
| 20 | + jsx: true |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +// ------------------------------------------------------------------------------ |
| 25 | +// Tests |
| 26 | +// ------------------------------------------------------------------------------ |
| 27 | + |
| 28 | +const ruleTester = new RuleTester({parserOptions}); |
| 29 | +ruleTester.run('jsx-embed-condition', rule, { |
| 30 | + valid: [].concat({ |
| 31 | + code: '<App>Test</App>' |
| 32 | + }, { |
| 33 | + code: '<App test>Another</App>' |
| 34 | + }, { |
| 35 | + code: '<App foo={e => bar(e)}>Hello World</App>' |
| 36 | + }, { |
| 37 | + code: '<App>{x ? <div></div> : null}</App>' |
| 38 | + }, { |
| 39 | + code: '<App>{x ? <div>Hello</div> : null}</App>' |
| 40 | + }, { |
| 41 | + code: '<App>{x ? <div>{y ? <y /> : <z />}</div> : null}</App>' |
| 42 | + }, { |
| 43 | + code: '<App x={x && y}>{x ? <div>{y ? <y /> : <z />}</div> : null}</App>' |
| 44 | + }, parsers.TS({ |
| 45 | + code: '<App test>{x ?? <div />}</App>', |
| 46 | + parserOptions: { |
| 47 | + ecmaVersion: 2020 |
| 48 | + } |
| 49 | + }, { |
| 50 | + code: '<App test>{x ?? <div />}</App>', |
| 51 | + parser: parsers.TYPESCRIPT_ESLINT |
| 52 | + }, { |
| 53 | + code: '<App test>{x ?? <div />}</App>', |
| 54 | + parser: parsers['@TYPEDCRIPT_ESLINT'] |
| 55 | + })), |
| 56 | + |
| 57 | + invalid: [{ |
| 58 | + code: '<div>{x && <div />}</div>', |
| 59 | + output: '<div>{x && <div />}</div>', |
| 60 | + errors: [ |
| 61 | + {message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'} |
| 62 | + ] |
| 63 | + }, { |
| 64 | + code: '<div>{x ? <div>{y && <div />}</div> : null}</div>', |
| 65 | + output: '<div>{x ? <div>{y && <div />}</div> : null}</div>', |
| 66 | + errors: [ |
| 67 | + {message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'} |
| 68 | + ] |
| 69 | + }] |
| 70 | +}); |
0 commit comments