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-action.js
More file actions
92 lines (83 loc) · 2.73 KB
/
template-no-action.js
File metadata and controls
92 lines (83 loc) · 2.73 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
function isActionHelperPath(node) {
if (!node.path || node.path.type !== 'GlimmerPathExpression') {
return false;
}
// Check if it's the action helper (not this.action or @action)
const path = node.path;
if (path.original !== 'action') {
return false;
}
// Avoid deprecated data/this properties — those are not the helper.
const head = path.head;
if (head && (head.type === 'AtHead' || head.type === 'ThisHead')) {
return false;
}
return true;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow {{action}} helper',
category: 'Deprecations',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-action.md',
},
fixable: null,
schema: [],
messages: {
subExpression:
'Do not use `action` as (action ...) — deprecated in Ember 5.9, removed in 6.0. Use the `fn` helper instead.',
mustache:
'Do not use `action` in templates — deprecated in Ember 5.9, removed in 6.0. Use the `on` modifier and `fn` helper instead.',
modifier:
'Do not use `action` as an element modifier — deprecated in Ember 5.9, removed in 6.0. Use the `on` modifier instead.',
},
},
create(context) {
// `action` is an ambient strict-mode keyword in Ember (registered in
// STRICT_MODE_KEYWORDS), so `{{action this.x}}` works in .gjs/.gts without
// an import. Still flag the ambient keyword everywhere — but skip when
// `action` resolves to a binding (JS import/const, or template block param).
// ember-eslint-parser already registers template block params in scope, so
// a single getScope walk covers both.
const sourceCode = context.sourceCode;
function isInScope(node) {
if (!sourceCode) {
return false;
}
try {
let scope = sourceCode.getScope(node);
while (scope) {
if (scope.variables.some((v) => v.name === 'action')) {
return true;
}
scope = scope.upper;
}
} catch {
// getScope not available in .hbs-only mode
}
return false;
}
function shouldFlag(node) {
return isActionHelperPath(node) && !isInScope(node);
}
return {
GlimmerSubExpression(node) {
if (shouldFlag(node)) {
context.report({ node, messageId: 'subExpression' });
}
},
GlimmerMustacheStatement(node) {
if (shouldFlag(node)) {
context.report({ node, messageId: 'mustache' });
}
},
GlimmerElementModifierStatement(node) {
if (shouldFlag(node)) {
context.report({ node, messageId: 'modifier' });
}
},
};
},
};