|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var helpers = require('../helpers'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Constructs a syntax complete selector for our selector matching and warning output |
| 7 | + * @param {object} val - the current node / part of our selector |
| 8 | + * @returns {object} - content: The current node with correct syntax e.g. class my-class = '.my-class' |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +var constructSelector = function (val) { |
| 13 | + |
| 14 | + var content = val.content; |
| 15 | + |
| 16 | + if (val.is('id')) { |
| 17 | + content = '#' + val.content; |
| 18 | + } |
| 19 | + |
| 20 | + else if (val.is('class')) { |
| 21 | + content = '.' + val.content; |
| 22 | + } |
| 23 | + |
| 24 | + else if (val.is('ident')) { |
| 25 | + content = val.content; |
| 26 | + } |
| 27 | + |
| 28 | + else if (val.is('attribute')) { |
| 29 | + var selector = '['; |
| 30 | + |
| 31 | + val.forEach( function (attrib) { |
| 32 | + selector += constructSelector(attrib); |
| 33 | + }); |
| 34 | + |
| 35 | + content = selector + ']'; |
| 36 | + } |
| 37 | + |
| 38 | + else if (val.is('pseudoClass')) { |
| 39 | + content = ':' + val.content; |
| 40 | + } |
| 41 | + |
| 42 | + else if (val.is('pseudoElement')) { |
| 43 | + content = '::' + val.content; |
| 44 | + } |
| 45 | + |
| 46 | + else if (val.is('nth')) { |
| 47 | + content = '(' + val.content + ')'; |
| 48 | + } |
| 49 | + |
| 50 | + else if (val.is('nthSelector')) { |
| 51 | + var nthSelector = ':'; |
| 52 | + |
| 53 | + val.forEach( function (attrib) { |
| 54 | + nthSelector += constructSelector(attrib); |
| 55 | + }); |
| 56 | + |
| 57 | + content = nthSelector; |
| 58 | + } |
| 59 | + |
| 60 | + else if (val.is('space')) { |
| 61 | + content = ' '; |
| 62 | + } |
| 63 | + |
| 64 | + else if (val.is('parentSelector')) { |
| 65 | + content = val.content; |
| 66 | + } |
| 67 | + |
| 68 | + else if (val.is('combinator')) { |
| 69 | + content = val.content; |
| 70 | + } |
| 71 | + |
| 72 | + return content; |
| 73 | + |
| 74 | +}; |
| 75 | + |
| 76 | +module.exports = { |
| 77 | + 'name': 'no-mergeable-selectors', |
| 78 | + 'defaults': { |
| 79 | + 'whitelist': [] |
| 80 | + }, |
| 81 | + 'detect': function (ast, parser) { |
| 82 | + var result = [], |
| 83 | + selectorList = [], |
| 84 | + parentSelector = [], |
| 85 | + |
| 86 | + // we use this array to keep track of the number of nested blocks and their levels |
| 87 | + // seen as we will always start with a single block at level 0 we just the first |
| 88 | + // level count to 1 |
| 89 | + childBlocks = [1], |
| 90 | + level = 0; |
| 91 | + |
| 92 | + ast.traverseByType('ruleset', function (ruleset) { |
| 93 | + var selectorBlock = { |
| 94 | + selector: parentSelector.join(' '), |
| 95 | + line: '' |
| 96 | + }, |
| 97 | + curSelector = '', |
| 98 | + delimOrder = []; |
| 99 | + |
| 100 | + ruleset.forEach('selector', function (selector) { |
| 101 | + // Keep track of the order of elements & delimeters |
| 102 | + selector.forEach( function (el) { |
| 103 | + var curNode = helpers.mapDelims(el); |
| 104 | + |
| 105 | + if (curNode !== false) { |
| 106 | + delimOrder.push(curNode); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + selector.forEach('simpleSelector', function (simpleSelector) { |
| 111 | + // check if the next selector is proceeded by a delimiter |
| 112 | + // if it is add it to the curSelector output |
| 113 | + var nextType = delimOrder[0]; |
| 114 | + |
| 115 | + if (nextType === 'd') { |
| 116 | + curSelector += ', '; |
| 117 | + // remove the next delim and selector from our order as we are only looping over selectors |
| 118 | + delimOrder.splice(0, 2); |
| 119 | + |
| 120 | + } |
| 121 | + else { |
| 122 | + // if no delim then just remove the current selectors marker in the order array |
| 123 | + delimOrder.splice(0, 1); |
| 124 | + } |
| 125 | + |
| 126 | + simpleSelector.forEach(function (val) { |
| 127 | + // construct our selector from its content parts |
| 128 | + curSelector += constructSelector(val); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + // Gonzales keeps the spaces after the selector, we remove them here to keep the selectors recognisable |
| 133 | + // and consisten in the result output. |
| 134 | + curSelector = helpers.stripLastSpace(curSelector); |
| 135 | + |
| 136 | + // check to see if we are on a level other than default (0) |
| 137 | + if (level) { |
| 138 | + // remove 1 from the block count on the current level |
| 139 | + childBlocks[level] -= 1; |
| 140 | + } |
| 141 | + |
| 142 | + // check to see if the current ruleset contains any child rulesets |
| 143 | + if (ruleset.first('block').contains('ruleset')) { |
| 144 | + ruleset.first('block').forEach('ruleset', function () { |
| 145 | + |
| 146 | + // Keep a record of the number of rulesets on the next level of nesting |
| 147 | + if (childBlocks[level + 1]) { |
| 148 | + childBlocks[level + 1] += 1; |
| 149 | + } |
| 150 | + else { |
| 151 | + childBlocks[level + 1] = 1; |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + // as we have another level we add a space to the end of the current selector reference |
| 156 | + if (parentSelector[level]) { |
| 157 | + parentSelector[level] += curSelector + ' '; |
| 158 | + } |
| 159 | + else { |
| 160 | + parentSelector[level] = curSelector + ' '; |
| 161 | + } |
| 162 | + |
| 163 | + // increase our level counter before we start parsing the child rulesets |
| 164 | + level = level + 1; |
| 165 | + } |
| 166 | + // if no child rulesets/blocks then we need to find the level we will next be working on |
| 167 | + else { |
| 168 | + |
| 169 | + // we scan backwards through our block counter array until we find a number greater than 0 |
| 170 | + // We've been decrementing our block counts on the correct level as we've parsed them so a |
| 171 | + // level with a positive count means there are still rulesets here to parse. |
| 172 | + for (var i = childBlocks.length - 1; i >= 0; i-- ) { |
| 173 | + if (childBlocks[i] === 0) { |
| 174 | + |
| 175 | + // remove the last element from the parent selector array as our level decreases effectively allowing |
| 176 | + // us to correctly concat our parent and child selectors |
| 177 | + parentSelector.splice(parentSelector.length - 1, 1); |
| 178 | + |
| 179 | + // if we're not on level 0 then we want to decrement the level as there are no child rules of this block left to parse. |
| 180 | + // We then remove the 0 count element from the end of the childblocks array to make it ready for use again. |
| 181 | + if (level) { |
| 182 | + level = level - 1; |
| 183 | + childBlocks.splice(childBlocks.length - 1, 1); |
| 184 | + } |
| 185 | + } |
| 186 | + else { |
| 187 | + |
| 188 | + // if the current level has rulesets to parse we break out of the for loop |
| 189 | + break; |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // set the line of the current selector blocks start for our error messages |
| 195 | + // keep a reference with this for our current selector |
| 196 | + selectorBlock.line = ruleset.start.line; |
| 197 | + selectorBlock.selector += curSelector; |
| 198 | + |
| 199 | + }); |
| 200 | + |
| 201 | + // we check to see if our selector has already been encountered, if it has we generate a lint warning/error |
| 202 | + // detailing which selector is failing and on which line. |
| 203 | + var present = helpers.propertySearch(selectorList, selectorBlock.selector, 'selector'); |
| 204 | + |
| 205 | + if (present !== -1) { |
| 206 | + result = helpers.addUnique(result, { |
| 207 | + 'ruleId': parser.rule.name, |
| 208 | + 'line': ruleset.start.line, |
| 209 | + 'column': ruleset.start.column, |
| 210 | + 'message': 'Rule `' + curSelector + '` should be merged with the rule on line ' + selectorList[present].line, |
| 211 | + 'severity': parser.severity |
| 212 | + }); |
| 213 | + } |
| 214 | + else { |
| 215 | + |
| 216 | + // if the current selector is whitelisted we don't add it to the selector list to be checked against. |
| 217 | + if (parser.options.whitelist.indexOf(selectorBlock.selector) === -1) { |
| 218 | + |
| 219 | + // push the selector to our master list/array of selectors currently parsed without error. |
| 220 | + selectorList.push(selectorBlock); |
| 221 | + } |
| 222 | + } |
| 223 | + }); |
| 224 | + return result; |
| 225 | + } |
| 226 | +}; |
0 commit comments