You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I did some changes that fixes the VariableDeclaration, but then I realized I need to improve the esformatter API to expose a few methods that are useful to plugin authors.
on v0.3.0 I plan to expose the indentation methods and also make the limit module smarter to simplify the process. I'll try to make these changes today to esformatter and release a new version so that other people can start implementing plugins.
please note that I avoided doing string manipulations and just edited the tokens linked list, that will allow other plugins to reuse these values and avoid problems (strings replacements are not safe!)
diff --git a/lib/limit.js b/lib/limit.js
new file mode 100644
index 0000000..4159d72
--- /dev/null+++ b/lib/limit.js@@ -0,0 +1,17 @@+// limit the WhiteSpace and LineBreak before/after a token++// FIXME: this paths are ridiculous, we need to update esformatter source+// structure to avoid the duplication. this will be needed by almost all the+// plugins!+var br = require('esformatter/lib/lineBreak/lineBreak');+var ws = require('esformatter/lib/whiteSpace/whiteSpace');++br.setOptions({value: '\n'})+ws.setOptions({value: ' '})++exports.brBefore = br.limitBefore;+exports.brAfter = br.limitAfter;+exports.wsBefore = ws.limitBefore;+exports.wsAfter = ws.limitAfter;++diff --git a/lib/variables.js b/lib/variables.js
index abd4f93..ff306cb 100644
--- a/lib/variables.js+++ b/lib/variables.js@@ -1,37 +1,42 @@
var tk = require('rocambole-token')
--var mustache = require('mustache')--var update = require('./update')+ , limit = require('./limit')
module.exports = function(node) {
if(node.type !== 'VariableDeclaration') {
return
}
- var children = node.declarations.map(function(point) {- return point.toString()- })-- var indents = {}+ node.declarations.forEach(function(declaration, i) {+ var start = declaration.startToken+ , end = declaration.endToken++ limit.brBefore(declaration.id.startToken, 0)+ limit.brAfter(declaration.id.endToken, 0)+ limit.wsAfter(declaration.id.endToken, 1)++ if (declaration.init) {+ limit.brBefore(declaration.init.startToken, 0)+ limit.wsBefore(declaration.init.startToken, 1)+ }++ // we need to search only inside the VariableDeclaration since+ // last item might not have a semi-colon or comma after it+ var nextComma = tk.findInBetween(end, node.endToken, ',')+ if (nextComma) {+ limit.brBefore(nextComma, 1)+ limit.brAfter(nextComma, 0)+ limit.wsAfter(nextComma, 1)+ limit.wsBefore(nextComma, 0)++ // FIXME: this should be abstracted into esformatter, we need to expose+ // the indentation methods for plugin authors!!!+ tk.before(nextComma, {+ type: 'WhiteSpace',+ value: ' '+ });+ }- indents.leading_indent = tk.findNext(- node.startToken- , ['WhiteSpace', 'Indent']- ).value-- indents.trailing_indent = tk.findPrev(- node.endToken- , ['WhiteSpace', 'Indent']- ).value-- var new_str = mustache.render(- '{{ leading_indent }}var ' +- children.join('\n {{ leading_indent }}, ') + '\n'- , indents- )+ })- update(node, mustache.render(new_str, indents))
}
-diff --git a/package.json b/package.json
index 2a199bc..b86dbf9 100644
--- a/package.json+++ b/package.json@@ -6,6 +6,7 @@
"author": "Andrew Winterman <[email protected]>",
"license": "MIT",
"dependencies": {
+ "esformatter": "^0.2.0",
"rocambole-token": "^1.1.1",
"rocambole": "^0.3.3",
"falafel": "^0.3.1",
diff --git a/test/expected-variables.js b/test/expected-variables.js
index 35bacb9..97c80ed 100644
--- a/test/expected-variables.js+++ b/test/expected-variables.js@@ -1,4 +1,4 @@- var a = 'holleycrapthat'- , b = 'yar'- , c ='he'- , e = 'sir'+var a = 'holleycrapthat'+ , b = 'yar'+ , c = 'he'+ , e = 'sir'
The text was updated successfully, but these errors were encountered:
varbr=require('esformatter/lib/lineBreak')// adds a single line break or remove multiple consecutive line breaks until// there is only one leftbr.limitBefore(token,1)varws=require('esformatter/lib/whiteSpace')// keep (or add) a single WhiteSpace after the tokenws.limitAfter(token,1)varindent=require('esformatter/lib/indent')// increase indent level before tokenindent.indentBefore(token)// increase indent level in between the tokensindent.indentInBetween(startToken,endToken)
this should make your job way easier.
I also recommend calling require('esformatter/lib/options').set(configOptions) before using the br, ws and indent methods.
if you write this lib as a esformatter plugin:
exports.setOptions=function(opts){// that way the local copy of esformatter will also have access to the config optionsrequire('esformatter/lib/options').set(opts)}exports.transform=function(ast){// do you magic here!}
I did some changes that fixes the VariableDeclaration, but then I realized I need to improve the esformatter API to expose a few methods that are useful to plugin authors.
on v0.3.0 I plan to expose the indentation methods and also make the
limit
module smarter to simplify the process. I'll try to make these changes today to esformatter and release a new version so that other people can start implementing plugins.please note that I avoided doing string manipulations and just edited the tokens linked list, that will allow other plugins to reuse these values and avoid problems (strings replacements are not safe!)
The text was updated successfully, but these errors were encountered: