From 9806b24ce34887f2be5e4bca9612fd40337d5c7b Mon Sep 17 00:00:00 2001 From: drugan Date: Tue, 16 May 2017 22:35:53 +0300 Subject: [PATCH 1/2] Add detect() method for the space-between-declarations.js --- src/options/space-between-declarations.js | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/options/space-between-declarations.js b/src/options/space-between-declarations.js index 2c14e60c..7f1540b7 100644 --- a/src/options/space-between-declarations.js +++ b/src/options/space-between-declarations.js @@ -92,6 +92,56 @@ module.exports = (function() { parent.insert(i + 1, space); } }); + }, + + /** + * Detects the value of this option in ast. + * @param {Node} ast + * @return {Array?} List of detected values + */ + detect(ast) { + var detected = []; + + ast.traverseByType('block', block => { + var prevDeclaration = false; + var nextDeclaration = false; + var comment = false; + + block.forEach(blockContent => { + if (blockContent.is('declaration')) { + comment = false; + if (prevDeclaration) { + prevDeclaration = false; + nextDeclaration = true; + } else { + prevDeclaration = true; + nextDeclaration = false; + } + } else if (prevDeclaration && blockContent.is('multilineComment')) { + comment = true; + } else if ((prevDeclaration || comment) && blockContent.is('space')) { + var space = blockContent.content; + + if (comment) { + // If there is comments between two declarations, then we need + // to clean up whitespace and new line characters before each + // comment keeping up the same characters after the comment. + var spaceBeforeComment = detected.splice(-1)[0]; + space = spaceBeforeComment ? spaceBeforeComment.replace(/( )|(\n)|(\n )/g, '') + space : space; + } + + detected.push(space); + } + }); + + if (!nextDeclaration) { + // Remove the last detected space content after the property + // declaration which does not have a pair for it in a block. + detected.splice(-1); + } + }); + + return detected; } }; })(); From 4c48f72dc50db9ef6cf32c72fa6441f59094ad09 Mon Sep 17 00:00:00 2001 From: drugan Date: Wed, 17 May 2017 10:04:36 +0300 Subject: [PATCH 2/2] Improve logic for detecting space between declarations when there is comments between them --- src/options/space-between-declarations.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/options/space-between-declarations.js b/src/options/space-between-declarations.js index 7f1540b7..174f9157 100644 --- a/src/options/space-between-declarations.js +++ b/src/options/space-between-declarations.js @@ -105,11 +105,9 @@ module.exports = (function() { ast.traverseByType('block', block => { var prevDeclaration = false; var nextDeclaration = false; - var comment = false; block.forEach(blockContent => { if (blockContent.is('declaration')) { - comment = false; if (prevDeclaration) { prevDeclaration = false; nextDeclaration = true; @@ -117,20 +115,15 @@ module.exports = (function() { prevDeclaration = true; nextDeclaration = false; } - } else if (prevDeclaration && blockContent.is('multilineComment')) { - comment = true; - } else if ((prevDeclaration || comment) && blockContent.is('space')) { - var space = blockContent.content; - - if (comment) { + } else if (prevDeclaration) { + if (blockContent.is('multilineComment')) { // If there is comments between two declarations, then we need // to clean up whitespace and new line characters before each // comment keeping up the same characters after the comment. - var spaceBeforeComment = detected.splice(-1)[0]; - space = spaceBeforeComment ? spaceBeforeComment.replace(/( )|(\n)|(\n )/g, '') + space : space; + detected.splice(-1); + } else if (blockContent.is('space')) { + detected.push(blockContent.content); } - - detected.push(space); } });