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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ rules in templates can be disabled with eslint directives with mustache or html
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
| [template-no-inline-styles](docs/rules/template-no-inline-styles.md) | disallow inline styles | | | |
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
Expand Down
40 changes: 40 additions & 0 deletions docs/rules/template-no-inline-styles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ember/template-no-inline-styles

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

Inline styles are not the best practice because they are hard to maintain and usually make the overall size of the project bigger. This rule forbids inline styles. Use CSS classes instead.

## Examples

This rule **forbids** the following:

```gjs
<template><div style='width:900px'></div></template>
```

This rule **allows** the following:

```gjs
<template><div class='wide-element'></div></template>
```

```gjs
<template>
{{! allowed when `allowDynamicStyles` is enabled }}
<div style={{html-safe (concat 'background-image: url(' url ')')}}></div>
</template>
```

## Options

| Name | Type | Default | Description |
| -------------------- | --------- | ------- | ------------------------------------------------------------------------------------- |
| `allowDynamicStyles` | `boolean` | `true` | When `true`, allows dynamic style values (e.g. `style={{...}}` or `style="{{...}}"`). |

## Related Rules

- [style-concatenation](style-concatenation.md)

## References

- [Deprecations/binding style attributes](https://emberjs.com/deprecations/v1.x/#toc_binding-style-attributes)
47 changes: 47 additions & 0 deletions lib/rules/template-no-inline-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow inline styles',
category: 'Best Practices',
recommendedGjs: false,
recommendedGts: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-inline-styles.md',
},
schema: [
{
type: 'object',
properties: {
allowDynamicStyles: { type: 'boolean' },
},
additionalProperties: false,
},
],
messages: { noInlineStyles: 'Inline styles are not allowed' },
},
create(context) {
const options = context.options[0] || {};
const allowDynamicStyles =
options.allowDynamicStyles === undefined ? true : options.allowDynamicStyles;

return {
GlimmerElementNode(node) {
const styleAttr = node.attributes?.find((a) => a.name === 'style');
if (!styleAttr) {
return;
}

// If allowDynamicStyles is true, skip dynamic style values (MustacheStatement/ConcatStatement)
if (allowDynamicStyles) {
const valType = styleAttr.value?.type;
if (valType === 'GlimmerMustacheStatement' || valType === 'GlimmerConcatStatement') {
return;
}
}

context.report({ node: styleAttr, messageId: 'noInlineStyles' });
},
};
},
};
25 changes: 25 additions & 0 deletions tests/lib/rules/template-no-inline-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const rule = require('../../../lib/rules/template-no-inline-styles');
const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
ruleTester.run('template-no-inline-styles', rule, {
valid: [
'<template><div class="foo"></div></template>',
// Test cases ported from ember-template-lint
'<template><div></div></template>',
'<template><span></span></template>',
'<template><ul class="dummy"></ul></template>',
'<template><div style={{foo}}></div></template>',
'<template><div style={{html-safe (concat "background-image: url(" url ")")}}></div></template>',
],
invalid: [
{
code: '<template><div style="color: red"></div></template>',
output: null,
errors: [{ messageId: 'noInlineStyles' }],
},
],
});
Loading