|
| 1 | +const eslint = require('eslint'); |
| 2 | +const rule = require('../../../lib/rules/template-no-unnecessary-component-helper'); |
| 3 | + |
| 4 | +const { RuleTester } = eslint; |
| 5 | + |
| 6 | +const validHbs = [ |
| 7 | + '{{component SOME_COMPONENT_NAME}}', |
| 8 | + '{{component SOME_COMPONENT_NAME SOME_ARG}}', |
| 9 | + '{{component SOME_COMPONENT_NAME "Hello World"}}', |
| 10 | + '{{my-component}}', |
| 11 | + '{{my-component "Hello world"}}', |
| 12 | + '{{my-component "Hello world" 123}}', |
| 13 | + '{{#component SOME_COMPONENT_NAME}}{{/component}}', |
| 14 | + '{{#component SOME_COMPONENT_NAME SOME_ARG}}{{/component}}', |
| 15 | + '{{#component SOME_COMPONENT_NAME "Hello World"}}{{/component}}', |
| 16 | + '{{#my-component}}{{/my-component}}', |
| 17 | + '{{#my-component "Hello world"}}{{/my-component}}', |
| 18 | + '{{#my-component "Hello world" 123}}{{/my-component}}', |
| 19 | + '(component SOME_COMPONENT_NAME)', |
| 20 | + '(component "my-component")', |
| 21 | + '<Foo @bar={{component SOME_COMPONENT_NAME}} />', |
| 22 | + '<Foo @bar={{component "my-component"}} />', |
| 23 | + '<Foo @bar={{component SOME_COMPONENT_NAME}}></Foo>', |
| 24 | + '<Foo @bar={{component "my-component"}}></Foo>', |
| 25 | + '<Foo @arg="foo" />', |
| 26 | + '<Foo class="foo" />', |
| 27 | + '<Foo data-test-bar="foo" />', |
| 28 | + '<Foo @arg={{if this.user.isAdmin "admin"}} />', |
| 29 | + '<Foo @arg={{if this.user.isAdmin (component "my-component")}} />', |
| 30 | + "{{component 'addon-name@component-name'}}", |
| 31 | + "{{#component 'addon-name@component-name'}}{{/component}}", |
| 32 | +]; |
| 33 | + |
| 34 | +const invalidHbs = [ |
| 35 | + { |
| 36 | + code: '{{component "my-component-name" foo=123 bar=456}}', |
| 37 | + output: '{{my-component-name foo=123 bar=456}}', |
| 38 | + errors: [{ message: 'Invoke component directly instead of using `component` helper' }], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: '{{#component "my-component-name" foo=123 bar=456}}{{/component}}', |
| 42 | + output: '{{#my-component-name foo=123 bar=456}}{{/my-component-name}}', |
| 43 | + errors: [{ message: 'Invoke component directly instead of using `component` helper' }], |
| 44 | + }, |
| 45 | + { |
| 46 | + code: '<Foo @arg={{component "allowed-component"}}>{{component "forbidden-component"}}</Foo>', |
| 47 | + output: '<Foo @arg={{component "allowed-component"}}>{{forbidden-component}}</Foo>', |
| 48 | + errors: [{ message: 'Invoke component directly instead of using `component` helper' }], |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +const validGjs = [ |
| 53 | + '<template>{{component SOME_COMPONENT_NAME}}</template>', |
| 54 | + '<template>{{component SOME_COMPONENT_NAME SOME_ARG}}</template>', |
| 55 | + '<template>{{component SOME_COMPONENT_NAME "Hello World"}}</template>', |
| 56 | + '<template>{{my-component}}</template>', |
| 57 | + '<template>{{my-component "Hello world"}}</template>', |
| 58 | + '<template>{{my-component "Hello world" 123}}</template>', |
| 59 | + '<template>{{#component SOME_COMPONENT_NAME}}{{/component}}</template>', |
| 60 | + '<template>{{#component SOME_COMPONENT_NAME SOME_ARG}}{{/component}}</template>', |
| 61 | + '<template>{{#component SOME_COMPONENT_NAME "Hello World"}}{{/component}}</template>', |
| 62 | + '<template>{{#my-component}}{{/my-component}}</template>', |
| 63 | + '<template>{{#my-component "Hello world"}}{{/my-component}}</template>', |
| 64 | + '<template>{{#my-component "Hello world" 123}}{{/my-component}}</template>', |
| 65 | + '<template><Foo @bar={{component SOME_COMPONENT_NAME}} /></template>', |
| 66 | + '<template><Foo @bar={{component "my-component"}} /></template>', |
| 67 | + '<template><Foo @bar={{component SOME_COMPONENT_NAME}}></Foo></template>', |
| 68 | + '<template><Foo @bar={{component "my-component"}}></Foo></template>', |
| 69 | + '<template><Foo @arg="foo" /></template>', |
| 70 | + '<template><Foo class="foo" /></template>', |
| 71 | + '<template><Foo data-test-bar="foo" /></template>', |
| 72 | + '<template><Foo @arg={{if this.user.isAdmin "admin"}} /></template>', |
| 73 | + '<template><Foo @arg={{if this.user.isAdmin (component "my-component")}} /></template>', |
| 74 | + "<template>{{component 'addon-name@component-name'}}</template>", |
| 75 | + "<template>{{#component 'addon-name@component-name'}}{{/component}}</template>", |
| 76 | +]; |
| 77 | + |
| 78 | +function wrapTemplate(entry) { |
| 79 | + return { |
| 80 | + ...entry, |
| 81 | + code: `<template>${entry.code}</template>`, |
| 82 | + output: entry.output ? `<template>${entry.output}</template>` : entry.output, |
| 83 | + errors: entry.errors.map(() => ({ messageId: 'noUnnecessaryComponent' })), |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +const gjsRuleTester = new RuleTester({ |
| 88 | + parser: require.resolve('ember-eslint-parser'), |
| 89 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 90 | +}); |
| 91 | + |
| 92 | +gjsRuleTester.run('template-no-unnecessary-component-helper', rule, { |
| 93 | + valid: validGjs, |
| 94 | + invalid: invalidHbs.map(wrapTemplate), |
| 95 | +}); |
| 96 | + |
| 97 | +const hbsRuleTester = new RuleTester({ |
| 98 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 99 | + parserOptions: { |
| 100 | + ecmaVersion: 2022, |
| 101 | + sourceType: 'module', |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +hbsRuleTester.run('template-no-unnecessary-component-helper', rule, { |
| 106 | + valid: validHbs, |
| 107 | + invalid: invalidHbs, |
| 108 | +}); |
0 commit comments