-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
161 lines (144 loc) · 4.52 KB
/
index.ts
File metadata and controls
161 lines (144 loc) · 4.52 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { AbstractDynamicPlugin, DYFACTOR_GLOBAL, Telemetry } from 'dyfactor';
import * as path from 'path';
import * as fs from 'fs';
import { preprocess, AST, print } from '@glimmer/syntax';
import * as pascalCase from 'pascal-case';
import * as tRecast from 'ember-template-recast';
const isComponentHelper = `
import {getOwner} from '@ember/application';
import Helper from '@ember/component/helper';
if (!${DYFACTOR_GLOBAL}) {
${DYFACTOR_GLOBAL} = {};
}
export default Helper.extend({
compute([comp, file]) {
let owner = getOwner(this);
if (comp.name && comp.template) {
comp = comp.name;
}
let isComponent = !!owner.lookup(\`component:$\{comp}\`);
if (isComponent) {
if (${DYFACTOR_GLOBAL}[file]) {
if (!${DYFACTOR_GLOBAL}[file].includes(comp)) {
${DYFACTOR_GLOBAL}[file].push(comp);
}
} else {
${DYFACTOR_GLOBAL}[file] = [comp];
}
}
return isComponent;
}
});
`;
function instrument(templatePath) {
return (env) => {
let { builders: b } = env.syntax;
let seen = [];
let builtinStatements = ['if', 'unless', 'with', 'let', 'component', 'each', 'link-to', 'textarea', 'outlet'];
function isComponent(original, node: AST.BlockStatement) {
let isComponentSexpr = b.sexpr(b.path(`-dyfactor-is-component`), [b.string(original), b.string(templatePath)]);
let componentHelper = b.mustache(b.path('component'), [b.string(original)]);
let mustache = b.mustache(b.path(original));
let conseq = b.program([node]);
let alt = b.program([node]);
return b.block(
b.path('if'),
[isComponentSexpr],
null,
conseq,
alt
);
}
let isAttr = false;
return {
name: 'instrument-mustaches',
visitor: {
AttrNode: {
enter() { isAttr = true; },
exit() { isAttr = false; }
},
MustacheStatement(node) {
if (builtinStatements.includes(node.path.original) || seen.includes(node)) return node;
if (!isAttr && node.loc.source !== '(synthetic)') {
seen.push(node);
return isComponent(node.path.original, node);
}
return node;
},
BlockStatement(node) {
if (builtinStatements.includes(node.path.original) || seen.includes(node)) {
return node;
}
seen.push(node);
return isComponent(node.path.original, node);
}
}
}
}
}
function shouldUpdate(data, path) {
return data.includes(path);
}
function applyTelemetry(data) {
return (env) => {
let { builders: b } = env.syntax;
function toAtArgs(pairs) {
return pairs.map(pair => {
return b.attr(`@${pair.key}`, b.mustache(pair.value))
});
}
return {
BlockStatement(node) {
if (node.params.length !== 0) return node;
if (shouldUpdate(data, node.path.original)) {
let atArgs = toAtArgs(node.hash.pairs);
let tagDesc = {
name: pascalCase(node.path.original),
selfClosing: false
};
let body = [b.text('\n'), ...node.program.body];
return b.element(tagDesc, atArgs, [], body, node.program.blockParams, node.loc);
}
return node;
},
MustacheStatement(node) {
if (node.params.length !== 0) return node;
if (shouldUpdate(data, node.path.original)) {
let atArgs = toAtArgs(node.hash.pairs);
let tagDesc = {
name: pascalCase(node.path.original),
selfClosing: true
};
return b.element(tagDesc, atArgs);
}
return node;
}
}
}
}
export default class extends AbstractDynamicPlugin {
instrument() {
let templates = this.inputs.filter(input => path.extname(input) === '.hbs');
templates.forEach(template => {
fs.writeFileSync(`./app/helpers/-dyfactor-is-component.js`, isComponentHelper);
let content = fs.readFileSync(template, 'utf8');
console.log(template);
let instrumented = preprocess(content, {
plugins: {
ast: [instrument(template)]
}
});
fs.writeFileSync(template, print(instrumented));
});
}
modify(telementry: Telemetry) {
telementry.data.forEach((datalet) => {
Object.keys(datalet).forEach((template) => {
let content = fs.readFileSync(template, 'utf8');
let data = datalet[template];
let { code } = tRecast.transform(content, applyTelemetry(data));
fs.writeFileSync(template, code);
});
});
}
}