Skip to content

Commit a6e958a

Browse files
committed
Extract rule: template-no-inline-linkto
1 parent 0149ef1 commit a6e958a

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ rules in templates can be disabled with eslint directives with mustache or html
199199
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
200200
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
201201
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
202+
| [template-no-inline-linkto](docs/rules/template-no-inline-linkto.md) | disallow inline form of LinkTo component | | | |
202203
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
203204

204205
### Components
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# ember/template-no-inline-linkto
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows inline form of the LinkTo component.
6+
7+
## Rule Details
8+
9+
The inline form of `<LinkTo>` (self-closing without content) should be avoided. Use the block form instead to provide link text.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
<LinkTo @route="index" />
18+
</template>
19+
```
20+
21+
```gjs
22+
<template>
23+
<LinkTo @route="about"></LinkTo>
24+
</template>
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```gjs
30+
<template>
31+
<LinkTo @route="index">Home</LinkTo>
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
<LinkTo @route="about">
38+
About Us
39+
</LinkTo>
40+
</template>
41+
```
42+
43+
## References
44+
45+
- [eslint-plugin-ember template-no-inline-link-to](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-inline-link-to.md)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow inline form of LinkTo component',
7+
category: 'Best Practices',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-inline-linkto.md',
11+
},
12+
fixable: null,
13+
schema: [],
14+
messages: {
15+
noInlineLinkTo: 'Use block form of LinkTo component instead of inline form.',
16+
},
17+
},
18+
19+
create(context) {
20+
return {
21+
GlimmerElementNode(node) {
22+
if (node.tag === 'LinkTo' && node.children && node.children.length === 0) {
23+
context.report({
24+
node,
25+
messageId: 'noInlineLinkTo',
26+
});
27+
}
28+
},
29+
};
30+
},
31+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-inline-linkto');
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-inline-linkto', rule, {
18+
valid: [
19+
`<template>
20+
<LinkTo @route="index">Home</LinkTo>
21+
</template>`,
22+
`<template>
23+
<LinkTo @route="about">
24+
About
25+
</LinkTo>
26+
</template>`,
27+
`<template>
28+
<div></div>
29+
</template>`,
30+
],
31+
32+
invalid: [
33+
{
34+
code: `<template>
35+
<LinkTo @route="index" />
36+
</template>`,
37+
output: null,
38+
errors: [
39+
{
40+
message: 'Use block form of LinkTo component instead of inline form.',
41+
type: 'GlimmerElementNode',
42+
},
43+
],
44+
},
45+
{
46+
code: `<template>
47+
<LinkTo @route="about" />
48+
</template>`,
49+
output: null,
50+
errors: [
51+
{
52+
message: 'Use block form of LinkTo component instead of inline form.',
53+
type: 'GlimmerElementNode',
54+
},
55+
],
56+
},
57+
{
58+
code: `<template>
59+
<LinkTo @route="contact"></LinkTo>
60+
</template>`,
61+
output: null,
62+
errors: [
63+
{
64+
message: 'Use block form of LinkTo component instead of inline form.',
65+
type: 'GlimmerElementNode',
66+
},
67+
],
68+
},
69+
],
70+
});

0 commit comments

Comments
 (0)