Skip to content

Commit

Permalink
Amended "insertNewLine..." formatting options
Browse files Browse the repository at this point in the history
  • Loading branch information
Anantachai Saothong (Manta) committed Apr 27, 2017
1 parent 8056843 commit 0ad7c6f
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 114 deletions.
32 changes: 14 additions & 18 deletions edge/createFormattingOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,20 @@ const schema = {
`
}
},
insertNewLineBetweenGroups: {
description: 'Represent a number of new-line between different type of groups.',
type: 'integer',
minimum: 0,
default: 1,
example: {
values: [0, 1],
code: `
.class1
$gray = #EEE
background red
color $gray
mixin1()
mixin2()
.class2
background blue
`
}
insertNewLineAroundBlock: {
description: '',
type: 'boolean',
default: true,
},
insertNewLineAroundProperties: {
description: '',
type: 'boolean',
default: true,
},
insertNewLineAroundOthers: {
description: '',
type: 'boolean',
default: true,
},
insertNewLineBetweenSelectors: {
description: 'Insert or remove a new-line between selectors.',
Expand Down
48 changes: 35 additions & 13 deletions edge/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function format(content, options = {}) {

const groups = []
_.difference(inputNode.nodes, commentNodes).forEach((node, rank, list) => {
if (rank === 0 || getType(node) !== getType(list[rank - 1]) || getType(node) === 'Group') {
if (rank === 0 || getType(node) !== getType(list[rank - 1]) || getType(node) === 'Block') {
groups.push([node])
} else {
_.last(groups).push(node)
Expand Down Expand Up @@ -231,14 +231,33 @@ function format(content, options = {}) {
}
})

const newLines = _.repeat(options.newLineChar, indentLevel === 0 || originalBaseIndent === '' ? options.insertNewLineBetweenOuterGroups : options.insertNewLineBetweenInnerGroups)

// Insert CSS body
outputBuffer.append(groups.map(group =>
group.map(node =>
travel(inputNode, node, childIndentLevel)
).join('')
).join(newLines))
outputBuffer.append(_.chain(groups)
.map(group => {
const nodeType = getType(group[0])

let newLineAround = ''
if (
nodeType === 'Block' && options.insertNewLineAroundBlock ||
nodeType === 'Property' && options.insertNewLineAroundProperties ||
nodeType === 'Other' && options.insertNewLineAroundOthers
) {
newLineAround = options.newLineChar
}

return _.compact([newLineAround, group.map(node => travel(inputNode, node, childIndentLevel)), newLineAround])
})
.flatten()
.reject((text, rank, list) => text === options.newLineChar && (
rank === 0 ||
rank > 1 && list[rank - 1] === options.newLineChar ||
rank === list.length - 1
))
.join('')
.value()
)

// _.repeat(options.newLineChar, indentLevel === 0 || originalBaseIndent === '' ? options.insertNewLineBetweenOuterGroups : options.insertNewLineBetweenInnerGroups)

// Insert the bottom comment(s)
const bottomCommentNodes = tryGetSingleLineCommentNodesOnTheBottomOf(_.last(nonCommentNodes))
Expand Down Expand Up @@ -1016,18 +1035,21 @@ function format(content, options = {}) {
}

function getType(inputNode) {
if (inputNode.block !== undefined || (inputNode instanceof Stylus.nodes.Ident && inputNode.val.block !== undefined)) {
return 'Group'
} else if (_.isFunction(inputNode.toJSON)) {
return inputNode.toJSON().__type
if (inputNode instanceof Stylus.nodes.Property) {
return 'Property'

} else if (inputNode.block !== undefined || (inputNode instanceof Stylus.nodes.Ident && inputNode.val.block !== undefined)) {
return 'Block'

} else {
return null
return 'Other'
}
}

function getProperVariableName(name) {
if (/^\d/.test(name) || /\s/.test(name)) {
return options.quoteChar + name + options.quoteChar

} else {
return name
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"insertNewLineAroundBlock": true,
"newLineChar": "\r\n"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
body
.class1
display none

.class1

.class2
display none

.class3
display none

mixin()
Expand Down

This file was deleted.

13 changes: 0 additions & 13 deletions spec/option-insert-new-line-between-group-0/input.styl

This file was deleted.

13 changes: 0 additions & 13 deletions spec/option-insert-new-line-between-group-0/output.styl

This file was deleted.

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions spec/option-insert-new-line-between-group-2/input.styl

This file was deleted.

21 changes: 0 additions & 21 deletions spec/option-insert-new-line-between-group-2/output.styl

This file was deleted.

15 changes: 7 additions & 8 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ filesAndDirectories.filter(directoriesOnly).forEach(directory => {
describe(testSpecName, () => {
it('can be formatted', () => {
if (fs.existsSync(actualFilePath)) fs.unlinkSync(actualFilePath)
if (fs.existsSync(debuggingFilePath)) fs.unlinkSync(debuggingFilePath)

try {
const tree = new Stylus.Parser(inputContent).parse()
fs.writeFileSync(debuggingFilePath, JSON.stringify(tree, null, '\t'))
} catch (ex) {
// Do nothing
}

const actualContent = format(inputContent, formattingOptions)

Expand All @@ -55,13 +61,6 @@ filesAndDirectories.filter(directoriesOnly).forEach(directory => {
} else { // In case of failure
fs.writeFileSync(actualFilePath, actualContent)

try {
const tree = new Stylus.Parser(inputContent).parse()
fs.writeFileSync(debuggingFilePath, JSON.stringify(tree, null, '\t'))
} catch (ex) {
// Do nothing
}

const stack = [
inputFilePath,
actualFilePath,
Expand Down

0 comments on commit 0ad7c6f

Please sign in to comment.