Skip to content

Commit 0f5ca93

Browse files
BPScottgucong3000
andcommitted
Handle object shorthand (stylelint#67)
* Ensure expectations run * Handle object shorthand Objects that use the shorthand notation should be stringified as such. This stops `<div style={{color}} />` being converted to `<div style={{color:color}} />` Co-authored-by: 刘祺 <gucong@gmail.com>
1 parent e318894 commit 0f5ca93

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

object-stringifier.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ class ObjectStringifier extends Stringifier {
2525
if (prop === "float") {
2626
prop = "cssFloat";
2727
}
28-
const between = this.raw(node, "between", "colon");
29-
const value = this.rawValue(node, "value");
30-
let string = prop + between + value;
28+
let string = prop;
29+
30+
const isObjectShorthand = node.raws.node && node.raws.node.shorthand;
31+
if (!isObjectShorthand) {
32+
const between = this.raw(node, "between", "colon");
33+
const value = this.rawValue(node, "value");
34+
string += between + value;
35+
}
3136

3237
if (semicolon) string += ",";
3338
this.builder(string, node);

test/fixtures/jsx.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ const App = props => (
4141
/>
4242
);
4343

44+
function ObjectShorthandComponent({color}) {
45+
return <div style={{color}}/>
46+
}
47+
4448
export default {
4549
HelloWorldComponent,
4650
React,
47-
App
51+
App,
52+
ObjectShorthandComponent
4853
};

test/fixtures/jsx.jsx.json

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,81 @@
568568
}
569569
}
570570
]
571+
},
572+
{
573+
"raws": {},
574+
"source": {
575+
"input": {
576+
"file": "jsx.jsx"
577+
},
578+
"start": {
579+
"line": 45,
580+
"column": 20
581+
},
582+
"inline": false,
583+
"lang": "object-literal",
584+
"syntax": {}
585+
},
586+
"type": "root",
587+
"nodes": [
588+
{
589+
"raws": {
590+
"after": "",
591+
"semicolon": false,
592+
"before": ""
593+
},
594+
"type": "object",
595+
"nodes": [
596+
{
597+
"raws": {
598+
"prop": {
599+
"prefix": "",
600+
"suffix": "",
601+
"raw": "color",
602+
"value": "color"
603+
},
604+
"value": {
605+
"prefix": "",
606+
"suffix": "",
607+
"raw": "color",
608+
"value": "color"
609+
},
610+
"between": "",
611+
"before": ""
612+
},
613+
"prop": "color",
614+
"value": "color",
615+
"type": "decl",
616+
"source": {
617+
"input": {
618+
"file": "jsx.jsx"
619+
},
620+
"start": {
621+
"line": 45,
622+
"column": 21
623+
},
624+
"end": {
625+
"line": 45,
626+
"column": 26
627+
}
628+
}
629+
}
630+
],
631+
"source": {
632+
"input": {
633+
"file": "jsx.jsx"
634+
},
635+
"start": {
636+
"line": 45,
637+
"column": 20
638+
},
639+
"end": {
640+
"line": 45,
641+
"column": 27
642+
}
643+
}
644+
}
645+
]
571646
}
572647
],
573648
"source": {

test/non-style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("not throw error for non-style js file", () => {
1313
from: file,
1414
});
1515
expect(document.source).to.haveOwnProperty("lang", "jsx");
16-
expect(document.toString(), code.toString());
16+
expect(document.toString()).to.equal(code.toString());
1717
});
1818
});
1919
});

test/supports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe("should support for each CSS in JS package", () => {
5151
from: file,
5252
});
5353
expect(document.source).to.haveOwnProperty("lang", "jsx");
54-
expect(document.toString(), code.toString());
54+
expect(document.toString()).to.equal(code.toString());
5555
expect(document.nodes.length).to.greaterThan(0);
5656
const parsed = JSON.stringify(clean(document), 0, "\t");
5757
// fs.writeFileSync(file + ".json", parsed + "\n");

0 commit comments

Comments
 (0)