Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 551cce5

Browse files
feature(formatting): Harmonize the function formatting
BREAKING CHANGE: If you use the `showFunctions: true` option, the function are now always inlined in the output by default. Before it was not always the case (depending one the engine, platform or babel versions) You could get back to the previous behavior by using the `preserveFunctionLineBreak` function export as a value of the option `functionValue`.
1 parent b96153f commit 551cce5

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

‎src/formatter/formatFunction.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import type { Options } from './../options';
22

33
function noRefCheck() {}
44

5-
const defaultFunctionValue = (fn: any): any => fn.toString();
5+
export const inlineFunction = (fn: any): string =>
6+
fn
7+
.toString()
8+
.split('\n')
9+
.map(line => line.trim())
10+
.join('');
11+
12+
export const preserveFunctionLineBreak = (fn: any): string => fn.toString();
13+
14+
const defaultFunctionValue = inlineFunction;
615

716
export default (fn: Function, options: Options): string => {
817
const { functionValue = defaultFunctionValue, showFunctions } = options;

‎src/formatter/formatFunction.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ describe('formatFunction', () => {
2222
});
2323

2424
it('should format a function if showFunctions is true', () => {
25-
expect(formatFunction(hello, { showFunctions: true }))
26-
.toEqual(`function hello() {
27-
return 1;
28-
}`);
25+
expect(formatFunction(hello, { showFunctions: true })).toEqual(
26+
'function hello() {return 1;}'
27+
);
2928
});
3029

3130
it('should format a function without name if showFunctions is true', () => {

‎src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ const reactElementToJsxString = (
4141
};
4242

4343
export default reactElementToJsxString;
44+
45+
export {
46+
inlineFunction,
47+
preserveFunctionLineBreak,
48+
} from './formatter/formatFunction';

‎src/index.spec.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import React, { Fragment, Component } from 'react';
66
import { createRenderer } from 'react-test-renderer/shallow';
77
import { mount } from 'enzyme';
8-
import reactElementToJSXString from './index';
8+
import reactElementToJSXString,{preserveFunctionLineBreak} from './index';
99
import AnonymousStatelessComponent from './AnonymousStatelessComponent';
1010

1111
class TestComponent extends React.Component {}
@@ -889,13 +889,25 @@ describe('reactElementToJSXString(ReactElement)', () => {
889889
reactElementToJSXString(<div fn={fn} />, {
890890
showFunctions: true,
891891
})
892-
).toEqual(
893-
`<div
892+
).toEqual(`<div fn={function fn() {return 'value';}} />`);
893+
});
894+
895+
it('should expose the multiline "functionValue" formatter', () => {
896+
/* eslint-disable arrow-body-style */
897+
const fn = () => {
898+
return 'value';
899+
};
900+
901+
expect(
902+
reactElementToJSXString(<div fn={fn} />, {
903+
showFunctions: true,
904+
functionValue: preserveFunctionLineBreak,
905+
})
906+
).toEqual(`<div
894907
fn={function fn() {
895908
return 'value';
896909
}}
897-
/>`
898-
);
910+
/>`);
899911
});
900912

901913
it('reactElementToJSXString(<DisplayNamePrecedence />)', () => {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /