Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ rules in templates can be disabled with eslint directives with mustache or html
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |

### Components
Expand Down
30 changes: 30 additions & 0 deletions docs/rules/template-no-input-tagname.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ember/template-no-input-tagname

<!-- end auto-generated rule header -->

`{{input tagName=x}}` will result in obtuse errors. Typically, the input will simply fail to render, whether used in block form or inline. The only valid `tagName` for the input helper is `input`. For `textarea`, `button`, and other input-like elements, you should instead create a new component or better use the DOM!

## Examples

This rule **forbids** the following:

```gjs
<template>
{{input tagName='foo'}}
{{input tagName=X}}
{{component 'input' tagName='foo'}}
{{component 'input' tagName=X}}
{{yield (component 'input' tagName='foo')}}
{{yield (component 'input' tagName=X)}}
</template>
```

## Related rules

- [no-link-to-tagname](no-link-to-tagname.md)
- [no-unknown-arguments-for-builtin-components](no-unknown-arguments-for-builtin-components.md)

## References

- [Ember api/input component](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Input?anchor=Input)
- [rfcs/built in components](https://emberjs.github.io/rfcs/0459-angle-bracket-built-in-components.html)
38 changes: 38 additions & 0 deletions lib/rules/template-no-input-tagname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow tagName attribute on {{input}} helper',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-input-tagname.md',
},
schema: [],
messages: { unexpected: 'Unexpected tagName usage on input helper.' },
},
create(context) {
function check(node) {
if (!node.path) {
return;
}
const attrs = node.hash?.pairs || [];
const hasTagName = attrs.some((a) => a.key === 'tagName');

if (node.path.original === 'input' && hasTagName) {
context.report({ node, messageId: 'unexpected' });
} else if (
node.path.original === 'component' &&
node.params?.[0]?.original === 'input' &&
hasTagName
) {
context.report({ node, messageId: 'unexpected' });
}
}
return {
GlimmerMustacheStatement: check,
GlimmerSubExpression: check,
};
},
};
55 changes: 55 additions & 0 deletions tests/lib/rules/template-no-input-tagname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const rule = require('../../../lib/rules/template-no-input-tagname');
const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
ruleTester.run('template-no-input-tagname', rule, {
valid: [
'<template>{{input value=this.foo}}</template>',
// Test cases ported from ember-template-lint
'<template>{{input type="text"}}</template>',
'<template>{{component "input" type="text"}}</template>',
'<template>{{yield (component "input" type="text")}}</template>',
],
invalid: [
{
code: '<template>{{input tagName="span"}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},

// Test cases ported from ember-template-lint
{
code: '<template>{{input tagName="foo"}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},
{
code: '<template>{{input tagName=bar}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},
{
code: '<template>{{component "input" tagName="foo"}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},
{
code: '<template>{{component "input" tagName=bar}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},
{
code: '<template>{{yield (component "input" tagName="foo")}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},
{
code: '<template>{{yield (component "input" tagName=bar)}}</template>',
output: null,
errors: [{ messageId: 'unexpected' }],
},
],
});
Loading