Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detect() method for the space-between-declarations.js #519

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/options/space-between-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,49 @@ 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;

block.forEach(blockContent => {
if (blockContent.is('declaration')) {
if (prevDeclaration) {
prevDeclaration = false;
nextDeclaration = true;
} else {
prevDeclaration = true;
nextDeclaration = false;
}
} 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.
detected.splice(-1);
} else if (blockContent.is('space')) {
detected.push(blockContent.content);
}
}
});

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;
}
};
})();