|
| 1 | +const rule = require('../../../lib/rules/template-no-nested-splattributes'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + parser: require.resolve('ember-eslint-parser'), |
| 6 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 7 | +}); |
| 8 | + |
| 9 | +ruleTester.run('template-no-nested-splattributes', rule, { |
| 10 | + valid: [ |
| 11 | + // Note: In standalone gjs/gts templates, the concept of "top-level" is different. |
| 12 | + // These tests focus on the clear nested cases. |
| 13 | + '<template><div><p>No splattributes here</p></div></template>', |
| 14 | + |
| 15 | + '<template><div>...</div></template>', |
| 16 | + '<template><div><div ...attributes>...</div></div></template>', |
| 17 | + '<template><div ...attributes>...</div></template>', |
| 18 | + '<template><div ...attributes>...</div><div ...attributes>...</div></template>', |
| 19 | + ], |
| 20 | + |
| 21 | + invalid: [ |
| 22 | + { |
| 23 | + code: '<template><div ...attributes><span ...attributes>Text</span></div></template>', |
| 24 | + output: null, |
| 25 | + errors: [ |
| 26 | + { |
| 27 | + message: |
| 28 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 29 | + type: 'GlimmerAttrNode', |
| 30 | + }, |
| 31 | + ], |
| 32 | + }, |
| 33 | + { |
| 34 | + code: '<template><section ...attributes><div><button ...attributes>Click</button></div></section></template>', |
| 35 | + output: null, |
| 36 | + errors: [ |
| 37 | + { |
| 38 | + message: |
| 39 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 40 | + type: 'GlimmerAttrNode', |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + { |
| 45 | + code: '<template><div ...attributes class="wrapper"><input ...attributes /></div></template>', |
| 46 | + output: null, |
| 47 | + errors: [ |
| 48 | + { |
| 49 | + message: |
| 50 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 51 | + type: 'GlimmerAttrNode', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + |
| 56 | + { |
| 57 | + code: `<template><div ...attributes> |
| 58 | + <div ...attributes> |
| 59 | + ... |
| 60 | + </div> |
| 61 | +</div> |
| 62 | +</template>`, |
| 63 | + output: null, |
| 64 | + errors: [ |
| 65 | + { |
| 66 | + message: |
| 67 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: `<template><div ...attributes> |
| 73 | + <div> |
| 74 | + <div ...attributes> |
| 75 | + ... |
| 76 | + </div> |
| 77 | + </div> |
| 78 | +</div> |
| 79 | +</template>`, |
| 80 | + output: null, |
| 81 | + errors: [ |
| 82 | + { |
| 83 | + message: |
| 84 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + ], |
| 89 | +}); |
| 90 | + |
| 91 | +const hbsRuleTester = new RuleTester({ |
| 92 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 93 | + parserOptions: { |
| 94 | + ecmaVersion: 2022, |
| 95 | + sourceType: 'module', |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +hbsRuleTester.run('template-no-nested-splattributes', rule, { |
| 100 | + valid: [ |
| 101 | + '<div>...</div>', |
| 102 | + '<div><div ...attributes>...</div></div>', |
| 103 | + '<div ...attributes>...</div>', |
| 104 | + '<div ...attributes>...</div><div ...attributes>...</div>', |
| 105 | + ], |
| 106 | + invalid: [ |
| 107 | + { |
| 108 | + code: '<div ...attributes>\n <div ...attributes>\n ...\n </div>\n</div>\n', |
| 109 | + output: null, |
| 110 | + errors: [ |
| 111 | + { |
| 112 | + message: |
| 113 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 114 | + }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + { |
| 118 | + code: '<div ...attributes>\n <div>\n <div ...attributes>\n ...\n </div>\n </div>\n</div>\n', |
| 119 | + output: null, |
| 120 | + errors: [ |
| 121 | + { |
| 122 | + message: |
| 123 | + 'Do not use ...attributes on nested elements. Only use it on the top-level element of a component.', |
| 124 | + }, |
| 125 | + ], |
| 126 | + }, |
| 127 | + ], |
| 128 | +}); |
0 commit comments