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

Fix the behavior of computed property name in style objects #37

Closed
Closed
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
10 changes: 10 additions & 0 deletions object-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ class objectParser {
}

rule = atRule;
} else if (node.computed) {
// for computed property name

rule = postcss.rule({
selector: key.value,
});
// recalculate trivial spaces
const [prefix, postfix] = rawKey.split(key.value);

defineRaws(rule, 'selector', prefix, postfix);
} else {
// rule = this.rule(key, keyWrap, node.value, parent);
rule = postcss.rule({
Expand Down
4 changes: 2 additions & 2 deletions test/emotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('javascript tests', () => {
code = code.toString();

expect(document.toString()).to.equal(code);
expect(document.nodes).to.lengthOf(4);
expect(document.nodes).to.lengthOf(5);

document.nodes.forEach((root) => {
expect(root.last.toString()).to.be.a('string');
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('javascript tests', () => {
code = code.toString();

expect(document.toString()).to.equal(code);
expect(document.nodes).to.lengthOf(6);
expect(document.nodes).to.lengthOf(7);

document.nodes.forEach((root) => {
expect(root.last.toString()).to.be.a('string');
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/emotion-10.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ const SomeComponent = styled.div`
background-color: ${props => props.color};
`;

const YetAnotherComponent = styled.p(
{
backgroundColor: "black",
}
);

const AnotherComponent = styled.h1(
{
color: "hotpink",
[YetAnotherComponent]: {
color: "white",
},
},
props => ({ flex: props.flex })
);
Expand All @@ -28,6 +37,7 @@ render(
}}>
Some other text.
</span>
<YetAnotherComponent>Yet another text.</YetAnotherComponent>
</AnotherComponent>
</SomeComponent>
);
Expand Down
180 changes: 160 additions & 20 deletions test/fixtures/emotion-10.jsx.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,81 @@
"syntax": {}
},
"type": "root",
"nodes": [
{
"raws": {
"after": "\n\t",
"semicolon": true,
"before": "\t"
},
"type": "object",
"nodes": [
{
"raws": {
"prop": {
"prefix": "",
"suffix": "",
"raw": "backgroundColor",
"value": "background-color"
},
"value": {
"prefix": "\"",
"suffix": "\"",
"raw": "black",
"value": "black"
},
"between": ": ",
"before": "\n\t\t"
},
"prop": "background-color",
"value": "black",
"type": "decl",
"source": {
"input": {
"file": "emotion-10.jsx"
},
"start": {
"line": 13,
"column": 2
},
"end": {
"line": 13,
"column": 26
}
}
}
],
"source": {
"input": {
"file": "emotion-10.jsx"
},
"start": {
"line": 12,
"column": 1
},
"end": {
"line": 14,
"column": 2
}
}
}
]
},
{
"raws": {},
"source": {
"input": {
"file": "emotion-10.jsx"
},
"start": {
"line": 18,
"column": 1
},
"inline": false,
"lang": "object-literal",
"syntax": {}
},
"type": "root",
"nodes": [
{
"raws": {
Expand Down Expand Up @@ -146,26 +221,91 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 13,
"line": 19,
"column": 2
},
"end": {
"line": 13,
"line": 19,
"column": 18
}
}
},
{
"raws": {
"selector": {
"prefix": "[",
"suffix": "]",
"raw": "YetAnotherComponent",
"value": "YetAnotherComponent"
},
"between": ": ",
"after": "\n\t\t",
"semicolon": true,
"before": "\n\t\t"
},
"selector": "YetAnotherComponent",
"type": "rule",
"nodes": [
{
"raws": {
"prop": {
"prefix": "",
"suffix": "",
"raw": "color",
"value": "color"
},
"value": {
"prefix": "\"",
"suffix": "\"",
"raw": "white",
"value": "white"
},
"between": ": ",
"before": "\n\t\t\t"
},
"prop": "color",
"value": "white",
"type": "decl",
"source": {
"input": {
"file": "emotion-10.jsx"
},
"start": {
"line": 21,
"column": 3
},
"end": {
"line": 21,
"column": 17
}
}
}
],
"source": {
"input": {
"file": "emotion-10.jsx"
},
"start": {
"line": 20,
"column": 2
},
"end": {
"line": 22,
"column": 3
}
}
}
],
"source": {
"input": {
"file": "emotion-10.jsx"
},
"start": {
"line": 12,
"line": 18,
"column": 1
},
"end": {
"line": 14,
"line": 23,
"column": 2
}
}
Expand All @@ -179,7 +319,7 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 15,
"line": 24,
"column": 11
},
"inline": false,
Expand Down Expand Up @@ -221,11 +361,11 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 15,
"line": 24,
"column": 13
},
"end": {
"line": 15,
"line": 24,
"column": 29
}
}
Expand All @@ -236,11 +376,11 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 15,
"line": 24,
"column": 11
},
"end": {
"line": 15,
"line": 24,
"column": 31
}
}
Expand All @@ -262,14 +402,14 @@
"type": "decl",
"source": {
"start": {
"line": 22,
"line": 31,
"column": 5
},
"input": {
"file": "emotion-10.jsx"
},
"end": {
"line": 22,
"line": 31,
"column": 22
}
},
Expand All @@ -282,7 +422,7 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 21,
"line": 30,
"column": 19
},
"inline": false,
Expand All @@ -297,7 +437,7 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 26,
"line": 35,
"column": 14
},
"inline": false,
Expand Down Expand Up @@ -339,11 +479,11 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 27,
"line": 36,
"column": 4
},
"end": {
"line": 27,
"line": 36,
"column": 23
}
}
Expand All @@ -354,11 +494,11 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 26,
"line": 35,
"column": 14
},
"end": {
"line": 28,
"line": 37,
"column": 4
}
}
Expand All @@ -380,14 +520,14 @@
"type": "decl",
"source": {
"start": {
"line": 36,
"line": 46,
"column": 2
},
"input": {
"file": "emotion-10.jsx"
},
"end": {
"line": 36,
"line": 46,
"column": 22
}
},
Expand All @@ -400,7 +540,7 @@
"file": "emotion-10.jsx"
},
"start": {
"line": 35,
"line": 45,
"column": 21
},
"inline": false,
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/react-emotion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ const SomeComponent = styled("div")`
background-color: ${props => props.color};
`;

const YetAnotherComponent = styled.p(
{
backgroundColor: "black",
},
);

const AnotherComponent = styled("h1")(
{
color: "hotpink",
[YetAnotherComponent]: {
color: "white",
},
},
props => ({ flex: props.flex })
);
Expand All @@ -18,6 +27,7 @@ render(
<AnotherComponent flex={1}>
Some text.
</AnotherComponent>
<YetAnotherComponent>Yet another text.</YetAnotherComponent>
</SomeComponent>
);
const app = document.getElementById("root");
Expand Down
Loading