Skip to content

Commit 2c7d75d

Browse files
committed
Merge pull request #7 from AoDev/upgrade
refactor: upgrade to postCSS 5, standard code style
2 parents cfbb4df + dad3ebf commit 2c7d75d

File tree

6 files changed

+194
-48
lines changed

6 files changed

+194
-48
lines changed

.eslintrc

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{
2+
"ecmaFeatures": {
3+
"modules": true,
4+
"experimentalObjectRestSpread": true
5+
},
6+
7+
"env": {
8+
"browser": true,
9+
"es6": true,
10+
"node": true
11+
},
12+
13+
"plugins": [
14+
"standard"
15+
],
16+
17+
"globals": {
18+
"document": false,
19+
"navigator": false,
20+
"window": false,
21+
"expect": false,
22+
"sinon": false,
23+
"afterEach": false,
24+
"describe": false,
25+
"beforeEach": false,
26+
"inject": false,
27+
"it": false
28+
},
29+
30+
"rules": {
31+
"accessor-pairs": 2,
32+
"arrow-spacing": [2, { "before": true, "after": true }],
33+
"block-spacing": [2, "always"],
34+
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
35+
"comma-dangle": [2, "never"],
36+
"comma-spacing": [2, { "before": false, "after": true }],
37+
"comma-style": [2, "last"],
38+
"constructor-super": 2,
39+
"curly": [2, "multi-line"],
40+
"dot-location": [2, "property"],
41+
"eol-last": 2,
42+
"eqeqeq": [2, "allow-null"],
43+
"generator-star-spacing": [2, { "before": true, "after": true }],
44+
"handle-callback-err": [2, "^(err|error)$" ],
45+
"indent": [2, 2, { "SwitchCase": 1 }],
46+
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
47+
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
48+
"new-parens": 2,
49+
"no-array-constructor": 2,
50+
"no-caller": 2,
51+
"no-class-assign": 2,
52+
"no-cond-assign": 2,
53+
"no-const-assign": 2,
54+
"no-control-regex": 2,
55+
"no-debugger": 2,
56+
"no-delete-var": 2,
57+
"no-dupe-args": 2,
58+
"no-dupe-class-members": 2,
59+
"no-dupe-keys": 2,
60+
"no-duplicate-case": 2,
61+
"no-empty-character-class": 2,
62+
"no-empty-label": 2,
63+
"no-eval": 2,
64+
"no-ex-assign": 2,
65+
"no-extend-native": 2,
66+
"no-extra-bind": 2,
67+
"no-extra-boolean-cast": 2,
68+
"no-extra-parens": [2, "functions"],
69+
"no-fallthrough": 2,
70+
"no-floating-decimal": 2,
71+
"no-func-assign": 2,
72+
"no-implied-eval": 2,
73+
"no-inner-declarations": [2, "functions"],
74+
"no-invalid-regexp": 2,
75+
"no-irregular-whitespace": 2,
76+
"no-iterator": 2,
77+
"no-label-var": 2,
78+
"no-labels": 2,
79+
"no-lone-blocks": 2,
80+
"no-mixed-spaces-and-tabs": 2,
81+
"no-multi-spaces": 2,
82+
"no-multi-str": 2,
83+
"no-multiple-empty-lines": [2, { "max": 1 }],
84+
"no-native-reassign": 2,
85+
"no-negated-in-lhs": 2,
86+
"no-new": 2,
87+
"no-new-func": 2,
88+
"no-new-object": 2,
89+
"no-new-require": 2,
90+
"no-new-wrappers": 2,
91+
"no-obj-calls": 2,
92+
"no-octal": 2,
93+
"no-octal-escape": 2,
94+
"no-proto": 2,
95+
"no-redeclare": 2,
96+
"no-regex-spaces": 2,
97+
"no-return-assign": 2,
98+
"no-self-compare": 2,
99+
"no-sequences": 2,
100+
"no-shadow-restricted-names": 2,
101+
"no-spaced-func": 2,
102+
"no-sparse-arrays": 2,
103+
"no-this-before-super": 2,
104+
"no-throw-literal": 2,
105+
"no-trailing-spaces": 2,
106+
"no-undef": 2,
107+
"no-undef-init": 2,
108+
"no-unexpected-multiline": 2,
109+
"no-unneeded-ternary": 2,
110+
"no-unreachable": 2,
111+
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
112+
"no-useless-call": 2,
113+
"no-with": 2,
114+
"one-var": [2, { "initialized": "never" }],
115+
"operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],
116+
"padded-blocks": [2, "never"],
117+
"quotes": [2, "single", "avoid-escape"],
118+
"radix": 2,
119+
"semi": [2, "never"],
120+
"space-after-keywords": [2, "always"],
121+
"space-before-blocks": [2, "always"],
122+
"space-before-function-paren": [2, "always"],
123+
"space-in-parens": [2, "never"],
124+
"space-infix-ops": 2,
125+
"space-return-throw-case": 2,
126+
"space-unary-ops": [2, { "words": true, "nonwords": false }],
127+
"spaced-comment": [2, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
128+
"use-isnan": 2,
129+
"valid-typeof": 2,
130+
"wrap-iife": [2, "any"],
131+
"yoda": [2, "never"],
132+
133+
"standard/object-curly-even-spacing": [2, "either"],
134+
"standard/array-bracket-even-spacing": [2, "either"],
135+
"standard/computed-property-even-spacing": [2, "even"]
136+
}
137+
}

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = function (grunt) {
3232
mochaTest: {
3333
all: {
3434
options: { reporter: 'spec' },
35-
src: ['test/spec.js']
35+
src: ['./**/*.spec.js']
3636
}
3737
}
3838
})

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
CSS Byebye
22
===========
33

4-
[![Join the chat at https://gitter.im/AoDev/css-byebye](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/AoDev/css-byebye?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5-
64
CSS Byebye is a node module that lets you explicitly remove the CSS rules that you don't want.
75

6+
---------
7+
8+
[![Join the chat at https://gitter.im/AoDev/css-byebye](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/AoDev/css-byebye?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
9+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
10+
[![Build Status](https://travis-ci.org/AoDev/css-byebye.svg)](https://travis-ci.org/AoDev/css-byebye)
811

912
Description
1013
------------
1114

12-
It's very simple: pass him a list of selectors that you want to exclude and it will remove them and the associated rules from your CSS.
15+
It's very simple: pass a list of selectors that you want to exclude and it will remove them and the associated rules from your CSS.
1316

1417
I've found some cases where this approach is easier than using more powerful tools like uncss.
1518
Use what's best for you and give some feedback :)
1619

1720
CSS Byebye is built with [postcss](https://github.com/postcss/postcss).
1821

19-
A grunt task for CSS Byebye exists: [grunt-css-byeby](https://github.com/AoDev/grunt-css-byebye).
22+
A grunt task for CSS Byebye exists: [grunt-css-byebye](https://github.com/AoDev/grunt-css-byebye).
2023

2124

2225
Usage
@@ -44,41 +47,61 @@ If you provide a string, it will remove the rule(s) for this exact selector.
4447

4548
### Examples ###
4649

50+
Some CSS:
51+
52+
```css
53+
a { font-size: 12px; }
54+
.hello .h1 { background: red }
55+
.world { color: blue }
56+
```
57+
58+
Using the plugin:
59+
4760
```js
4861
var postcss = require('postcss')
4962
var cssbyebye = require('css-byebye')
5063

51-
var css = 'a { font-size: 12px; } .hello .h1 { background: red } .world { color: blue }'
5264
var rulesToRemove = ['.hello .h1', '.world']
5365
var options = { rulesToRemove: rulesToRemove, map: false }
66+
67+
// pretend that css var contains the css above
5468
var result = postcss(cssbyebye(options)).process(css)
5569
```
5670

5771
`result` will be an object like this:
5872

59-
```css
73+
```js
6074
{
6175
css: 'a { font-size: 12px; }'
6276
}
6377
```
6478

65-
If you use the postcss map option, then it will be added to the result object.
79+
If you use the postcss `map` option, then source map will be added to the result object.
6680

6781

6882
#### You can mix strings and regular expressions
6983

7084
```js
71-
var rulesToRemove = ['.hello', /.*\.world.*/]
85+
var rulesToRemove = [
86+
'.hello',
87+
/.*\.world.*/
88+
]
7289
```
7390

74-
In this case, it would remove a rule with the exact selector `.hello` and any rule
75-
that contains the `.world` class.
91+
In this case, it would:
92+
* remove a rule with the exact selector `.hello`
93+
* remove any rule that contains the `.world` class.
7694

7795

7896

7997
Changelog
8098
----------
8199

100+
### 2015-09-25 v1.0.1
101+
* Upgrade to postCSS 5.x
102+
* Docs improved.
103+
* The project uses js standard code style.
104+
82105
### 2015-06-09 v1.0.0
83106
* **Breaking changes** and bumped to 1.0.0
84107
* Update to last postCSS version

lib/css-byebye.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,82 @@
22
* @module css-byebye
33
*/
44

5-
65
var postcss = require('postcss')
76

8-
97
/**
108
* Escape a string so that it can be turned into a regex
119
* @param {String} str String to transform
1210
* @return {String} Escaped string
1311
*/
14-
function escapeRegExp(str) {
15-
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
12+
function escapeRegExp (str) {
13+
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')
1614
}
1715

18-
1916
/**
2017
* Turn strings from rules to remove into a regexp to concat them later
2118
* @param {Mixed Array} rulesToRemove
2219
* @return {RegExp Array}
2320
*/
24-
function regexize(rulesToRemove) {
21+
function regexize (rulesToRemove) {
2522
var rulesRegexes = []
2623
for (var i = 0, l = rulesToRemove.length; i < l; i++) {
27-
if (typeof rulesToRemove[i] == 'string') {
28-
rulesRegexes.push(new RegExp('^\s*' + escapeRegExp(rulesToRemove[i])+ '\s*$'))
29-
}
30-
else {
24+
if (typeof rulesToRemove[i] === 'string') {
25+
rulesRegexes.push(new RegExp('^\s*' + escapeRegExp(rulesToRemove[i]) + '\s*$'))
26+
} else {
3127
rulesRegexes.push(rulesToRemove[i])
3228
}
3329
}
3430
return rulesRegexes
3531
}
3632

37-
3833
/**
3934
* Concat various regular expressions into one
4035
* @param {RegExp Array} regexes
4136
* @return {RegExp} concatanated regexp
4237
*/
43-
function concatRegexes(regexes) {
44-
38+
function concatRegexes (regexes) {
4539
var rconcat = ''
4640

4741
if (Array.isArray(regexes)) {
48-
4942
for (var i = 0, l = regexes.length; i < l; i++) {
5043
rconcat += regexes[i].source + '|'
5144
}
5245

53-
rconcat = rconcat.substr(0, rconcat.length -1)
46+
rconcat = rconcat.substr(0, rconcat.length - 1)
5447

5548
return new RegExp(rconcat)
5649
}
5750
}
5851

59-
6052
/**
6153
* Return the actual postcss plugin to remove rules from the css
6254
*/
6355
var cssbyebye = postcss.plugin('css-byebye', function (options) {
64-
65-
return function byebye(css) {
56+
return function byebye (css) {
6657
var remregexes = regexize(options.rulesToRemove)
6758
var regex = concatRegexes(remregexes)
68-
var count = 0
6959

70-
var filterRule = function filterRule(rule) {
60+
css.walkRules(filterRule)
7161

62+
function filterRule (rule) {
7263
var selectors = rule.selectors
7364
var filtered = []
7465

7566
for (var j = 0, len = selectors.length; j < len; j++) {
76-
if (selectors[j].match(regex) === null)
67+
if (selectors[j].match(regex) === null) {
7768
filtered.push(selectors[j])
69+
}
7870
}
7971

80-
if (filtered.length > 1)
72+
if (filtered.length > 1) {
8173
rule.selector = filtered.join(', ')
82-
else if (filtered.length == 1)
74+
} else if (filtered.length === 1) {
8375
rule.selector = filtered[0].trim()
84-
else {
85-
rule.removeSelf()
76+
} else {
77+
rule.remove()
8678
}
8779
}
88-
89-
css.eachRule(filterRule)
9080
}
9181
})
9282

93-
9483
module.exports = cssbyebye

test/spec.js renamed to lib/css-byebye.spec.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ var cssbyebye = require('../lib/css-byebye')
44
var postcss = require('postcss')
55

66
describe('cssbyebye', function () {
7-
8-
it('should remove ruleset(s) with a selector that contains any of the given selectors to remove ', function (done) {
9-
7+
it('should remove ruleset(s) that matches any given selectors to remove ', function (done) {
108
var css = 'a { font-size: 12px; } .hello .h1 { background: red } .world { color: blue }'
119
var rulesToRemove = ['.hello .h1', '.world']
1210
var expected = 'a { font-size: 12px; }'
@@ -18,7 +16,6 @@ describe('cssbyebye', function () {
1816
})
1917

2018
it('should remove only the matching selector(s) from a group of selectors', function (done) {
21-
2219
var css = '.hello .world, .title, #id { color: red }'
2320
var rulesToRemove = ['.hello .world']
2421
var expected = '.title, #id { color: red }'
@@ -30,16 +27,13 @@ describe('cssbyebye', function () {
3027
})
3128

3229
it('should support regex matching', function (done) {
33-
3430
var css = '.item {} .item .desc { background: red } .list .item {}'
3531
var rulesToRemove = [/^\.item/]
36-
var expected = '.list .item {}'
32+
var expected = '.list .item {}'
3733
var options = { rulesToRemove: rulesToRemove, map: false }
3834
var result = postcss(cssbyebye(options)).process(css)
3935

4036
assert.strictEqual(result.css, expected)
4137
done()
4238
})
4339
})
44-
45-

0 commit comments

Comments
 (0)