-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
assert-against-attrs.ts
119 lines (103 loc) · 3.39 KB
/
assert-against-attrs.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { assert, deprecate } from '@ember/debug';
import type { AST, ASTPlugin } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import type { EmberASTPluginEnvironment } from '../types';
/**
@module ember
*/
/**
A Glimmer2 AST transformation that asserts against
```handlebars
{{attrs.foo.bar}}
```
...as well as `{{#if attrs.foo}}`, `{{deeply (nested attrs.foobar.baz)}}`.
@private
@class AssertAgainstAttrs
*/
export default function assertAgainstAttrs(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let moduleName = env.meta?.moduleName;
let stack: string[][] = [[]];
function updateBlockParamsStack(blockParams: string[]) {
let parent = stack[stack.length - 1];
assert('has parent', parent);
stack.push(parent.concat(blockParams));
}
return {
name: 'assert-against-attrs',
visitor: {
Template: {
enter(node: AST.Template) {
updateBlockParamsStack(node.blockParams);
},
exit() {
stack.pop();
},
},
Block: {
enter(node: AST.Block) {
updateBlockParamsStack(node.blockParams);
},
exit() {
stack.pop();
},
},
ElementNode: {
enter(node: AST.ElementNode) {
updateBlockParamsStack(node.blockParams);
},
exit() {
stack.pop();
},
},
PathExpression(node: AST.PathExpression): AST.Node | void {
if (isAttrs(node, stack[stack.length - 1]!)) {
assert(
`Using {{attrs}} to reference named arguments is not supported. {{${
node.original
}}} should be updated to {{@${node.original.slice(6)}}}. ${calculateLocationDisplay(
moduleName,
node.loc
)}`
);
} else if (isThisDotAttrs(node)) {
// When removing this, ensure `{{this.attrs.foo}}` is left as-is, without triggering
// any assertions/deprecations. It's perfectly legal to reference `{{this.attrs.foo}}`
// in the template since it is a real property on the backing class – it will give you
// a `MutableCell` wrapper object, but maybe that's what you want. And in any case,
// there is no compelling to special case that property access.
deprecate(
`Using {{this.attrs}} to reference named arguments has been deprecated. {{${
node.original
}}} should be updated to {{@${node.original.slice(11)}}}. ${calculateLocationDisplay(
moduleName,
node.loc
)}`,
false,
{
id: 'attrs-arg-access',
url: 'https://deprecations.emberjs.com/v3.x/#toc_attrs-arg-access',
until: '6.0.0',
for: 'ember-source',
since: {
available: '3.26.0',
enabled: '3.26.0',
},
}
);
return b.path(`@${node.original.slice(11)}`, node.loc);
}
},
},
};
}
function isAttrs(node: AST.PathExpression, symbols: string[]) {
return (
node.head.type === 'VarHead' &&
node.head.name === 'attrs' &&
symbols.indexOf(node.head.name) === -1
);
}
function isThisDotAttrs(node: AST.PathExpression) {
return node.head.type === 'ThisHead' && node.tail[0] === 'attrs';
}