Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created May 29, 2014 21:20
Show Gist options
  • Save jimfleming/7b945d19105ca9d95c6c to your computer and use it in GitHub Desktop.
Save jimfleming/7b945d19105ca9d95c6c to your computer and use it in GitHub Desktop.
diff --git a/examples/reduce.js b/examples/reduce.js
deleted file mode 100644
index ed4cee9..0000000
--- a/examples/reduce.js
+++ /dev/null
@@ -1,4 +0,0 @@
var values = [1, 2, 3, 4];
_.reduce(values, function(sum, value) {
 return sum + value;
}, 0);
diff --git a/examples/styleGuide.js b/examples/styleGuide.js
new file mode 100644
index 0000000..ab2cfe4
--- /dev/null
+++ b/examples/styleGuide.js
@@ -0,0 +1,122 @@
(function() {
 var test = "This is a sample declaration.";
 var arr = [
 0,
 1,
 2,
 3
 ];
 var a, b, c;
 var another = test;
 var sum = 0;
 for (var i = 0; i < test.length; i++) {
 sum += i;
 }
 function fun(num) {
 if (num > 100) {
 return num;
 }
 return num * fun(num);
 }
 var anotherFun = function(a, b, c) {
 // TODO: Should wrap to one line or indent properly
 return a == "a" ||
 b == "b" ||
 c == "c";
 };
 var myObj = {
 hello: "world"
 };
 switch (myObj.hello) {
 case "world":
 alert(test);
 break;
 default:
 alert(myObj.hello);
 break;
 }
 var that = "Empty";
 function then(that) {
 return that;
 }
 // This is a test of conditional wrapping
 if (this) {
 then(that);
 } else if (that) {
 then(this);
 // TODO: This `else` comment should align with the subsequent statement
 } else {
 console.error("Wat?");
 }
 console.log("After if/else if/else conditional");
 if (this) {
 then(that);
 } else if (that) {
 then(this);
 }
 console.log("After if/else if conditional");
 try {
 throw new Error("Whoa now");
 } catch (err) {
 console.error(err);
 }
 var str = '<span>' + fun([
 1, 2, 3
 ]) + '</span>';
 // TODO: Keep indentation of BinaryOperators inside function call args
 $(document.body).append('<li>' +
 myVar.toString() +
 '</li>');
 // TODO: Wrap + indent accessor expression
 var myVar = new myVarTypes[
 'A type']();
 // TODO: Indent call args if wrapped
 callFunc(
 'An arg',
 'Another arg',
 [
 'Some final args'
 ]
 );
 // TODO: Indent in-line with `if` or don't wrap object expression inside conditional
 if (!this.model.set(values, {
 validate: true
 })) {
 return;
 }
 // TODO: Should indent to same level as `var`
 var nestedObjects = [{
 a: true
 }, {
 a: false
 }];
}).call(this);
(function() {
 console.log('This is another block');
})();
diff --git a/examples/unformatted.js b/examples/unformatted.js
deleted file mode 100644
index cda9a48..0000000
--- a/examples/unformatted.js
+++ /dev/null
@@ -1,2 +0,0 @@
// This is a test comment
for(var myVar=1;myVar<=100;++myVar){if(myVar%15===0){console.log("FizzBuzz")}else if(myVar%3===0){console.log('Fizz')}else if(myVar%5===0){console.log('Buzz')}else{console.log(myVar)}}
diff --git a/lib/defaultStyle.json b/lib/defaultStyle.json
index 7e8d7d0..5899e0e 100644
--- a/lib/defaultStyle.json
+++ b/lib/defaultStyle.json
@@ -1,16 +1,15 @@
{
"preset": "default",
"indent" :"indent": {
"value": " ",
"ArrayExpression": 1,
"BinaryExpression": 1,0,
"ChainedMemberExpression": 1,0,
"ConditionalExpression": 1,
"MultipleVariableDeclaration": 1,
"ObjectExpression": 1,
"SwitchCase": 1,
"SwitchStatement": 1,
"CatchClause": 1,
"DoWhileStatement": 1,
"ForInStatement": 1,
@@ -24,273 +23,270 @@
"TopLevelFunctionBlock": 1
},
"lineBreak": {
 "value": "\n",
"lineBreak" :"before": {
"value" : "\n",
 "before" : {
 "AssignmentExpression" :"AssignmentExpression": ">=1",
"AssignmentOperator": 0,
"BlockStatement" :"BlockStatement": 0,
"CallExpression" :"CallExpression": -1,
"ConditionalExpression" :"ConditionalExpression": ">=1",
"CatchOpeningBrace" :"CatchOpeningBrace": 0,
"CatchClosingBrace" :"CatchClosingBrace": ">=1",
"DeleteOperator" :"DeleteOperator": ">=1",
"DoWhileStatement" :"DoWhileStatement": ">=1",
"DoWhileStatementOpeningBrace" :"DoWhileStatementOpeningBrace": 0,
"DoWhileStatementClosingBrace" :"DoWhileStatementClosingBrace": ">=1",
"EmptyStatement" :"EmptyStatement": -1,
"FinallyOpeningBrace" :"FinallyOpeningBrace": 0,
"FinallyClosingBrace" :"FinallyClosingBrace": ">=1",
"ForInStatement" :"ForInStatement": ">=1",
"ForInStatementExpressionOpening" :"ForInStatementExpressionOpening": 0,
"ForInStatementExpressionClosing" :"ForInStatementExpressionClosing": 0,
"ForInStatementOpeningBrace" :"ForInStatementOpeningBrace": 0,
"ForInStatementClosingBrace" :"ForInStatementClosingBrace": ">=1",
"ForStatement" :"ForStatement": ">=1",
"ForStatementExpressionOpening" :"ForStatementExpressionOpening": 0,
"ForStatementExpressionClosing" :"ForStatementExpressionClosing": "<2",
"ForStatementOpeningBrace" :"ForStatementOpeningBrace": 0,
"ForStatementClosingBrace" :"ForStatementClosingBrace": ">=1",
"FunctionExpression" :"FunctionExpression": 0,
"FunctionExpressionOpeningBrace" :"FunctionExpressionOpeningBrace": 0,
"FunctionExpressionClosingBrace" :"FunctionExpressionClosingBrace": ">=1",
"FunctionDeclaration" :"FunctionDeclaration": ">=1",
"FunctionDeclarationOpeningBrace" :"FunctionDeclarationOpeningBrace": 0,
"FunctionDeclarationClosingBrace" :"FunctionDeclarationClosingBrace": ">=1",
"IfStatement" :"IfStatement": ">=1",
"IfStatementOpeningBrace" :"IfStatementOpeningBrace": 0,
"IfStatementClosingBrace" :"IfStatementClosingBrace": ">=1",
"ElseIfStatement" :"ElseIfStatement": 0,
"ElseIfStatementOpeningBrace" :"ElseIfStatementOpeningBrace": 0,
"ElseIfStatementClosingBrace" :"ElseIfStatementClosingBrace": ">=1",
"ElseStatement" :"ElseStatement": 0,
"ElseStatementOpeningBrace" :"ElseStatementOpeningBrace": 0,
"ElseStatementClosingBrace" :"ElseStatementClosingBrace": ">=1",
"LogicalExpression" :"LogicalExpression": -1,
"ObjectExpressionClosingBrace" :"ObjectExpressionClosingBrace": ">=1",
"Property" :"Property": ">=1",
"ReturnStatement" :"ReturnStatement": -1,
"SwitchOpeningBrace" :"SwitchOpeningBrace": 0,
"SwitchClosingBrace" :"SwitchClosingBrace": ">=1",
"ThisExpression" :"ThisExpression": -1,
"ThrowStatement" :"ThrowStatement": ">=1",
"TryOpeningBrace" :"TryOpeningBrace": 0,
"TryClosingBrace" :"TryClosingBrace": ">=1",
"VariableName" :"VariableName": ">=1",
"VariableValue" :"VariableValue": 0,
"VariableDeclaration" :"VariableDeclaration": ">=1",
"VariableDeclarationWithoutInit" :"VariableDeclarationWithoutInit": ">=1",
"WhileStatement" :"WhileStatement": ">=1",
"WhileStatementOpeningBrace" :"WhileStatementOpeningBrace": 0,
"WhileStatementClosingBrace" :"WhileStatementClosingBrace": ">=1"
},
"after" :"after": {
"AssignmentExpression" :"AssignmentExpression": ">=1",
"AssignmentOperator" :"AssignmentOperator": 0,
"BlockStatement" :"BlockStatement": 0,
"CallExpression" :"CallExpression": -1,
"CatchOpeningBrace" :"CatchOpeningBrace": ">=1",
"CatchClosingBrace" :"CatchClosingBrace": ">=0",
"ConditionalExpression" :"ConditionalExpression": ">=1",
"DeleteOperator" :"DeleteOperator": ">=1",
"DoWhileStatement" :"DoWhileStatement": ">=1",
"DoWhileStatementOpeningBrace" :"DoWhileStatementOpeningBrace": ">=1",
"DoWhileStatementClosingBrace" :"DoWhileStatementClosingBrace": 0,
"EmptyStatement" :"EmptyStatement": -1,
"FinallyOpeningBrace" :"FinallyOpeningBrace": ">=1",
"FinallyClosingBrace" :"FinallyClosingBrace": ">=1",
"ForInStatement" :"ForInStatement": ">=1",
"ForInStatementExpressionOpening" :"ForInStatementExpressionOpening": "<2",
"ForInStatementExpressionClosing" :"ForInStatementExpressionClosing": -1,
"ForInStatementOpeningBrace" :"ForInStatementOpeningBrace": ">=1",
"ForInStatementClosingBrace" :"ForInStatementClosingBrace": ">=1",
"ForStatement" :"ForStatement": ">=1",
"ForStatementExpressionOpening" :"ForStatementExpressionOpening": "<2",
"ForStatementExpressionClosing" :"ForStatementExpressionClosing": -1,
"ForStatementOpeningBrace" :"ForStatementOpeningBrace": ">=1",
"ForStatementClosingBrace" :"ForStatementClosingBrace": ">=1",
"FunctionExpression" :"FunctionExpression": ">=1",
"FunctionExpressionOpeningBrace" :"FunctionExpressionOpeningBrace": ">=1",
"FunctionExpressionClosingBrace" :"FunctionExpressionClosingBrace": -1,
"FunctionDeclaration" :"FunctionDeclaration": ">=1",
"FunctionDeclarationOpeningBrace" :"FunctionDeclarationOpeningBrace": ">=1",
"FunctionDeclarationClosingBrace" :"FunctionDeclarationClosingBrace": ">=1",
"IfStatement" :"IfStatement": ">=1",
"IfStatementOpeningBrace" :"IfStatementOpeningBrace": ">=1",
"IfStatementClosingBrace" :"IfStatementClosingBrace": ">=1",
"ElseIfStatement" :"ElseIfStatement": ">=1",
"ElseIfStatementOpeningBrace" :"ElseIfStatementOpeningBrace": ">=1",
"ElseIfStatementClosingBrace" :"ElseIfStatementClosingBrace": ">=1",
"ElseStatement" :"ElseStatement": ">=1",
"ElseStatementOpeningBrace" :"ElseStatementOpeningBrace": ">=1",
"ElseStatementClosingBrace" :"ElseStatementClosingBrace": ">=1",
"LogicalExpression" :"LogicalExpression": -1,
"ObjectExpressionOpeningBrace" :"ObjectExpressionOpeningBrace": ">=1",
"Property" :"Property": 0,
"ReturnStatement" :"ReturnStatement": -1,
"SwitchOpeningBrace" :"SwitchOpeningBrace": ">=1",
"SwitchClosingBrace" :"SwitchClosingBrace": ">=1",
"ThisExpression" :"ThisExpression": 0,
"ThrowStatement" :"ThrowStatement": ">=1",
"TryOpeningBrace" :"TryOpeningBrace": ">=1",
"TryClosingBrace" :"TryClosingBrace": 0,
"VariableDeclaration" :"VariableDeclaration": ">=1",
"WhileStatement" :"WhileStatement": ">=1",
"WhileStatementOpeningBrace" :"WhileStatementOpeningBrace": ">=1",
"WhileStatementClosingBrace" :"WhileStatementClosingBrace": ">=1"
}
},
"whiteSpace": {
 "value": " ",
 "removeTrailing": 1,
"whiteSpace" :"before": {
"value" : " ",
 "removeTrailing" : 1,
 "before" : {
 "ArrayExpressionOpening" :"ArrayExpressionOpening": 0,
"ArrayExpressionClosing" :"ArrayExpressionClosing": 0,
"ArrayExpressionComma" :"ArrayExpressionComma": 0,
"ArgumentComma" :"ArgumentComma": 0,
"ArgumentList" :"ArgumentList": 0,
"ArgumentListArrayExpression" :"ArgumentListArrayExpression": 0,
"ArgumentListFunctionExpression" :"ArgumentListFunctionExpression": 0,
"ArgumentListObjectExpression" :"ArgumentListObjectExpression": 0,
"AssignmentOperator" :"AssignmentOperator": 1,
"BinaryExpression": 0,
"BinaryExpressionOperator" :"BinaryExpressionOperator": 1,
"BlockComment" :"BlockComment": 1,
"CallExpression" :"CallExpression": -1,
"CatchParameterList" :"CatchParameterList": 1,
"CatchOpeningBrace" :"CatchOpeningBrace": 1,
"CatchClosingBrace" :"CatchClosingBrace": 1,
"CommaOperator" :"CommaOperator": 0,
"ConditionalExpressionConsequent" :"ConditionalExpressionConsequent": 1,
"ConditionalExpressionAlternate" :"ConditionalExpressionAlternate": 1,
"DoWhileStatementOpeningBrace" :"DoWhileStatementOpeningBrace": 1,
"DoWhileStatementClosingBrace" :"DoWhileStatementClosingBrace": 1,
"DoWhileStatementConditional" :"DoWhileStatementConditional": 1,
"EmptyStatement" :"EmptyStatement": 0,
"ExpressionClosingParentheses" :"ExpressionClosingParentheses": 0,
"FinallyOpeningBrace" :"FinallyOpeningBrace": 1,
"FinallyClosingBrace" :"FinallyClosingBrace": 1,
"ForInStatement" :"ForInStatement": 1,
"ForInStatementExpressionOpening" :"ForInStatementExpressionOpening": 1,
"ForInStatementExpressionClosing" :"ForInStatementExpressionClosing": 0,
"ForInStatementOpeningBrace" :"ForInStatementOpeningBrace": 1,
"ForInStatementClosingBrace" :"ForInStatementClosingBrace": 1,
"ForStatement" :"ForStatement": 1,
"ForStatementExpressionOpening" :"ForStatementExpressionOpening": 1,
"ForStatementExpressionClosing" :"ForStatementExpressionClosing": 0,
"ForStatementOpeningBrace" :"ForStatementOpeningBrace": 1,
"ForStatementClosingBrace" :"ForStatementClosingBrace": 1,
"ForStatementSemicolon" :"ForStatementSemicolon": 0,
"FunctionDeclarationOpeningBrace" :"FunctionDeclarationOpeningBrace": 1,
"FunctionDeclarationClosingBrace" :"FunctionDeclarationClosingBrace": 1,
"FunctionExpressionOpeningBrace" :"FunctionExpressionOpeningBrace": 1,
"FunctionExpressionClosingBrace" :"FunctionExpressionClosingBrace": 1,
"IfStatementConditionalOpening" :"IfStatementConditionalOpening": 1,
"IfStatementConditionalClosing" :"IfStatementConditionalClosing": 0,
"IfStatementOpeningBrace" :"IfStatementOpeningBrace": 1,
"IfStatementClosingBrace" :"IfStatementClosingBrace": 1,
"ElseStatementOpeningBrace" :"ElseStatementOpeningBrace": 1,
"ElseStatementClosingBrace" :"ElseStatementClosingBrace": 1,
"ElseIfStatementOpeningBrace" :"ElseIfStatementOpeningBrace": 1,
"ElseIfStatementClosingBrace" :"ElseIfStatementClosingBrace": 1,
"MemberExpressionClosing" :"MemberExpressionClosing": 0,
"LineComment" :"LineComment": 1,
"LogicalExpressionOperator" :"LogicalExpressionOperator": 1,
"Property" :"Property": 1,
"PropertyValue" :"PropertyValue": 1,
"ParameterComma" :"ParameterComma": 0,
"ParameterList" :"ParameterList": 0,
"SwitchDiscriminantOpening" :"SwitchDiscriminantOpening": 1,
"SwitchDiscriminantClosing" :"SwitchDiscriminantClosing": 0,
"ThrowKeyword": 1,
"TryOpeningBrace" :"TryOpeningBrace": 1,
"TryClosingBrace" :"TryClosingBrace": 1,
"UnaryExpressionOperator": 0,
"VariableName" :"VariableName": 1,
"VariableValue" :"VariableValue": 1,
"WhileStatementConditionalOpening" :"WhileStatementConditionalOpening": 1,
"WhileStatementConditionalClosing" :"WhileStatementConditionalClosing": 0,
"WhileStatementOpeningBrace" :"WhileStatementOpeningBrace": 1,
"WhileStatementClosingBrace" :"WhileStatementClosingBrace": 1
},
"after" :"after": {
"ArrayExpressionOpening" :"ArrayExpressionOpening": 0,
"ArrayExpressionClosing" :"ArrayExpressionClosing": 0,
"ArrayExpressionComma" :"ArrayExpressionComma": 1,
"ArgumentComma" :"ArgumentComma": 1,
"ArgumentList" :"ArgumentList": 0,
"ArgumentListArrayExpression" :"ArgumentListArrayExpression": 0,
"ArgumentListFunctionExpression" :"ArgumentListFunctionExpression": 0,
"ArgumentListObjectExpression" :"ArgumentListObjectExpression": 0,
"AssignmentOperator" :"AssignmentOperator": 1,
"BinaryExpression": 0,
"BinaryExpressionOperator" :"BinaryExpressionOperator": 1,
"BlockComment" :"BlockComment": 1,
"CallExpression" :"CallExpression": 0,
"CatchParameterList" :"CatchParameterList": 1,
"CatchOpeningBrace" :"CatchOpeningBrace": 1,
"CatchClosingBrace" :"CatchClosingBrace": 1,
"CommaOperator" :"CommaOperator": 1,
"ConditionalExpressionConsequent" :"ConditionalExpressionConsequent": 1,
"ConditionalExpressionTest" :"ConditionalExpressionTest": 1,
"DoWhileStatementOpeningBrace" :"DoWhileStatementOpeningBrace": 1,
"DoWhileStatementClosingBrace" :"DoWhileStatementClosingBrace": 1,
"DoWhileStatementBody" :"DoWhileStatementBody": 1,
"EmptyStatement" :"EmptyStatement": 0,
"ExpressionOpeningParentheses" :"ExpressionOpeningParentheses": 0,
"FinallyOpeningBrace" :"FinallyOpeningBrace": 1,
"FinallyClosingBrace" :"FinallyClosingBrace": 1,
"ForInStatement" :"ForInStatement": 1,
"ForInStatementExpressionOpening" :"ForInStatementExpressionOpening": 0,
"ForInStatementExpressionClosing" :"ForInStatementExpressionClosing": 1,
"ForInStatementOpeningBrace" :"ForInStatementOpeningBrace": 1,
"ForInStatementClosingBrace" :"ForInStatementClosingBrace": 1,
"ForStatement" :"ForStatement": 1,
"ForStatementExpressionOpening" :"ForStatementExpressionOpening": 0,
"ForStatementExpressionClosing" :"ForStatementExpressionClosing": 1,
"ForStatementClosingBrace" :"ForStatementClosingBrace": 1,
"ForStatementOpeningBrace" :"ForStatementOpeningBrace": 1,
"ForStatementSemicolon" :"ForStatementSemicolon": 1,
"FunctionReservedWord": 0,
"FunctionName" :"FunctionName": 0,
"FunctionExpressionOpeningBrace" :"FunctionExpressionOpeningBrace": 1,
"FunctionExpressionClosingBrace" :"FunctionExpressionClosingBrace": 0,
"FunctionDeclarationOpeningBrace" :"FunctionDeclarationOpeningBrace": 1,
"FunctionDeclarationClosingBrace" :"FunctionDeclarationClosingBrace": 1,
"IfStatementConditionalOpening" :"IfStatementConditionalOpening": 0,
"IfStatementConditionalClosing" :"IfStatementConditionalClosing": 1,
"IfStatementOpeningBrace" :"IfStatementOpeningBrace": 1,
"IfStatementClosingBrace" :"IfStatementClosingBrace": 1,
"ElseStatementOpeningBrace" :"ElseStatementOpeningBrace": 1,
"ElseStatementClosingBrace" :"ElseStatementClosingBrace": 1,
"ElseIfStatementOpeningBrace" :"ElseIfStatementOpeningBrace": 1,
"ElseIfStatementClosingBrace" :"ElseIfStatementClosingBrace": 1,
"MemberExpressionOpening" :"MemberExpressionOpening": 0,
"LogicalExpressionOperator" :"LogicalExpressionOperator": 1,
"ObjectExpressionClosingBrace": 0,
"PropertyName" :"PropertyName": 0,
"PropertyValue" :"PropertyValue": 0,
"ParameterComma" :"ParameterComma": 1,
"ParameterList" :"ParameterList": 0,
"SwitchDiscriminantOpening" :"SwitchDiscriminantOpening": 0,
"SwitchDiscriminantClosing" :"SwitchDiscriminantClosing": 1,
"ThrowKeyword": 1,
"TryOpeningBrace" :"TryOpeningBrace": 1,
"TryClosingBrace" :"TryClosingBrace": 1,
"UnaryExpressionOperator": 0,
"VariableName" :"VariableName": 1,
"WhileStatementConditionalOpening" :"WhileStatementConditionalOpening": 0,
"WhileStatementConditionalClosing" :"WhileStatementConditionalClosing": 1,
"WhileStatementOpeningBrace" :"WhileStatementOpeningBrace": 1,
"WhileStatementClosingBrace" :"WhileStatementClosingBrace": 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment