-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
transform-attrs-into-args.ts
112 lines (91 loc) · 2.54 KB
/
transform-attrs-into-args.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
import { deprecate } from '@ember/debug';
import { AST, ASTPlugin } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import { EmberASTPluginEnvironment } from '../types';
/**
@module ember
*/
/**
A Glimmer2 AST transformation that replaces all instances of
```handlebars
{{attrs.foo.bar}}
```
to
```handlebars
{{@foo.bar}}
```
as well as `{{#if attrs.foo}}`, `{{deeply (nested attrs.foobar.baz)}}`,
`{{this.attrs.foo}}` etc
@private
@class TransformAttrsToProps
*/
export default function transformAttrsIntoArgs(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];
stack.push(parent.concat(blockParams));
}
return {
name: 'transform-attrs-into-args',
visitor: {
Program: {
enter(node: AST.Program) {
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])) {
let path = b.path(node.original.substr(6)) as AST.PathExpression;
deprecate(
`Using {{attrs}} to reference named arguments has been deprecated. {{attrs.${
path.original
}}} should be updated to {{@${path.original}}}. ${calculateLocationDisplay(
moduleName,
node.loc
)}`,
false,
{
id: 'attrs-arg-access',
url: 'https://deprecations.emberjs.com/v3.x/#toc_attrs-arg-access',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.26.0',
},
}
);
path.original = `@${path.original}`;
path.data = true;
return path;
}
},
},
};
}
function isAttrs(node: AST.PathExpression, symbols: string[]) {
let name = node.parts[0];
if (symbols.indexOf(name) !== -1) {
return false;
}
if (name === 'attrs') {
if (node.this === true) {
node.parts.shift();
node.original = node.original.slice(5);
}
return true;
}
return false;
}