A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/styleguidist/react-styleguidist/commit/c223f9a279f4e872cf6ff925fb21d2a7f6e27a77 below:

prevent unnecessary wrapping of single line jsx elements (#2183) · styleguidist/react-styleguidist@c223f9a · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+26

-14

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+26

-14

lines changed Original file line number Diff line number Diff line change

@@ -51,18 +51,16 @@ React.createElement( Button, null )"

51 51

`);

52 52

});

53 53 54 -

test('wrap JSX in Fragment', () => {

55 -

const result = compileCode(

56 -

`<div>

57 -

<button>Click</button>

58 -

</div>`,

59 -

compilerConfig

54 +

test('wrap JSX in Fragment if adjacent on line 1', () => {

55 +

const result = compileCode(`<span /><span />`, compilerConfig);

56 +

expect(result).toMatchInlineSnapshot(

57 +

`"React.createElement( React.Fragment, null, React.createElement( 'span', null ), React.createElement( 'span', null ) );"`

60 58

);

61 -

expect(result).toMatchInlineSnapshot(`

62 -

"React.createElement( React.Fragment, null, React.createElement( 'div', null,

63 -

React.createElement( 'button', null, \\"Click\\" )

64 -

) );"

65 -

`);

59 +

});

60 + 61 +

test('don’t wrap JSX in Fragment if there is only one statement', () => {

62 +

const result = compileCode(`<Button />;`, compilerConfig);

63 +

expect(result).toMatchInlineSnapshot(`"React.createElement( Button, null );"`);

66 64

});

67 65 68 66

test('don’t wrap JSX in Fragment if it’s in the middle', () => {

Original file line number Diff line number Diff line change

@@ -18,9 +18,23 @@ export default function compileCode(

18 18

onError?: (err: Error) => void

19 19

): string {

20 20

try {

21 -

const wrappedCode = startsWithJsx(code) ? wrapCodeInFragment(code) : code;

22 -

const compiledCode = compile(wrappedCode, compilerConfig);

23 -

return transpileImports(compiledCode);

21 +

let compiledCode;

22 + 23 +

try {

24 +

compiledCode = compile(code, compilerConfig);

25 +

} catch (err) {

26 +

if (

27 +

err instanceof SyntaxError &&

28 +

err.message.startsWith('Adjacent JSX elements must be wrapped in an enclosing tag')

29 +

) {

30 +

const wrappedCode = startsWithJsx(code) ? wrapCodeInFragment(code) : code;

31 +

compiledCode = compile(wrappedCode, compilerConfig);

32 +

} else if (onError && err instanceof Error) {

33 +

onError(err);

34 +

}

35 +

}

36 + 37 +

return compiledCode ? transpileImports(compiledCode) : '';

24 38

} catch (err) {

25 39

if (onError && err instanceof Error) {

26 40

onError(err);

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4