diff --git a/README.md b/README.md index 011b480..5e4bc06 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,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/lib/rewrite.js b/lib/rewrite.js index bfa544b..4fa1b53 100644 --- a/lib/rewrite.js +++ b/lib/rewrite.js @@ -129,6 +129,11 @@ 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; + } + return match(wildcards, pattern.argument, node.argument); case 'ArrayExpression': return partial(wildcards, pattern.elements, node.elements); case 'ObjectExpression': @@ -288,6 +293,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]); @@ -366,7 +374,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/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": { diff --git a/tests/rewrite.js b/tests/rewrite.js index 1a80ca4..086bd9a 100644 --- a/tests/rewrite.js +++ b/tests/rewrite.js @@ -53,4 +53,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 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;'); + }); }); diff --git a/tests/search.js b/tests/search.js index 3502121..b6ef572 100644 --- a/tests/search.js +++ b/tests/search.js @@ -33,4 +33,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); + }); });