list-selectors
Advanced tools
Comparing version 0.2.0 to 0.3.0
# Changelog | ||
## v0.3.0 | ||
- Major refactor to use `postcss-selector-parser`. | ||
## v0.2.0 | ||
@@ -4,0 +7,0 @@ - Major refactor, including upgrade to PostCSS 4.1. |
'use strict'; | ||
var postcss = require('postcss'); | ||
var selectorParser = require('postcss-selector-parser'); | ||
var _ = require('lodash'); | ||
var selectorSort = require('./selectorSort'); | ||
var selectorProcessors = require('./selectorProcessors'); | ||
var processIncludes = require('./processIncludes'); | ||
@@ -25,3 +25,3 @@ | ||
return function(cssTree, postcssResult) { | ||
return function(cssRoot, postcssResult) { | ||
var selectorObj = {}; | ||
@@ -32,5 +32,5 @@ | ||
var accumulatedSelectors = []; | ||
cssTree.eachRule(function(rule) { | ||
cssRoot.eachRule(function(rule) { | ||
// Ignore keyframes, which can log e.g. 10%, 20% as selectors | ||
if (rule.parent.type === 'atrule' && /keyframes/.test(rule.parent.name)) { return; } | ||
if (rule.parent.type === 'atrule' && /keyframes/.test(rule.parent.name)) return; | ||
@@ -54,18 +54,55 @@ rule.selectors.forEach(function(selector) { | ||
// Add sorted, unique selectors to results | ||
selectorObj.selectors = _.sortBy(_.uniq(accumulatedSelectors), selectorSort); | ||
var sortedUniqueSelectors = _.chain(accumulatedSelectors) | ||
.uniq() | ||
.sortBy(selectorSort) | ||
.value(); | ||
// Add sorted, unique simple selectors to results | ||
selectorObj.simpleSelectors = {}; | ||
selectorObj.simpleSelectors.all = _.sortBy( | ||
_.uniq(selectorProcessors.reduceToSimpleSelectors(selectorObj.selectors)), | ||
selectorSort | ||
); | ||
selectorObj.selectors = sortedUniqueSelectors; | ||
// Add all of the category lists (of simple selectors) to results | ||
selectorObj.simpleSelectors.ids = selectorProcessors.getIds(selectorObj.simpleSelectors.all); | ||
selectorObj.simpleSelectors.classes = selectorProcessors.getClasses(selectorObj.simpleSelectors.all); | ||
selectorObj.simpleSelectors.attributes = selectorProcessors.getAttributes(selectorObj.simpleSelectors.all); | ||
selectorObj.simpleSelectors.types = selectorProcessors.getTypes(selectorObj.simpleSelectors.all); | ||
selectorObj.simpleSelectors = { | ||
all: [], | ||
ids: [], | ||
classes: [], | ||
attributes: [], | ||
types: [] | ||
}; | ||
// Refine the results according to any `include` options passed | ||
// Add sorted, unique *simple* selectors to results | ||
sortedUniqueSelectors.forEach(function(selector) { | ||
selectorParser(function(selectors) { | ||
selectors.eachInside(function(node) { | ||
// Various nodes the selector-parser loops through should be ignored | ||
if (_.includes(['selector', 'comment', 'combinator', 'pseudo'], node.type)) return; | ||
// Arguments of `:nth-*` pseudo-classes should be ignored, as they | ||
// are integers, an+b expressions, or "odd" and "even" | ||
if (node.parent.parent.value && node.parent.parent.value.substr(0, 5) === ':nth-') return; | ||
selectorObj.simpleSelectors.all.push(node.toString()); | ||
switch (node.type) { | ||
case 'id': | ||
selectorObj.simpleSelectors.ids.push(node.toString()); | ||
break; | ||
case 'class': | ||
selectorObj.simpleSelectors.classes.push(node.toString()); | ||
break; | ||
case 'attribute': | ||
selectorObj.simpleSelectors.attributes.push(node.toString()); | ||
break; | ||
case 'tag': | ||
selectorObj.simpleSelectors.types.push(node.toString()); | ||
break; | ||
default: | ||
} | ||
}); | ||
}).process(selector); | ||
}); | ||
_.forOwn(selectorObj.simpleSelectors, function(selectorList, key) { | ||
selectorObj.simpleSelectors[key] = _.chain(selectorList) | ||
.flatten() | ||
.uniq() | ||
.sortBy(selectorSort) | ||
.value(); | ||
}); | ||
// // Refine the results according to any `include` options passed | ||
selectorObj = processIncludes(selectorObj, opts.include, postcssResult); | ||
@@ -72,0 +109,0 @@ |
@@ -27,3 +27,3 @@ 'use strict'; | ||
module.exports = function(selectorList, includes, postcssResult) { | ||
if (!includes) return selectorList; | ||
if (_.isEmpty(includes)) return selectorList; | ||
@@ -34,4 +34,4 @@ if (_.isString(includes)) includes = [includes]; | ||
if (_.contains(TOP_LEVEL_INCLUDES, incl)) { | ||
if (_.contains(['simple', 'simpleSelectors'], incl)) { | ||
if (_.includes(TOP_LEVEL_INCLUDES, incl)) { | ||
if (_.includes(['simple', 'simpleSelectors'], incl)) { | ||
r.simpleSelectors = selectorList.simpleSelectors.all; | ||
@@ -44,3 +44,3 @@ } else { | ||
if (_.contains(SIMPLE_INCLUDES, incl)) { | ||
if (_.includes(SIMPLE_INCLUDES, incl)) { | ||
r[incl] = selectorList.simpleSelectors[incl]; | ||
@@ -50,3 +50,4 @@ return r; | ||
postcssResult.warn('Invalid include "' + incl + '" passed. ' + | ||
postcssResult.warn( | ||
'Invalid include "' + incl + '" passed. ' + | ||
'The possibilities are: ' + | ||
@@ -53,0 +54,0 @@ TOP_LEVEL_INCLUDES.concat(SIMPLE_INCLUDES).join(', ') + '. ' + |
@@ -9,3 +9,3 @@ 'use strict'; | ||
var globby = require('globby'); | ||
var logWarnings = require('postcss-log-warnings'); | ||
var reporter = require('postcss-reporter'); | ||
var plugin = require('./plugin'); | ||
@@ -57,3 +57,3 @@ | ||
.use(plugin(opts, cb)) | ||
.use(logWarnings({ plugins: ['list-selectors'] })) | ||
.use(reporter({ plugins: ['list-selectors'] })) | ||
.process(body) | ||
@@ -83,3 +83,3 @@ .then(_.noop, function(errB) { | ||
.use(plugin(opts, cb)) | ||
.use(logWarnings({ plugins: ['list-selectors'] })) | ||
.use(reporter({ plugins: ['list-selectors'] })) | ||
.process(fullCss) | ||
@@ -86,0 +86,0 @@ .then(_.noop, function(errB) { |
@@ -12,3 +12,3 @@ { | ||
"license": "MIT", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "List the selectors used in your CSS. Use as a standalone function, CLI, or PostCSS plugin.", | ||
@@ -40,3 +40,3 @@ "homepage": "https://github.com/davidtheclark/list-selectors", | ||
"devDependencies": { | ||
"eslint": "0.18.0", | ||
"eslint": "0.24.1", | ||
"tape": "4.0.0" | ||
@@ -50,5 +50,6 @@ }, | ||
"postcss": "^4.1.0", | ||
"postcss-log-warnings": "^0.1.2", | ||
"postcss-reporter": "^0.2.0", | ||
"postcss-selector-parser": "^1.1.2", | ||
"request": "^2.0" | ||
} | ||
} |
@@ -13,3 +13,3 @@ # list-selectors [![Build Status](https://travis-ci.org/davidtheclark/list-selectors.svg?branch=master)](https://travis-ci.org/davidtheclark/list-selectors) | ||
*The latest version should be used as a PostCSS plugin only with PostCSS v4.1+.* | ||
*v0.2.0+ should be used as a PostCSS plugin only with PostCSS v4.1.0+.* | ||
@@ -242,5 +242,1 @@ ## Installation | ||
- `'types'`: Only types. | ||
## Caveats | ||
- Anything within parentheses will be ignored. One situation in which you might use a selector in parentheses is with `:not`, e.g. `.foo:not(.bar)` — and in that case, `.bar` will not be included in the list (unless it's used elsewhere). |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
19503
8
10
253
241
+ Addedpostcss-reporter@^0.2.0
+ Addedflatten@1.0.3(transitive)
+ Addedindexes-of@1.0.1(transitive)
+ Addedlodash._basedifference@3.0.3(transitive)
+ Addedlodash._baseflatten@3.1.4(transitive)
+ Addedlodash._baseindexof@3.1.0(transitive)
+ Addedlodash._cacheindexof@3.0.2(transitive)
+ Addedlodash._createcache@3.1.2(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash.difference@3.2.2(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.restparam@3.6.1(transitive)
+ Addedlog-symbols@1.0.2(transitive)
+ Addedpostcss-reporter@0.2.0(transitive)
+ Addedpostcss-selector-parser@1.3.3(transitive)
+ Addeduniq@1.0.1(transitive)
- Removedpostcss-log-warnings@^0.1.2
- Removedpostcss-log-warnings@0.1.3(transitive)