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 tab-size.js #520

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions src/options/tab-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,55 @@ module.exports = {
ast.traverseByType('space', function(space) {
space.content = space.content.replace(/\t/, value);
});
},

/**
* Detects the value of this option in ast.
* @param {Node} ast
* @return {Array?} List of detected values
*/
detect(ast) {
var detected = [];

ast.forEach(node => {
if (node.is('ruleset') || node.is('atrule')) {
var spaces = 0;
var tabs = 0;
var tabsize;

node.forEach(block => {
if (block && block.is('block')) {
block.forEach(blockContent => {
// The indent before the very first property declaration in the
// block usually has a length of one tab or the number of
// whitespaces set for a tab. Because there might be nested
// blocks having incremented indent, we continue to the next root
// block in the each ruleset where tabsize is not detected yet.
if (tabsize !== 'detected') {
if (blockContent.is('space')) {
spaces = blockContent.content.replace(/\n/g, '');
tabs = spaces.match(/\t/g);
spaces = spaces.match(/( )/g);
if (tabs) {
// It is a very rare case when tabs are used in a file
// instead of whitespaces and it requires a lot of efforts
// to find the tab size in a particular environment. So we
// imply that tab size = 2.
tabsize = 2;
} else if (spaces) {
tabsize = spaces.length;
}
} else if (tabsize && blockContent.is('declaration')) {
detected.push(tabsize);
tabsize = 'detected';
}
}
});
}
});
}
});

return detected;
}
};
2 changes: 2 additions & 0 deletions test/options/integral/detect/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('Option `integral`, detect', function() {
let expected = JSON.parse(JSON.stringify(test.Comb.getConfig('csscomb')));
delete expected['sort-order'];
delete expected.exclude;
// The default csscomb.json has no tab-size option.
expected['tab-size'] = 4;
mariovalney marked this conversation as resolved.
Show resolved Hide resolved
test.shouldDetect(undefined, input, expected);
});
});
Expand Down