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
20 changes: 14 additions & 6 deletions lib/rules/template-no-arguments-for-html-elements.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/** @type {import('eslint').Rule.RuleModule} */
const htmlTags = require('html-tags');
const svgTags = require('svg-tags');
const { mathmlTagNames } = require('mathml-tag-names');

const ELEMENT_TAGS = new Set([...htmlTags, ...svgTags, ...mathmlTagNames]);

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
Expand All @@ -16,27 +20,31 @@ module.exports = {
noArgumentsForHtmlElements:
'@arguments can only be used on components, not HTML elements. Use regular attributes instead.',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-arguments-for-html-elements.js',
docs: 'docs/rule/no-arguments-for-html-elements.md',
tests: 'test/unit/rules/no-arguments-for-html-elements-test.js',
},
},

create(context) {
const sourceCode = context.sourceCode;
const HTML_ELEMENTS = new Set(htmlTags);

return {
GlimmerElementNode(node) {
// Check if this is an HTML element (lowercase)
if (!HTML_ELEMENTS.has(node.tag)) {
if (!ELEMENT_TAGS.has(node.tag)) {
return;
}

// If the tag name is a variable in scope, it's being used as a component, not an HTML element
// A known HTML/SVG tag can still be a component if it's bound in scope
// (block param, import, local).
const scope = sourceCode.getScope(node.parent);
const isVariable = scope.references.some((ref) => ref.identifier === node.parts[0]);
if (isVariable) {
return;
}

// Check for @arguments
for (const attr of node.attributes) {
if (attr.type === 'GlimmerAttrNode' && attr.name.startsWith('@')) {
context.report({
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@
"html-tags": "^3.3.1",
"lodash.camelcase": "^4.3.0",
"lodash.kebabcase": "^4.1.1",
"mathml-tag-names": "^4.0.0",
"requireindex": "^1.2.0",
"snake-case": "^3.0.3"
"snake-case": "^3.0.3",
"svg-tags": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.25.9",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/lib/rules/template-no-arguments-for-html-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ ruleTester.run('template-no-arguments-for-html-elements', rule, {
'<template><MyComponent @title="Hello" @onClick={{this.handler}} /></template>',
'<template><CustomButton @disabled={{true}} /></template>',
'<template><input value={{this.value}} /></template>',
// Custom elements aren't in the html-tags/svg-tags allowlists, so they're
// not flagged. Accepted false negative — web component namespace is open.
'<template><my-element @foo="x" /></template>',
// Namespaced/path component invocations aren't in the allowlists either.
'<template><NS.Foo @bar="baz" /></template>',
// Named blocks (colon-prefixed) aren't in the allowlists either.
'<template><Thing><:slot @item="x">content</:slot></Thing></template>',
`let div = <template>{{@greeting}}</template>

<template>
Expand Down Expand Up @@ -54,5 +61,29 @@ ruleTester.run('template-no-arguments-for-html-elements', rule, {
},
],
},
{
// SVG element — in svg-tags allowlist.
code: '<template><circle @r="5" /></template>',
output: null,
errors: [
{
message:
'@arguments can only be used on components, not HTML elements. Use regular attributes instead.',
type: 'GlimmerAttrNode',
},
],
},
{
// MathML element — in mathml-tag-names allowlist.
code: '<template><mfrac @numerator="x" /></template>',
output: null,
errors: [
{
message:
'@arguments can only be used on components, not HTML elements. Use regular attributes instead.',
type: 'GlimmerAttrNode',
},
],
},
],
});
Loading