|
| 1 | +const { RuleTester } = require('eslint'); |
| 2 | +const rule = require('../../../lib/rules/template-no-potential-path-strings'); |
| 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-potential-path-strings', rule, { |
| 10 | + valid: [ |
| 11 | + { |
| 12 | + filename: 'my-component.gjs', |
| 13 | + code: ` |
| 14 | + import Component from '@glimmer/component'; |
| 15 | + export default class MyComponent extends Component { |
| 16 | + <template> |
| 17 | + <div>{{this.propertyName}}</div> |
| 18 | + </template> |
| 19 | + } |
| 20 | + `, |
| 21 | + output: null, |
| 22 | + }, |
| 23 | + { |
| 24 | + filename: 'my-component.gjs', |
| 25 | + code: ` |
| 26 | + import Component from '@glimmer/component'; |
| 27 | + export default class MyComponent extends Component { |
| 28 | + <template> |
| 29 | + <div>Hello world</div> |
| 30 | + </template> |
| 31 | + } |
| 32 | + `, |
| 33 | + output: null, |
| 34 | + }, |
| 35 | + |
| 36 | + '<template><img src="foo.png"></template>', |
| 37 | + '<template><img src={{picture}}></template>', |
| 38 | + '<template><img src={{this.picture}}></template>', |
| 39 | + '<template><img src={{@img}}></template>', |
| 40 | + '<template><SomeComponent @foo={{@bar}} /></template>', |
| 41 | + '<template><Ui::Demo @title="@my-org/my-package" /></template>', |
| 42 | + '<template><Ui::Demo @title="@my-org\\my-package" /></template>', |
| 43 | + '<template><Ui::Demo @title="@my-org|my-package" /></template>', |
| 44 | + ], |
| 45 | + |
| 46 | + invalid: [ |
| 47 | + { |
| 48 | + filename: 'my-component.gjs', |
| 49 | + code: ` |
| 50 | + import Component from '@glimmer/component'; |
| 51 | + export default class MyComponent extends Component { |
| 52 | + <template> |
| 53 | + <div>this.propertyName</div> |
| 54 | + </template> |
| 55 | + } |
| 56 | + `, |
| 57 | + output: null, |
| 58 | + errors: [ |
| 59 | + { |
| 60 | + messageId: 'noPotentialPathStrings', |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + { |
| 65 | + filename: 'my-component.gjs', |
| 66 | + code: ` |
| 67 | + import Component from '@glimmer/component'; |
| 68 | + export default class MyComponent extends Component { |
| 69 | + <template> |
| 70 | + <div>foo.bar</div> |
| 71 | + </template> |
| 72 | + } |
| 73 | + `, |
| 74 | + output: null, |
| 75 | + errors: [ |
| 76 | + { |
| 77 | + messageId: 'noPotentialPathStrings', |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + |
| 82 | + { |
| 83 | + code: '<template><img src="this.picture"></template>', |
| 84 | + output: null, |
| 85 | + errors: [{ messageId: 'noPotentialPathStrings' }], |
| 86 | + }, |
| 87 | + { |
| 88 | + code: '<template><img src=this.picture></template>', |
| 89 | + output: null, |
| 90 | + errors: [{ messageId: 'noPotentialPathStrings' }], |
| 91 | + }, |
| 92 | + { |
| 93 | + code: '<template><img src="@img"></template>', |
| 94 | + output: null, |
| 95 | + errors: [{ messageId: 'noPotentialPathStrings' }], |
| 96 | + }, |
| 97 | + { |
| 98 | + code: '<template><img src=@img></template>', |
| 99 | + output: null, |
| 100 | + errors: [{ messageId: 'noPotentialPathStrings' }], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: '<template><SomeComponent @foo=@bar /></template>', |
| 104 | + output: null, |
| 105 | + errors: [{ messageId: 'noPotentialPathStrings' }], |
| 106 | + }, |
| 107 | + { |
| 108 | + code: '<template><SomeComponent @foo=this.bar /></template>', |
| 109 | + output: null, |
| 110 | + errors: [{ messageId: 'noPotentialPathStrings' }], |
| 111 | + }, |
| 112 | + ], |
| 113 | +}); |
| 114 | + |
| 115 | +const hbsRuleTester = new RuleTester({ |
| 116 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 117 | + parserOptions: { |
| 118 | + ecmaVersion: 2022, |
| 119 | + sourceType: 'module', |
| 120 | + }, |
| 121 | +}); |
| 122 | + |
| 123 | +hbsRuleTester.run('template-no-potential-path-strings', rule, { |
| 124 | + valid: [ |
| 125 | + '<img src="foo.png">', |
| 126 | + '<img src={{picture}}>', |
| 127 | + '<img src={{this.picture}}>', |
| 128 | + '<img src={{@img}}>', |
| 129 | + '<SomeComponent @foo={{@bar}} />', |
| 130 | + '<Ui::Demo @title="@my-org/my-package" />', |
| 131 | + '<Ui::Demo @title="@my-org\\my-package" />', |
| 132 | + '<Ui::Demo @title="@my-org|my-package" />', |
| 133 | + ], |
| 134 | + invalid: [ |
| 135 | + { |
| 136 | + code: '<img src="this.picture">', |
| 137 | + output: null, |
| 138 | + errors: [ |
| 139 | + { message: 'Potential path string detected. Use dynamic values instead of path strings.' }, |
| 140 | + ], |
| 141 | + }, |
| 142 | + { |
| 143 | + code: '<img src=this.picture>', |
| 144 | + output: null, |
| 145 | + errors: [ |
| 146 | + { message: 'Potential path string detected. Use dynamic values instead of path strings.' }, |
| 147 | + ], |
| 148 | + }, |
| 149 | + { |
| 150 | + code: '<img src="@img">', |
| 151 | + output: null, |
| 152 | + errors: [ |
| 153 | + { message: 'Potential path string detected. Use dynamic values instead of path strings.' }, |
| 154 | + ], |
| 155 | + }, |
| 156 | + { |
| 157 | + code: '<img src=@img>', |
| 158 | + output: null, |
| 159 | + errors: [ |
| 160 | + { message: 'Potential path string detected. Use dynamic values instead of path strings.' }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + { |
| 164 | + code: '<SomeComponent @foo=@bar />', |
| 165 | + output: null, |
| 166 | + errors: [ |
| 167 | + { message: 'Potential path string detected. Use dynamic values instead of path strings.' }, |
| 168 | + ], |
| 169 | + }, |
| 170 | + { |
| 171 | + code: '<SomeComponent @foo=this.bar />', |
| 172 | + output: null, |
| 173 | + errors: [ |
| 174 | + { message: 'Potential path string detected. Use dynamic values instead of path strings.' }, |
| 175 | + ], |
| 176 | + }, |
| 177 | + ], |
| 178 | +}); |
0 commit comments