Skip to content

Commit 4d1509e

Browse files
committed
Merge pull request #109 from brettlangdon/format-code
run jsfmt on jsfmt
2 parents 0dc67de + 8f4d6f7 commit 4d1509e

File tree

7 files changed

+75
-72
lines changed

7 files changed

+75
-72
lines changed

examples/styleGuide.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
}
2727

2828
var anotherFun = function(a, b, c) {
29-
// TODO: Should wrap to one line or indent properly
3029
return a == "a" ||
3130
b == "b" ||
3231
c == "c";
@@ -58,7 +57,6 @@
5857
} else if (that) {
5958
then(this);
6059

61-
// TODO: This `else` comment should align with the subsequent statement
6260
} else {
6361
console.error("Wat?");
6462
}
@@ -75,7 +73,7 @@
7573

7674
try {
7775
throw new Error("Whoa now");
78-
} catch (err) {
76+
} catch ( err ) {
7977
console.error(err);
8078
}
8179

@@ -88,11 +86,9 @@
8886
myVar.toString() +
8987
'</li>');
9088

91-
// TODO: Wrap + indent accessor expression
9289
var myVar = new myVarTypes[
93-
'A type']();
90+
'A type']();
9491

95-
// TODO: Indent call args if wrapped
9692
callFunc(
9793
'An arg',
9894
'Another arg',
@@ -108,7 +104,6 @@
108104
return;
109105
}
110106

111-
// TODO: Should indent to same level as `var`
112107
var nestedObjects = [{
113108
a: true
114109
}, {

lib/ast.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var escodegen = require('escodegen');
22
var esprima = require('esprima');
33

44

5-
module.exports.parseAST = function(ast){
5+
module.exports.parseAST = function(ast) {
66
var js = escodegen.generate(ast, {
77
comment: true,
88
format: {
@@ -12,7 +12,7 @@ module.exports.parseAST = function(ast){
1212
return js;
1313
};
1414

15-
module.exports.generateAST = function(js){
15+
module.exports.generateAST = function(js) {
1616
var ast = esprima.parse(js, {
1717
raw: true,
1818
tokens: true,

lib/rewrite.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function matchNode(wildcards, pattern, node) {
157157
if (_.isArray(pattern[key])) {
158158
if (!matchPartial(wildcards, pattern[key], node[key])) {
159159
return false;
160-
}
160+
}
161161

162162
// Match object property
163163
} else if (_.isObject(pattern[key])) {
@@ -168,7 +168,7 @@ function matchNode(wildcards, pattern, node) {
168168

169169
} else if (!matchNode(wildcards, pattern[key], node[key])) {
170170
return false;
171-
}
171+
}
172172

173173
// Match other properties (string, boolean, null, etc.)
174174
} else if (pattern[key] !== node[key]) {
@@ -294,13 +294,13 @@ exports.search = function(js, searchRule) {
294294
raw: true,
295295
loc: true
296296
}, function(node) {
297-
var wildcards = {};
298-
if (matchNode(wildcards, pattern, node)) {
299-
matches.push({
300-
node: node,
301-
wildcards: wildcards
302-
});
303-
}
304-
});
297+
var wildcards = {};
298+
if (matchNode(wildcards, pattern, node)) {
299+
matches.push({
300+
node: node,
301+
wildcards: wildcards
302+
});
303+
}
304+
});
305305
return matches;
306306
};

lib/run.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ function handleDiff(fullPath, originalJavascript, formattedJavascript) {
6767
fs.writeSync(fdB, formattedJavascript);
6868

6969
diff(pathA, pathB, function(err, stdout, stderr) {
70-
if (stdout) console.log(stdout);
71-
if (stderr) console.log(stderr);
70+
if (stdout) {
71+
console.log(stdout);
72+
}
73+
if (stderr) {
74+
console.log(stderr);
75+
}
7276
});
7377
});
7478
});
@@ -81,8 +85,12 @@ function handleDiff(fullPath, originalJavascript, formattedJavascript) {
8185
fs.writeSync(fdA, formattedJavascript);
8286

8387
diff(fullPath, pathA, function(err, stdout, stderr) {
84-
if (stdout) console.log(stdout);
85-
if (stderr) console.log(stderr);
88+
if (stdout) {
89+
console.log(stdout);
90+
}
91+
if (stderr) {
92+
console.log(stderr);
93+
}
8694
});
8795
});
8896
}
@@ -95,7 +103,7 @@ function handleJavascript(fullPath, original) {
95103
if (argv['--ast']) {
96104
try {
97105
js = jsfmt.parseAST(JSON.parse(js));
98-
} catch(err) {
106+
} catch ( err ) {
99107
console.error(relativePath, err.message);
100108
return;
101109
}
@@ -113,7 +121,7 @@ function handleJavascript(fullPath, original) {
113121
var partialJavascript = js.split('\n').slice(startLine - 1, endLine).join('\n');
114122
console.log(partialJavascript, '\n');
115123
});
116-
} catch (err) {
124+
} catch ( err ) {
117125
console.error(relativePath, err.message);
118126
}
119127
return;
@@ -122,7 +130,7 @@ function handleJavascript(fullPath, original) {
122130
if (argv['--rewrite']) {
123131
try {
124132
js = jsfmt.rewrite(js, argv['--rewrite']).toString();
125-
} catch (err) {
133+
} catch ( err ) {
126134
console.error(relativePath, err);
127135
return;
128136
}
@@ -135,7 +143,7 @@ function handleJavascript(fullPath, original) {
135143
} else {
136144
js = jsfmt.format(js);
137145
}
138-
} catch (err) {
146+
} catch ( err ) {
139147
console.error(relativePath, err);
140148
return;
141149
}
@@ -161,7 +169,7 @@ function handleJavascript(fullPath, original) {
161169
handleDiff(fullPath, original, js);
162170
} else if (argv['--list']) {
163171
// Print filenames who differ
164-
if(original != js) {
172+
if (original != js) {
165173
console.log(relativePath);
166174
}
167175
} else if (argv['--write']) {

tests/format.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ describe('jsfmt.format', function() {
1111
it('should test basic formatting', function() {
1212
var js = 'var func = function(test){console.log( test );};';
1313
var result = jsfmt.format(js, {});
14-
result.should.eql('var func = function(test) {\n console.log(test);\n};');
15-
});
14+
result.should.eql('var func = function(test) {\n console.log(test);\n};');
15+
});
1616

17-
it('should test shebangs', function() {
18-
var js = '#!/usr/bin/env node\nvar func = function(test){console.log( test );};';
19-
var result = jsfmt.format(js, {});
20-
result.should.eql('#!/usr/bin/env node\nvar func = function(test) {\n console.log(test);\n};');
21-
});
22-
});
17+
it('should test shebangs', function() {
18+
var js = '#!/usr/bin/env node\nvar func = function(test){console.log( test );};';
19+
var result = jsfmt.format(js, {});
20+
result.should.eql('#!/usr/bin/env node\nvar func = function(test) {\n console.log(test);\n};');
21+
});
22+
});
2323

24-
describe('jsfmt.formatJSON', function() {
25-
it('should test formatting json object', function() {
26-
var json = '{"hello":"world"}';
27-
var result = jsfmt.formatJSON(json, {});
28-
result.should.eql('{\n "hello": "world"\n}');
29-
});
24+
describe('jsfmt.formatJSON', function() {
25+
it('should test formatting json object', function() {
26+
var json = '{"hello":"world"}';
27+
var result = jsfmt.formatJSON(json, {});
28+
result.should.eql('{\n "hello": "world"\n}');
29+
});
3030

31-
it('should test formatting json array', function() {
32-
var json = '["hello","world"]';
33-
var result = jsfmt.formatJSON(json, {});
34-
result.should.eql('["hello", "world"]');
35-
});
31+
it('should test formatting json array', function() {
32+
var json = '["hello","world"]';
33+
var result = jsfmt.formatJSON(json, {});
34+
result.should.eql('["hello", "world"]');
35+
});
3636

37-
it('should test formatting json array of objects', function() {
38-
var json = '[{"hello":"world"},{"foo":500.0}]';
39-
var result = jsfmt.formatJSON(json, {});
40-
result.should.eql('[{\n "hello": "world"\n }, {\n "foo": 500.0\n}]');
41-
});
42-
});
37+
it('should test formatting json array of objects', function() {
38+
var json = '[{"hello":"world"},{"foo":500.0}]';
39+
var result = jsfmt.formatJSON(json, {});
40+
result.should.eql('[{\n "hello": "world"\n }, {\n "foo": 500.0\n}]');
41+
});
42+
});

tests/rewrite.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,48 @@ var jsfmt = require('../' + libPath + '/index');
1010
describe('jsfmt.rewrite', function() {
1111
it('should test basic rewrite', function() {
1212
jsfmt.rewrite('_.each(a, b)', '_.each(a, b) -> a.forEach(b)')
13-
.toString().should.eql('a.forEach(b)');
13+
.toString().should.eql('a.forEach(b)');
1414

1515
jsfmt.rewrite('_.each(e, f)', '_.each(a, b) -> a.forEach(b)')
16-
.toString().should.eql('e.forEach(f)');
16+
.toString().should.eql('e.forEach(f)');
1717

1818
jsfmt.rewrite('_.reduce(a,b,c)', '_.reduce(a, b, c) -> a.reduce(b, c)')
19-
.toString().should.eql('a.reduce(b, c)');
19+
.toString().should.eql('a.reduce(b, c)');
2020
});
2121

2222
it('should test basic rewrite with shebang', function() {
2323
jsfmt.rewrite('#!/usr/bin/env node\n_.each(a, b)', '_.each(a, b) -> a.forEach(b)')
24-
.toString().should.eql('#!/usr/bin/env node\na.forEach(b)');
24+
.toString().should.eql('#!/usr/bin/env node\na.forEach(b)');
2525

2626
jsfmt.rewrite('#!/usr/bin/env node\n_.each(e, f)', '_.each(a, b) -> a.forEach(b)')
27-
.toString().should.eql('#!/usr/bin/env node\ne.forEach(f)');
27+
.toString().should.eql('#!/usr/bin/env node\ne.forEach(f)');
2828

2929
jsfmt.rewrite('#!/usr/bin/env node\n_.reduce(a,b,c)', '_.reduce(a, b, c) -> a.reduce(b, c)')
30-
.toString().should.eql('#!/usr/bin/env node\na.reduce(b, c)');
30+
.toString().should.eql('#!/usr/bin/env node\na.reduce(b, c)');
3131
});
3232

3333
it('should be able to rewrite variable declaration', function() {
3434
jsfmt.rewrite('var myA = 1, myB = 2;', 'noop -> noop')
35-
.toString().should.eql('var myA = 1, myB = 2;');
35+
.toString().should.eql('var myA = 1, myB = 2;');
3636

3737
// As "Program"
3838
jsfmt.rewrite('var myA = 1, myB = 2;', 'var a = c, b = d; -> var a = c; var b = d;')
39-
.toString().should.eql('var myA = 1;\nvar myB = 2;');
39+
.toString().should.eql('var myA = 1;\nvar myB = 2;');
4040

4141
// Inside of "BlockStatement" instead of "Program"
4242
jsfmt.rewrite('function test() { var myA = 1, myB = 2; }', 'var a = c, b = d; -> var a = c; var b = d;')
43-
.toString().should.eql('function test() {\n var myA = 1;\n var myB = 2;\n}');
43+
.toString().should.eql('function test() {\n var myA = 1;\n var myB = 2;\n}');
4444
});
4545

4646
it('should be able to rewrite FunctionDeclaration', function() {
4747
jsfmt.rewrite('function myFunc() { return false; }', 'function a() {} -> function wrapper(a) {}')
48-
.toString().should.eql('function wrapper(myFunc) {\n}');
48+
.toString().should.eql('function wrapper(myFunc) {\n}');
4949
});
5050

5151
it('should be able to perform a basic rewrite inside a block', function() {
5252
jsfmt.rewrite('function test() { return _.map([0, 1, 2], function(val) { return val * val; }); }',
53-
'_.map(a, b) -> a.map(b)')
54-
.toString().should.eql('function test() { return [\n 0,\n 1,\n 2\n].map(function (val) {\n return val * val;\n}); }');
53+
'_.map(a, b) -> a.map(b)')
54+
.toString().should.eql('function test() { return [\n 0,\n 1,\n 2\n].map(function (val) {\n return val * val;\n}); }');
5555
});
5656

5757
it('should be able to rewrite unary expression', function() {

tests/search.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,38 @@ describe('jsfmt.search', function() {
3030

3131
it('should be able to perform a basic search inside a block', function() {
3232
var results = jsfmt.search('function test() { return _.map([0, 1, 2], function(val) { return val * val; }); }',
33-
'_.map(a, b)');
33+
'_.map(a, b)');
3434
results.length.should.eql(1);
3535
});
3636

3737
it('should support wildcard rest params in CallExpression', function() {
3838
// Can transfer arguments
3939
jsfmt.rewrite('jade_mixins["my_key"](argA, argB, argC)', 'jade_mixins[a](...b) -> templates[a](...b)')
40-
.toString().should.eql("templates['my_key'](argA, argB, argC)");
40+
.toString().should.eql("templates['my_key'](argA, argB, argC)");
4141

4242
// Can drop argument
4343
jsfmt.rewrite('jade_mixins["my_key"](argA, argB, argC)', 'jade_mixins[a](b, c, ...d) -> templates[a](b, c)')
44-
.toString().should.eql("templates['my_key'](argA, argB)");
44+
.toString().should.eql("templates['my_key'](argA, argB)");
4545
});
4646

4747
it('should support wildcard rest params in FunctionDeclaration', function() {
4848
// Can transfer arguments
4949
jsfmt.rewrite('function test(argA, argB, argC) {}', 'function test(...a) {} -> function test(...a) {}')
50-
.toString().should.eql("function test(argA, argB, argC) {\n}");
50+
.toString().should.eql("function test(argA, argB, argC) {\n}");
5151

5252
// Can drop argument
5353
jsfmt.rewrite('function test(argA, argB, argC) {}', 'function test(a, b, ...c) {} -> function test(a, b) {}')
54-
.toString().should.eql("function test(argA, argB) {\n}");
54+
.toString().should.eql("function test(argA, argB) {\n}");
5555
});
5656

5757
it('should support wildcard rest params in FunctionExpression', function() {
5858
// Can transfer arguments
5959
jsfmt.rewrite('callMe(function(argA, argB, argC) {})', 'callMe(function(...a) {}) -> callMe(function(...a) {})')
60-
.toString().should.eql("callMe(function (argA, argB, argC) {\n})");
60+
.toString().should.eql("callMe(function (argA, argB, argC) {\n})");
6161

6262
// Can drop argument
6363
jsfmt.rewrite('callMe(function(argA, argB, argC) {})', 'callMe(function(a, b, ...c) {}) -> callMe(function(a, b) {})')
64-
.toString().should.eql("callMe(function (argA, argB) {\n})");
64+
.toString().should.eql("callMe(function (argA, argB) {\n})");
6565
});
6666

6767
it('should be able to search for unary expression', function() {

0 commit comments

Comments
 (0)