Skip to content

Commit a2adc22

Browse files
committed
glayzzle#194 - add a preBuild callback helper
1 parent 1eb6cba commit a2adc22

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/ast.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ AST.prototype.prepare = function(kind, docs, parser) {
254254
let location = null;
255255
const args = Array.prototype.slice.call(arguments);
256256
args.push(docs);
257+
if (typeof result.preBuild === "function") {
258+
result.preBuild(arguments);
259+
}
257260
if (self.withPositions || self.withSource) {
258261
let src = null;
259262
if (self.withSource) {
@@ -280,9 +283,13 @@ AST.prototype.prepare = function(kind, docs, parser) {
280283
if (typeof node !== "function") {
281284
throw new Error('Undefined node "' + kind + '"');
282285
}
283-
const result = Object.create(node.prototype);
284-
node.apply(result, args);
285-
return self.resolvePrecedence(result);
286+
const astNode = Object.create(node.prototype);
287+
node.apply(astNode, args);
288+
result.instance = astNode;
289+
if (result.trailingComments) {
290+
astNode.trailingComments = result.trailingComments;
291+
}
292+
return self.resolvePrecedence(astNode);
286293
};
287294
/**
288295
* Helper to change a node kind
@@ -291,6 +298,18 @@ AST.prototype.prepare = function(kind, docs, parser) {
291298
result.setKind = function(newKind) {
292299
kind = newKind;
293300
};
301+
/**
302+
* Sets a list of trailing comments
303+
* @param {*} docs
304+
*/
305+
result.setTrailingComments = function(docs) {
306+
if (result.instance) {
307+
// already created
308+
result.instance.trailingComments = docs;
309+
} else {
310+
result.trailingComments = docs;
311+
}
312+
};
294313
/**
295314
* Release a node without using it on the AST
296315
*/

0 commit comments

Comments
 (0)