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

Commit

Permalink
Don't change casing of at-rule properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlangelaan committed Sep 15, 2020
1 parent bade2c2 commit 321b135
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
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

0 comments on commit 321b135

Please sign in to comment.