From e95a7c02fe065e391971d75ac31250d219810685 Mon Sep 17 00:00:00 2001 From: Armand Abric Date: Wed, 18 May 2022 12:00:08 +0200 Subject: [PATCH] feature: Show false props when not egal to default value --- package.json | 9 ++------- src/formatter/formatProp.js | 10 +--------- src/formatter/formatProp.spec.js | 5 +++-- src/index.spec.js | 20 ++++++++++++++++++-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index ab3db5b4f..50389c144 100644 --- a/package.json +++ b/package.json @@ -24,10 +24,7 @@ "smoke": "node tests/smoke/run" }, "lint-staged": { - "*.js": [ - "prettier --write \"**/*.{js,json}\"", - "git add" - ] + "*.js": ["prettier --write \"**/*.{js,json}\"", "git add"] }, "author": { "name": "Algolia, Inc.", @@ -87,8 +84,6 @@ "react-is": "18.1.0" }, "jest": { - "setupFilesAfterEnv": [ - "tests/setupTests.js" - ] + "setupFilesAfterEnv": ["tests/setupTests.js"] } } diff --git a/src/formatter/formatProp.js b/src/formatter/formatProp.js index 9fcac3c01..9c2949c58 100644 --- a/src/formatter/formatProp.js +++ b/src/formatter/formatProp.js @@ -34,15 +34,7 @@ export default ( let attributeFormattedMultiline = `\n${spacer(lvl + 1, tabStop)}`; const isMultilineAttribute = formattedPropValue.includes('\n'); - if ( - useBooleanShorthandSyntax && - formattedPropValue === '{false}' && - !hasDefaultValue - ) { - // If a boolean is false and not different from it's default, we do not render the attribute - attributeFormattedInline = ''; - attributeFormattedMultiline = ''; - } else if (useBooleanShorthandSyntax && formattedPropValue === '{true}') { + if (useBooleanShorthandSyntax && formattedPropValue === '{true}') { attributeFormattedInline += `${name}`; attributeFormattedMultiline += `${name}`; } else { diff --git a/src/formatter/formatProp.spec.js b/src/formatter/formatProp.spec.js index 700c8fa9b..75ad0d8fb 100644 --- a/src/formatter/formatProp.spec.js +++ b/src/formatter/formatProp.spec.js @@ -107,8 +107,9 @@ describe('formatProp', () => { expect( formatProp('foo', true, false, false, null, true, 0, options) ).toEqual({ - attributeFormattedInline: '', - attributeFormattedMultiline: '', + attributeFormattedInline: ' foo={false}', + attributeFormattedMultiline: ` + foo={false}`, isMultilineAttribute: false, }); diff --git a/src/index.spec.js b/src/index.spec.js index e7ec29b08..b1a1f6771 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -825,6 +825,22 @@ describe('reactElementToJSXString(ReactElement)', () => { ); }); + it('reactElementToJSXString(, { useBooleanShorthandSyntax: true })', () => { + expect( + reactElementToJSXString( + , + { + useBooleanShorthandSyntax: true, + } + ) + ).toEqual( + `` + ); + }); + it('should render default props', () => { expect(reactElementToJSXString()).toEqual( ` { ).toEqual('
'); }); - it('should omit attributes with false as value', () => { + it('should not omit attributes with false as value', () => { expect( reactElementToJSXString(
) // eslint-disable-line react/jsx-boolean-value - ).toEqual('
'); + ).toEqual('
'); }); it('should return the actual functions when "showFunctions" is true', () => {