From f11d423ae43f2eb57f603bd13cef7f58f0216a07 Mon Sep 17 00:00:00 2001 From: George Pittarelli Date: Fri, 29 Mar 2019 18:31:27 -0400 Subject: [PATCH] fix: don't break when spreading falsy value (#249) --- src/createSpreadMapper.js | 43 +++++++++++-------- .../output.js | 2 +- .../handle spread attributes/output.js | 14 +++--- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/createSpreadMapper.js b/src/createSpreadMapper.js index 81d3cb6..176fb0a 100644 --- a/src/createSpreadMapper.js +++ b/src/createSpreadMapper.js @@ -4,6 +4,7 @@ import { Expression, memberExpression, binaryExpression, + conditionalExpression, stringLiteral, logicalExpression, identifier, @@ -43,25 +44,33 @@ const createSpreadMapper = (path: *, stats: *): { [destinationName: string]: Exp result[destinationName] = binaryExpression( '+', result[destinationName], - binaryExpression( - '+', - stringLiteral(' '), - logicalExpression( - '||', - memberExpression( - spread.argument, - identifier(destinationName), - ), - stringLiteral('') - ) - ), + conditionalExpression( + spread.argument, + binaryExpression( + '+', + stringLiteral(' '), + logicalExpression( + '||', + memberExpression( + spread.argument, + identifier(destinationName), + ), + stringLiteral('') + ) + ), + stringLiteral('') + ) ); } else { - result[destinationName] = logicalExpression( - '||', - memberExpression( - spread.argument, - identifier(destinationName), + result[destinationName] = conditionalExpression( + spread.argument, + logicalExpression( + '||', + memberExpression( + spread.argument, + identifier(destinationName), + ), + stringLiteral('') ), stringLiteral('') ); diff --git a/test/fixtures/react-css-modules/does not throw error if attribute has no name property/output.js b/test/fixtures/react-css-modules/does not throw error if attribute has no name property/output.js index c2084e1..a325415 100644 --- a/test/fixtures/react-css-modules/does not throw error if attribute has no name property/output.js +++ b/test/fixtures/react-css-modules/does not throw error if attribute has no name property/output.js @@ -5,4 +5,4 @@ require("./bar.css"); const props = { foo: 'bar' }; -
; +
; diff --git a/test/fixtures/react-css-modules/handle spread attributes/output.js b/test/fixtures/react-css-modules/handle spread attributes/output.js index 59ec5c2..421a3f8 100644 --- a/test/fixtures/react-css-modules/handle spread attributes/output.js +++ b/test/fixtures/react-css-modules/handle spread attributes/output.js @@ -3,17 +3,17 @@ require("./foo.css"); const rest = {}; -
; -
; -
; -
; // Should be okay if rest is put on last +
; +
; +
; +
; // Should be okay if rest is put on last -
; +
; const rest2 = {}; -
; // Should not do anything +
; // Should not do anything
;
;
-
+
;