Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure that media params remain string values #558

Merged
merged 2 commits into from Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 1 addition & 15 deletions lib/parse-statements.js
Expand Up @@ -6,20 +6,6 @@ const valueParser = require("postcss-value-parser")
// extended tooling
const { stringify } = valueParser

function split(params, start) {
const list = []
const last = params.reduce((item, node, index) => {
if (index < start) return ""
if (node.type === "div" && node.value === ",") {
list.push(item)
return ""
}
return item + stringify(node)
}, "")
list.push(last)
return list
}
Comment on lines -9 to -21
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I overlooked that this was still present and JavaScript automatically joins arrays of strings with ,.

This entire section has become redundant with the refactors for v16.


module.exports = function parseStatements(result, styles, conditions, from) {
const statements = []
let nodes = []
Expand Down Expand Up @@ -223,7 +209,7 @@ function parseImport(result, atRule, conditions, from) {
continue
}

media = split(params, i)
media = stringify(params.slice(i))
break
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/filter-all.expected.css
Expand Up @@ -7,5 +7,5 @@
@import url("foobar.css");
@import url("foobar.css") screen and (min-width: 25em);
@import url('foobarbaz.css');
@import url('foobarbaz.css') print,screen and (min-width: 25em);
@import url('foobarbaz.css') print, screen and (min-width: 25em);
content{}
2 changes: 1 addition & 1 deletion test/fixtures/filter-some.expected.css
Expand Up @@ -14,7 +14,7 @@ baz{}
baz{}
}
foobarbaz{}
@media print,screen and (min-width: 25em){
@media print, screen and (min-width: 25em){
foobarbaz{}
}
content{}
4 changes: 2 additions & 2 deletions test/fixtures/ignore.expected.css
Expand Up @@ -15,8 +15,8 @@
@import url(//css);
@import "http://css" layer;
@import "http://css" layer(bar);
@import "http://css" layer screen and (min-width: 25em),print;
@import "http://css" layer(bar) screen and (min-width: 25em),print;
@import "http://css" layer screen and (min-width: 25em), print;
@import "http://css" layer(bar) screen and (min-width: 25em), print;
@media (min-width: 25em){
ignore{}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/media-combine.expected.css
Expand Up @@ -7,7 +7,7 @@
@media and-left-2 and and-right-2 {}
}

@media or-left-1,or-right-1 {
@media or-left-1, or-right-1 {

@media one-2 {}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/simple.css
Expand Up @@ -7,6 +7,6 @@
@import url("foobar.css");
@import url("foobar.css") screen and (min-width: 25em);
@import url('foobarbaz.css');
@import url('foobarbaz.css') print, screen and (min-width: 25em);
@import url('foobarbaz.css') print,screen and (min-width: 25em);

content{}
48 changes: 48 additions & 0 deletions test/helpers/ast-checker.js
@@ -0,0 +1,48 @@
"use strict"

const astCheckerPlugin = () => {
return {
postcssPlugin: "ast-checker-plugin",
OnceExit(root) {
root.walkAtRules(node => {
if (typeof node.params !== "string") {
throw node.error(
`Params must be of type 'string', found '${typeof node.params}' instead`,
)
}

if (typeof node.type !== "string") {
throw node.error(
`Type must be of type 'string', found '${typeof node.type}' instead`,
)
}

if (node.type !== "atrule") {
throw node.error(
`Type must be 'atrule', found '${node.type}' instead`,
)
}

if (typeof node.name !== "string") {
throw node.error(
`Name must be of type 'string', found '${typeof node.name}' instead`,
)
}

if (node.nodes && !Array.isArray(node.nodes)) {
throw node.error(
`Nodes must be of type 'Array' when it is present, found '${typeof node.nodes}' instead`,
)
}

if (!("parent" in node)) {
throw node.error("AtRule must have a 'parent' property")
}
})
},
}
}

astCheckerPlugin.postcss = true

module.exports = astCheckerPlugin
3 changes: 2 additions & 1 deletion test/helpers/check-fixture.js
Expand Up @@ -8,6 +8,7 @@ const postcss = require("postcss")

// plugin
const atImport = require("../..")
const astCheckerPlugin = require("./ast-checker")

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

return postcss(atImport(opts))
return postcss([atImport(opts), astCheckerPlugin()])
.process(read(name, ext), postcssOpts || {})
.then(result => {
const actual = result.css
Expand Down