From 06e1eeb3e7b3600b1bdd8b37ec98182dde0c8825 Mon Sep 17 00:00:00 2001 From: Tomdy Qin Date: Fri, 10 Feb 2017 03:35:53 +0800 Subject: [PATCH 1/2] Create lines-between-rulesets.js clone from csscomb.js but change to es5 , add for https://github.com/csscomb/csscomb.js/blob/dev/doc/options.md#lines-between-rulesets --- .../lib/options/lines-between-rulesets.js | 245 ++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 node_modules/csscomb/lib/options/lines-between-rulesets.js diff --git a/node_modules/csscomb/lib/options/lines-between-rulesets.js b/node_modules/csscomb/lib/options/lines-between-rulesets.js new file mode 100644 index 0000000..0598dab --- /dev/null +++ b/node_modules/csscomb/lib/options/lines-between-rulesets.js @@ -0,0 +1,245 @@ +'use strict'; + +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + +var gonzales = require('gonzales-pe'); + +var option = { + newLinesString: '\n', + newLinesNode: null, + + /** + * Option's name as it's used in config. + * @type {String} + */ + get name() { + return 'lines-between-rulesets'; + }, + + /** + * Name of option that must run after this option. + * @type {String} + */ + get runBefore() { + return 'block-indent'; + }, + + /** + * List of syntaxes that are supported by this option. + * @type {Array} + */ + get syntax() { + return ['css', 'less', 'sass', 'scss']; + }, + + /** + * Types of values this option accepts in config. + * @type {Object} + */ + get accepts() { + return { + number: true + }; + }, + + /** + * @param {number} value + * @returns {number} + */ + /* + ** Still need to override, as the core implementation of setValue doesn't + ** pass numbers through, but creates a string of spaces of the same length. + */ + setValue: function setValue(value) { + var valueType = typeof value === 'undefined' ? 'undefined' : _typeof(value); + + if (valueType !== 'number') { + throw new Error('Value must be a number.'); + } + + return value; + }, + buildSpacing: function buildSpacing(syntax,value) { + var spacing = ''; + var numNewLines = 0; + var newLinesOffset = 1; + + if (syntax === 'sass') { + newLinesOffset = 0; + } + numNewLines = Math.round(value) + newLinesOffset; + + for (var i = 0; i < numNewLines; i++) { + spacing += '\n'; + } + + return spacing; + }, + + /** + * Processes ast and fixes found code style errors. + * @param {Node} ast + */ + process: function process(ast) { + var value = this.getValue('lines-between-rulesets'); + option.newLinesString = option.buildSpacing(ast.syntax,value); + option.newLinesNode = gonzales.createNode({ + type: 'space', + content: option.newLinesString + }); + option.processBlock(ast); + }, + processBlock: function processBlock(x) { + var _this = option; + + if (x.is('stylesheet')) { + // Check all @rules + option.processAtRules(x); + + // Check all rulesets + option.processRuleSets(x); + } + + x.forEach(function (node) { + if (!node.is('block')) { + return _this.processBlock(node); + } + + // Check all @rules + _this.processAtRules(node); + + // Check all rulesets + _this.processRuleSets(node); + + _this.processBlock(node); + }); + }, + processAtRules: function processAtRules(node) { + var _this2 = option; + + node.forEach('atrule', function (atRuleNode, index) { + _this2.insertNewlines(node, index); + }); + }, + processRuleSets: function processRuleSets(node) { + var _this3 = option; + + node.forEach('ruleset', function (ruleSetNode, index) { + _this3.insertNewlines(node, index); + }); + }, + isComment: function isComment(node) { + if (!node) { + return false; + } + return node.is('singlelineComment') || node.is('multilineComment'); + }, + isNewline: function isNewline(node) { + if (!node) { + return false; + } + return node.content === '\n'; + }, + prevLineIsComment: function prevLineIsComment(parent, index) { + var indexThreshold = 2; + var prevChild = void 0; + var prevMinusOneChild = void 0; + var prevMinusTwoChild = void 0; + var parentSyntax = parent ? parent.syntax : null; + + // Sass is troublesome because newlines are counted as separate nodes + if (parentSyntax === 'sass') { + indexThreshold = 3; + } + + if (!parent || index < indexThreshold) { + return false; + } + + prevChild = parent.get(index - 1); + prevMinusOneChild = parent.get(index - 2); + + if (parentSyntax === 'sass') { + prevMinusTwoChild = parent.get(index - 3); + return option.isComment(prevMinusTwoChild) && option.isNewline(prevMinusOneChild) && prevChild.is('space'); + } + + return option.isComment(prevMinusOneChild) && prevChild.is('space'); + }, + + + /* + ** Find the latest previous child that isn't a comment, and return its index. + */ + findLatestNonCommentNode: function findLatestNonCommentNode(parent, index) { + var prevChild = void 0; + var lastNonCommentIndex = -1; + var currentIndex = index; + var jumpSize = 2; + + if (parent.syntax === 'sass') { + jumpSize = 3; + } + + while (currentIndex >= 0) { + if (option.prevLineIsComment(parent, currentIndex)) { + currentIndex -= jumpSize; + continue; + } + + prevChild = parent.get(currentIndex - 1); + + if (!option.isComment(prevChild)) { + lastNonCommentIndex = currentIndex - 1; + break; + } + + currentIndex--; + } + + return lastNonCommentIndex; + }, + insertNewlinesAsString: function insertNewlinesAsString(node) { + var content = node.content; + var lastNewline = content.lastIndexOf('\n'); + var newContent = void 0; + + if (lastNewline > -1) { + content = content.substring(lastNewline + 1); + } + + newContent = option.newLinesString + content; + node.content = newContent; + }, + insertNewlinesAsNode: function insertNewlinesAsNode(node) { + node.insert(node.length, option.newLinesNode); + }, + insertNewlines: function insertNewlines(node, index) { + var prevChild = node.get(index - 1); + var shouldInsert = false; + + // Check for previous nodes that are not a space + // Do not insert if the ruleset is the first item + for (var i = 0; i < index; i++) { + if (!node.get(i).is('space')) { + shouldInsert = true; + break; + } + } + + if (prevChild && shouldInsert) { + if (option.prevLineIsComment(node, index) || option.isComment(prevChild)) { + var lastNonCommentIndex = option.findLatestNonCommentNode(node, index); + prevChild = node.get(lastNonCommentIndex); + } + + if (prevChild.is('space')) { + option.insertNewlinesAsString(prevChild); + } else { + option.insertNewlinesAsNode(prevChild); + } + } + } +}; + +module.exports = option; From 80bd78984a8fe8eaaf011e496dd8f002853b767c Mon Sep 17 00:00:00 2001 From: Tomdy Qin Date: Fri, 10 Feb 2017 08:31:00 +0800 Subject: [PATCH 2/2] Update lines-between-rulesets.js not need to es5 --- .../lib/options/lines-between-rulesets.js | 134 ++++++++++-------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/node_modules/csscomb/lib/options/lines-between-rulesets.js b/node_modules/csscomb/lib/options/lines-between-rulesets.js index 0598dab..269125f 100644 --- a/node_modules/csscomb/lib/options/lines-between-rulesets.js +++ b/node_modules/csscomb/lib/options/lines-between-rulesets.js @@ -1,11 +1,9 @@ 'use strict'; -var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; +let gonzales = require('gonzales-pe'); -var gonzales = require('gonzales-pe'); - -var option = { - newLinesString: '\n', +let option = { + newLinesString: '', newLinesNode: null, /** @@ -50,8 +48,8 @@ var option = { ** Still need to override, as the core implementation of setValue doesn't ** pass numbers through, but creates a string of spaces of the same length. */ - setValue: function setValue(value) { - var valueType = typeof value === 'undefined' ? 'undefined' : _typeof(value); + setValue(value) { + let valueType = typeof value; if (valueType !== 'number') { throw new Error('Value must be a number.'); @@ -59,14 +57,16 @@ var option = { return value; }, - buildSpacing: function buildSpacing(syntax,value) { - var spacing = ''; - var numNewLines = 0; - var newLinesOffset = 1; + + buildSpacing(syntax, value) { + let spacing = ''; + let numNewLines = 0; + let newLinesOffset = 1; if (syntax === 'sass') { newLinesOffset = 0; } + numNewLines = Math.round(value) + newLinesOffset; for (var i = 0; i < numNewLines; i++) { @@ -80,27 +80,29 @@ var option = { * Processes ast and fixes found code style errors. * @param {Node} ast */ - process: function process(ast) { + process(ast) { + var _this = option; var value = this.getValue('lines-between-rulesets'); - option.newLinesString = option.buildSpacing(ast.syntax,value); - option.newLinesNode = gonzales.createNode({ + // console.log(value,_this) + _this.newLinesString = _this.buildSpacing(ast.syntax, value); + _this.newLinesNode = gonzales.createNode({ type: 'space', - content: option.newLinesString + content: _this.newLinesString }); - option.processBlock(ast); + _this.processBlock(ast); }, - processBlock: function processBlock(x) { - var _this = option; + processBlock(x) { + var _this = option; if (x.is('stylesheet')) { // Check all @rules - option.processAtRules(x); + _this.processAtRules(x); // Check all rulesets - option.processRuleSets(x); + _this.processRuleSets(x); } - x.forEach(function (node) { + x.forEach((node) => { if (!node.is('block')) { return _this.processBlock(node); } @@ -114,39 +116,40 @@ var option = { _this.processBlock(node); }); }, - processAtRules: function processAtRules(node) { - var _this2 = option; - node.forEach('atrule', function (atRuleNode, index) { - _this2.insertNewlines(node, index); + processAtRules(node) { + node.forEach('atrule', (atRuleNode, index) => { + option.insertNewlines(node, index); }); }, - processRuleSets: function processRuleSets(node) { - var _this3 = option; - node.forEach('ruleset', function (ruleSetNode, index) { - _this3.insertNewlines(node, index); + processRuleSets(node) { + node.forEach('ruleset', (ruleSetNode, index) => { + option.insertNewlines(node, index); }); }, - isComment: function isComment(node) { + + isComment(node) { if (!node) { return false; } - return node.is('singlelineComment') || node.is('multilineComment'); + return (node.is('singlelineComment') || node.is('multilineComment')); }, - isNewline: function isNewline(node) { + + isNewline(node) { if (!node) { return false; } - return node.content === '\n'; + return (node.content === '\n'); }, - prevLineIsComment: function prevLineIsComment(parent, index) { - var indexThreshold = 2; - var prevChild = void 0; - var prevMinusOneChild = void 0; - var prevMinusTwoChild = void 0; - var parentSyntax = parent ? parent.syntax : null; + prevLineIsComment(parent, index) { + let indexThreshold = 2; + let prevChild; + let prevMinusOneChild; + let prevMinusTwoChild; + let parentSyntax = parent ? parent.syntax : null; + var _this = option; // Sass is troublesome because newlines are counted as separate nodes if (parentSyntax === 'sass') { indexThreshold = 3; @@ -161,35 +164,38 @@ var option = { if (parentSyntax === 'sass') { prevMinusTwoChild = parent.get(index - 3); - return option.isComment(prevMinusTwoChild) && option.isNewline(prevMinusOneChild) && prevChild.is('space'); + return ( + _this.isComment(prevMinusTwoChild) && + _this.isNewline(prevMinusOneChild) && + prevChild.is('space') + ); } - return option.isComment(prevMinusOneChild) && prevChild.is('space'); + return (_this.isComment(prevMinusOneChild) && prevChild.is('space')); }, - /* ** Find the latest previous child that isn't a comment, and return its index. */ - findLatestNonCommentNode: function findLatestNonCommentNode(parent, index) { - var prevChild = void 0; - var lastNonCommentIndex = -1; - var currentIndex = index; - var jumpSize = 2; - + findLatestNonCommentNode(parent, index) { + let prevChild; + let lastNonCommentIndex = -1; + let currentIndex = index; + let jumpSize = 2; + var _this = option; if (parent.syntax === 'sass') { jumpSize = 3; } while (currentIndex >= 0) { - if (option.prevLineIsComment(parent, currentIndex)) { + if (_this.prevLineIsComment(parent, currentIndex)) { currentIndex -= jumpSize; continue; } prevChild = parent.get(currentIndex - 1); - if (!option.isComment(prevChild)) { + if (!_this.isComment(prevChild)) { lastNonCommentIndex = currentIndex - 1; break; } @@ -199,10 +205,11 @@ var option = { return lastNonCommentIndex; }, - insertNewlinesAsString: function insertNewlinesAsString(node) { - var content = node.content; - var lastNewline = content.lastIndexOf('\n'); - var newContent = void 0; + + insertNewlinesAsString(node) { + let content = node.content; + let lastNewline = content.lastIndexOf('\n'); + let newContent; if (lastNewline > -1) { content = content.substring(lastNewline + 1); @@ -211,12 +218,14 @@ var option = { newContent = option.newLinesString + content; node.content = newContent; }, - insertNewlinesAsNode: function insertNewlinesAsNode(node) { + + insertNewlinesAsNode(node) { node.insert(node.length, option.newLinesNode); }, - insertNewlines: function insertNewlines(node, index) { - var prevChild = node.get(index - 1); - var shouldInsert = false; + + insertNewlines(node, index) { + let prevChild = node.get(index - 1); + let shouldInsert = false; // Check for previous nodes that are not a space // Do not insert if the ruleset is the first item @@ -228,15 +237,16 @@ var option = { } if (prevChild && shouldInsert) { - if (option.prevLineIsComment(node, index) || option.isComment(prevChild)) { - var lastNonCommentIndex = option.findLatestNonCommentNode(node, index); + var _this = option; + if (_this.prevLineIsComment(node, index) || _this.isComment(prevChild)) { + let lastNonCommentIndex = _this.findLatestNonCommentNode(node, index); prevChild = node.get(lastNonCommentIndex); } if (prevChild.is('space')) { - option.insertNewlinesAsString(prevChild); + _this.insertNewlinesAsString(prevChild); } else { - option.insertNewlinesAsNode(prevChild); + _this.insertNewlinesAsNode(prevChild); } } }