forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-inline-link-to.js
More file actions
76 lines (68 loc) · 2.78 KB
/
template-inline-link-to.js
File metadata and controls
76 lines (68 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow inline link-to, use the block form instead',
category: 'Stylistic Issues',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-inline-link-to.md',
},
fixable: 'code',
schema: [],
messages: {},
},
create(context) {
const MESSAGE = 'The inline form of link-to is not allowed. Use the block form instead.';
return {
GlimmerMustacheStatement(node) {
if (
node.path &&
node.path.type === 'GlimmerPathExpression' &&
node.path.original === 'link-to'
) {
const titleNode = node.params?.[0];
const isFixable =
titleNode &&
(titleNode.type === 'GlimmerSubExpression' ||
titleNode.type === 'GlimmerStringLiteral');
context.report({
node,
message: MESSAGE,
fix: isFixable
? (fixer) => {
const sourceCode = context.getSourceCode();
const text = sourceCode.getText(node);
// Convert {{link-to 'text' 'route' ...}} to {{#link-to 'route' ...}}text{{/link-to}}
let blockBody;
if (titleNode.type === 'GlimmerSubExpression') {
// {{link-to (helper ...) 'route'}} -> {{#link-to 'route'}}{{helper ...}}{{/link-to}}
const helperText = sourceCode.getText(titleNode);
blockBody = helperText.replace(/^\(/, '{{').replace(/\)$/, '}}');
} else if (titleNode.type === 'GlimmerStringLiteral') {
// {{link-to 'text' 'route'}} -> {{#link-to 'route'}}text{{/link-to}}
blockBody = titleNode.value;
}
// Get remaining params (everything after the first param)
const remainingParams = node.params.slice(1);
const remainingParamsText = remainingParams
.map((param) => sourceCode.getText(param))
.join(' ');
// Get hash if present
const hashText =
node.hash && node.hash.pairs && node.hash.pairs.length > 0
? ` ${node.hash.pairs
.map((pair) => `${pair.key}=${sourceCode.getText(pair.value)}`)
.join(' ')}`
: '';
const fixedText = `{{#link-to ${remainingParamsText}${hashText}}}${blockBody}{{/link-to}}`;
return fixer.replaceText(node, fixedText);
}
: null,
});
}
},
};
},
};