Skip to content

Commit afe55e4

Browse files
author
Gabriela Seabra
committed
feat(formatting) add ident & inline options to formatFunction
1 parent 78b45d4 commit afe55e4

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

src/formatter/formatComplexDataStructure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default (
3131
}
3232

3333
if (typeof currentValue === 'function') {
34-
return formatFunction(currentValue, options);
34+
return formatFunction(currentValue, true, lvl, options);
3535
}
3636

3737
return originalResult;

src/formatter/formatFunction.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Options } from './../options';
2+
import spacer from './spacer';
23

34
function noRefCheck() {}
45

@@ -11,13 +12,20 @@ export const inlineFunction = (fn: any): string =>
1112

1213
export const preserveFunctionLineBreak = (fn: any): string => fn.toString();
1314

14-
const defaultFunctionValue = inlineFunction;
15+
export default (
16+
fn: Function,
17+
inline: boolean,
18+
lvl: number,
19+
options: Options
20+
): string => {
21+
const { functionValue, showFunctions } = options;
22+
const functionFn =
23+
functionValue || (inline ? inlineFunction : preserveFunctionLineBreak);
24+
const shouldShowFunction = showFunctions || functionValue;
1525

16-
export default (fn: Function, options: Options): string => {
17-
const { functionValue = defaultFunctionValue, showFunctions } = options;
18-
if (!showFunctions && functionValue === defaultFunctionValue) {
19-
return functionValue(noRefCheck);
20-
}
21-
22-
return functionValue(fn);
26+
return String(functionFn(shouldShowFunction ? fn : noRefCheck))
27+
.split('\n')
28+
.map(ln => spacer(lvl, options.tabStop) + ln)
29+
.join('\n')
30+
.trim();
2331
};

src/formatter/formatFunction.spec.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,38 @@ function hello() {
1212

1313
describe('formatFunction', () => {
1414
it('should replace a function with noRefCheck without showFunctions option', () => {
15-
expect(formatFunction(hello, {})).toEqual('function noRefCheck() {}');
15+
expect(formatFunction(hello, true, 0, {})).toEqual(
16+
'function noRefCheck() {}'
17+
);
1618
});
1719

1820
it('should replace a function with noRefCheck if showFunctions is false', () => {
19-
expect(formatFunction(hello, { showFunctions: false })).toEqual(
21+
expect(formatFunction(hello, true, 0, { showFunctions: false })).toEqual(
2022
'function noRefCheck() {}'
2123
);
2224
});
2325

2426
it('should format a function if showFunctions is true', () => {
25-
expect(formatFunction(hello, { showFunctions: true })).toEqual(
27+
expect(formatFunction(hello, true, 0, { showFunctions: true })).toEqual(
2628
'function hello() {return 1;}'
2729
);
2830
});
2931

3032
it('should format a function without name if showFunctions is true', () => {
31-
expect(formatFunction(() => 1, { showFunctions: true })).toEqual(
33+
expect(formatFunction(() => 1, true, 0, { showFunctions: true })).toEqual(
3234
'function () {return 1;}'
3335
);
3436
});
3537

3638
it('should use the functionValue option', () => {
37-
expect(formatFunction(hello, { functionValue: () => '<Test />' })).toEqual(
38-
'<Test />'
39-
);
39+
expect(
40+
formatFunction(hello, true, 0, { functionValue: () => '<Test />' })
41+
).toEqual('<Test />');
4042
});
4143

4244
it('should use the functionValue option even if showFunctions is true', () => {
4345
expect(
44-
formatFunction(hello, {
46+
formatFunction(hello, true, 0, {
4547
showFunctions: true,
4648
functionValue: () => '<Test />',
4749
})
@@ -50,10 +52,22 @@ describe('formatFunction', () => {
5052

5153
it('should use the functionValue option even if showFunctions is false', () => {
5254
expect(
53-
formatFunction(hello, {
55+
formatFunction(hello, true, 0, {
5456
showFunctions: false,
5557
functionValue: () => '<Test />',
5658
})
5759
).toEqual('<Test />');
5860
});
61+
62+
it('should format multi-line function', () => {
63+
expect(
64+
formatFunction(hello, false, 0, { showFunctions: true, tabStop: 2 })
65+
).toEqual('function hello() {\n return 1;\n}');
66+
});
67+
68+
it('should format multi-line function with indentation', () => {
69+
expect(
70+
formatFunction(hello, false, 1, { showFunctions: true, tabStop: 2 })
71+
).toEqual('function hello() {\n return 1;\n }');
72+
});
5973
});

src/formatter/formatPropValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const formatPropValue = (
4141
}
4242

4343
if (typeof propValue === 'function') {
44-
return `{${formatFunction(propValue, options)}}`;
44+
return `{${formatFunction(propValue, true, lvl, options)}}`;
4545
}
4646

4747
if (isValidElement(propValue)) {

src/formatter/formatTreeNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default (
4848
}
4949

5050
if (node.type === 'function') {
51-
return `{${formatFunction(node.value, options)}}`;
51+
return `{${formatFunction(node.value, inline, lvl, options)}}`;
5252
}
5353

5454
if (node.type === 'ReactElement') {

src/index.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,29 @@ describe('reactElementToJSXString(ReactElement)', () => {
903903
).toEqual(`<div fn={function fn() {return 'value';}} />`);
904904
});
905905

906+
it('should ident deeply nested multi-line functions correctly', () => {
907+
/* eslint-disable arrow-body-style */
908+
const fn = () => {
909+
return 'value';
910+
};
911+
912+
expect(
913+
reactElementToJSXString(
914+
<div fn={fn}>
915+
<div fn={fn}>
916+
<div fn={fn} />
917+
</div>
918+
</div>,
919+
{
920+
showFunctions: true,
921+
functionValue: preserveFunctionLineBreak,
922+
}
923+
)
924+
).toEqual(
925+
"<div\n fn={function fn() {\n return 'value';\n }}\n>\n <div\n fn={function fn() {\n return 'value';\n }}\n >\n <div\n fn={function fn() {\n return 'value';\n }}\n />\n </div>\n</div>"
926+
);
927+
});
928+
906929
it('should expose the multiline "functionValue" formatter', () => {
907930
/* eslint-disable arrow-body-style */
908931
const fn = () => {

0 commit comments

Comments
 (0)