Skip to content

Commit 686dc75

Browse files
committed
Preserve line break
1 parent 3297048 commit 686dc75

10 files changed

+163
-2
lines changed

src/formatter/formatTree.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import formatTree from './formatTree';
22
import formatTreeNode from './formatTreeNode';
3+
34
jest.mock('./formatTreeNode', () => jest.fn(() => '<MockedComponent />'));
5+
46
describe('formatTree', () => {
57
it('should format the node as a root node', () => {
68
const tree = {};
79
const options = {};
810
const result = formatTree(tree, options);
11+
912
expect(formatTreeNode).toHaveBeenCalledWith(tree, false, 0, options);
13+
1014
expect(result).toBe('<MockedComponent />');
1115
});
1216
});

src/formatter/formatTree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import formatTreeNode from './formatTreeNode';
22
import type { Options } from './../options';
33
import type { TreeNode } from './../tree';
4+
45
export default (node: TreeNode, options: Options): string =>
56
formatTreeNode(node, false, 0, options);

src/formatter/formatTreeNode.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import formatTreeNode from './formatTreeNode';
2+
23
jest.mock(
34
'./formatReactElementNode',
45
() => () => '<MockedFormatReactElementNodeResult />'
56
);
7+
68
describe('formatTreeNode', () => {
79
it('should format number tree node', () => {
810
expect(
@@ -17,6 +19,7 @@ describe('formatTreeNode', () => {
1719
)
1820
).toBe('42');
1921
});
22+
2023
it('should format string tree node', () => {
2124
expect(
2225
formatTreeNode(
@@ -30,6 +33,7 @@ describe('formatTreeNode', () => {
3033
)
3134
).toBe('foo');
3235
});
36+
3337
it('should format react element tree node', () => {
3438
expect(
3539
formatTreeNode(
@@ -43,6 +47,7 @@ describe('formatTreeNode', () => {
4347
)
4448
).toBe('<MockedFormatReactElementNodeResult />');
4549
});
50+
4651
const jsxDelimiters = ['<', '>', '{', '}'];
4752
jsxDelimiters.forEach((char) => {
4853
it(`should escape string that contains the JSX delimiter "${char}"`, () => {
@@ -59,6 +64,7 @@ describe('formatTreeNode', () => {
5964
).toBe(`{\`I contain ${char}, is will be escaped\`}`);
6065
});
6166
});
67+
6268
it('should preserve the format of string', () => {
6369
expect(
6470
formatTreeNode(
@@ -72,6 +78,7 @@ describe('formatTreeNode', () => {
7278
)
7379
).toBe(`foo
7480
bar`);
81+
7582
expect(
7683
formatTreeNode(
7784
{

src/formatter/formatTreeNode.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Options } from './../options';
44
import type { TreeNode } from './../tree';
55

66
const jsxStopChars = ['<', '>', '{', '}'];
7-
87
const shouldBeEscaped = (s: string) =>
98
jsxStopChars.some((jsxStopChar) => s.includes(jsxStopChar));
109

@@ -18,7 +17,6 @@ const escape = (s: string) => {
1817

1918
const preserveTrailingSpace = (s: string) => {
2019
let result = s;
21-
2220
if (result.endsWith(' ')) {
2321
result = result.replace(/^(.*?)(\s+)$/, "$1{'$2'}");
2422
}

src/formatter/mergeSiblingPlainStringChildrenReducer.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import {
55
createReactElementTreeNode,
66
} from './../tree';
77
import type { TreeNode } from './../tree';
8+
89
test('mergeSiblingPlainStringChildrenReducer should merge sibling string tree nodes', () => {
910
const childrens: TreeNode[] = [
1011
createStringTreeNode('a'),
1112
createStringTreeNode('b'),
1213
createStringTreeNode('c'),
1314
];
15+
1416
expect(childrens.reduce(mergeSiblingPlainStringChildrenReducer, [])).toEqual([
1517
{
1618
type: 'string',
1719
value: 'abc',
1820
},
1921
]);
2022
});
23+
2124
test('mergeSiblingPlainStringChildrenReducer should consider number as string', () => {
2225
expect(
2326
[
@@ -31,6 +34,7 @@ test('mergeSiblingPlainStringChildrenReducer should consider number as string',
3134
value: 'a51c',
3235
},
3336
]);
37+
3438
expect(
3539
[
3640
createStringTreeNode(5),
@@ -44,6 +48,7 @@ test('mergeSiblingPlainStringChildrenReducer should consider number as string',
4448
},
4549
]);
4650
});
51+
4752
test('mergeSiblingPlainStringChildrenReducer should detect non string node', () => {
4853
const childrens: TreeNode[] = [
4954
createReactElementTreeNode('MyFoo', {}, {}, ['foo']),
@@ -54,6 +59,7 @@ test('mergeSiblingPlainStringChildrenReducer should detect non string node', ()
5459
createNumberTreeNode(42),
5560
createReactElementTreeNode('MyBaz', {}, {}, ['baz']),
5661
];
62+
5763
expect(childrens.reduce(mergeSiblingPlainStringChildrenReducer, [])).toEqual([
5864
{
5965
type: 'ReactElement',
@@ -86,6 +92,7 @@ test('mergeSiblingPlainStringChildrenReducer should detect non string node', ()
8692
},
8793
]);
8894
});
95+
8996
test('mergeSiblingPlainStringChildrenReducer should reduce empty array to an empty array', () => {
9097
expect([].reduce(mergeSiblingPlainStringChildrenReducer, [])).toEqual([]);
9198
});

src/formatter/mergeSiblingPlainStringChildrenReducer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createStringTreeNode } from './../tree';
22
import type { TreeNode } from './../tree';
3+
34
export default (
45
previousNodes: TreeNode[],
56
currentNode: TreeNode

src/formatter/sortObject.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sortObject from './sortObject';
2+
23
describe('sortObject', () => {
34
it('should sort keys in objects', () => {
45
const fixture = {
@@ -14,6 +15,7 @@ describe('sortObject', () => {
1415
},
1516
],
1617
};
18+
1719
expect(JSON.stringify(sortObject(fixture))).toEqual(
1820
JSON.stringify({
1921
a: [
@@ -30,6 +32,7 @@ describe('sortObject', () => {
3032
})
3133
);
3234
});
35+
3336
it('should process an array', () => {
3437
const fixture = [
3538
{
@@ -43,6 +46,7 @@ describe('sortObject', () => {
4346
a: 3,
4447
},
4548
];
49+
4650
expect(JSON.stringify(sortObject(fixture))).toEqual(
4751
JSON.stringify([
4852
{
@@ -58,6 +62,7 @@ describe('sortObject', () => {
5862
])
5963
);
6064
});
65+
6166
it('should not break special values', () => {
6267
const date = new Date();
6368
const regexp = /test/g;
@@ -66,6 +71,7 @@ describe('sortObject', () => {
6671
b: regexp,
6772
c: date,
6873
};
74+
6975
expect(sortObject(fixture)).toEqual({
7076
a: [date, regexp],
7177
b: regexp,

src/formatter/sortPropsByNames.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sortPropsByNames from './sortPropsByNames';
2+
23
test('sortPropsByNames should always move the `key` and `ref` keys first', () => {
34
const fixtures = ['c', 'key', 'a', 'ref', 'b'];
5+
46
expect(sortPropsByNames(false)(fixtures)).toEqual([
57
'key',
68
'ref',
@@ -9,8 +11,10 @@ test('sortPropsByNames should always move the `key` and `ref` keys first', () =>
911
'b',
1012
]);
1113
});
14+
1215
test('sortPropsByNames should always sort the props and keep `key` and `ref` keys first', () => {
1316
const fixtures = ['c', 'key', 'a', 'ref', 'b'];
17+
1418
expect(sortPropsByNames(true)(fixtures)).toEqual([
1519
'key',
1620
'ref',

src/formatter/spacer.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import spacer from './spacer';
2+
23
describe('spacer', () => {
34
it('should generate a spaced string', () => {
45
expect(spacer(0, 1)).toEqual('');
56
expect(spacer(0, 2)).toEqual('');
67
expect(spacer(0, 3)).toEqual('');
8+
79
expect(spacer(1, 1)).toEqual(' ');
810
expect(spacer(1, 2)).toEqual(' ');
911
expect(spacer(1, 3)).toEqual(' ');
12+
1013
expect(spacer(2, 1)).toEqual(' ');
1114
expect(spacer(2, 2)).toEqual(' ');
1215
expect(spacer(2, 3)).toEqual(' ');

0 commit comments

Comments
 (0)