Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit ac9a18c

Browse files
committed
Fixed: No more useless warnings for undefined non custom selectors
Also: - Changed: simpler message when a custom selector is undefined - Changed: warnings now use PostCSS message API - Some fixes in the Changelog + simpler formatting - Added some references in the Changelog Prepared as 2.2.0 Close #22
1 parent 721c576 commit ac9a18c

File tree

6 files changed

+72
-41
lines changed

6 files changed

+72
-41
lines changed

CHANGELOG.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1-
# 2.1.0 - 2015-06-30
1+
# 2.2.0 - 2015-06-30
22

3-
* \- Fixed: the lineBreak option keeping the selectors indent [#18](https://github.com/postcss/postcss-custom-selectors/issues/18).
4-
* \- Fixed: the tip of an undefined selector [#20](https://github.com/postcss/postcss-custom-selectors/issues/20).
3+
* Fixed: No more useless warnings for undefined non custom selectors
4+
([#22](https://github.com/postcss/postcss-custom-selectors/issues/22))
5+
* Changed: warnings now use PostCSS message API
6+
7+
# 2.1.1 - 2015-06-30
8+
9+
* Fixed: the lineBreak option keeping the selectors indent
10+
([#18](https://github.com/postcss/postcss-custom-selectors/issues/18))
11+
* Fixed: the tip of an undefined selector
12+
([#20](https://github.com/postcss/postcss-custom-selectors/issues/20))
513

614
# 2.1.0 - 2015-06-04
715

8-
* \- Fixed: use PostCSS 4.1 plugin API.
16+
* Changed: use PostCSS 4.1 plugin API
17+
([#13](https://github.com/postcss/postcss-custom-selectors/issues/13))
918

1019
# 2.0.1 - 2015-06-03
1120

12-
* \- Fixed: `(foo, bar)` conversion error exists in the selector(See also [:matches() test](test/fixtures/matches/input.css)).
21+
* Fixed: `(foo, bar)` conversion error exists in the selector
22+
(See also [:matches() test](test/fixtures/matches/input.css))
1323

1424
# 2.0.0 - 2015-05-29
1525

16-
* \x Remove: no longer support `::` or `--` to defined a custom selectors, we must use the `:--` to defined it.
26+
* Removed: no longer support `::` or `--` to defined a custom selectors,
27+
you must use the syntax `:--` to define it.
28+
([#6](https://github.com/postcss/postcss-custom-selectors/issues/6))
29+
* Fixed: two or more consecutive hyphens in selector don't output `undefined`
30+
([#14](https://github.com/postcss/postcss-custom-selectors/issues/14))
1731

18-
* \- Fixed: two or more consecutive hyphens in selector outputs is "undefined".
1932

2033
# 1.1.1 - 2015-04-06
2134

22-
* \- Fixed: add support for multilines definition
35+
* Fixed: add support for multilines definition
2336

2437
# 1.1.0 - 2014-12-06
2538

26-
* \- Added: "lineBreak" option
39+
* Added: "lineBreak" option
2740

2841
# 1.0.0 - 2014-12-06
2942

index.js

+29-27
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = postcss.plugin("postcss-custom-selectors", function(options) {
2626
/**
2727
* 读取和替换自定义选择器
2828
*/
29-
return function(css) {
29+
return function(css, result) {
3030
// 读取自定义选择器
3131
css.eachAtRule(function(rule) {
3232
if (rule.name !== "custom-selector") {
@@ -55,39 +55,41 @@ module.exports = postcss.plugin("postcss-custom-selectors", function(options) {
5555

5656
// 转换自定义的选择器别名
5757
css.eachRule(function(rule) {
58-
var flag = 0
59-
for (var prop in customSelectors) {
60-
if (rule.selector.indexOf(prop) >= 0) {
61-
customSelector = customSelectors[prop]
58+
if (rule.selector.indexOf(":--") > -1) {
59+
var flag = 0
60+
for (var prop in customSelectors) {
61+
if (rule.selector.indexOf(prop) >= 0) {
62+
customSelector = customSelectors[prop]
6263

63-
// $2 = <extension-name> (自定义的选择器名称)
64-
rule.selector = rule.selector.replace(re_CUSTOM_SELECTOR, function($0, $1, $2, $3) {
64+
// $2 = <extension-name> (自定义的选择器名称)
65+
rule.selector = rule.selector.replace(re_CUSTOM_SELECTOR, function($0, $1, $2, $3) {
6566

66-
if ($2 === prop) {
67-
var newSelector = customSelector.split(",").map(function(selector) {
68-
return $1 + selector.trim() + $3
69-
})
67+
if ($2 === prop) {
68+
var newSelector = customSelector.split(",").map(function(selector) {
69+
return $1 + selector.trim() + $3
70+
})
7071

71-
// 选择器不换行
72-
if (!options.lineBreak && options.lineBreak === false) {
73-
line_break = " "
74-
newSelector = newSelector.join("," + line_break)
75-
} else {
76-
// 选择器换行,同时替换多个换行为一个
77-
newSelector = newSelector.join("," + line_break + rule.before).replace(reBLANK_LINE, line_break)
72+
// 选择器不换行
73+
if (!options.lineBreak && options.lineBreak === false) {
74+
line_break = " "
75+
newSelector = newSelector.join("," + line_break)
76+
} else {
77+
// 选择器换行,同时替换多个换行为一个
78+
newSelector = newSelector.join("," + line_break + rule.before).replace(reBLANK_LINE, line_break)
79+
}
80+
flag ++
81+
return newSelector
7882
}
79-
flag ++
80-
return newSelector
81-
}
82-
else if ($2 !== prop) {
83-
return $2
83+
else if ($2 !== prop) {
84+
return $2
85+
}
86+
})
87+
if(flag === 0){
88+
result.warn("The selector '" + rule.selector + "' is undefined", {node: rule})
8489
}
85-
})
90+
}
8691
}
8792
}
88-
if(flag === 0){
89-
console.log("Warning: The selector '" + rule.selector + "' is undefined in CSS!")
90-
}
9193
})
9294

9395
// 删除 @custom-selector

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-custom-selectors",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"description": "PostCSS plugin to transform W3C CSS Extensions(Custom Selectors) to more compatible CSS",
55
"keywords": [
66
"css",

test/fixtures/similar-matches/input.css

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@
1212
:--test p {
1313
display: block;
1414
}
15+
16+
whatever {
17+
display: block;
18+
}

test/fixtures/similar-matches/output.css

+4
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ h4 h5 h6 {
99
:--test p {
1010
display: block;
1111
}
12+
13+
whatever {
14+
display: block;
15+
}

test/index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ function compareFixtures(t, name, msg, opts, postcssOpts) {
1313
//input
1414
postcssOpts.from = filename("fixtures/" + name + "/input")
1515
opts = opts || {}
16-
var actual = postcss()
16+
var result = postcss()
1717
.use(plugin(opts))
1818
.process(read(postcssOpts.from), postcssOpts)
19-
.css
19+
20+
var actual = result.css
2021
//output
2122
var output = read(filename("fixtures/" + name + "/output"))
2223
//actual
2324
fs.writeFile(filename("fixtures/" + name + "/actual"), actual)
2425
t.equal(actual.trim(), output.trim(), msg)
26+
27+
return result
2528
}
2629

2730
test("@custom-selector", function(t) {
@@ -30,10 +33,15 @@ test("@custom-selector", function(t) {
3033
compareFixtures(t, "multiline", "should transform multiline")
3134
compareFixtures(t, "some-hyphen", "should transform some hyphen")
3235
compareFixtures(t, "matches", "should transform matches selector")
33-
compareFixtures(t, "similar-matches", "should transform matches selector")
36+
var similarMatchesResult = compareFixtures(t, "similar-matches", "should transform matches selector")
37+
t.ok(
38+
similarMatchesResult.messages && similarMatchesResult.messages.length === 1,
39+
"should add a message when a custom selectors is undefined"
40+
)
41+
3442
compareFixtures(t, "comment", "should transform comment")
3543
compareFixtures(t, "line-break", "should transform line break", {
36-
lineBreak: false
44+
lineBreak: false
3745
})
3846

3947
compareFixtures(t, "extension", "should transform local extensions", {

0 commit comments

Comments
 (0)