|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/template-no-obscure-array-access'); |
| 6 | +const RuleTester = require('eslint').RuleTester; |
| 7 | + |
| 8 | +const ruleTester = new RuleTester({ |
| 9 | + parser: require.resolve('ember-eslint-parser'), |
| 10 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 11 | +}); |
| 12 | + |
| 13 | +// Note: @each and [] are not valid in standard Glimmer templates and cause parse errors |
| 14 | +// This rule is designed to catch edge cases or custom syntax if they were to exist |
| 15 | +// We'll use simpler valid/invalid cases that actually parse |
| 16 | +ruleTester.run('template-no-obscure-array-access', rule, { |
| 17 | + valid: [ |
| 18 | + '<template>{{items}}</template>', |
| 19 | + '<template>{{this.items}}</template>', |
| 20 | + '<template>{{#each items as |item|}}{{item.name}}{{/each}}</template>', |
| 21 | + '<template>{{get items 0}}</template>', |
| 22 | + '<template>{{items.firstObject.name}}</template>', |
| 23 | + |
| 24 | + "<template>{{foo bar=(get this 'list.0' )}}</template>", |
| 25 | + "<template><Foo @bar={{get this 'list.0'}} /></template>", |
| 26 | + "<template>{{get this 'list.0'}}</template>", |
| 27 | + '<template>{{foo bar @list}}</template>', |
| 28 | + '<template>Just a regular text in the template bar.[1] bar.1</template>', |
| 29 | + '<template><Foo foo="bar.[1]" /></template>', |
| 30 | + `<template><FooBar |
| 31 | + @subHeaderText={{if |
| 32 | + this.isFooBarV2Enabled |
| 33 | + "foobar" |
| 34 | + }} |
| 35 | + /></template>`, |
| 36 | + ], |
| 37 | + invalid: [ |
| 38 | + // Since @each and [] cause parse errors, this rule serves as documentation |
| 39 | + // In practice, the parser will catch these issues before the rule runs |
| 40 | + |
| 41 | + { |
| 42 | + code: '<template><Foo @onClick={{fn this.func @foo.0.bar}} /></template>', |
| 43 | + output: null, |
| 44 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 45 | + }, |
| 46 | + { |
| 47 | + code: '<template>{{foo bar=this.list.[0]}}</template>', |
| 48 | + output: null, |
| 49 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 50 | + }, |
| 51 | + { |
| 52 | + code: '<template>{{foo bar=@list.[1]}}</template>', |
| 53 | + output: null, |
| 54 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 55 | + }, |
| 56 | + { |
| 57 | + code: '<template>{{this.list.[0]}}</template>', |
| 58 | + output: null, |
| 59 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 60 | + }, |
| 61 | + { |
| 62 | + code: '<template>{{this.list.[0].name}}</template>', |
| 63 | + output: null, |
| 64 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 65 | + }, |
| 66 | + { |
| 67 | + code: '<template><Foo @bar={{this.list.[0]}} /></template>', |
| 68 | + output: null, |
| 69 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: '<template><Foo @bar={{this.list.[0].name.[1].foo}} /></template>', |
| 73 | + output: null, |
| 74 | + errors: [{ messageId: 'noObscureArrayAccess' }], |
| 75 | + }, |
| 76 | + ], |
| 77 | +}); |
| 78 | + |
| 79 | +const hbsRuleTester = new RuleTester({ |
| 80 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 81 | + parserOptions: { |
| 82 | + ecmaVersion: 2022, |
| 83 | + sourceType: 'module', |
| 84 | + }, |
| 85 | +}); |
| 86 | + |
| 87 | +hbsRuleTester.run('template-no-obscure-array-access', rule, { |
| 88 | + valid: [ |
| 89 | + "{{foo bar=(get this 'list.0' )}}", |
| 90 | + "<Foo @bar={{get this 'list.0'}}", |
| 91 | + "{{get this 'list.0'}}", |
| 92 | + '{{foo bar @list}}', |
| 93 | + 'Just a regular text in the template bar.[1] bar.1', |
| 94 | + '<Foo foo="bar.[1]" />', |
| 95 | + `<FooBar |
| 96 | + @subHeaderText={{if |
| 97 | + this.isFooBarV2Enabled |
| 98 | + "foobar" |
| 99 | + }} |
| 100 | + />`, |
| 101 | + ], |
| 102 | + invalid: [ |
| 103 | + { |
| 104 | + code: '<Foo @onClick={{fn this.func @foo.0.bar}} />', |
| 105 | + output: null, |
| 106 | + errors: [ |
| 107 | + { |
| 108 | + message: |
| 109 | + 'Unexpected obscure array access pattern "@foo.0.bar". Use computed properties or helpers instead.', |
| 110 | + }, |
| 111 | + ], |
| 112 | + }, |
| 113 | + { |
| 114 | + code: '{{foo bar=this.list.[0]}}', |
| 115 | + output: null, |
| 116 | + errors: [ |
| 117 | + { |
| 118 | + message: |
| 119 | + 'Unexpected obscure array access pattern "this.list.[0]". Use computed properties or helpers instead.', |
| 120 | + }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + { |
| 124 | + code: '{{foo bar=@list.[1]}}', |
| 125 | + output: null, |
| 126 | + errors: [ |
| 127 | + { |
| 128 | + message: |
| 129 | + 'Unexpected obscure array access pattern "@list.[1]". Use computed properties or helpers instead.', |
| 130 | + }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + { |
| 134 | + code: '{{this.list.[0]}}', |
| 135 | + output: null, |
| 136 | + errors: [ |
| 137 | + { |
| 138 | + message: |
| 139 | + 'Unexpected obscure array access pattern "this.list.[0]". Use computed properties or helpers instead.', |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + { |
| 144 | + code: '{{this.list.[0].name}}', |
| 145 | + output: null, |
| 146 | + errors: [ |
| 147 | + { |
| 148 | + message: |
| 149 | + 'Unexpected obscure array access pattern "this.list.[0].name". Use computed properties or helpers instead.', |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + { |
| 154 | + code: '<Foo @bar={{this.list.[0]}} />', |
| 155 | + output: null, |
| 156 | + errors: [ |
| 157 | + { |
| 158 | + message: |
| 159 | + 'Unexpected obscure array access pattern "this.list.[0]". Use computed properties or helpers instead.', |
| 160 | + }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + { |
| 164 | + code: '<Foo @bar={{this.list.[0].name.[1].foo}} />', |
| 165 | + output: null, |
| 166 | + errors: [ |
| 167 | + { |
| 168 | + message: |
| 169 | + 'Unexpected obscure array access pattern "this.list.[0].name.[1].foo". Use computed properties or helpers instead.', |
| 170 | + }, |
| 171 | + ], |
| 172 | + }, |
| 173 | + ], |
| 174 | +}); |
0 commit comments