Skip to content

Commit

Permalink
Amended "insertNewLine..." formatting options (cont')
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta authored and Anantachai Saothong (Manta) committed Apr 27, 2017
1 parent 0ad7c6f commit 2625a8c
Show file tree
Hide file tree
Showing 30 changed files with 393 additions and 39 deletions.
30 changes: 21 additions & 9 deletions edge/createFormattingOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ const schema = {
`
}
},
insertNewLineAroundBlock: {
insertNewLineAroundProperties: {
description: '',
type: 'boolean',
oneOf: [true, false],
default: true,
},
insertNewLineAroundProperties: {
insertNewLineAroundOthers: {
description: '',
type: 'boolean',
oneOf: [true, false, 'root', 'nested'],
default: true,
},
insertNewLineAroundOthers: {
insertNewLineAroundBlock: {
description: '',
type: 'boolean',
oneOf: [true, false, 'root', 'nested'],
default: true,
},
insertNewLineBetweenSelectors: {
Expand Down Expand Up @@ -290,8 +290,7 @@ function createFormattingOptions(options = {}) {
hash[name] = options[name]
}
} catch (ex) {
ex.message += ` at "${name}".`
throw ex
throw new Error(ex.message + ` at "${name}".`)
}
return hash
}, {})
Expand All @@ -300,7 +299,20 @@ function createFormattingOptions(options = {}) {

function verify(data, info) {
if (info.oneOf !== undefined) {
info.oneOf.some(item => _.isObject(item) ? verify(data, item) : (item === data))
const matchAnyValue = info.oneOf.some(item => {
if (_.isObject(item)) {
try {
return verify(data, item)
} catch (ex) {
return false
}
} else {
return item === data
}
})
if (matchAnyValue === false) {
throw new Error(`Expected ${data} to be one of the defined values`)
}

} else if (info.type === 'integer') {
if (_.isInteger(data) === false) {
Expand Down
35 changes: 26 additions & 9 deletions edge/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,39 @@ function format(content, options = {}) {
}
})

const checkIf = (value) => {
if (value === true) {
return true
}

if (options.wrapMode) {
return _.some(originalBaseIndent) ? value === 'nested' : value === 'root'
} else {
return inputNode instanceof Stylus.nodes.Root ? value === 'root' : value === 'nested'
}

return false
}

// Insert CSS body
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
nodeType === 'Block' && checkIf(options.insertNewLineAroundBlock) ||
nodeType === 'Property' && checkIf(options.insertNewLineAroundProperties) ||
nodeType === 'Other' && checkIf(options.insertNewLineAroundOthers)
) {
newLineAround = options.newLineChar
}

return _.compact([newLineAround, group.map(node => travel(inputNode, node, childIndentLevel)), newLineAround])
return _.compact([
newLineAround,
group.map(node => travel(inputNode, node, childIndentLevel)).join(''),
newLineAround
])
})
.flatten()
.reject((text, rank, list) => text === options.newLineChar && (
Expand All @@ -257,8 +275,6 @@ function format(content, options = {}) {
.value()
)

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

// Insert the bottom comment(s)
const bottomCommentNodes = tryGetSingleLineCommentNodesOnTheBottomOf(_.last(nonCommentNodes))
if (bottomCommentNodes) {
Expand Down Expand Up @@ -754,9 +770,9 @@ function format(content, options = {}) {
}
if (inputNode.nodes.length > 0) {
if (inputNode.type) {
outputBuffer.append(' and ')
}
outputBuffer.append(inputNode.nodes.map(node => travel(inputNode, node, indentLevel, true)).join(' and '))
outputBuffer.append(' and ')
}
outputBuffer.append(inputNode.nodes.map(node => travel(inputNode, node, indentLevel, true)).join(' and '))
}

} else if (inputNode instanceof Stylus.nodes.Feature) {
Expand Down Expand Up @@ -821,6 +837,7 @@ function format(content, options = {}) {
if (options.insertSemicolons) {
outputBuffer.append(';')
}
outputBuffer.append(options.newLineChar)

} else if (inputNode instanceof Stylus.nodes.Comment && inputNode.str.startsWith('//')) { // In case of single-line comments
if (insideExpression === false) {
Expand Down
2 changes: 0 additions & 2 deletions spec/function/output.styl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
add(a, b) {
a = unit(a, px);
b = unit(b, px);

a + b;
}

Expand All @@ -14,7 +13,6 @@ func(x, y = unit(a, px)) {
}

func(x: 10, y: 25);

alias = func;

multi-returned-value-func() {
Expand Down
1 change: 0 additions & 1 deletion spec/hash/output.styl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ foo = {
a2: { c2: true },
a3: {}
};

foo['0'] = bar;

body {
Expand Down
2 changes: 0 additions & 2 deletions spec/operator/output.styl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ body {
range = 1...5;
math = 1 + 2 - 3 * (4 / 5) % 6 ** 7;
color = color is defined ? unit(num, 'px') : white;

color ?= white;
color ?= white;

border-color: #fff is a 'rgba';
background-color: #f00 - rgba(100, 0, 0, 0.5);

name = 'blue';

lookup('light-' + name);

font-size: num em;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"insertNewLineAroundProperties": true,
"insertNewLineAroundOthers": true,
"insertNewLineAroundBlock": true,
"newLineChar": "\r\n"
}
23 changes: 16 additions & 7 deletions spec/option-insert-new-line-around-any-1/input.styl
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
.class1
display none
mixin()
display func()
func()
1+2
var = func()
mixin()
margin 0
padding 0

.class2
display none
margin 0
padding 0

.class3
display none
.class4
display none

mixin()
display block

var = 1

display func()
func()
1+2
1+2
var = func()
mixin()
36 changes: 29 additions & 7 deletions spec/option-insert-new-line-around-any-1/output.styl
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
body {
display: none;
.class1 {
mixin() {
display: func();
}

func() {
1 + 2;
}

var = func();
mixin();

margin: 0;
padding: 0;
}

.class2 {
margin: 0;
padding: 0;

.class1 {
.class3 {
display: none;
}

.class4 {
display: none;
}
}

mixin() {
display: block;
display: func();
}

var = 1;

func() {
1 + 2;
}
}

var = func();
mixin();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"insertNewLineAroundProperties": false,
"insertNewLineAroundOthers": false,
"insertNewLineAroundBlock": true,
"newLineChar": "\r\n"
}
26 changes: 26 additions & 0 deletions spec/option-insert-new-line-around-any-2/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.class1
mixin()
display func()
func()
1+2
var = func()
mixin()

margin 0
padding 0

.class2
margin 0
padding 0

.class3
display none
.class4
display none

mixin()
display func()
func()
1+2
var = func()
mixin()
38 changes: 38 additions & 0 deletions spec/option-insert-new-line-around-any-2/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.class1 {
mixin() {
display: func();
}

func() {
1 + 2;
}

var = func();
mixin();
margin: 0;
padding: 0;
}

.class2 {
margin: 0;
padding: 0;

.class3 {
display: none;
}

.class4 {
display: none;
}
}

mixin() {
display: func();
}

func() {
1 + 2;
}

var = func();
mixin();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"insertNewLineAroundProperties": false,
"insertNewLineAroundOthers": false,
"insertNewLineAroundBlock": false,
"newLineChar": "\r\n"
}
25 changes: 25 additions & 0 deletions spec/option-insert-new-line-around-any-3/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.class1
mixin()
display func()
func()
1+2
var = func()
mixin()
margin 0
padding 0

.class2
margin 0
padding 0

.class3
display none
.class4
display none

mixin()
display func()
func()
1+2
var = func()
mixin()
Loading

0 comments on commit 2625a8c

Please sign in to comment.