|
| 1 | +const eslint = require('eslint'); |
| 2 | +const rule = require('../../../lib/rules/template-no-unnecessary-component-helper'); |
| 3 | + |
| 4 | +const { RuleTester } = eslint; |
| 5 | + |
| 6 | +const ruleTester = new RuleTester({ |
| 7 | + parser: require.resolve('ember-eslint-parser'), |
| 8 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 9 | +}); |
| 10 | + |
| 11 | +ruleTester.run('template-no-unnecessary-component-helper', rule, { |
| 12 | + valid: [ |
| 13 | + // Angle bracket invocation |
| 14 | + '<template><MyComponent /></template>', |
| 15 | + |
| 16 | + // Dynamic component names (necessary use) |
| 17 | + '<template>{{component this.componentName}}</template>', |
| 18 | + '<template>{{component @componentName}}</template>', |
| 19 | + |
| 20 | + // No component helper |
| 21 | + '<template>{{my-helper}}</template>', |
| 22 | + |
| 23 | + // Dynamic component with extra args (MustacheStatement) |
| 24 | + '<template>{{component SOME_COMPONENT_NAME}}</template>', |
| 25 | + '<template>{{component SOME_COMPONENT_NAME SOME_ARG}}</template>', |
| 26 | + '<template>{{component SOME_COMPONENT_NAME "Hello World"}}</template>', |
| 27 | + |
| 28 | + // Regular component invocations (not using component helper) |
| 29 | + '<template>{{my-component "Hello world"}}</template>', |
| 30 | + '<template>{{my-component "Hello world" 123}}</template>', |
| 31 | + |
| 32 | + // Block statements with dynamic component name |
| 33 | + '<template>{{#component SOME_COMPONENT_NAME}}{{/component}}</template>', |
| 34 | + '<template>{{#component SOME_COMPONENT_NAME SOME_ARG}}{{/component}}</template>', |
| 35 | + '<template>{{#component SOME_COMPONENT_NAME "Hello World"}}{{/component}}</template>', |
| 36 | + |
| 37 | + // Regular block components (no component helper) |
| 38 | + '<template>{{#my-component}}{{/my-component}}</template>', |
| 39 | + '<template>{{#my-component "Hello world"}}{{/my-component}}</template>', |
| 40 | + '<template>{{#my-component "Hello world" 123}}{{/my-component}}</template>', |
| 41 | + |
| 42 | + // Dynamic in angle bracket attribute (valid - first param not string) |
| 43 | + '<template><Foo @bar={{component SOME_COMPONENT_NAME}} /></template>', |
| 44 | + '<template><Foo @bar={{component SOME_COMPONENT_NAME}}></Foo></template>', |
| 45 | + |
| 46 | + // Static args on angle bracket (no component helper) |
| 47 | + '<template><Foo @arg="foo" /></template>', |
| 48 | + |
| 49 | + // If expressions without (component) - should not trigger |
| 50 | + '<template><Foo @arg={{if this.user.isAdmin "admin"}} /></template>', |
| 51 | + ], |
| 52 | + invalid: [ |
| 53 | + { |
| 54 | + code: '<template>{{component "my-component"}}</template>', |
| 55 | + output: '<template>{{my-component}}</template>', |
| 56 | + errors: [{ messageId: 'noUnnecessaryComponent' }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: '<template>{{component "MyComponent"}}</template>', |
| 60 | + output: '<template>{{MyComponent}}</template>', |
| 61 | + errors: [{ messageId: 'noUnnecessaryComponent' }], |
| 62 | + }, |
| 63 | + |
| 64 | + { |
| 65 | + code: '<template>{{component "my-component-name" foo=123 bar=456}}</template>', |
| 66 | + output: '<template>{{my-component-name foo=123 bar=456}}</template>', |
| 67 | + errors: [{ messageId: 'noUnnecessaryComponent' }], |
| 68 | + }, |
| 69 | + { |
| 70 | + code: '<template>{{#component "my-component-name" foo=123 bar=456}}{{/component}}</template>', |
| 71 | + output: null, |
| 72 | + errors: [{ messageId: 'noUnnecessaryComponent' }], |
| 73 | + }, |
| 74 | + { |
| 75 | + code: '<template><Foo @arg={{component "allowed-component"}}>{{component "forbidden-component"}}</Foo></template>', |
| 76 | + output: |
| 77 | + '<template><Foo @arg={{component "allowed-component"}}>{{forbidden-component}}</Foo></template>', |
| 78 | + errors: [{ messageId: 'noUnnecessaryComponent' }], |
| 79 | + }, |
| 80 | + ], |
| 81 | +}); |
| 82 | + |
| 83 | +const hbsRuleTester = new RuleTester({ |
| 84 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 85 | + parserOptions: { |
| 86 | + ecmaVersion: 2022, |
| 87 | + sourceType: 'module', |
| 88 | + }, |
| 89 | +}); |
| 90 | + |
| 91 | +hbsRuleTester.run('template-no-unnecessary-component-helper', rule, { |
| 92 | + valid: [ |
| 93 | + '{{component SOME_COMPONENT_NAME}}', |
| 94 | + '{{component SOME_COMPONENT_NAME SOME_ARG}}', |
| 95 | + '{{component SOME_COMPONENT_NAME "Hello World"}}', |
| 96 | + '{{my-component}}', |
| 97 | + '{{my-component "Hello world"}}', |
| 98 | + '{{my-component "Hello world" 123}}', |
| 99 | + '{{#component SOME_COMPONENT_NAME}}{{/component}}', |
| 100 | + '{{#component SOME_COMPONENT_NAME SOME_ARG}}{{/component}}', |
| 101 | + '{{#component SOME_COMPONENT_NAME "Hello World"}}{{/component}}', |
| 102 | + '{{#my-component}}{{/my-component}}', |
| 103 | + '{{#my-component "Hello world"}}{{/my-component}}', |
| 104 | + '{{#my-component "Hello world" 123}}{{/my-component}}', |
| 105 | + '(component SOME_COMPONENT_NAME)', |
| 106 | + '(component "my-component")', |
| 107 | + '<Foo @bar={{component SOME_COMPONENT_NAME}} />', |
| 108 | + '<Foo @bar={{component SOME_COMPONENT_NAME}}></Foo>', |
| 109 | + '<Foo @arg="foo" />', |
| 110 | + '<Foo class="foo" />', |
| 111 | + '<Foo data-test-bar="foo" />', |
| 112 | + '<Foo @arg={{if this.user.isAdmin "admin"}} />', |
| 113 | + // component helper in attribute values is safe (passing components as args) |
| 114 | + '<Foo @bar={{component "my-component"}} />', |
| 115 | + '<Foo @bar={{component "my-component"}}></Foo>', |
| 116 | + '<Foo @arg={{if this.user.isAdmin (component "my-component")}} />', |
| 117 | + // addon-scoped component names with @ are allowed |
| 118 | + '{{component "addon-name@component-name"}}', |
| 119 | + '{{#component "addon-name@component-name"}}{{/component}}', |
| 120 | + ], |
| 121 | + invalid: [ |
| 122 | + { |
| 123 | + code: '{{component "my-component-name" foo=123 bar=456}}', |
| 124 | + output: '{{my-component-name foo=123 bar=456}}', |
| 125 | + errors: [ |
| 126 | + { message: 'Unnecessary use of (component) helper. Use the component name directly.' }, |
| 127 | + ], |
| 128 | + }, |
| 129 | + { |
| 130 | + code: '{{#component "my-component-name" foo=123 bar=456}}{{/component}}', |
| 131 | + output: null, |
| 132 | + errors: [ |
| 133 | + { message: 'Unnecessary use of (component) helper. Use the component name directly.' }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + { |
| 137 | + code: '<Foo @arg={{component "allowed-component"}}>{{component "forbidden-component"}}</Foo>', |
| 138 | + output: '<Foo @arg={{component "allowed-component"}}>{{forbidden-component}}</Foo>', |
| 139 | + errors: [ |
| 140 | + { message: 'Unnecessary use of (component) helper. Use the component name directly.' }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + ], |
| 144 | +}); |
0 commit comments