Skip to content

Commit

Permalink
Merge pull request #823 from DanPurdy/feature/tidy-mergeable-selectors
Browse files Browse the repository at this point in the history
Tidy mergeable selectors
  • Loading branch information
bgriffith authored Aug 16, 2016
2 parents 91dfe0b + dc2d59d commit 052dcd1
Showing 1 changed file with 5 additions and 108 deletions.
113 changes: 5 additions & 108 deletions lib/rules/no-mergeable-selectors.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,17 @@
'use strict';

var helpers = require('../helpers');
var helpers = require('../helpers'),
selectorHelpers = require('../selector-helpers');

var mergeableNodes = ['atrule', 'include', 'ruleset'],
validAtRules = ['media'],
simpleIdents = ['ident', 'number', 'operator', 'combinator', 'string', 'parentSelector', 'delimiter', 'typeSelector', 'attributeMatch'],
curLevel = 0,
curSelector = [],
parentSelector = [],
selectorList = [],
syntax = '';


/**
* Adds grammar around our content blocks to construct selectors with
* more readable formats.
*
* @param {object} val - The current value node
* @param {string} prefix - The grammar to prefix the value with
* @param {string} suffix - The grammar to add after the value
* @returns {string} The correct readable format
*/
var addGrammar = function (val, prefix, suffix) {
return prefix + val.content + suffix;
};

/**
* Adds grammar around our content blocks to construct selectors with
* more readable formats and loops the content as they're within sub blocks.
*
* @param {object} val - The current value node
* @param {string} prefix - The grammar to prefix the value with
* @param {string} suffix - The grammar to add after the value
* @param {function} constructSelector - The callback we wish to use which means constructSelector in this instance
* @returns {string} The correct readable format
*/
var constructSubSelector = function (val, prefix, suffix, constructSelector) {
var content = prefix;
val.forEach(function (subItem) {
content += constructSelector(subItem);
});

return content + suffix;
};

/**
* Constructs a syntax complete selector for our selector matching and warning output
*
* @param {object} val - The current node / part of our selector
* @returns {string} - Content: The current node with correct syntax e.g. class my-class = '.my-class'
*/
var constructSelector = function (val) {
var content = null;

if (val.is('id')) {
content = addGrammar(val, '#', '');
}

else if (val.is('class')) {
content = addGrammar(val, '.', '');
}

else if (simpleIdents.indexOf(val.type) !== -1) {
content = val.content;
}

else if (val.is('attributeSelector')) {
content = constructSubSelector(val, '[', ']', constructSelector);
}

else if (val.is('atkeyword')) {
content = constructSubSelector(val, '@', '', constructSelector);
}

else if (val.is('placeholder')) {
content = constructSubSelector(val, '%', '', constructSelector);
}

else if (val.is('variable')) {
content = constructSubSelector(val, '$', '', constructSelector);
}

else if (val.is('pseudoClass')) {
content = addGrammar(val, ':', '');
}

else if (val.is('pseudoElement')) {
content = addGrammar(val, '::', '');
}

else if (val.is('nth')) {
content = addGrammar(val, '(', ')');
}

else if (val.is('nthSelector')) {
content = constructSubSelector(val, ':', '', constructSelector);
}

else if (val.is('parentheses')) {
content = constructSubSelector(val, '(', ')', constructSelector);
}

else if (val.is('space')) {
content = ' ';
}

else if (val.is('parentSelectorExtension') || val.is('attributeName') || val.is('attributeValue') || val.is('dimension')) {
content = constructSubSelector(val, '', '', constructSelector);
}

else if (val.is('interpolation')) {
content = constructSubSelector(val, '#{', '}', constructSelector);
}
return content;
};

/**
* Traverses a block and calls our callback function for each block encountered
*
Expand Down Expand Up @@ -161,11 +58,11 @@ var checkRuleset = function (ruleNode) {
if (!ruleNodeItem.is('block')) {
if (ruleNodeItem.is('selector')) {
ruleNodeItem.forEach(function (selectorContent) {
ruleSet += constructSelector(selectorContent);
ruleSet += selectorHelpers.constructSelector(selectorContent);
});
}
else if (ruleNodeItem.is('delimiter') || ruleNodeItem.is('space')) {
ruleSet += constructSelector(ruleNodeItem);
ruleSet += selectorHelpers.constructSelector(ruleNodeItem);
}
}
});
Expand All @@ -184,7 +81,7 @@ var checkAtRule = function (atRule) {
var test = '';
atRule.forEach(function (atRuleItem) {
if (!atRuleItem.is('block')) {
test += constructSelector(atRuleItem);
test += selectorHelpers.constructSelector(atRuleItem);
}
});
updateList(test, true, atRule.start.line, atRule.start.column);
Expand Down

0 comments on commit 052dcd1

Please sign in to comment.