From 62527d0260e2988927a256d655be3ff8e563209c Mon Sep 17 00:00:00 2001 From: Ben Scott Date: Wed, 25 Dec 2019 17:42:27 -0800 Subject: [PATCH 1/2] Ensure expectations run --- test/non-style.js | 2 +- test/supports.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/non-style.js b/test/non-style.js index 57473fd..e0d9a4e 100644 --- a/test/non-style.js +++ b/test/non-style.js @@ -13,7 +13,7 @@ describe("not throw error for non-style js file", () => { from: file, }); expect(document.source).to.haveOwnProperty("lang", "jsx"); - expect(document.toString(), code.toString()); + expect(document.toString()).to.equal(code.toString()); }); }); }); diff --git a/test/supports.js b/test/supports.js index b0a4198..d63c14d 100644 --- a/test/supports.js +++ b/test/supports.js @@ -50,7 +50,7 @@ describe("should support for each CSS in JS package", () => { from: file, }); expect(document.source).to.haveOwnProperty("lang", "jsx"); - expect(document.toString(), code.toString()); + expect(document.toString()).to.equal(code.toString()); expect(document.nodes.length).to.greaterThan(0); const parsed = JSON.stringify(clean(document), 0, "\t"); // fs.writeFileSync(file + ".json", parsed + "\n"); From 603d25c1f413bdb20fc44ecf8e4a5e5134151d56 Mon Sep 17 00:00:00 2001 From: Ben Scott Date: Wed, 25 Dec 2019 17:42:56 -0800 Subject: [PATCH 2/2] Handle object shorthand Objects that use the shorthand notation should be stringified as such. This stops `
` being converted to `
` --- object-stringifier.js | 11 ++++-- test/fixtures/jsx.jsx | 7 +++- test/fixtures/jsx.jsx.json | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/object-stringifier.js b/object-stringifier.js index c2ac967..99f7c72 100644 --- a/object-stringifier.js +++ b/object-stringifier.js @@ -25,9 +25,14 @@ class ObjectStringifier extends Stringifier { if (prop === "float") { prop = "cssFloat"; } - const between = this.raw(node, "between", "colon"); - const value = this.rawValue(node, "value"); - let string = prop + between + value; + let string = prop; + + const isObjectShorthand = node.raws.node && node.raws.node.shorthand; + if (!isObjectShorthand) { + const between = this.raw(node, "between", "colon"); + const value = this.rawValue(node, "value"); + string += between + value; + } if (semicolon) string += ","; this.builder(string, node); diff --git a/test/fixtures/jsx.jsx b/test/fixtures/jsx.jsx index af84fc4..a3a9aba 100644 --- a/test/fixtures/jsx.jsx +++ b/test/fixtures/jsx.jsx @@ -41,8 +41,13 @@ const App = props => ( /> ); +function ObjectShorthandComponent({color}) { + return
+} + export default { HelloWorldComponent, React, - App + App, + ObjectShorthandComponent }; diff --git a/test/fixtures/jsx.jsx.json b/test/fixtures/jsx.jsx.json index 3120a5b..405b7ee 100644 --- a/test/fixtures/jsx.jsx.json +++ b/test/fixtures/jsx.jsx.json @@ -568,6 +568,81 @@ } } ] + }, + { + "raws": {}, + "source": { + "input": { + "file": "jsx.jsx" + }, + "start": { + "line": 45, + "column": 20 + }, + "inline": false, + "lang": "object-literal", + "syntax": {} + }, + "type": "root", + "nodes": [ + { + "raws": { + "after": "", + "semicolon": false, + "before": "" + }, + "type": "object", + "nodes": [ + { + "raws": { + "prop": { + "prefix": "", + "suffix": "", + "raw": "color", + "value": "color" + }, + "value": { + "prefix": "", + "suffix": "", + "raw": "color", + "value": "color" + }, + "between": "", + "before": "" + }, + "prop": "color", + "value": "color", + "type": "decl", + "source": { + "input": { + "file": "jsx.jsx" + }, + "start": { + "line": 45, + "column": 21 + }, + "end": { + "line": 45, + "column": 26 + } + } + } + ], + "source": { + "input": { + "file": "jsx.jsx" + }, + "start": { + "line": 45, + "column": 20 + }, + "end": { + "line": 45, + "column": 27 + } + } + } + ] } ], "source": {