From 2625a8ce4e0f896fc61e3365e970e32710cbf2fe Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Wed, 26 Apr 2017 01:39:06 +0700 Subject: [PATCH] Amended "insertNewLine..." formatting options (cont') --- edge/createFormattingOptions.js | 30 ++++++++++----- edge/format.js | 35 ++++++++++++----- spec/function/output.styl | 2 - spec/hash/output.styl | 1 - spec/operator/output.styl | 2 - .../formattingOptions.json | 2 + .../input.styl | 23 +++++++---- .../output.styl | 36 ++++++++++++++---- .../formattingOptions.json | 6 +++ .../input.styl | 26 +++++++++++++ .../output.styl | 38 +++++++++++++++++++ .../formattingOptions.json | 6 +++ .../input.styl | 25 ++++++++++++ .../output.styl | 30 +++++++++++++++ .../formattingOptions.json | 6 +++ .../input.styl | 25 ++++++++++++ .../output.styl | 34 +++++++++++++++++ .../formattingOptions.json | 6 +++ .../input.styl | 25 ++++++++++++ .../output.styl | 35 +++++++++++++++++ .../option-wrap-mode-5/formattingOptions.json | 3 +- spec/option-wrap-mode-5/input.styl | 1 + spec/option-wrap-mode-5/output.styl | 2 + .../option-wrap-mode-6/formattingOptions.json | 8 ++++ spec/option-wrap-mode-6/input.styl | 4 ++ spec/option-wrap-mode-6/output.styl | 4 ++ .../option-wrap-mode-7/formattingOptions.json | 8 ++++ spec/option-wrap-mode-7/input.styl | 4 ++ spec/option-wrap-mode-7/output.styl | 4 ++ spec/rule/output.styl | 1 - 30 files changed, 393 insertions(+), 39 deletions(-) create mode 100644 spec/option-insert-new-line-around-any-2/formattingOptions.json create mode 100644 spec/option-insert-new-line-around-any-2/input.styl create mode 100644 spec/option-insert-new-line-around-any-2/output.styl create mode 100644 spec/option-insert-new-line-around-any-3/formattingOptions.json create mode 100644 spec/option-insert-new-line-around-any-3/input.styl create mode 100644 spec/option-insert-new-line-around-any-3/output.styl create mode 100644 spec/option-insert-new-line-around-any-4/formattingOptions.json create mode 100644 spec/option-insert-new-line-around-any-4/input.styl create mode 100644 spec/option-insert-new-line-around-any-4/output.styl create mode 100644 spec/option-insert-new-line-around-any-5/formattingOptions.json create mode 100644 spec/option-insert-new-line-around-any-5/input.styl create mode 100644 spec/option-insert-new-line-around-any-5/output.styl create mode 100644 spec/option-wrap-mode-6/formattingOptions.json create mode 100644 spec/option-wrap-mode-6/input.styl create mode 100644 spec/option-wrap-mode-6/output.styl create mode 100644 spec/option-wrap-mode-7/formattingOptions.json create mode 100644 spec/option-wrap-mode-7/input.styl create mode 100644 spec/option-wrap-mode-7/output.styl diff --git a/edge/createFormattingOptions.js b/edge/createFormattingOptions.js index 98a2ccf..5b9aab4 100644 --- a/edge/createFormattingOptions.js +++ b/edge/createFormattingOptions.js @@ -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: { @@ -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 }, {}) @@ -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) { diff --git a/edge/format.js b/edge/format.js index 82b60e8..5ea1850 100644 --- a/edge/format.js +++ b/edge/format.js @@ -231,6 +231,20 @@ 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 => { @@ -238,14 +252,18 @@ function format(content, options = {}) { 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 && ( @@ -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) { @@ -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) { @@ -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) { diff --git a/spec/function/output.styl b/spec/function/output.styl index 5de32fb..544060f 100644 --- a/spec/function/output.styl +++ b/spec/function/output.styl @@ -1,7 +1,6 @@ add(a, b) { a = unit(a, px); b = unit(b, px); - a + b; } @@ -14,7 +13,6 @@ func(x, y = unit(a, px)) { } func(x: 10, y: 25); - alias = func; multi-returned-value-func() { diff --git a/spec/hash/output.styl b/spec/hash/output.styl index c0bfa9e..a19bd66 100644 --- a/spec/hash/output.styl +++ b/spec/hash/output.styl @@ -8,7 +8,6 @@ foo = { a2: { c2: true }, a3: {} }; - foo['0'] = bar; body { diff --git a/spec/operator/output.styl b/spec/operator/output.styl index dd671df..ecae4b4 100644 --- a/spec/operator/output.styl +++ b/spec/operator/output.styl @@ -12,7 +12,6 @@ body { range = 1...5; math = 1 + 2 - 3 * (4 / 5) % 6 ** 7; color = color is defined ? unit(num, 'px') : white; - color ?= white; color ?= white; @@ -20,7 +19,6 @@ body { background-color: #f00 - rgba(100, 0, 0, 0.5); name = 'blue'; - lookup('light-' + name); font-size: num em; diff --git a/spec/option-insert-new-line-around-any-1/formattingOptions.json b/spec/option-insert-new-line-around-any-1/formattingOptions.json index d5340d7..4cdd5e6 100644 --- a/spec/option-insert-new-line-around-any-1/formattingOptions.json +++ b/spec/option-insert-new-line-around-any-1/formattingOptions.json @@ -1,4 +1,6 @@ { + "insertNewLineAroundProperties": true, + "insertNewLineAroundOthers": true, "insertNewLineAroundBlock": true, "newLineChar": "\r\n" } \ No newline at end of file diff --git a/spec/option-insert-new-line-around-any-1/input.styl b/spec/option-insert-new-line-around-any-1/input.styl index 71bb284..8d9fd1b 100644 --- a/spec/option-insert-new-line-around-any-1/input.styl +++ b/spec/option-insert-new-line-around-any-1/input.styl @@ -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 \ No newline at end of file + 1+2 +var = func() +mixin() diff --git a/spec/option-insert-new-line-around-any-1/output.styl b/spec/option-insert-new-line-around-any-1/output.styl index 480613e..f26aba1 100644 --- a/spec/option-insert-new-line-around-any-1/output.styl +++ b/spec/option-insert-new-line-around-any-1/output.styl @@ -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; -} \ No newline at end of file +} + +var = func(); +mixin(); diff --git a/spec/option-insert-new-line-around-any-2/formattingOptions.json b/spec/option-insert-new-line-around-any-2/formattingOptions.json new file mode 100644 index 0000000..b338cc8 --- /dev/null +++ b/spec/option-insert-new-line-around-any-2/formattingOptions.json @@ -0,0 +1,6 @@ +{ + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": false, + "insertNewLineAroundBlock": true, + "newLineChar": "\r\n" +} \ No newline at end of file diff --git a/spec/option-insert-new-line-around-any-2/input.styl b/spec/option-insert-new-line-around-any-2/input.styl new file mode 100644 index 0000000..0bbd85d --- /dev/null +++ b/spec/option-insert-new-line-around-any-2/input.styl @@ -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() diff --git a/spec/option-insert-new-line-around-any-2/output.styl b/spec/option-insert-new-line-around-any-2/output.styl new file mode 100644 index 0000000..6de5a3c --- /dev/null +++ b/spec/option-insert-new-line-around-any-2/output.styl @@ -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(); diff --git a/spec/option-insert-new-line-around-any-3/formattingOptions.json b/spec/option-insert-new-line-around-any-3/formattingOptions.json new file mode 100644 index 0000000..b415383 --- /dev/null +++ b/spec/option-insert-new-line-around-any-3/formattingOptions.json @@ -0,0 +1,6 @@ +{ + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": false, + "insertNewLineAroundBlock": false, + "newLineChar": "\r\n" +} \ No newline at end of file diff --git a/spec/option-insert-new-line-around-any-3/input.styl b/spec/option-insert-new-line-around-any-3/input.styl new file mode 100644 index 0000000..8d9fd1b --- /dev/null +++ b/spec/option-insert-new-line-around-any-3/input.styl @@ -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() diff --git a/spec/option-insert-new-line-around-any-3/output.styl b/spec/option-insert-new-line-around-any-3/output.styl new file mode 100644 index 0000000..8e3701f --- /dev/null +++ b/spec/option-insert-new-line-around-any-3/output.styl @@ -0,0 +1,30 @@ +.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(); diff --git a/spec/option-insert-new-line-around-any-4/formattingOptions.json b/spec/option-insert-new-line-around-any-4/formattingOptions.json new file mode 100644 index 0000000..b59ade1 --- /dev/null +++ b/spec/option-insert-new-line-around-any-4/formattingOptions.json @@ -0,0 +1,6 @@ +{ + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": "root", + "insertNewLineAroundBlock": "root", + "newLineChar": "\r\n" +} \ No newline at end of file diff --git a/spec/option-insert-new-line-around-any-4/input.styl b/spec/option-insert-new-line-around-any-4/input.styl new file mode 100644 index 0000000..8d9fd1b --- /dev/null +++ b/spec/option-insert-new-line-around-any-4/input.styl @@ -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() diff --git a/spec/option-insert-new-line-around-any-4/output.styl b/spec/option-insert-new-line-around-any-4/output.styl new file mode 100644 index 0000000..38519cb --- /dev/null +++ b/spec/option-insert-new-line-around-any-4/output.styl @@ -0,0 +1,34 @@ +.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(); diff --git a/spec/option-insert-new-line-around-any-5/formattingOptions.json b/spec/option-insert-new-line-around-any-5/formattingOptions.json new file mode 100644 index 0000000..5326e73 --- /dev/null +++ b/spec/option-insert-new-line-around-any-5/formattingOptions.json @@ -0,0 +1,6 @@ +{ + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": "nested", + "insertNewLineAroundBlock": "nested", + "newLineChar": "\r\n" +} \ No newline at end of file diff --git a/spec/option-insert-new-line-around-any-5/input.styl b/spec/option-insert-new-line-around-any-5/input.styl new file mode 100644 index 0000000..8d9fd1b --- /dev/null +++ b/spec/option-insert-new-line-around-any-5/input.styl @@ -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() diff --git a/spec/option-insert-new-line-around-any-5/output.styl b/spec/option-insert-new-line-around-any-5/output.styl new file mode 100644 index 0000000..5ba6c42 --- /dev/null +++ b/spec/option-insert-new-line-around-any-5/output.styl @@ -0,0 +1,35 @@ +.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(); diff --git a/spec/option-wrap-mode-5/formattingOptions.json b/spec/option-wrap-mode-5/formattingOptions.json index f028979..105878e 100644 --- a/spec/option-wrap-mode-5/formattingOptions.json +++ b/spec/option-wrap-mode-5/formattingOptions.json @@ -1,7 +1,8 @@ { "wrapMode": true, "insertBraces": false, - "insertNewLineBetweenGroups": 1, + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": "nested", "sortProperties": "grouped", "newLineChar": "\r\n" } \ No newline at end of file diff --git a/spec/option-wrap-mode-5/input.styl b/spec/option-wrap-mode-5/input.styl index c2b6e6a..ce14221 100644 --- a/spec/option-wrap-mode-5/input.styl +++ b/spec/option-wrap-mode-5/input.styl @@ -1,3 +1,4 @@ height auto width auto + mixin() diff --git a/spec/option-wrap-mode-5/output.styl b/spec/option-wrap-mode-5/output.styl index 6b9fe41..c4397c7 100644 --- a/spec/option-wrap-mode-5/output.styl +++ b/spec/option-wrap-mode-5/output.styl @@ -1,3 +1,5 @@ width: auto; height: auto; + + mixin(); diff --git a/spec/option-wrap-mode-6/formattingOptions.json b/spec/option-wrap-mode-6/formattingOptions.json new file mode 100644 index 0000000..e188655 --- /dev/null +++ b/spec/option-wrap-mode-6/formattingOptions.json @@ -0,0 +1,8 @@ +{ + "wrapMode": true, + "insertBraces": false, + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": "root", + "sortProperties": "grouped", + "newLineChar": "\r\n" +} \ No newline at end of file diff --git a/spec/option-wrap-mode-6/input.styl b/spec/option-wrap-mode-6/input.styl new file mode 100644 index 0000000..ce14221 --- /dev/null +++ b/spec/option-wrap-mode-6/input.styl @@ -0,0 +1,4 @@ + + height auto + width auto + mixin() diff --git a/spec/option-wrap-mode-6/output.styl b/spec/option-wrap-mode-6/output.styl new file mode 100644 index 0000000..3fbae77 --- /dev/null +++ b/spec/option-wrap-mode-6/output.styl @@ -0,0 +1,4 @@ + + width: auto; + height: auto; + mixin(); diff --git a/spec/option-wrap-mode-7/formattingOptions.json b/spec/option-wrap-mode-7/formattingOptions.json new file mode 100644 index 0000000..105878e --- /dev/null +++ b/spec/option-wrap-mode-7/formattingOptions.json @@ -0,0 +1,8 @@ +{ + "wrapMode": true, + "insertBraces": false, + "insertNewLineAroundProperties": false, + "insertNewLineAroundOthers": "nested", + "sortProperties": "grouped", + "newLineChar": "\r\n" +} \ No newline at end of file diff --git a/spec/option-wrap-mode-7/input.styl b/spec/option-wrap-mode-7/input.styl new file mode 100644 index 0000000..653d5ef --- /dev/null +++ b/spec/option-wrap-mode-7/input.styl @@ -0,0 +1,4 @@ + +height auto +width auto +mixin() diff --git a/spec/option-wrap-mode-7/output.styl b/spec/option-wrap-mode-7/output.styl new file mode 100644 index 0000000..e6c3d8f --- /dev/null +++ b/spec/option-wrap-mode-7/output.styl @@ -0,0 +1,4 @@ + +width: auto; +height: auto; +mixin(); diff --git a/spec/rule/output.styl b/spec/rule/output.styl index a60cf25..4872e73 100644 --- a/spec/rule/output.styl +++ b/spec/rule/output.styl @@ -26,5 +26,4 @@ -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=25)"; } } - @reset-global pc; \ No newline at end of file