Skip to content
Closed
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,12 @@ rules in templates can be disabled with eslint directives with mustache or html

### Best Practices

| Name | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
| Name | Description | 💼 | 🔧 | 💡 |
| :------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-attribute-splat-on-html-element](docs/rules/template-no-attribute-splat-on-html-element.md) | disallow ...attributes on HTML elements | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |

### Components

Expand Down
43 changes: 43 additions & 0 deletions docs/rules/template-no-attribute-splat-on-html-element.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ember/template-no-attribute-splat-on-html-element

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

Disallows using `...attributes` on HTML elements.

## Rule Details

The `...attributes` syntax should only be used on component elements, not on HTML elements, to avoid confusion and maintain clear component boundaries.

## Examples

Examples of **incorrect** code for this rule:

```gjs
<template>
<div ...attributes></div>
</template>
```

```gjs
<template>
<span ...attributes></span>
</template>
```

Examples of **correct** code for this rule:

```gjs
<template>
<MyComponent ...attributes />
</template>
```

```gjs
<template>
<div class="wrapper"></div>
</template>
```

## References

- [eslint-plugin-ember no-attrs-in-components](https://github.com/eslint-plugin-ember/eslint-plugin-ember/blob/master/docs/rule/no-attrs-in-components.md)
39 changes: 39 additions & 0 deletions lib/rules/template-no-attribute-splat-on-html-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow ...attributes on HTML elements',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-attribute-splat-on-html-element.md',
},
fixable: null,
schema: [],
messages: {
noAttributeSplat:
'Do not use ...attributes on HTML elements. Use it only on component elements.',
},
},

create(context) {
return {
GlimmerElementNode(node) {
// Check if it's an HTML element (starts with lowercase letter)
if (node.tag && node.tag.length > 0 && node.tag[0] === node.tag[0].toLowerCase()) {
if (node.attributes) {
for (const attr of node.attributes) {
if (attr.name === '...attributes') {
context.report({
node: attr,
messageId: 'noAttributeSplat',
});
}
}
}
}
},
};
},
};
68 changes: 68 additions & 0 deletions tests/lib/rules/template-no-attribute-splat-on-html-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/template-no-attribute-splat-on-html-element');
const RuleTester = require('eslint').RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});

ruleTester.run('template-no-attribute-splat-on-html-element', rule, {
valid: [
`<template>
<MyComponent ...attributes />
</template>`,
`<template>
<div class="foo"></div>
</template>`,
`<template>
<CustomComponent ...attributes />
</template>`,
],

invalid: [
{
code: `<template>
<div ...attributes></div>
</template>`,
output: null,
errors: [
{
message: 'Do not use ...attributes on HTML elements. Use it only on component elements.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<span ...attributes></span>
</template>`,
output: null,
errors: [
{
message: 'Do not use ...attributes on HTML elements. Use it only on component elements.',
type: 'GlimmerAttrNode',
},
],
},
{
code: `<template>
<input ...attributes />
</template>`,
output: null,
errors: [
{
message: 'Do not use ...attributes on HTML elements. Use it only on component elements.',
type: 'GlimmerAttrNode',
},
],
},
],
});
Loading