ast-types
Advanced tools
Comparing version 0.3.10 to 0.3.11
@@ -394,2 +394,29 @@ var assert = require("assert"); | ||
// This object is used as prototype for any node created by a builder. | ||
var nodePrototype = {}; | ||
// Call this function to define a new method to be shared by all AST | ||
// nodes. The replaced method (if any) is returned for easy wrapping. | ||
Object.defineProperty(exports, "defineMethod", { | ||
value: function(name, func) { | ||
var old = nodePrototype[name]; | ||
// Pass undefined as func to delete nodePrototype[name]. | ||
if (isUndefined.check(func)) { | ||
delete nodePrototype[name]; | ||
} else { | ||
isFunction.assert(func); | ||
Object.defineProperty(nodePrototype, name, { | ||
enumerable: true, // For discoverability. | ||
configurable: true, // For delete proto[name]. | ||
value: func | ||
}); | ||
} | ||
return old; | ||
} | ||
}); | ||
// Calling the .build method of a Def simultaneously marks the type as | ||
@@ -421,3 +448,3 @@ // buildable (by defining builders[getBuilderName(typeName)]) and | ||
var argc = args.length; | ||
var built = {}; | ||
var built = Object.create(nodePrototype); | ||
@@ -424,0 +451,0 @@ assert.ok( |
@@ -18,2 +18,3 @@ var types = require("./lib/types"); | ||
exports.builders = types.builders; | ||
exports.defineMethod = types.defineMethod; | ||
exports.getFieldValue = types.getFieldValue; | ||
@@ -20,0 +21,0 @@ exports.eachField = types.eachField; |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.3.10", | ||
"version": "0.3.11", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -510,1 +510,31 @@ var types = require("../main"); | ||
}; | ||
exports.testDefineMethod = function(t, assert) { | ||
function at(loc) { | ||
types.namedTypes.SourceLocation.assert(loc); | ||
this.loc = loc; | ||
} | ||
assert.strictEqual(types.defineMethod("at", at), void 0); | ||
var thisExpr = b.thisExpression(); | ||
assert.strictEqual(thisExpr.loc, null); | ||
thisExpr.at(b.sourceLocation( | ||
b.position(1, 0), | ||
b.position(1, 4) | ||
)); | ||
assert.strictEqual(thisExpr.loc.start.line, 1); | ||
assert.strictEqual(thisExpr.loc.start.column, 0); | ||
assert.strictEqual(thisExpr.loc.end.line, 1); | ||
assert.strictEqual(thisExpr.loc.end.column, 4); | ||
// Now try removing the method. | ||
assert.strictEqual(types.defineMethod("at"), at); | ||
assert.strictEqual(thisExpr.at, void 0); | ||
assert.strictEqual("at" in thisExpr, false); | ||
t.finish(); | ||
}; |
411638
11514