Skip to content
Open
Changes from 1 commit
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
Next Next commit
Add detect() method for the tab-size.js
  • Loading branch information
drugan committed May 17, 2017
commit 6954668eda5698f9cc155bb1364234ce59ed7b33
33 changes: 33 additions & 0 deletions src/options/tab-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,38 @@ 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.traverseByType('block', block => {
var spaces = 0;
var tabs = 0;

block.forEach(blockContent => {
if (blockContent.is('space')) {
spaces = blockContent.content.replace(/\n/g, '');
tabs = spaces.match(/\t/g);
// It is 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.
tabs = tabs ? tabs.length * 2 : 0;
spaces = spaces.match(/( )/g);
spaces = spaces ? spaces.length + tabs : tabs;

} else if (spaces && blockContent.is('declaration')) {
detected.push(spaces);
spaces = 0;
}
});
});

return detected;
}
};