Skip to content

Commit

Permalink
Added support for hashes and iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Apr 3, 2017
1 parent b8e0bd8 commit ad94fcf
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 6 deletions.
78 changes: 72 additions & 6 deletions edge/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ function format(content, options) {
}

if (inputNode instanceof stylus.nodes.Import) {
outputBuffer.append(indent + `@${options.alwaysUseImport || inputNode.once === false ? 'import' : 'require'} '${inputNode.path.nodes[0].val}'`)
const quote = options.stringQuoteChar || '\''

outputBuffer.append(indent)
outputBuffer.append(options.alwaysUseImport || inputNode.once === false ? 'import' : 'require')
outputBuffer.append(' ' + quote + inputNode.path.nodes[0].val + quote)

if (options.insertSemicolons) {
outputBuffer.append(';')
Expand Down Expand Up @@ -308,6 +312,9 @@ function format(content, options) {
} else if (inputNode.val instanceof stylus.nodes.Expression) {
outputBuffer.append(' = ')
outputBuffer.append(travel(inputNode, inputNode.val, indentLevel, true))

} else if (inputNode.val instanceof stylus.nodes.BinOp && inputNode.val.left instanceof stylus.nodes.Ident && inputNode.val.left.name === inputNode.name && inputNode.val.right) {
outputBuffer.append(' ' + inputNode.val.op + '= ' + travel(inputNode.val, inputNode.val.right, indentLevel, true))
}

if (insideExpression === false) {
Expand Down Expand Up @@ -397,12 +404,16 @@ function format(content, options) {
outputBuffer.append(inputNode.op === '!' && options.alwaysUseNot ? 'not ' : inputNode.op).append(travel(inputNode, inputNode.expr, indentLevel, true))

} else if (inputNode instanceof stylus.nodes.BinOp) {
if (inputNode.op === '[]') {
if (inputNode.op === '[]') { // In case of array accessing
outputBuffer.append(travel(inputNode, inputNode.left, indentLevel, true) + '[' + travel(inputNode, inputNode.right, indentLevel, true) + ']')

} else if (inputNode.op === '...') {
} else if (inputNode.op === '...') { // In case of ranges
outputBuffer.append(travel(inputNode, inputNode.left, indentLevel, true) + '...' + travel(inputNode, inputNode.right, indentLevel, true))

} else if (inputNode.op === '[]=') { // In case of object-property assignment
outputBuffer.append(travel(inputNode, inputNode.left, indentLevel, true) + '[' + travel(inputNode, inputNode.right, indentLevel, true) + '] = ')
outputBuffer.append(travel(inputNode, inputNode.val, indentLevel, true))

} else {
outputBuffer.append(travel(inputNode, inputNode.left, indentLevel, true) + ' ' + inputNode.op)
if (inputNode.right) {
Expand Down Expand Up @@ -443,6 +454,30 @@ function format(content, options) {
} else if (inputNode instanceof stylus.nodes.RGBA) {
outputBuffer.append(inputNode.raw.trim())

} else if (inputNode instanceof stylus.nodes.Object) {
const keyValuePairs = _.toPairs(inputNode.vals)
if (keyValuePairs.length === 0) {
outputBuffer.append('{}')

} else if (keyValuePairs.map(pair => pair[1]).every(node => node.lineno === inputNode.lineno)) {
outputBuffer.append('{ ')
outputBuffer.append(keyValuePairs.map(pair =>
getProperVariableName(pair[0]) + ': ' +
travel(inputNode, pair[1], indentLevel, true)
).join(', '))
outputBuffer.append(' }')

} else {
const childIndent = indent + options.indentChar
outputBuffer.append('{' + options.newLineChar)
outputBuffer.append(keyValuePairs.map(pair =>
childIndent +
getProperVariableName(pair[0]) + ': ' +
travel(inputNode, pair[1], indentLevel + 1, true)
).join(',' + options.newLineChar))
outputBuffer.append(options.newLineChar + indent + '}')
}

} else if (inputNode instanceof stylus.nodes.If) {
if (insideExpression === false) {
outputBuffer.append(indent)
Expand All @@ -464,10 +499,12 @@ function format(content, options) {
outputBuffer.append(')')
}

if (options.insertSemicolons) {
outputBuffer.append(';')
if (insideExpression === false) {
if (options.insertSemicolons) {
outputBuffer.append(';')
}
outputBuffer.append(options.newLineChar)
}
outputBuffer.append(options.newLineChar)

} else {
if (insideExpression) {
Expand Down Expand Up @@ -509,6 +546,27 @@ function format(content, options) {
}
}

} else if (inputNode instanceof stylus.nodes.Each) {
if (insideExpression === false) {
outputBuffer.append(indent)
}

if (_.size(inputNode.block.nodes) === 1 && inputNode.lineno === inputNode.block.nodes[0].lineno && inputNode.block.nodes[0].column < inputNode.column) { // In case of postfix
outputBuffer.append(travel(inputNode, inputNode.block.nodes[0], indentLevel, true))
outputBuffer.append(' for ' + _.compact([inputNode.val, inputNode.key]).join(', ') + ' in ' + travel(inputNode, inputNode.expr, indentLevel, true))

if (insideExpression === false) {
if (options.insertSemicolons) {
outputBuffer.append(';')
}
outputBuffer.append(options.newLineChar)
}

} else {
outputBuffer.append('for ' + _.compact([inputNode.val, inputNode.key]).join(', ') + ' in ' + travel(inputNode, inputNode.expr, indentLevel, true))
outputBuffer.append(travel(inputNode, inputNode.block, indentLevel, false))
}

} else if (inputNode instanceof stylus.nodes.Comment && inputNode.str.startsWith('//')) { // In case of single-line comment
if (insideExpression === false) {
outputBuffer.append(indent)
Expand Down Expand Up @@ -676,6 +734,14 @@ function format(content, options) {
}
}

function getProperVariableName(name) {
if (/^\d/.test(name) || /\s/.test(name)) {
return (options.stringQuoteChar || '\'') + name + (options.stringQuoteChar || '\'')
} else {
return name
}
}

let output = travel(null, rootNode, 0)
if (output.startsWith(options.newLineChar)) {
output = output.substring(options.newLineChar.length)
Expand Down
13 changes: 13 additions & 0 deletions spec/hash/formattingOptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"insertColons": true,
"insertSemicolons": true,
"insertBraces": true,
"insertNewLineBetweenGroups": 1,
"insertNewLineBetweenSelectors": false,
"insertSpaceBeforeComments": true,
"insertSpaceAfterComments": true,
"indentChar": "\t",
"newLineChar": "\r\n",
"sortProperties": false,
"alwaysUseImport": false
}
15 changes: 15 additions & 0 deletions spec/hash/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
foo={width:10px,height:20px}
foo={
a1:{
b1:true,
b2:false
},
'a2':{c2:true},
'a3':{},
'0':false
}
foo['0']=bar

body
{foo}
display a3 in foo
18 changes: 18 additions & 0 deletions spec/hash/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
foo = { width: 10px, height: 20px };
foo = {
'0': false,
a1: {
b1: true,
b2: false
},
a2: { c2: true },
a3: {}
};

foo['0'] = bar;

body {
{foo};

display: a3 in foo;
}
14 changes: 14 additions & 0 deletions spec/iteration/formattingOptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"insertColons": true,
"insertSemicolons": true,
"insertBraces": true,
"insertNewLineBetweenGroups": 1,
"insertNewLineBetweenSelectors": false,
"insertSpaceBeforeComments": true,
"insertSpaceAfterComments": true,
"insertParenthesisAroundConditions": true,
"indentChar": "\t",
"newLineChar": "\r\n",
"sortProperties": false,
"alwaysUseImport": false
}
13 changes: 13 additions & 0 deletions spec/iteration/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body
for num,index in 1 2 3
foo num

for key,value in obj
{key}:value

sum(nums)
sum = 0
sum += n for n in nums

get(hash,key)
return pair[1] if pair[0] == key for pair in hash
18 changes: 18 additions & 0 deletions spec/iteration/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
body {
for num, index in 1 2 3 {
foo: num;
}
}

for key, value in obj {
{key}: value;
}

sum(nums) {
sum = 0;

sum += n for n in nums;
}
get(hash, key) {
return pair[1] if (pair[0] == key) for pair in hash;
}

0 comments on commit ad94fcf

Please sign in to comment.