Skip to content
This repository was archived by the owner on Feb 9, 2023. It is now read-only.

At rule param case #41

Merged
merged 1 commit into from
Sep 15, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions object-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ function forEach(arr, callback) {
arr && arr.forEach(callback);
}

const replaceProp = (fn) => (value) =>
value.replace(/(\(\s*)(.*?)(\s*:)/g, (s, prefix, prop, suffix) => prefix + fn(prop) + suffix);
const camelCaseProp = replaceProp(camelCase);
const unCamelCaseProp = replaceProp(unCamelCase);

function defineRaws(node, prop, prefix, suffix, props) {
if (!props) {
props = {};
Expand Down Expand Up @@ -239,16 +234,8 @@ class objectParser {
});

if (params) {
atRule.params = unCamelCaseProp(params);
defineRaws(atRule, 'params', '', key.suffix, {
raw: {
enumerable: true,
get: () => camelCaseProp(atRule.params),
set: (value) => {
atRule.params = unCamelCaseProp(value);
},
},
});
atRule.params = params;
defineRaws(atRule, 'params', '', key.suffix);
}

rule = atRule;
Expand Down
24 changes: 22 additions & 2 deletions test/css-in-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ describe('CSS in JS', () => {
});
});

it('leaves kebab-case and camelCase in media query params untouched', () => {
// In previous versions, at-rule properties were converted from camelCase to kebab-case
// during parsing, and back to camelCase during stringifying. This is however not correct,
// params should not be changed. Also see:
// https://github.com/stylelint/postcss-css-in-js/issues/38
const code = `
import glm from 'glamorous';
const Component1 = glm.a({
"@media (max-width: 1000px)": {
color: "red",
},
"@media (maxWidth: 1000px)": {
color: "red",
},
});
`;

expect(syntax.parse(code).toString()).toBe(code);
});

describe('setter for object literals', () => {
it('decl.raws.prop.raw & decl.raws.value.raw', () => {
const decl = syntax.parse(
Expand All @@ -74,7 +94,7 @@ describe('CSS in JS', () => {
`
import glm from 'glamorous';
const Component1 = glm.a({
'@media (maxWidth: 500px)': {
'@media (max-width: 500px)': {
borderRadius: '5px'
}
});
Expand All @@ -84,7 +104,7 @@ describe('CSS in JS', () => {
},
).first.first.first;

atRule.raws.params.raw = "(minWidth: ' + minWidth + ')";
atRule.raws.params.raw = "(min-width: ' + minWidth + ')";
expect(atRule.params).toBe("(min-width: ' + minWidth + ')");
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/glamorous.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Component1 = glm.a(
[`unknownPropertyaa${a}`]: "1.8em", // must not trigger any warnings
["unknownProperty" + 1 + "a"]: "1.8em", // must not trigger any warnings
display: "inline-block",
[`@media (minWidth: ${minWidth}px)`]: {
[`@media (min-width: ${minWidth}px)`]: {
color: "red",
},
// unkown pseudo class selector
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/glamorous.jsx.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
"params": {
"prefix": "",
"suffix": "`]",
"raw": "(minWidth: ${minWidth}px)",
"raw": "(min-width: ${minWidth}px)",
"value": "(min-width: ${minWidth}px)"
},
"between": ": ",
Expand Down