From 9889a514e1a3e5615552f758798484d92957cdb6 Mon Sep 17 00:00:00 2001 From: Jim Fleming Date: Mon, 16 Jun 2014 10:32:13 -0700 Subject: [PATCH 1/3] Adding support for UnaryExpression --- lib/rewrite.js | 15 ++++++++++++++- tests/rewrite.js | 5 +++++ tests/search.js | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/rewrite.js b/lib/rewrite.js index 3138829..f156a63 100644 --- a/lib/rewrite.js +++ b/lib/rewrite.js @@ -129,6 +129,14 @@ function match(wildcards, pattern, node) { var matchedObject = match(wildcards, pattern.object, node.object); var matchedProperty = match(wildcards, pattern.property, node.property); return matchedObject && matchedProperty; + case 'UnaryExpression': + if (pattern.operator != node.operator) { + return false; + } + if (pattern.prefix != node.prefix) { + return false; + } + return match(wildcards, pattern.argument, node.argument); case 'ArrayExpression': return partial(wildcards, pattern.elements, node.elements); case 'ObjectExpression': @@ -288,6 +296,9 @@ function replaceWildcards(wildcards, replacement) { replacement.left = replaceWildcards(wildcards, replacement.left); replacement.right = replaceWildcards(wildcards, replacement.right); break; + case 'UnaryExpression': + replacement.argument = replaceWildcards(wildcards, replacement.argument); + break; case 'VariableDeclaration': for (i = 0; i < replacement.declarations.length; i++) { replacement.declarations[i] = replaceWildcards(wildcards, replacement.declarations[i]); @@ -355,7 +366,9 @@ exports.rewrite = function(js, rewriteRule) { // Set replacement node type to match original node type. This is to // account for cases when two nodes are comparable but not equal. - clonedReplacement.type = node.type; + if (comparable(clonedReplacement, node)) { + clonedReplacement.type = node.type; + } var updatedNode = replaceWildcards(wildcards, clonedReplacement); var generated = escodegen.generate(updatedNode); diff --git a/tests/rewrite.js b/tests/rewrite.js index a170c0b..3c3ca0c 100644 --- a/tests/rewrite.js +++ b/tests/rewrite.js @@ -42,4 +42,9 @@ describe('jsfmt.rewrite', function() { '_.map(a, b) -> a.map(b)') .toString().should.eql('function test() { return [\n 0,\n 1,\n 2\n].map(function (val) {\n return val * val;\n}); }'); }); + + it('should be able to search for unary expression', function() { + jsfmt.rewrite('var test = !0;', '!0 -> true').toString().should.eql('var test = true;'); + jsfmt.rewrite('var test = !0;', '!0 -> !1').toString().should.eql('var test = !1;'); + }); }); diff --git a/tests/search.js b/tests/search.js index ba8ba91..6301b85 100644 --- a/tests/search.js +++ b/tests/search.js @@ -27,4 +27,12 @@ describe('jsfmt.search', function() { '_.map(a, b)'); results.length.should.eql(1); }); + + it('should be able to search for unary expression', function() { + var resultsA = jsfmt.search('!0', '!0'); + resultsA.length.should.eql(1); + + var resultsB = jsfmt.search('var test = !0;', '!0'); + resultsB.length.should.eql(1); + }); }); From 8e6cb648c258fa0ff07a7b29495b1df29f6ff246 Mon Sep 17 00:00:00 2001 From: Jim Fleming Date: Tue, 17 Jun 2014 10:18:17 -0700 Subject: [PATCH 2/3] Bumping version and updating README --- README.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3df40fa..7ee9a32 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,11 @@ Links Changelog --- +### v0.3.2 + +- Adding support for `UnaryExpression` +- Fixing bug where rewrite types were not being set properly + ### v0.3.1 - Fixed bug when searching for expressions within BlockStatement or Program body diff --git a/package.json b/package.json index 6eac984..b9b9190 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jsfmt", "description": "gofmt for javascript", - "version": "0.3.1", + "version": "0.3.2", "homepage": "https://github.com/rdio/jsfmt", "main": "./lib/index.js", "engines": { From a06d8cb4dbddf41cdabb2f7e4eebfcb1083031d3 Mon Sep 17 00:00:00 2001 From: Jim Fleming Date: Tue, 17 Jun 2014 10:34:44 -0700 Subject: [PATCH 3/3] Removing prefix check (undefined); renaming test to be more accurate --- lib/rewrite.js | 3 --- tests/rewrite.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/rewrite.js b/lib/rewrite.js index f156a63..a2c42b3 100644 --- a/lib/rewrite.js +++ b/lib/rewrite.js @@ -133,9 +133,6 @@ function match(wildcards, pattern, node) { if (pattern.operator != node.operator) { return false; } - if (pattern.prefix != node.prefix) { - return false; - } return match(wildcards, pattern.argument, node.argument); case 'ArrayExpression': return partial(wildcards, pattern.elements, node.elements); diff --git a/tests/rewrite.js b/tests/rewrite.js index 3c3ca0c..f2ba22e 100644 --- a/tests/rewrite.js +++ b/tests/rewrite.js @@ -43,7 +43,7 @@ describe('jsfmt.rewrite', function() { .toString().should.eql('function test() { return [\n 0,\n 1,\n 2\n].map(function (val) {\n return val * val;\n}); }'); }); - it('should be able to search for unary expression', function() { + it('should be able to rewrite unary expression', function() { jsfmt.rewrite('var test = !0;', '!0 -> true').toString().should.eql('var test = true;'); jsfmt.rewrite('var test = !0;', '!0 -> !1').toString().should.eql('var test = !1;'); });