Last active
January 3, 2016 09:29
-
-
Save shellscape/8443537 to your computer and use it in GitHub Desktop.
esformatter plugin examples using real-world formatting requirements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEFORE: | |
function( | |
_, $, | |
id) { | |
AFTER: | |
function (_, $, id) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEFORE: | |
createModule ( | |
'total' + | |
Dot | |
+ 'crap', | |
[ "vendor.underscore" | |
, "vendor" + Dot + "JQUERY".toLowerCase(), 'common.id'],function( | |
_, $, | |
id) { | |
AFTER: | |
createModule ('total' + Dot + 'crap', | |
[ | |
"vendor.underscore", | |
"vendor" + Dot + "JQUERY".toLowerCase(), | |
'common.id' | |
], | |
function (_, $, id) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEFORE: | |
var foo = bar,baz = fooz, foose = foz, | |
turd = poop; | |
AFTER: | |
var foo = bar, | |
baz = fooz, | |
foose = foz, | |
turd = poop; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Gilt Groupe - Function Spacing | |
* | |
* Inserts a space between function keyword and parens. eg. `function(` | |
* Removes egrigious newlines between function parameters. | |
* | |
*/ | |
module.exports = { | |
name: 'esformatter-gilt-functions', | |
/* | |
* Called immediately before core transformation tasks. | |
*/ | |
beforeTransform: function (node, options) { | |
var _ws = this.utils.whitespace, | |
_br = this.utils.linebreaks, | |
_tk = this.utils.tokens, | |
paramsStart, | |
paramsEnd; | |
if (node.type === 'FunctionExpression' && node.startToken.next && node.startToken.next.type === 'Punctuator' && node.startToken.next.value === '(') { | |
_ws.after(node.startToken); | |
} | |
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') { | |
paramsStart = _tk.findNext(node.startToken, '('); | |
paramsEnd = _tk.findNext(paramsStart, ')'); | |
_tk.removeInBetween(paramsStart, paramsEnd, '\n'); | |
} | |
}, | |
/* | |
* Called after the core runs transformat tasks, after preprocessing, before postprocessing. | |
*/ | |
transform: function (node, options) { | |
if (node.type === 'FunctionExpression' && node.startToken.next && node.startToken.next.type === 'Punctuator' && node.startToken.next.value === '(') { | |
this.utils.whitespace.after(node.startToken); | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Gilt Groupe - Modules Block Formatting | |
* | |
* Formats createModule, requireModules, and requireSpecs blocks according | |
* to company style guide. | |
* | |
*/ | |
module.exports = { | |
name: 'esformatter-gilt-modules', | |
/* | |
* Called after the core runs initial formatting tasks, before transformation. | |
*/ | |
preformat : function (str, options) { | |
return str; | |
}, | |
/* | |
* Called after the core runs preprocessing tasks, before transformation. | |
*/ | |
preprocess: function (token, options) { | |
}, | |
/* | |
* Called after the core runs transformat tasks, after preprocessing, before postprocessing. | |
*/ | |
transform: function (node, options) { | |
}, | |
/* | |
* Called after the core runs postprocessing tasks, after transformation. | |
*/ | |
postprocess: function (token, options) { | |
}, | |
/* | |
* Walks the ast node structure. | |
* | |
* This is necessary when custom transofmration/formatting conflicts with core | |
* transformations or formatting. | |
*/ | |
walk: function (node, options) { | |
if (node.name === 'createModule'){ | |
var _ws = this.utils.whitespace, | |
_br = this.utils.linebreaks, | |
_tk = this.utils.tokens, | |
paramsStart = _tk.findNext(node.startToken, '('), | |
nodeAgs = node.parent.arguments, | |
params = { | |
moduleName: nodeAgs[0], | |
deps: nodeAgs[1], | |
callback: nodeAgs[2], | |
options: nodeAgs.length > 3 ? nodeAgs[3] : null | |
}, | |
prevLine = node.startToken.prev.prev; | |
if (prevLine.type !== 'BlockComment' && prevLine.type !== 'LineBreak') { | |
_br.before(node.startToken); | |
} | |
_tk.removeInBetween(node.startToken, params.callback.startToken, '\n'); | |
_ws.before(paramsStart, ' '); | |
_br.before(params.callback.startToken); // \n before function | |
_br.before(params.callback.startToken); | |
_br.after(params.moduleName.endToken.next); // \n after the comma | |
_br.after(params.moduleName.endToken.next); | |
_tk.removeInBetween(params.deps.startToken, params.deps.endToken, '\n'); | |
_br.before(params.deps.endToken); | |
params.deps.elements.forEach(function(element){ | |
_br.before(element.startToken) | |
}); | |
} | |
}, | |
/* | |
* Called after the core runs transformation. | |
*/ | |
postformat: function (str) { | |
// since our massaging of the module blocks can result in some | |
// trailing whitespace, clean that up. | |
return this.utils.whitespace.removeTrailing(str) | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Gilt Groupe - Variable Block Formatting | |
* | |
* Corrects variable block spacing and newlines. | |
* | |
*/ | |
module.exports = { | |
name: 'esformatter-gilt-functions', | |
/* | |
* Called for each node in the AST tree after the core runs transformat tasks, | |
* after preprocessing, before postprocessing. | |
*/ | |
transform: function (node, options) { | |
var _tk = this.utils.tokens; | |
if (node.startToken.value === 'var'){ | |
_tk.eachInBetween(node.startToken, node.endToken, function (token) { | |
if (token.prev && token.prev.type === 'LineBreak' && token.type === 'LineBreak') { | |
_tk.remove(token.prev); | |
} | |
if (token.type === 'LineBreak' && token.next && token.next.type === 'LineBreak') { | |
_tk.remove(token.next); | |
} | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment