Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ast-query

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-query - npm Package Compare versions

Comparing version 0.1.0-pre.1 to 0.1.0-pre.2

ast-query.sublime-project

0

examples/object.js

@@ -0,0 +0,0 @@ var Tree = require("../lib/tree");

@@ -0,0 +0,0 @@ var Tree = require("../lib/tree");

@@ -0,0 +0,0 @@ /*jshint strict:false */

@@ -31,4 +31,6 @@ var helpers = module.exports = {};

.replace(/(\{\s*)(,\s?)/, "$1") // comma starting an object literal
.replace(/(\[\s*)(,\s?)/, "$1") // comma starting an array literal
.replace(/(,\s?)(\s*\])/gi, "$2") // comma ending an array literal
.replace(/(,)\s?;/gi, ";"); // ending comma
node.update( src );
};

@@ -61,2 +61,10 @@ var falafel = require("falafel");

/**
* Add an array token to the queue
* @return {Var token} Selected variable token
*/
Tree.prototype.array = function() {
var token = new types.Array();
this.tokens.push( token );
return token;
};

@@ -33,2 +33,3 @@ var helpers = require("../helpers");

Property.prototype.value = function( value ) {
this.newValue = value;
this.action = function( node ) {

@@ -35,0 +36,0 @@ if ( typeof value === "function" ) {

module.exports.Var = require("./var");
module.exports.Object = require("./object");
module.exports.Array = require("./array");

@@ -62,9 +62,20 @@ var helpers = require("../helpers");

Obj.prototype.key = function( name ) {
var self = this;
var key = new Property( name );
this.action = function( node ) {
node.properties.forEach(function( property ) {
key.use( property );
var properties = node.properties.filter(function( node ) {
return node.type === "Property" && node.key.name === name;
});
if ( properties.length ) {
properties.forEach(key.use.bind(key));
} else if ( key.newValue != null ) {
// Check if we prepared to add a value, if so, add it here.
// @fixme: that is super ugly and not correctly placed.
var code = node.source();
var dummyKey = " " + name + ": " + key.newValue;
if (node.properties.length) dummyKey = ", " + dummyKey;
node.update( code.substring(0, code.length - 1) + dummyKey + " }" );
}
};
return key;
};

@@ -0,0 +0,0 @@ var helpers = require("../helpers");

2

package.json
{
"name": "ast-query",
"version": "0.1.0-pre.1",
"version": "0.1.0-pre.2",
"description": "Declarative JavaScript AST modification façade",

@@ -5,0 +5,0 @@ "main": "lib/tree.js",

@@ -0,0 +0,0 @@ AST Query

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

@@ -75,9 +75,35 @@ var expect = require("chai").expect;

it("should select sub key", function() {
var tree = new Tree("var a = { a: 'a', b: { c: 'foo' }}");
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' }}");
expect(tree.toString()).to.equal("var a = { a: 'a', b: { c: 'foo' }};");
});
it("add a key", function () {
var tree = new Tree("var a = {};");
tree.object().key("b").value("'foo'");
expect(tree.toString()).to.equal("var a = { b: 'foo' };");
});
it("add a key after other keys", function () {
var tree = new Tree("var a = { m: 'b' };");
tree.object().key("b").value("'foo'");
expect(tree.toString()).to.equal("var a = { m: 'b' , b: 'foo' };");
});
it("doesn't break if key doesn't exist", function () {
var tree = new Tree("var a = { m: 'b' };");
tree.object().key("b");
expect(tree.toString()).to.equal("var a = { m: 'b' };");
});
it("doesn't add multiple keys if it initially didn't exist", function () {
var tree = new Tree("var a = { m: 'b' };");
tree.object().key("b").value("'foo'");
tree._process();
tree.object().key("b").value("'foo'");
expect(tree.toString()).to.equal("var a = { m: 'b' , b: 'foo' };");
});
});
});

@@ -0,0 +0,0 @@ var expect = require("chai").expect;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc