forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-args-paths.js
More file actions
97 lines (86 loc) · 2.44 KB
/
template-no-args-paths.js
File metadata and controls
97 lines (86 loc) · 2.44 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow args.foo paths in templates, use @foo instead',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-args-paths.md',
templateMode: 'both',
},
fixable: 'code',
schema: [],
messages: {
argsPath:
'Component templates should avoid "{{path}}" usage, try "@{{replacement}}" instead.',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-args-paths.js',
docs: 'docs/rule/no-args-paths.md',
tests: 'test/unit/rules/no-args-paths-test.js',
},
},
create(context) {
const localScopes = [];
function pushLocals(params) {
localScopes.push(new Set(params || []));
}
function popLocals() {
localScopes.pop();
}
function isLocal(name) {
for (const scope of localScopes) {
if (scope.has(name)) {
return true;
}
}
return false;
}
return {
GlimmerBlockStatement(node) {
if (node.program && node.program.blockParams) {
pushLocals(node.program.blockParams);
}
},
'GlimmerBlockStatement:exit'(node) {
if (node.program && node.program.blockParams) {
popLocals();
}
},
GlimmerElementNode(node) {
if (node.blockParams && node.blockParams.length > 0) {
pushLocals(node.blockParams);
}
},
'GlimmerElementNode:exit'(node) {
if (node.blockParams && node.blockParams.length > 0) {
popLocals();
}
},
GlimmerPathExpression(node) {
const path = node.original;
// @args.foo is a valid named argument — skip paths starting with @
if (node.head?.type === 'AtHead') {
return;
}
if (!path?.startsWith('args.') && !path?.startsWith('this.args.')) {
return;
}
// Skip when 'args' is a block param in the current scope
if (isLocal('args')) {
return;
}
const replacement = path.replace(/^(this\.)?args\./, '');
context.report({
node,
messageId: 'argsPath',
data: { path, replacement },
fix(fixer) {
return fixer.replaceText(node, `@${replacement}`);
},
});
},
};
},
};