|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +let gonzales = require('gonzales-pe'); |
| 4 | + |
| 5 | +let option = { |
| 6 | + newLinesString: '', |
| 7 | + newLinesNode: null, |
| 8 | + |
| 9 | + /** |
| 10 | + * Option's name as it's used in config. |
| 11 | + * @type {String} |
| 12 | + */ |
| 13 | + get name() { |
| 14 | + return 'lines-between-rulesets'; |
| 15 | + }, |
| 16 | + |
| 17 | + /** |
| 18 | + * Name of option that must run after this option. |
| 19 | + * @type {String} |
| 20 | + */ |
| 21 | + get runBefore() { |
| 22 | + return 'block-indent'; |
| 23 | + }, |
| 24 | + |
| 25 | + /** |
| 26 | + * List of syntaxes that are supported by this option. |
| 27 | + * @type {Array} |
| 28 | + */ |
| 29 | + get syntax() { |
| 30 | + return ['css', 'less', 'sass', 'scss']; |
| 31 | + }, |
| 32 | + |
| 33 | + /** |
| 34 | + * Types of values this option accepts in config. |
| 35 | + * @type {Object} |
| 36 | + */ |
| 37 | + get accepts() { |
| 38 | + return { |
| 39 | + number: true |
| 40 | + }; |
| 41 | + }, |
| 42 | + |
| 43 | + /** |
| 44 | + * @param {number} value |
| 45 | + * @returns {number} |
| 46 | + */ |
| 47 | + /* |
| 48 | + ** Still need to override, as the core implementation of setValue doesn't |
| 49 | + ** pass numbers through, but creates a string of spaces of the same length. |
| 50 | + */ |
| 51 | + setValue(value) { |
| 52 | + let valueType = typeof value; |
| 53 | + |
| 54 | + if (valueType !== 'number') { |
| 55 | + throw new Error('Value must be a number.'); |
| 56 | + } |
| 57 | + |
| 58 | + return value; |
| 59 | + }, |
| 60 | + |
| 61 | + buildSpacing(syntax) { |
| 62 | + let spacing = ''; |
| 63 | + let numNewLines = 0; |
| 64 | + let newLinesOffset = 1; |
| 65 | + |
| 66 | + if (syntax === 'sass') { |
| 67 | + newLinesOffset = 0; |
| 68 | + } |
| 69 | + |
| 70 | + numNewLines = Math.round(this.value) + newLinesOffset; |
| 71 | + |
| 72 | + for (var i = 0; i < numNewLines; i++) { |
| 73 | + spacing += '\n'; |
| 74 | + } |
| 75 | + |
| 76 | + return spacing; |
| 77 | + }, |
| 78 | + |
| 79 | + /** |
| 80 | + * Processes ast and fixes found code style errors. |
| 81 | + * @param {Node} ast |
| 82 | + */ |
| 83 | + process(ast) { |
| 84 | + this.newLinesString = this.buildSpacing(ast.syntax); |
| 85 | + this.newLinesNode = gonzales.createNode({ |
| 86 | + type: 'space', |
| 87 | + content: this.newLinesString |
| 88 | + }); |
| 89 | + this.processBlock(ast); |
| 90 | + }, |
| 91 | + |
| 92 | + processBlock(x) { |
| 93 | + if (x.is('stylesheet')) { |
| 94 | + // Check all @rules |
| 95 | + this.processAtRules(x); |
| 96 | + |
| 97 | + // Check all rulesets |
| 98 | + this.processRuleSets(x); |
| 99 | + } |
| 100 | + |
| 101 | + x.forEach((node) => { |
| 102 | + if (!node.is('block')) { |
| 103 | + return this.processBlock(node); |
| 104 | + } |
| 105 | + |
| 106 | + // Check all @rules |
| 107 | + this.processAtRules(node); |
| 108 | + |
| 109 | + // Check all rulesets |
| 110 | + this.processRuleSets(node); |
| 111 | + |
| 112 | + this.processBlock(node); |
| 113 | + }); |
| 114 | + }, |
| 115 | + |
| 116 | + processAtRules(node) { |
| 117 | + node.forEach('atrule', (atRuleNode, index) => { |
| 118 | + this.insertNewlines(node, index); |
| 119 | + }); |
| 120 | + }, |
| 121 | + |
| 122 | + processRuleSets(node) { |
| 123 | + node.forEach('ruleset', (ruleSetNode, index) => { |
| 124 | + this.insertNewlines(node, index); |
| 125 | + }); |
| 126 | + }, |
| 127 | + |
| 128 | + isComment(node) { |
| 129 | + if (!node) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + return (node.is('singlelineComment') || node.is('multilineComment')); |
| 133 | + }, |
| 134 | + |
| 135 | + isNewline(node) { |
| 136 | + if (!node) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + return (node.content === '\n'); |
| 140 | + }, |
| 141 | + |
| 142 | + prevLineIsComment(parent, index) { |
| 143 | + let indexThreshold = 2; |
| 144 | + let prevChild; |
| 145 | + let prevMinusOneChild; |
| 146 | + let prevMinusTwoChild; |
| 147 | + let parentSyntax = parent ? parent.syntax : null; |
| 148 | + |
| 149 | + // Sass is troublesome because newlines are counted as separate nodes |
| 150 | + if (parentSyntax === 'sass') { |
| 151 | + indexThreshold = 3; |
| 152 | + } |
| 153 | + |
| 154 | + if (!parent || index < indexThreshold) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + prevChild = parent.get(index - 1); |
| 159 | + prevMinusOneChild = parent.get(index - 2); |
| 160 | + |
| 161 | + if (parentSyntax === 'sass') { |
| 162 | + prevMinusTwoChild = parent.get(index - 3); |
| 163 | + return ( |
| 164 | + this.isComment(prevMinusTwoChild) && |
| 165 | + this.isNewline(prevMinusOneChild) && |
| 166 | + prevChild.is('space') |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + return (this.isComment(prevMinusOneChild) && prevChild.is('space')); |
| 171 | + }, |
| 172 | + |
| 173 | + /* |
| 174 | + ** Find the latest previous child that isn't a comment, and return its index. |
| 175 | + */ |
| 176 | + findLatestNonCommentNode(parent, index) { |
| 177 | + let prevChild; |
| 178 | + let lastNonCommentIndex = -1; |
| 179 | + let currentIndex = index; |
| 180 | + let jumpSize = 2; |
| 181 | + |
| 182 | + if (parent.syntax === 'sass') { |
| 183 | + jumpSize = 3; |
| 184 | + } |
| 185 | + |
| 186 | + while (currentIndex >= 0) { |
| 187 | + if (this.prevLineIsComment(parent, currentIndex)) { |
| 188 | + currentIndex -= jumpSize; |
| 189 | + continue; |
| 190 | + } |
| 191 | + |
| 192 | + prevChild = parent.get(currentIndex - 1); |
| 193 | + |
| 194 | + if (!this.isComment(prevChild)) { |
| 195 | + lastNonCommentIndex = currentIndex - 1; |
| 196 | + break; |
| 197 | + } |
| 198 | + |
| 199 | + currentIndex--; |
| 200 | + } |
| 201 | + |
| 202 | + return lastNonCommentIndex; |
| 203 | + }, |
| 204 | + |
| 205 | + insertNewlinesAsString(node) { |
| 206 | + let content = node.content; |
| 207 | + let lastNewline = content.lastIndexOf('\n'); |
| 208 | + let newContent; |
| 209 | + |
| 210 | + if (lastNewline > -1) { |
| 211 | + content = content.substring(lastNewline + 1); |
| 212 | + } |
| 213 | + |
| 214 | + newContent = this.newLinesString + content; |
| 215 | + node.content = newContent; |
| 216 | + }, |
| 217 | + |
| 218 | + insertNewlinesAsNode(node) { |
| 219 | + node.insert(node.length, this.newLinesNode); |
| 220 | + }, |
| 221 | + |
| 222 | + insertNewlines(node, index) { |
| 223 | + let prevChild = node.get(index - 1); |
| 224 | + let shouldInsert = false; |
| 225 | + |
| 226 | + // Check for previous nodes that are not a space |
| 227 | + // Do not insert if the ruleset is the first item |
| 228 | + for (var i = 0; i < index; i++) { |
| 229 | + if (!node.get(i).is('space')) { |
| 230 | + shouldInsert = true; |
| 231 | + break; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + if (prevChild && shouldInsert) { |
| 236 | + if (this.prevLineIsComment(node, index) || this.isComment(prevChild)) { |
| 237 | + let lastNonCommentIndex = this.findLatestNonCommentNode(node, index); |
| 238 | + prevChild = node.get(lastNonCommentIndex); |
| 239 | + } |
| 240 | + |
| 241 | + if (prevChild.is('space')) { |
| 242 | + this.insertNewlinesAsString(prevChild); |
| 243 | + } else { |
| 244 | + this.insertNewlinesAsNode(prevChild); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | +}; |
| 249 | + |
| 250 | +module.exports = option; |
0 commit comments