Skip to content

Commit f25c047

Browse files
committed
first try for decorating babels NodePath traverse
SQUASHED: AUTO-COMMIT-demos-tom-plugin-explorer-worker.js,AUTO-COMMIT-demos-tom-trace.js,
1 parent fffe590 commit f25c047

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

demos/tom/plugin-explorer-worker.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,22 @@ async function unloadModule(path) {
5757
delete System.loads[normalizedPath]
5858
}
5959

60+
function decorateFunction(toBeDecorated, decorator) {
61+
return function() {
62+
const result = toBeDecorated(...arguments);
63+
decorator(result);
64+
return result;
65+
}
66+
}
67+
6068

6169
async function importPlugin(url) {
6270
const module = await System.import(url);
6371
const plugin = module.default;
64-
65-
const modifiedPlugin = function(...args) {
66-
const result = plugin(...args)
72+
73+
const modifiedPlugin = decorateFunction(plugin, result => {
6774
result.name = result.name || 'Please name your plugin!';
68-
return result;
69-
}
75+
});
7076

7177
return modifiedPlugin;
7278
}
@@ -93,6 +99,16 @@ self.onmessage = function(msg) {
9399
const Trace = imports[0].default;
94100
const babel = imports[1].default.babel;
95101
const wrapAST = imports[2].default;
102+
103+
babel.traverse.NodePath = function() {
104+
debugger
105+
106+
const result = babel.traverse.NodePath(...arguments);
107+
result.traverse = function() {
108+
debugger
109+
result.traverse(...arguments);
110+
};
111+
}
96112

97113
const config = {
98114
filename: 'tmpfile.js',
@@ -130,8 +146,34 @@ self.onmessage = function(msg) {
130146
trace.leavePlugin(pluginAlias);
131147
}
132148
};
149+
150+
config.plugins = config.plugins
151+
.map(plugin => decorateFunction(plugin, result => {
152+
if(result.pre) {
153+
const oldPre = result.pre;
154+
result.pre = function(path) {
155+
const oldTraverse = path.prototype.traverse;
156+
path.prototype.traverse = function() {
157+
trace.startTraversePlugin();
158+
oldTraverse(...arguments);
159+
trace.endTraversePlugin();
160+
}
161+
oldPre(...arguments);
162+
};
163+
} else {
164+
result.pre = function(path) {
165+
const oldTraverse = path.prototype.traverse;
166+
path.prototype.traverse = function() {
167+
trace.startTraversePlugin();
168+
oldTraverse(...arguments);
169+
trace.endTraversePlugin();
170+
}
171+
};
172+
}
173+
}));
133174

134175
trace.startTraversion();
176+
debugger
135177
const ast = babel.transform(msg.data.source, enumerationConfig(createTraceID)).ast;
136178
const oldASTAsString = JSON.stringify(ast);
137179

demos/tom/trace.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export default class Trace {
3434

3535
createTraceID() {
3636
return {
37-
pluginRoundID: this.pluginRound,
37+
// plugin round not needed, but nice for debugging
38+
pluginRound: this.pluginRound,
3839
nodeID: this.counter++,
3940
isTraceID: true
4041
}

0 commit comments

Comments
 (0)