Skip to content

Commit 94d1aeb

Browse files
Patrick Sachsvvo
authored andcommitted
fix(functionValue): handle nested datastructures
Before this commit, the option functionValue was ignored
1 parent fd4f53b commit 94d1aeb

File tree

5 files changed

+120
-12
lines changed

5 files changed

+120
-12
lines changed

src/formatter/formatComplexDataStructure.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import stringify from 'stringify-object';
55
import sortObject from './sortObject';
66
import parseReactElement from './../parser/parseReactElement';
77
import formatTreeNode from './formatTreeNode';
8+
import formatFunction from './formatFunction';
89
import spacer from './spacer';
910
import type { Options } from './../options';
1011

11-
function noRefCheck() {}
12-
1312
export default (
1413
value: Object | Array<any>,
1514
inline: boolean,
@@ -32,7 +31,7 @@ export default (
3231
}
3332

3433
if (typeof currentValue === 'function') {
35-
return noRefCheck;
34+
return formatFunction(currentValue, options);
3635
}
3736

3837
return originalResult;

src/formatter/formatComplexDataStructure.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,46 @@ describe('formatComplexDataStructure', () => {
102102
`{a: /test/g}`
103103
);
104104
});
105+
106+
it('should replace a function with noRefCheck', () => {
107+
const fixture = {
108+
a: function hello() {
109+
return 1;
110+
},
111+
};
112+
113+
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
114+
'{a: function noRefCheck() {}}'
115+
);
116+
});
117+
118+
it('should format a function', () => {
119+
const fixture = {
120+
a: function hello() {
121+
return 1;
122+
},
123+
};
124+
125+
expect(
126+
formatComplexDataStructure(fixture, true, 0, {
127+
...options,
128+
showFunctions: true,
129+
})
130+
).toEqual('{a: function hello() {return 1;}}');
131+
});
132+
133+
it('should use the functionValue option', () => {
134+
const fixture = {
135+
a: function hello() {
136+
return 1;
137+
},
138+
};
139+
140+
expect(
141+
formatComplexDataStructure(fixture, true, 0, {
142+
...options,
143+
functionValue: () => '<Test />',
144+
})
145+
).toEqual('{a: <Test />}');
146+
});
105147
});

src/formatter/formatFunction.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Options } from './../options';
2+
3+
function noRefCheck() {}
4+
5+
const defaultFunctionValue = (fn: any): any => fn.toString();
6+
7+
export default (fn: Function, options: Options): string => {
8+
const { functionValue = defaultFunctionValue, showFunctions } = options;
9+
if (!showFunctions && functionValue === defaultFunctionValue) {
10+
return functionValue(noRefCheck);
11+
}
12+
13+
return functionValue(fn);
14+
};

src/formatter/formatFunction.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* @flow */
2+
3+
import formatFunction from './formatFunction';
4+
5+
jest.mock('./formatReactElementNode.js', () => node =>
6+
`<${node.displayName} />`
7+
);
8+
9+
function hello() {
10+
return 1;
11+
}
12+
13+
describe('formatFunction', () => {
14+
it('should replace a function with noRefCheck without showFunctions option', () => {
15+
expect(formatFunction(hello, {})).toEqual('function noRefCheck() {}');
16+
});
17+
18+
it('should replace a function with noRefCheck if showFunctions is false', () => {
19+
expect(formatFunction(hello, { showFunctions: false })).toEqual(
20+
'function noRefCheck() {}'
21+
);
22+
});
23+
24+
it('should format a function if showFunctions is true', () => {
25+
expect(formatFunction(hello, { showFunctions: true }))
26+
.toEqual(`function hello() {
27+
return 1;
28+
}`);
29+
});
30+
31+
it('should format a function without name if showFunctions is true', () => {
32+
expect(formatFunction(() => 1, { showFunctions: true })).toEqual(
33+
'function () {return 1;}'
34+
);
35+
});
36+
37+
it('should use the functionValue option', () => {
38+
expect(formatFunction(hello, { functionValue: () => '<Test />' })).toEqual(
39+
'<Test />'
40+
);
41+
});
42+
43+
it('should use the functionValue option even if showFunctions is true', () => {
44+
expect(
45+
formatFunction(hello, {
46+
showFunctions: true,
47+
functionValue: () => '<Test />',
48+
})
49+
).toEqual('<Test />');
50+
});
51+
52+
it('should use the functionValue option even if showFunctions is false', () => {
53+
expect(
54+
formatFunction(hello, {
55+
showFunctions: false,
56+
functionValue: () => '<Test />',
57+
})
58+
).toEqual('<Test />');
59+
});
60+
});

src/formatter/formatPropValue.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import isPlainObject from 'is-plain-object';
44
import { isValidElement } from 'react';
55
import formatComplexDataStructure from './formatComplexDataStructure';
6+
import formatFunction from './formatFunction';
67
import formatTreeNode from './formatTreeNode';
78
import type { Options } from './../options';
89
import parseReactElement from './../parser/parseReactElement';
910

10-
const noRefCheck = () => {};
1111
const escape = (s: string): string => s.replace(/"/g, '&quot;');
1212

13-
const defaultFunctionValue = (fn: any): any => fn;
14-
1513
const formatPropValue = (
1614
propValue: any,
1715
inline: boolean,
@@ -43,12 +41,7 @@ const formatPropValue = (
4341
}
4442

4543
if (typeof propValue === 'function') {
46-
const { functionValue = defaultFunctionValue, showFunctions } = options;
47-
if (!showFunctions && functionValue === defaultFunctionValue) {
48-
return `{${functionValue(noRefCheck)}}`;
49-
}
50-
51-
return `{${functionValue(propValue)}}`;
44+
return `{${formatFunction(propValue, options)}}`;
5245
}
5346

5447
if (isValidElement(propValue)) {

0 commit comments

Comments
 (0)