Comparing version 0.1.0-pre to 0.1.0-pre.1
@@ -17,6 +17,6 @@ var Tree = require("../lib/tree"); | ||
// Test object as argument | ||
var tree = new Tree("grunt.init({ key: 'value' })"); | ||
var tree = new Tree("grunt.init({ key: { sub: 'foo' } })"); | ||
console.log( tree.toString() + "\n\n" ); | ||
tree.object().passedTo("grunt.init").key("key").value("'foo'"); | ||
tree.object().passedTo("grunt.init").key("key").key("sub").value("'neat'"); | ||
console.log( tree.toString() + "\n\n" ); |
@@ -27,3 +27,3 @@ var helpers = module.exports = {}; | ||
src = src | ||
.replace(/(,)+/gi, ",") // multiple comma | ||
.replace(/(,)+\s*(,)+/gi, ",") // multiple comma | ||
.replace(/(var\s*)(,\s?)*/gi, "$1") // comma following a `var` | ||
@@ -30,0 +30,0 @@ .replace(/(,\s?)(\s*\})/gi, "$2") // comma ending an object literal |
@@ -62,1 +62,17 @@ var helpers = require("../helpers"); | ||
}; | ||
/** | ||
* Select a key if the property is an object | ||
* @param {String} name Key name | ||
* @return {null} | ||
*/ | ||
Property.prototype.key = function( name ) { | ||
var key = new Property( name ); | ||
this.action = function( node ) { | ||
if ( node.value.type !== "ObjectExpression" ) return; | ||
node.value.properties.forEach(function( property ) { | ||
key.use( property ); | ||
}); | ||
}; | ||
return key; | ||
}; |
{ | ||
"name": "ast-query", | ||
"version": "0.1.0-pre", | ||
"version": "0.1.0-pre.1", | ||
"description": "Declarative JavaScript AST modification façade", | ||
@@ -5,0 +5,0 @@ "main": "lib/tree.js", |
@@ -147,3 +147,10 @@ AST Query | ||
### `.key( name )` | ||
- **name** | ||
- type: String | ||
- If the property value is an object, key name to select (if value is not an object, this | ||
condition is just ignored). | ||
- Returns: A `Property` token. | ||
Contributing | ||
@@ -165,3 +172,3 @@ ===================== | ||
Copyright (c) 2012 Simon Boudrias (twitter: @vaxilart) | ||
Copyright (c) 2013 Simon Boudrias (twitter: @vaxilart) | ||
Licensed under the MIT license. |
@@ -68,6 +68,16 @@ var expect = require("chai").expect; | ||
expect(tree.toString()).to.equal("var a = { b: 'b' };"); | ||
var tree2 = new Tree("var a = { a: 'a', b: 'b', c: 'c' };"); | ||
tree2.object().key("b").delete(); | ||
expect(tree2.toString()).to.equal("var a = { a: 'a', c: 'c' };"); | ||
}); | ||
it("should select sub key", function() { | ||
var tree = new Tree("var a = { a: 'a', b: { c: 'foo' }}"); | ||
tree.object().key("b").key("c").value("'foo'"); | ||
expect(tree.toString()).to.equal("var a = { a: 'a', b: { c: 'foo' }}"); | ||
}); | ||
}); | ||
}); |
22489
494
173