Skip to content

Commit ba92913

Browse files
authored
ensure that media params remain string values (#558)
* failing test * ensure that media params remain string values
1 parent d8a9f21 commit ba92913

File tree

8 files changed

+57
-22
lines changed

8 files changed

+57
-22
lines changed

lib/parse-statements.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@ const valueParser = require("postcss-value-parser")
66
// extended tooling
77
const { stringify } = valueParser
88

9-
function split(params, start) {
10-
const list = []
11-
const last = params.reduce((item, node, index) => {
12-
if (index < start) return ""
13-
if (node.type === "div" && node.value === ",") {
14-
list.push(item)
15-
return ""
16-
}
17-
return item + stringify(node)
18-
}, "")
19-
list.push(last)
20-
return list
21-
}
22-
239
module.exports = function parseStatements(result, styles, conditions, from) {
2410
const statements = []
2511
let nodes = []
@@ -223,7 +209,7 @@ function parseImport(result, atRule, conditions, from) {
223209
continue
224210
}
225211

226-
media = split(params, i)
212+
media = stringify(params.slice(i))
227213
break
228214
}
229215

test/fixtures/filter-all.expected.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
@import url("foobar.css");
88
@import url("foobar.css") screen and (min-width: 25em);
99
@import url('foobarbaz.css');
10-
@import url('foobarbaz.css') print,screen and (min-width: 25em);
10+
@import url('foobarbaz.css') print, screen and (min-width: 25em);
1111
content{}

test/fixtures/filter-some.expected.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ baz{}
1414
baz{}
1515
}
1616
foobarbaz{}
17-
@media print,screen and (min-width: 25em){
17+
@media print, screen and (min-width: 25em){
1818
foobarbaz{}
1919
}
2020
content{}

test/fixtures/ignore.expected.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
@import url(//css);
1616
@import "http://css" layer;
1717
@import "http://css" layer(bar);
18-
@import "http://css" layer screen and (min-width: 25em),print;
19-
@import "http://css" layer(bar) screen and (min-width: 25em),print;
18+
@import "http://css" layer screen and (min-width: 25em), print;
19+
@import "http://css" layer(bar) screen and (min-width: 25em), print;
2020
@media (min-width: 25em){
2121
ignore{}
2222
}

test/fixtures/media-combine.expected.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@media and-left-2 and and-right-2 {}
88
}
99

10-
@media or-left-1,or-right-1 {
10+
@media or-left-1, or-right-1 {
1111

1212
@media one-2 {}
1313

test/fixtures/simple.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
@import url("foobar.css");
88
@import url("foobar.css") screen and (min-width: 25em);
99
@import url('foobarbaz.css');
10-
@import url('foobarbaz.css') print, screen and (min-width: 25em);
10+
@import url('foobarbaz.css') print,screen and (min-width: 25em);
1111

1212
content{}

test/helpers/ast-checker.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict"
2+
3+
const astCheckerPlugin = () => {
4+
return {
5+
postcssPlugin: "ast-checker-plugin",
6+
OnceExit(root) {
7+
root.walkAtRules(node => {
8+
if (typeof node.params !== "string") {
9+
throw node.error(
10+
`Params must be of type 'string', found '${typeof node.params}' instead`,
11+
)
12+
}
13+
14+
if (typeof node.type !== "string") {
15+
throw node.error(
16+
`Type must be of type 'string', found '${typeof node.type}' instead`,
17+
)
18+
}
19+
20+
if (node.type !== "atrule") {
21+
throw node.error(
22+
`Type must be 'atrule', found '${node.type}' instead`,
23+
)
24+
}
25+
26+
if (typeof node.name !== "string") {
27+
throw node.error(
28+
`Name must be of type 'string', found '${typeof node.name}' instead`,
29+
)
30+
}
31+
32+
if (node.nodes && !Array.isArray(node.nodes)) {
33+
throw node.error(
34+
`Nodes must be of type 'Array' when it is present, found '${typeof node.nodes}' instead`,
35+
)
36+
}
37+
38+
if (!("parent" in node)) {
39+
throw node.error("AtRule must have a 'parent' property")
40+
}
41+
})
42+
},
43+
}
44+
}
45+
46+
astCheckerPlugin.postcss = true
47+
48+
module.exports = astCheckerPlugin

test/helpers/check-fixture.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const postcss = require("postcss")
88

99
// plugin
1010
const atImport = require("../..")
11+
const astCheckerPlugin = require("./ast-checker")
1112

1213
function read(name, ext) {
1314
ext = ext || ".css"
@@ -20,7 +21,7 @@ module.exports = function (t, file, opts, postcssOpts, warnings) {
2021
if (typeof file === "string") file = { name: file, ext: ".css" }
2122
const { name, ext } = file
2223

23-
return postcss(atImport(opts))
24+
return postcss([atImport(opts), astCheckerPlugin()])
2425
.process(read(name, ext), postcssOpts || {})
2526
.then(result => {
2627
const actual = result.css

0 commit comments

Comments
 (0)