-
-
Notifications
You must be signed in to change notification settings - Fork 211
Extract rule: template-no-index-component-invocation #2439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NullVoxPopuli
merged 1 commit into
ember-cli:master
from
NullVoxPopuli:nvp/template-lint-extract-rule-template-no-index-component-invocation
Mar 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # ember/template-no-index-component-invocation | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Disallows invoking components using an explicit `/index` or `::Index` suffix. | ||
|
|
||
| Components and Component Templates can be structured as `app/components/foo-bar/index.js` and | ||
| `app/components/foo-bar/index.hbs`. This allows additional files related to the | ||
| component (such as a `README.md` file) to be co-located on the filesystem. | ||
|
|
||
| For template-only components, they can be either `app/components/foo-bar.hbs` | ||
| or `app/components/foo-bar/index.hbs` without a corresponding JavaScript file. | ||
|
|
||
| Similarly, for addons, templates can be placed inside `addon/components` with | ||
| the same rules laid out above. | ||
|
|
||
| In all of these case, if a template file is present in `app/components` or | ||
| `addon/components`, it will take precedence over any corresponding template | ||
| files in `app/templates`, the `layout` property on classic components, or a | ||
| template with the same name that is made available with the resolver API. | ||
| Instead of being resolved at runtime, a template in `app/components` will be | ||
| associated with the component's JavaScript class at build time. | ||
|
|
||
| ## Examples | ||
|
|
||
| This rule **forbids** the following: | ||
|
|
||
| ```gjs | ||
| <template><Foo::Index /></template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template>{{component 'foo/index'}}</template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template>{{foo/index}}</template> | ||
| ``` | ||
|
|
||
| This rule **allows** the following: | ||
|
|
||
| ```gjs | ||
| <template><Foo /></template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template>{{component 'foo'}}</template> | ||
| ``` | ||
|
|
||
| ```gjs | ||
| <template>{{foo}}</template> | ||
| ``` | ||
|
|
||
| ## Migration | ||
|
|
||
| - replace all `::Index>` to `>` | ||
| - replace all `/index}}` to `}}` | ||
|
|
||
| ## References | ||
|
|
||
| - [RFC #481](https://github.com/emberjs/rfcs/blob/master/text/0481-component-templates-co-location.md#high-level-design) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* eslint-disable complexity, eslint-plugin/prefer-placeholders, unicorn/explicit-length-check */ | ||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'disallow index component invocations', | ||
| category: 'Best Practices', | ||
| recommended: false, | ||
| url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-index-component-invocation.md', | ||
| templateMode: 'both', | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| messages: {}, | ||
| originallyFrom: { | ||
| name: 'ember-template-lint', | ||
| rule: 'lib/rules/no-index-component-invocation.js', | ||
| docs: 'docs/rule/no-index-component-invocation.md', | ||
| tests: 'test/unit/rules/no-index-component-invocation-test.js', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| function lintIndexUsage(node) { | ||
| // Handle angle bracket components: <Foo::Index /> | ||
| if (node.type === 'GlimmerElementNode') { | ||
| if (node.tag && node.tag.endsWith('::Index')) { | ||
| const invocation = `<${node.tag}`; | ||
| const replacement = `<${node.tag.replace('::Index', '')}`; | ||
|
|
||
| context.report({ | ||
| node, | ||
| message: `Replace \`${invocation} ...\` to \`${replacement} ...\``, | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Handle mustache and block statements: {{foo/index}} or {{#foo/index}} | ||
| if (node.type === 'GlimmerMustacheStatement' || node.type === 'GlimmerBlockStatement') { | ||
| if ( | ||
| node.path && | ||
| node.path.type === 'GlimmerPathExpression' && | ||
| node.path.original && | ||
| node.path.original.endsWith('/index') | ||
| ) { | ||
| const invocationPrefix = node.type === 'GlimmerBlockStatement' ? '{{#' : '{{'; | ||
| const invocation = `${invocationPrefix}${node.path.original}`; | ||
| const replacement = `${invocationPrefix}${node.path.original.replace('/index', '')}`; | ||
|
|
||
| context.report({ | ||
| node: node.path, | ||
| message: `Replace \`${invocation} ...\` to \`${replacement} ...\``, | ||
| }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Handle component helper: {{component "foo/index"}} or (component "foo/index") | ||
| if ( | ||
| node.type === 'GlimmerMustacheStatement' || | ||
| node.type === 'GlimmerBlockStatement' || | ||
| node.type === 'GlimmerSubExpression' | ||
| ) { | ||
| const prefix = | ||
| node.type === 'GlimmerMustacheStatement' | ||
| ? '{{' | ||
| : node.type === 'GlimmerBlockStatement' | ||
| ? '{{#' | ||
| : '('; | ||
|
|
||
| if ( | ||
| node.path && | ||
| node.path.type === 'GlimmerPathExpression' && | ||
| node.path.original === 'component' && | ||
| node.params && | ||
| node.params.length > 0 && | ||
| node.params[0].type === 'GlimmerStringLiteral' | ||
| ) { | ||
| const componentName = node.params[0].value; | ||
|
|
||
| if (componentName.endsWith('/index')) { | ||
| const invocation = `${prefix}component "${componentName}"`; | ||
| const replacement = `${prefix}component "${componentName.replace('/index', '')}"`; | ||
|
|
||
| context.report({ | ||
| node: node.params[0], | ||
| message: `Replace \`${invocation} ...\` to \`${replacement} ...\``, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| GlimmerElementNode: lintIndexUsage, | ||
| GlimmerMustacheStatement: lintIndexUsage, | ||
| GlimmerBlockStatement: lintIndexUsage, | ||
| GlimmerSubExpression: lintIndexUsage, | ||
| }; | ||
| }, | ||
| }; | ||
| /* eslint-enable complexity, eslint-plugin/prefer-placeholders, unicorn/explicit-length-check */ | ||
168 changes: 168 additions & 0 deletions
168
tests/lib/rules/template-no-index-component-invocation.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| const rule = require('../../../lib/rules/template-no-index-component-invocation'); | ||
| const RuleTester = require('eslint').RuleTester; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('ember-eslint-parser'), | ||
| parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, | ||
| }); | ||
|
|
||
| ruleTester.run('template-no-index-component-invocation', rule, { | ||
| valid: [ | ||
| '<template><Foo::Bar /></template>', | ||
| '<template><Foo::IndexItem /></template>', | ||
| '<template><Foo::MyIndex /></template>', | ||
| '<template><Foo::MyIndex></Foo::MyIndex></template>', | ||
| '<template>{{foo/index-item}}</template>', | ||
| '<template>{{foo/my-index}}</template>', | ||
| '<template>{{foo/bar}}</template>', | ||
| '<template>{{#foo/bar}}{{/foo/bar}}</template>', | ||
| '<template>{{component "foo/bar"}}</template>', | ||
| '<template>{{component "foo/my-index"}}</template>', | ||
| '<template>{{component "foo/index-item"}}</template>', | ||
| '<template>{{#component "foo/index-item"}}{{/component}}</template>', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: '<template>{{foo/index}}</template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: 'Replace `{{foo/index ...` to `{{foo ...`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template>{{component "foo/index"}}</template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: 'Replace `{{component "foo/index" ...` to `{{component "foo" ...`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template>{{#foo/index}}{{/foo/index}}</template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: 'Replace `{{#foo/index ...` to `{{#foo ...`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template>{{#component "foo/index"}}{{/component}}</template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: 'Replace `{{#component "foo/index" ...` to `{{#component "foo" ...`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template><Foo::Index /></template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: 'Replace `<Foo::Index ...` to `<Foo ...`', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<template><Foo::Index></Foo::Index></template>', | ||
| output: null, | ||
| errors: [ | ||
| { | ||
| message: 'Replace `<Foo::Index ...` to `<Foo ...`', | ||
| }, | ||
| ], | ||
| }, | ||
|
|
||
| { | ||
| code: '<template>{{foo/bar (component "foo/index")}}</template>', | ||
| output: null, | ||
| errors: [{ message: 'Replace `(component "foo/index" ...` to `(component "foo" ...`' }], | ||
| }, | ||
| { | ||
| code: '<template>{{foo/bar name=(component "foo/index")}}</template>', | ||
| output: null, | ||
| errors: [{ message: 'Replace `(component "foo/index" ...` to `(component "foo" ...`' }], | ||
| }, | ||
| { | ||
| code: '<template><Foo::Bar::Index /></template>', | ||
| output: null, | ||
| errors: [{ message: 'Replace `<Foo::Bar::Index ...` to `<Foo::Bar ...`' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const hbsRuleTester = new RuleTester({ | ||
| parser: require.resolve('ember-eslint-parser/hbs'), | ||
| parserOptions: { | ||
| ecmaVersion: 2022, | ||
| sourceType: 'module', | ||
| }, | ||
| }); | ||
|
|
||
| hbsRuleTester.run('template-no-index-component-invocation', rule, { | ||
| valid: [ | ||
| '<Foo::Bar />', | ||
| '<Foo::IndexItem />', | ||
| '<Foo::MyIndex />', | ||
| '<Foo::MyIndex></Foo::MyIndex>', | ||
| '{{foo/index-item}}', | ||
| '{{foo/my-index}}', | ||
| '{{foo/bar}}', | ||
| '{{#foo/bar}}{{/foo/bar}}', | ||
| '{{component "foo/bar"}}', | ||
| '{{component "foo/my-index"}}', | ||
| '{{component "foo/index-item"}}', | ||
| '{{#component "foo/index-item"}}{{/component}}', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: '{{foo/index}}', | ||
| output: null, | ||
| errors: [{ message: 'Replace `{{foo/index ...` to `{{foo ...`' }], | ||
| }, | ||
| { | ||
| code: '{{component "foo/index"}}', | ||
| output: null, | ||
| errors: [{ message: 'Replace `{{component "foo/index" ...` to `{{component "foo" ...`' }], | ||
| }, | ||
| { | ||
| code: '{{#foo/index}}{{/foo/index}}', | ||
| output: null, | ||
| errors: [{ message: 'Replace `{{#foo/index ...` to `{{#foo ...`' }], | ||
| }, | ||
| { | ||
| code: '{{#component "foo/index"}}{{/component}}', | ||
| output: null, | ||
| errors: [{ message: 'Replace `{{#component "foo/index" ...` to `{{#component "foo" ...`' }], | ||
| }, | ||
| { | ||
| code: '{{foo/bar (component "foo/index")}}', | ||
| output: null, | ||
| errors: [{ message: 'Replace `(component "foo/index" ...` to `(component "foo" ...`' }], | ||
| }, | ||
| { | ||
| code: '{{foo/bar name=(component "foo/index")}}', | ||
| output: null, | ||
| errors: [{ message: 'Replace `(component "foo/index" ...` to `(component "foo" ...`' }], | ||
| }, | ||
| { | ||
| code: '<Foo::Index />', | ||
| output: null, | ||
| errors: [{ message: 'Replace `<Foo::Index ...` to `<Foo ...`' }], | ||
| }, | ||
| { | ||
| code: '<Foo::Bar::Index />', | ||
| output: null, | ||
| errors: [{ message: 'Replace `<Foo::Bar::Index ...` to `<Foo::Bar ...`' }], | ||
| }, | ||
| { | ||
| code: '<Foo::Index></Foo::Index>', | ||
| output: null, | ||
| errors: [{ message: 'Replace `<Foo::Index ...` to `<Foo ...`' }], | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to re-enable at the bottom of a file. Not sure if the other PRs also did this and I missed them.