Skip to content

Commit 621c5f6

Browse files
committed
Sync with template-lint
1 parent 7efa92f commit 621c5f6

3 files changed

Lines changed: 55 additions & 34 deletions

File tree

docs/rules/template-require-strict-mode.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,52 @@
66

77
Require templates to be in strict mode.
88

9-
Templates should use the strict mode syntax (template tag format) rather than loose template files. Strict mode templates (`.gjs` / `.gts` files) provide better integration with JavaScript and type checking.
9+
Ember's Polaris edition component authoring format is template tag, which makes
10+
templates follow "strict mode" semantics.
11+
12+
This rule requires all templates to use strict mode (template tag). Effectively this
13+
means you may only have template content in `.gjs`/`.gts` files, not in `.hbs` or
14+
`.js`/`.ts`.
1015

1116
## Examples
1217

1318
This rule **forbids** the following:
1419

1520
```hbs
16-
<div>
17-
Hello World
18-
</div>
21+
// button.hbs
22+
<button>{{yield}}</button>
1923
```
2024

21-
(in a `.hbs` file)
22-
23-
This rule **allows** the following:
25+
```js
26+
// button-test.js
27+
import { hbs } from 'ember-cli-htmlbars';
2428

25-
```hbs
26-
Hello World
29+
test('it renders', async (assert) => {
30+
await render(hbs`<Button>Ok</Button>`);
31+
// ...
32+
});
2733
```
2834

29-
(in a `.gjs` or `.gts` file)
35+
This rule **allows** the following:
3036

31-
## Why?
37+
```gjs
38+
// button.gjs
39+
<template>
40+
<button>{{yield}}</button>
41+
</template>
42+
```
3243

33-
Strict mode templates provide:
44+
```gjs
45+
// button-test.gjs
46+
import { Button } from 'ember-awesome-button';
3447
35-
- Better integration with JavaScript tooling
36-
- Type safety in TypeScript projects
37-
- Clearer component boundaries
38-
- Easier refactoring and navigation
48+
test('it renders', async (assert) => {
49+
await render(<template><Button>Ok</Button></template>);
50+
// ..
51+
});
52+
```
3953

4054
## References
4155

42-
- [Ember.js Guides - Template Syntax](https://guides.emberjs.com/release/templates/syntax/)
56+
- [Template Tag Guide](https://guides.emberjs.com/release/components/template-tag-format/)
57+
- [Strict Mode RFC](https://rfcs.emberjs.com/id/0496-handlebars-strict-mode/)

lib/rules/template-require-strict-mode.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const ERROR_MESSAGE =
22
'Templates are required to be in strict mode. Consider refactoring to template tag format.';
33

4+
function isStrictModeFile(filePath) {
5+
return filePath?.endsWith('.gjs') || filePath?.endsWith('.gts');
6+
}
7+
48
/** @type {import('eslint').Rule.RuleModule} */
59
module.exports = {
610
meta: {
@@ -24,16 +28,11 @@ module.exports = {
2428
},
2529

2630
create(context) {
31+
const filePath = context.getFilename ? context.getFilename() : context.filename;
32+
2733
return {
2834
'GlimmerTemplate:exit'(node) {
29-
// Check if the template is in strict mode
30-
// Strict mode templates are in .gjs/.gts files
31-
// Non-strict templates are in .hbs files
32-
33-
const filePath = context.getFilename ? context.getFilename() : context.filename;
34-
35-
// Only flag .hbs files as non-strict
36-
if (filePath && filePath.endsWith('.hbs')) {
35+
if (!isStrictModeFile(filePath)) {
3736
context.report({
3837
node,
3938
message: ERROR_MESSAGE,

tests/lib/rules/template-require-strict-mode.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ ruleTester.run('template-require-strict-mode', rule, {
1212
filename: 'hello.gjs',
1313
code: '<template>hello</template>',
1414
},
15+
{
16+
filename: 'hello.gts',
17+
code: '<template>hello</template>',
18+
},
1519
],
1620
invalid: [
1721
{
@@ -25,7 +29,6 @@ ruleTester.run('template-require-strict-mode', rule, {
2529
},
2630
],
2731
},
28-
2932
{
3033
filename: 'hello.hbs',
3134
code: `<template><div>
@@ -51,13 +54,17 @@ const hbsRuleTester = new RuleTester({
5154
});
5255

5356
hbsRuleTester.run('template-require-strict-mode', rule, {
54-
valid: [
55-
'<template>hello</template>',
56-
`import Component from '@glimmer/component';
57-
58-
export default class HelloComponent extends Component {
59-
<template>hello</template>
60-
}`,
57+
valid: [],
58+
invalid: [
59+
{
60+
code: '<div>hello</div>',
61+
output: null,
62+
errors: [
63+
{
64+
message:
65+
'Templates are required to be in strict mode. Consider refactoring to template tag format.',
66+
},
67+
],
68+
},
6169
],
62-
invalid: [],
6370
});

0 commit comments

Comments
 (0)