|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/template-no-duplicate-attributes'); |
| 6 | +const RuleTester = require('eslint').RuleTester; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Tests |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const ruleTester = new RuleTester({ |
| 13 | + parser: require.resolve('ember-eslint-parser'), |
| 14 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 15 | +}); |
| 16 | + |
| 17 | +ruleTester.run('template-no-duplicate-attributes', rule, { |
| 18 | + valid: [ |
| 19 | + `<template> |
| 20 | + <div class="foo" id="bar"></div> |
| 21 | + </template>`, |
| 22 | + `<template> |
| 23 | + <button type="button" disabled></button> |
| 24 | + </template>`, |
| 25 | + `<template> |
| 26 | + {{helper arg1="a" arg2="b"}} |
| 27 | + </template>`, |
| 28 | + `<template> |
| 29 | + {{#each items as |item|}} |
| 30 | + {{item}} |
| 31 | + {{/each}} |
| 32 | + </template>`, |
| 33 | + |
| 34 | + '<template>{{my-component firstName=firstName lastName=lastName}}</template>', |
| 35 | + '<template> {{fullName}}</template>', |
| 36 | + '<template><a class="btn">{{btnLabel}}</a></template>', |
| 37 | + '<template>{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}</template>', |
| 38 | + '<template>{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}</template>', |
| 39 | + |
| 40 | + // Block form with params (no duplicates) |
| 41 | + '<template>{{#my-component firstName=firstName lastName=lastName as |fullName|}}{{fullName}}{{/my-component}}</template>', |
| 42 | + ], |
| 43 | + |
| 44 | + invalid: [ |
| 45 | + { |
| 46 | + code: `<template> |
| 47 | + <div class="foo" class="bar"></div> |
| 48 | + </template>`, |
| 49 | + output: `<template> |
| 50 | + <div class="foo"></div> |
| 51 | + </template>`, |
| 52 | + errors: [ |
| 53 | + { |
| 54 | + message: "Duplicate attribute 'class' found in the Element.", |
| 55 | + type: 'GlimmerAttrNode', |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + { |
| 60 | + code: `<template> |
| 61 | + <input type="text" disabled type="email" /> |
| 62 | + </template>`, |
| 63 | + output: `<template> |
| 64 | + <input type="text" disabled /> |
| 65 | + </template>`, |
| 66 | + errors: [ |
| 67 | + { |
| 68 | + message: "Duplicate attribute 'type' found in the Element.", |
| 69 | + type: 'GlimmerAttrNode', |
| 70 | + }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + { |
| 74 | + code: `<template> |
| 75 | + {{helper foo="bar" foo="baz"}} |
| 76 | + </template>`, |
| 77 | + output: `<template> |
| 78 | + {{helper foo="bar"}} |
| 79 | + </template>`, |
| 80 | + errors: [ |
| 81 | + { |
| 82 | + message: "Duplicate attribute 'foo' found in the MustacheStatement.", |
| 83 | + type: 'GlimmerHashPair', |
| 84 | + }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + { |
| 88 | + code: `<template> |
| 89 | + {{#if condition key="a" key="b"}} |
| 90 | + content |
| 91 | + {{/if}} |
| 92 | + </template>`, |
| 93 | + output: `<template> |
| 94 | + {{#if condition key="a"}} |
| 95 | + content |
| 96 | + {{/if}} |
| 97 | + </template>`, |
| 98 | + errors: [ |
| 99 | + { |
| 100 | + message: "Duplicate attribute 'key' found in the BlockStatement.", |
| 101 | + type: 'GlimmerHashPair', |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + |
| 106 | + { |
| 107 | + code: '<template>{{my-component firstName=firstName lastName=lastName firstName=firstName}}</template>', |
| 108 | + output: '<template>{{my-component firstName=firstName lastName=lastName}}</template>', |
| 109 | + errors: [{ messageId: 'duplicateMustache', data: { name: 'firstName' } }], |
| 110 | + }, |
| 111 | + { |
| 112 | + code: '<template>{{#my-component firstName=firstName lastName=lastName firstName=firstName as |fullName|}}{{/my-component}}</template>', |
| 113 | + output: |
| 114 | + '<template>{{#my-component firstName=firstName lastName=lastName as |fullName|}}{{/my-component}}</template>', |
| 115 | + errors: [{ messageId: 'duplicateBlock', data: { name: 'firstName' } }], |
| 116 | + }, |
| 117 | + { |
| 118 | + code: '<template><a class="btn" class="btn">{{btnLabel}}</a></template>', |
| 119 | + output: '<template><a class="btn">{{btnLabel}}</a></template>', |
| 120 | + errors: [{ messageId: 'duplicateElement', data: { name: 'class' } }], |
| 121 | + }, |
| 122 | + { |
| 123 | + code: '<template>{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age firstName=firstName)}}</template>', |
| 124 | + output: |
| 125 | + '<template>{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}</template>', |
| 126 | + errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }], |
| 127 | + }, |
| 128 | + { |
| 129 | + code: '<template>{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName firstName=firstName) age=age)}}</template>', |
| 130 | + output: |
| 131 | + '<template>{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}</template>', |
| 132 | + errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }], |
| 133 | + }, |
| 134 | + ], |
| 135 | +}); |
| 136 | + |
| 137 | +const hbsRuleTester = new RuleTester({ |
| 138 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 139 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 140 | +}); |
| 141 | + |
| 142 | +hbsRuleTester.run('template-no-duplicate-attributes (hbs)', rule, { |
| 143 | + valid: [ |
| 144 | + '{{my-component firstName=firstName lastName=lastName}}', |
| 145 | + '{{#my-component firstName=firstName lastName=lastName as |fullName|}} {{fullName}}{{/my-component}}', |
| 146 | + '<a class="btn">{{btnLabel}}</a>', |
| 147 | + '{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}', |
| 148 | + '{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}', |
| 149 | + ], |
| 150 | + invalid: [ |
| 151 | + { |
| 152 | + code: '{{my-component firstName=firstName lastName=lastName firstName=firstName}}', |
| 153 | + output: '{{my-component firstName=firstName lastName=lastName}}', |
| 154 | + errors: [{ messageId: 'duplicateMustache', data: { name: 'firstName' } }], |
| 155 | + }, |
| 156 | + { |
| 157 | + code: '{{#my-component firstName=firstName lastName=lastName firstName=firstName as |fullName|}} {{fullName}}{{/my-component}}', |
| 158 | + output: |
| 159 | + '{{#my-component firstName=firstName lastName=lastName as |fullName|}} {{fullName}}{{/my-component}}', |
| 160 | + errors: [{ messageId: 'duplicateBlock', data: { name: 'firstName' } }], |
| 161 | + }, |
| 162 | + { |
| 163 | + code: '<a class="btn" class="btn">{{btnLabel}}</a>', |
| 164 | + output: '<a class="btn">{{btnLabel}}</a>', |
| 165 | + errors: [{ messageId: 'duplicateElement', data: { name: 'class' } }], |
| 166 | + }, |
| 167 | + { |
| 168 | + code: '{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age firstName=firstName)}}', |
| 169 | + output: '{{employee-profile employee=(hash firstName=firstName lastName=lastName age=age)}}', |
| 170 | + errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }], |
| 171 | + }, |
| 172 | + { |
| 173 | + code: '{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName firstName=firstName) age=age)}}', |
| 174 | + output: |
| 175 | + '{{employee-profile employee=(hash fullName=(hash firstName=firstName lastName=lastName) age=age)}}', |
| 176 | + errors: [{ messageId: 'duplicateSubExpr', data: { name: 'firstName' } }], |
| 177 | + }, |
| 178 | + ], |
| 179 | +}); |
0 commit comments