|
| 1 | +const rule = require('../../../lib/rules/template-no-builtin-form-components'); |
| 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-builtin-form-components', rule, { |
| 10 | + valid: [ |
| 11 | + // Native HTML elements are always fine |
| 12 | + { filename: 'test.gjs', code: '<template><input type="text" /></template>' }, |
| 13 | + { filename: 'test.gjs', code: '<template><input type="checkbox" /></template>' }, |
| 14 | + { filename: 'test.gjs', code: '<template><input type="radio" /></template>' }, |
| 15 | + { filename: 'test.gjs', code: '<template><textarea></textarea></template>' }, |
| 16 | + { filename: 'test.gjs', code: '<template><div></div></template>' }, |
| 17 | + |
| 18 | + // In GJS without an import from @ember/component, <Input>/<Textarea> are not the builtins |
| 19 | + { filename: 'test.gjs', code: '<template><Input /></template>' }, |
| 20 | + { filename: 'test.gjs', code: '<template><Textarea></Textarea></template>' }, |
| 21 | + |
| 22 | + // Importing from a different source is fine |
| 23 | + { |
| 24 | + filename: 'test.gjs', |
| 25 | + code: "import { Input } from './my-components'; <template><Input /></template>", |
| 26 | + }, |
| 27 | + { |
| 28 | + filename: 'test.gjs', |
| 29 | + code: "import { Textarea } from './my-components'; <template><Textarea></Textarea></template>", |
| 30 | + }, |
| 31 | + ], |
| 32 | + invalid: [ |
| 33 | + { |
| 34 | + filename: 'test.gjs', |
| 35 | + code: "import { Input } from '@ember/component'; <template><Input /></template>", |
| 36 | + output: null, |
| 37 | + errors: [{ messageId: 'noInput' }], |
| 38 | + }, |
| 39 | + { |
| 40 | + filename: 'test.gjs', |
| 41 | + code: 'import { Input } from \'@ember/component\'; <template><Input type="text" /></template>', |
| 42 | + output: null, |
| 43 | + errors: [{ messageId: 'noInput' }], |
| 44 | + }, |
| 45 | + { |
| 46 | + // Aliased import must still be flagged |
| 47 | + filename: 'test.gjs', |
| 48 | + code: "import { Input as EmberInput } from '@ember/component'; <template><EmberInput /></template>", |
| 49 | + output: null, |
| 50 | + errors: [{ messageId: 'noInput' }], |
| 51 | + }, |
| 52 | + { |
| 53 | + filename: 'test.gjs', |
| 54 | + code: "import { Textarea } from '@ember/component'; <template><Textarea></Textarea></template>", |
| 55 | + output: null, |
| 56 | + errors: [{ messageId: 'noTextarea' }], |
| 57 | + }, |
| 58 | + { |
| 59 | + filename: 'test.gjs', |
| 60 | + code: "import { Textarea } from '@ember/component'; <template><Textarea @value={{this.body}}></Textarea></template>", |
| 61 | + output: null, |
| 62 | + errors: [{ messageId: 'noTextarea' }], |
| 63 | + }, |
| 64 | + { |
| 65 | + // Aliased Textarea import must still be flagged |
| 66 | + filename: 'test.gjs', |
| 67 | + code: "import { Textarea as EmberTextarea } from '@ember/component'; <template><EmberTextarea></EmberTextarea></template>", |
| 68 | + output: null, |
| 69 | + errors: [{ messageId: 'noTextarea' }], |
| 70 | + }, |
| 71 | + // Yielded as a value |
| 72 | + { |
| 73 | + filename: 'test.gjs', |
| 74 | + code: "import { Input } from '@ember/component'; <template>{{yield Input}}</template>", |
| 75 | + output: null, |
| 76 | + errors: [{ messageId: 'noInput' }], |
| 77 | + }, |
| 78 | + { |
| 79 | + filename: 'test.gjs', |
| 80 | + code: "import { Input as EmberInput } from '@ember/component'; <template>{{yield EmberInput}}</template>", |
| 81 | + output: null, |
| 82 | + errors: [{ messageId: 'noInput' }], |
| 83 | + }, |
| 84 | + { |
| 85 | + filename: 'test.gjs', |
| 86 | + code: "import { Textarea } from '@ember/component'; <template>{{yield Textarea}}</template>", |
| 87 | + output: null, |
| 88 | + errors: [{ messageId: 'noTextarea' }], |
| 89 | + }, |
| 90 | + // Used in helpers / passed as argument |
| 91 | + { |
| 92 | + filename: 'test.gjs', |
| 93 | + code: "import { Input } from '@ember/component'; <template><MyForm @field={{Input}} /></template>", |
| 94 | + output: null, |
| 95 | + errors: [{ messageId: 'noInput' }], |
| 96 | + }, |
| 97 | + { |
| 98 | + filename: 'test.gjs', |
| 99 | + code: "import { Input } from '@ember/component'; <template>{{component Input}}</template>", |
| 100 | + output: null, |
| 101 | + errors: [{ messageId: 'noInput' }], |
| 102 | + }, |
| 103 | + ], |
| 104 | +}); |
| 105 | + |
| 106 | +const hbsRuleTester = new RuleTester({ |
| 107 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 108 | + parserOptions: { |
| 109 | + ecmaVersion: 2022, |
| 110 | + sourceType: 'module', |
| 111 | + }, |
| 112 | +}); |
| 113 | + |
| 114 | +hbsRuleTester.run('template-no-builtin-form-components (hbs)', rule, { |
| 115 | + valid: [ |
| 116 | + '<input type="text" />', |
| 117 | + '<input type="checkbox" />', |
| 118 | + '<input type="radio" />', |
| 119 | + '<textarea></textarea>', |
| 120 | + ], |
| 121 | + invalid: [ |
| 122 | + { |
| 123 | + code: '<Input />', |
| 124 | + output: null, |
| 125 | + errors: [{ messageId: 'noInput' }], |
| 126 | + }, |
| 127 | + { |
| 128 | + code: '<Input type="text" />', |
| 129 | + output: null, |
| 130 | + errors: [{ messageId: 'noInput' }], |
| 131 | + }, |
| 132 | + { |
| 133 | + code: '<Textarea></Textarea>', |
| 134 | + output: null, |
| 135 | + errors: [{ messageId: 'noTextarea' }], |
| 136 | + }, |
| 137 | + { |
| 138 | + code: '<Textarea @value={{this.body}}></Textarea>', |
| 139 | + output: null, |
| 140 | + errors: [{ messageId: 'noTextarea' }], |
| 141 | + }, |
| 142 | + // Yielded as a value |
| 143 | + { |
| 144 | + code: '{{yield Input}}', |
| 145 | + output: null, |
| 146 | + errors: [{ messageId: 'noInput' }], |
| 147 | + }, |
| 148 | + { |
| 149 | + code: '{{yield Textarea}}', |
| 150 | + output: null, |
| 151 | + errors: [{ messageId: 'noTextarea' }], |
| 152 | + }, |
| 153 | + // Used in helpers / passed as argument |
| 154 | + { |
| 155 | + code: '<MyForm @field={{Input}} />', |
| 156 | + output: null, |
| 157 | + errors: [{ messageId: 'noInput' }], |
| 158 | + }, |
| 159 | + { |
| 160 | + code: '{{component Input}}', |
| 161 | + output: null, |
| 162 | + errors: [{ messageId: 'noInput' }], |
| 163 | + }, |
| 164 | + ], |
| 165 | +}); |
0 commit comments