Skip to content

Commit f4020e7

Browse files
committed
Extract rule: template-builtin-component-arguments
1 parent 5d2ad94 commit f4020e7

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# ember/template-builtin-component-arguments
2+
3+
💼 This rule is enabled in the following [configs](https://github.com/ember-cli/eslint-plugin-ember#-configurations): `strict-gjs`, `strict-gts`.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
✅ The `extends: 'recommended'` property in a configuration file enables this rule.
8+
9+
The builtin `Input` component has several arguments that match attributes
10+
of the lower-case `input` HTML element. These arguments should be set via e.g.
11+
`@type`, instead of `type`, but it is easy to forget and can cause subtle
12+
issues.
13+
14+
This rule warns about `Input` component invocations that use the following attributes instead of arguments:
15+
16+
- `checked`
17+
- `type`
18+
- `value`
19+
20+
The builtin `Textarea` component has several arguments that match properties
21+
of the lower-case `textarea` HTML element. These arguments should be set via e.g.
22+
`@value`, instead of `value`, but it is easy to forget and can cause subtle
23+
issues.
24+
25+
This rule warns about `Textarea` component invocations that use the following attributes instead of arguments:
26+
27+
- `value`
28+
29+
Please note that this rule currently only warns about these three attributes on
30+
the `Input`, and one property on the `Textarea` components, but might be extended in the future to also warn about
31+
other attributes or builtin components.
32+
33+
## Examples
34+
35+
This rule **forbids** the following:
36+
37+
```hbs
38+
<Input type='text' size='10' />
39+
```
40+
41+
```hbs
42+
<Input @type='checkbox' checked />
43+
```
44+
45+
```hbs
46+
<Textarea value="Hello, Tom!" /></Textarea>
47+
```
48+
49+
This rule **allows** the following:
50+
51+
```hbs
52+
<input type='text' size='10' />
53+
```
54+
55+
```hbs
56+
<Input @type='text' size='10' />
57+
```
58+
59+
```hbs
60+
<Input @type='checkbox' @checked={{true}} />
61+
```
62+
63+
```hbs
64+
<Textarea @value="Hello, Tom!" /></Textarea>
65+
```
66+
67+
## Migration
68+
69+
- Add the `@` character in front of the relevant attributes to convert them
70+
into component argument
71+
72+
## Related Rules
73+
74+
- [template-no-unknown-arguments-for-builtin-components](template-no-unknown-arguments-for-builtin-components.md)
75+
76+
## References
77+
78+
- [`Input` component API documentation](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Input?anchor=Input)
79+
- [`Textarea` component API documentation](https://api.emberjs.com/ember/release/classes/Ember.Templates.components/methods/Textarea?anchor=Textarea)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const FORBIDDEN_ATTRIBUTES = {
2+
Input: new Set(['checked', 'type', 'value']),
3+
Textarea: new Set(['value']),
4+
};
5+
6+
function generateErrorMessage(component, attribute) {
7+
return `Setting the \`${attribute}\` attribute on the builtin <${component}> component is not allowed. Did you mean \`@${attribute}\`?`;
8+
}
9+
10+
/** @type {import('eslint').Rule.RuleModule} */
11+
module.exports = {
12+
meta: {
13+
type: 'suggestion',
14+
docs: {
15+
description: 'disallow setting certain attributes on builtin components',
16+
category: 'Best Practices',
17+
strictGjs: true,
18+
strictGts: true,
19+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-builtin-component-arguments.md',
20+
},
21+
fixable: null,
22+
schema: [],
23+
messages: {},
24+
},
25+
26+
create(context) {
27+
return {
28+
GlimmerElementNode(node) {
29+
const { tag, attributes } = node;
30+
const forbiddenAttributes = FORBIDDEN_ATTRIBUTES[tag];
31+
32+
if (forbiddenAttributes && attributes) {
33+
for (const attribute of attributes) {
34+
if (attribute.name && forbiddenAttributes.has(attribute.name)) {
35+
context.report({
36+
node: attribute,
37+
message: generateErrorMessage(tag, attribute.name),
38+
});
39+
}
40+
}
41+
}
42+
},
43+
};
44+
},
45+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const rule = require('../../../lib/rules/template-builtin-component-arguments');
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+
9+
ruleTester.run('template-builtin-component-arguments', rule, {
10+
valid: [
11+
'<template><input type="text" size="10" /></template>',
12+
'<template><Input @type="text" size="10" /></template>',
13+
'<template><Input @type="checkbox" @checked={{true}} /></template>',
14+
'<template><Textarea @value="Tomster" /></template>',
15+
],
16+
invalid: [
17+
{
18+
code: '<template><Input type="text" size="10" /></template>',
19+
output: null,
20+
errors: [
21+
{
22+
message:
23+
'Setting the `type` attribute on the builtin <Input> component is not allowed. Did you mean `@type`?',
24+
},
25+
],
26+
},
27+
{
28+
code: '<template><Input @type="checkbox" checked /></template>',
29+
output: null,
30+
errors: [
31+
{
32+
message:
33+
'Setting the `checked` attribute on the builtin <Input> component is not allowed. Did you mean `@checked`?',
34+
},
35+
],
36+
},
37+
{
38+
code: '<template><Textarea value="Tomster" /></template>',
39+
output: null,
40+
errors: [
41+
{
42+
message:
43+
'Setting the `value` attribute on the builtin <Textarea> component is not allowed. Did you mean `@value`?',
44+
},
45+
],
46+
},
47+
],
48+
});

0 commit comments

Comments
 (0)