Skip to content

Commit 1ce8872

Browse files
committed
Extract rule: template-no-builtin-form-components
1 parent ffc4ad8 commit 1ce8872

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ rules in templates can be disabled with eslint directives with mustache or html
185185
| Name | Description | 💼 | 🔧 | 💡 |
186186
| :----------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
187187
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
188+
| [template-no-builtin-form-components](docs/rules/template-no-builtin-form-components.md) | disallow usage of built-in form components | | | |
188189
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
189190
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
190191

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ember/template-no-builtin-form-components
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows usage of built-in form components.
6+
7+
## Rule Details
8+
9+
Built-in Ember components like `<Input>` and `<Textarea>` should be replaced with native HTML elements for better performance and simpler code.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
<Input @type="text" />
18+
</template>
19+
```
20+
21+
```gjs
22+
<template>
23+
<Textarea @value={{this.text}} />
24+
</template>
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```gjs
30+
<template>
31+
<input type="text" />
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
<textarea></textarea>
38+
</template>
39+
```
40+
41+
## References
42+
43+
- [eslint-plugin-ember no-builtin-form-components](https://github.com/eslint-plugin-ember/eslint-plugin-ember/blob/master/docs/rule/no-builtin-form-components.md)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow usage of built-in form components',
7+
category: 'Best Practices',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-builtin-form-components.md',
11+
},
12+
fixable: null,
13+
schema: [],
14+
messages: {
15+
noBuiltinFormComponent:
16+
'Do not use built-in form components. Use native HTML elements instead.',
17+
},
18+
},
19+
20+
create(context) {
21+
const BUILTIN_FORM_COMPONENTS = new Set(['Input', 'Textarea']);
22+
23+
return {
24+
GlimmerElementNode(node) {
25+
if (BUILTIN_FORM_COMPONENTS.has(node.tag)) {
26+
context.report({
27+
node,
28+
messageId: 'noBuiltinFormComponent',
29+
});
30+
}
31+
},
32+
};
33+
},
34+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-builtin-form-components');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
//------------------------------------------------------------------------------
9+
// Tests
10+
//------------------------------------------------------------------------------
11+
12+
const ruleTester = new RuleTester({
13+
parser: require.resolve('ember-eslint-parser'),
14+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15+
});
16+
17+
ruleTester.run('template-no-builtin-form-components', rule, {
18+
valid: [
19+
`<template>
20+
<input type="text" />
21+
</template>`,
22+
`<template>
23+
<textarea></textarea>
24+
</template>`,
25+
`<template>
26+
<a href="/home">Home</a>
27+
</template>`,
28+
],
29+
30+
invalid: [
31+
{
32+
code: `<template>
33+
<Input @type="text" />
34+
</template>`,
35+
output: null,
36+
errors: [
37+
{
38+
message: 'Do not use built-in form components. Use native HTML elements instead.',
39+
type: 'GlimmerElementNode',
40+
},
41+
],
42+
},
43+
{
44+
code: `<template>
45+
<Textarea @value={{this.text}} />
46+
</template>`,
47+
output: null,
48+
errors: [
49+
{
50+
message: 'Do not use built-in form components. Use native HTML elements instead.',
51+
type: 'GlimmerElementNode',
52+
},
53+
],
54+
},
55+
],
56+
});

0 commit comments

Comments
 (0)