Skip to content

Commit

Permalink
Merge pull request #81 from jimfleming/unary
Browse files Browse the repository at this point in the history
Adding support for UnaryExpression
  • Loading branch information
jimfleming committed Jun 17, 2014
2 parents 1271496 + a06d8cb commit 41116cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions tests/rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;');
});
});
8 changes: 8 additions & 0 deletions tests/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 41116cb

Please sign in to comment.