|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/template-style-concatenation'); |
| 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-style-concatenation', rule, { |
| 18 | + valid: [ |
| 19 | + `<template> |
| 20 | + <div style="color: red;">Content</div> |
| 21 | + </template>`, |
| 22 | + `<template> |
| 23 | + <div style={{this.computedStyle}}>Content</div> |
| 24 | + </template>`, |
| 25 | + `<template> |
| 26 | + <div style={{html-safe this.styleString}}>Content</div> |
| 27 | + </template>`, |
| 28 | + |
| 29 | + '<template><img></template>', |
| 30 | + '<template><img style={{myStyle}}></template>', |
| 31 | + '<template><img style={{background-image url}}></template>', |
| 32 | + '<template><img style="background-image: url(/foo.png)"}}></template>', |
| 33 | + '<template><img style={{html-safe (concat "background-image: url(" url ")")}}></template>', |
| 34 | + '<template><img style={{html-safe (concat knownSafeStyle1 ";" knownSafeStyle2)}}></template>', |
| 35 | + ], |
| 36 | + |
| 37 | + invalid: [ |
| 38 | + { |
| 39 | + code: `<template> |
| 40 | + <div style="color: {{this.color}};">Content</div> |
| 41 | + </template>`, |
| 42 | + output: null, |
| 43 | + errors: [ |
| 44 | + { |
| 45 | + message: |
| 46 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 47 | + type: 'GlimmerAttrNode', |
| 48 | + }, |
| 49 | + ], |
| 50 | + }, |
| 51 | + { |
| 52 | + code: `<template> |
| 53 | + <div style={{concat "width: " this.width "px;"}}>Content</div> |
| 54 | + </template>`, |
| 55 | + output: null, |
| 56 | + errors: [ |
| 57 | + { |
| 58 | + message: |
| 59 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 60 | + type: 'GlimmerAttrNode', |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + |
| 65 | + { |
| 66 | + code: '<template><img style="{{myStyle}}"></template>', |
| 67 | + output: null, |
| 68 | + errors: [ |
| 69 | + { |
| 70 | + message: |
| 71 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { |
| 76 | + code: '<template><img style="background-image: {{url}}"></template>', |
| 77 | + output: null, |
| 78 | + errors: [ |
| 79 | + { |
| 80 | + message: |
| 81 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + { |
| 86 | + code: '<template><img style="{{background-image url}}"></template>', |
| 87 | + output: null, |
| 88 | + errors: [ |
| 89 | + { |
| 90 | + message: |
| 91 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + { |
| 96 | + code: '<template><img style={{concat knownSafeStyle1 ";" knownSafeStyle2}}></template>', |
| 97 | + output: null, |
| 98 | + errors: [ |
| 99 | + { |
| 100 | + message: |
| 101 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + ], |
| 106 | +}); |
| 107 | + |
| 108 | +const hbsRuleTester = new RuleTester({ |
| 109 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 110 | + parserOptions: { |
| 111 | + ecmaVersion: 2022, |
| 112 | + sourceType: 'module', |
| 113 | + }, |
| 114 | +}); |
| 115 | + |
| 116 | +hbsRuleTester.run('template-style-concatenation', rule, { |
| 117 | + valid: [ |
| 118 | + '<img>', |
| 119 | + '<img style={{myStyle}}>', |
| 120 | + '<img style={{background-image url}}>', |
| 121 | + '<img style="background-image: url(/foo.png)"}}>', |
| 122 | + '<img style={{html-safe (concat "background-image: url(" url ")")}}>', |
| 123 | + '<img style={{html-safe (concat knownSafeStyle1 ";" knownSafeStyle2)}}>', |
| 124 | + ], |
| 125 | + invalid: [ |
| 126 | + { |
| 127 | + code: '<img style="{{myStyle}}">', |
| 128 | + output: null, |
| 129 | + errors: [ |
| 130 | + { |
| 131 | + message: |
| 132 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 133 | + }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + { |
| 137 | + code: '<img style="background-image: {{url}}">', |
| 138 | + output: null, |
| 139 | + errors: [ |
| 140 | + { |
| 141 | + message: |
| 142 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + { |
| 147 | + code: '<img style="{{background-image url}}">', |
| 148 | + output: null, |
| 149 | + errors: [ |
| 150 | + { |
| 151 | + message: |
| 152 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 153 | + }, |
| 154 | + ], |
| 155 | + }, |
| 156 | + { |
| 157 | + code: '<img style={{concat knownSafeStyle1 ";" knownSafeStyle2}}>', |
| 158 | + output: null, |
| 159 | + errors: [ |
| 160 | + { |
| 161 | + message: |
| 162 | + 'Avoid string concatenation in style attributes. Use a computed property with htmlSafe instead.', |
| 163 | + }, |
| 164 | + ], |
| 165 | + }, |
| 166 | + ], |
| 167 | +}); |
0 commit comments