Skip to content

Commit 0988ad1

Browse files
Merge pull request #2491 from NullVoxPopuli/nvp/template-lint-extract-rule-template-no-page-title-component
Extract rule: template-no-page-title-component
2 parents 3f6a7c8 + f2c5cea commit 0988ad1

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ rules in templates can be disabled with eslint directives with mustache or html
208208
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
209209
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
210210
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
211+
| [template-no-page-title-component](docs/rules/template-no-page-title-component.md) | disallow usage of ember-page-title component | | | |
211212

212213
### Components
213214

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ember/template-no-page-title-component
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows usage of the `<PageTitle>` component.
6+
7+
## Rule Details
8+
9+
Use the `{{pageTitle}}` helper instead of the `<PageTitle>` component from ember-page-title.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
<PageTitle>My Page</PageTitle>
18+
</template>
19+
```
20+
21+
```gjs
22+
<template>
23+
<PageTitle @title="My Page" />
24+
</template>
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```gjs
30+
<template>
31+
{{pageTitle "My Page"}}
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
{{pageTitle this.dynamicTitle}}
38+
</template>
39+
```
40+
41+
## References
42+
43+
- [ember-page-title documentation](https://github.com/ember-cli/ember-page-title)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow usage of ember-page-title component',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-page-title-component.md',
9+
templateMode: 'both',
10+
},
11+
fixable: null,
12+
schema: [],
13+
messages: {
14+
noPageTitle: 'Use the `pageTitle` helper instead of the <PageTitle> component.',
15+
},
16+
},
17+
18+
create(context) {
19+
return {
20+
GlimmerElementNode(node) {
21+
if (node.tag === 'PageTitle') {
22+
context.report({
23+
node,
24+
messageId: 'noPageTitle',
25+
});
26+
}
27+
},
28+
};
29+
},
30+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-page-title-component');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
//------------------------------------------------------------------------------
9+
// Tests
10+
//------------------------------------------------------------------------------
11+
12+
const ruleTester = new RuleTester({
13+
parser: require.resolve('ember-eslint-parser'),
14+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15+
});
16+
17+
ruleTester.run('template-no-page-title-component', rule, {
18+
valid: [
19+
`<template>
20+
{{pageTitle "My Page"}}
21+
</template>`,
22+
`<template>
23+
{{pageTitle this.dynamicTitle}}
24+
</template>`,
25+
`<template>
26+
<div></div>
27+
</template>`,
28+
],
29+
30+
invalid: [
31+
{
32+
code: `<template>
33+
<PageTitle>My Page</PageTitle>
34+
</template>`,
35+
output: null,
36+
errors: [
37+
{
38+
message: 'Use the `pageTitle` helper instead of the <PageTitle> component.',
39+
type: 'GlimmerElementNode',
40+
},
41+
],
42+
},
43+
{
44+
code: `<template>
45+
<PageTitle @title="My Page" />
46+
</template>`,
47+
output: null,
48+
errors: [
49+
{
50+
message: 'Use the `pageTitle` helper instead of the <PageTitle> component.',
51+
type: 'GlimmerElementNode',
52+
},
53+
],
54+
},
55+
{
56+
code: `<template>
57+
<PageTitle>{{this.title}}</PageTitle>
58+
</template>`,
59+
output: null,
60+
errors: [
61+
{
62+
message: 'Use the `pageTitle` helper instead of the <PageTitle> component.',
63+
type: 'GlimmerElementNode',
64+
},
65+
],
66+
},
67+
],
68+
});

0 commit comments

Comments
 (0)