Skip to content

Commit 512df0f

Browse files
committed
Extract rule: template-no-input-block
1 parent 0453cbe commit 512df0f

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ rules in templates can be disabled with eslint directives with mustache or html
210210
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
211211
| [template-no-inline-event-handlers](docs/rules/template-no-inline-event-handlers.md) | disallow DOM event handler attributes | | | |
212212
| [template-no-inline-styles](docs/rules/template-no-inline-styles.md) | disallow inline styles | | | |
213+
| [template-no-input-block](docs/rules/template-no-input-block.md) | disallow block usage of {{input}} helper | | | |
213214
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
214215
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
215216
| [template-no-invalid-meta](docs/rules/template-no-invalid-meta.md) | disallow invalid meta tags | | | |
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ember/template-no-input-block
2+
3+
> **HBS Only**: This rule applies to classic `.hbs` template files only (loose mode). It is not relevant for `gjs`/`gts` files (strict mode), where these patterns cannot occur.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Use of the block form of the handlebars `input` helper will result in an error at runtime.
8+
9+
## Examples
10+
11+
This rule **forbids** the following:
12+
13+
```hbs
14+
{{#input}}Some Content{{/input}}
15+
```
16+
17+
This rule **allows** the following:
18+
19+
```hbs
20+
{{input type='text' value=this.firstName disabled=this.entryNotAllowed size='50'}}
21+
```
22+
23+
## References
24+
25+
- [Ember api/input component](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Input?anchor=Input)
26+
- [rfcs/built in components](https://emberjs.github.io/rfcs/0459-angle-bracket-built-in-components.html)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'problem',
5+
docs: {
6+
description: 'disallow block usage of {{input}} helper',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-input-block.md',
9+
templateMode: 'loose',
10+
},
11+
schema: [],
12+
messages: { blockUsage: 'Unexpected block usage. The input helper may only be used inline.' },
13+
originallyFrom: {
14+
name: 'ember-template-lint',
15+
rule: 'lib/rules/no-input-block.js',
16+
docs: 'docs/rule/no-input-block.md',
17+
tests: 'test/unit/rules/no-input-block-test.js',
18+
},
19+
},
20+
create(context) {
21+
return {
22+
GlimmerBlockStatement(node) {
23+
if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'input') {
24+
context.report({ node, messageId: 'blockUsage' });
25+
}
26+
},
27+
};
28+
},
29+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const rule = require('../../../lib/rules/template-no-input-block');
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+
ruleTester.run('template-no-input-block', rule, {
9+
valid: [
10+
'<template>{{input value=this.foo}}</template>',
11+
'<template>{{button}}</template>',
12+
'<template>{{#x-button}}{{/x-button}}</template>',
13+
'<template>{{input}}</template>',
14+
],
15+
invalid: [
16+
{
17+
code: '<template>{{#input}}{{/input}}</template>',
18+
output: null,
19+
errors: [{ messageId: 'blockUsage' }],
20+
},
21+
],
22+
});
23+
24+
const hbsRuleTester = new RuleTester({
25+
parser: require.resolve('ember-eslint-parser/hbs'),
26+
parserOptions: {
27+
ecmaVersion: 2022,
28+
sourceType: 'module',
29+
},
30+
});
31+
32+
hbsRuleTester.run('template-no-input-block', rule, {
33+
valid: ['{{button}}', '{{#x-button}}{{/x-button}}', '{{input}}'],
34+
invalid: [
35+
{
36+
code: '{{#input}}{{/input}}',
37+
output: null,
38+
errors: [{ message: 'Unexpected block usage. The input helper may only be used inline.' }],
39+
},
40+
],
41+
});

0 commit comments

Comments
 (0)