Skip to content

Commit 8bacb1b

Browse files
committed
fix: Rework the propNameSorter to be less dependents of node sort internals
1 parent 8110e09 commit 8bacb1b

File tree

5 files changed

+55
-52
lines changed

5 files changed

+55
-52
lines changed

src/formatter/formatReactElementNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import spacer from './spacer';
44
import formatTreeNode from './formatTreeNode';
55
import formatProp from './formatProp';
66
import mergeSiblingPlainStringChildrenReducer from './mergeSiblingPlainStringChildrenReducer';
7-
import propNameSorter from './propNameSorter';
7+
import sortPropsByNames from './sortPropsByNames';
88
import type { Options } from './../options';
99
import type { ReactElementTreeNode } from './../tree';
1010

@@ -137,7 +137,7 @@ export default (
137137
.filter(defaultPropName => !visibleAttributeNames.includes(defaultPropName))
138138
.forEach(defaultPropName => visibleAttributeNames.push(defaultPropName));
139139

140-
const attributes = visibleAttributeNames.sort(propNameSorter(sortProps));
140+
const attributes = sortPropsByNames(sortProps)(visibleAttributeNames);
141141

142142
attributes.forEach(attributeName => {
143143
const {

src/formatter/propNameSorter.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/formatter/propNameSorter.spec.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/formatter/sortPropsByNames.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* @flow */
2+
3+
const isKeyOrRefProps = (propName: string) => ['key', 'ref'].includes(propName);
4+
5+
export default (shouldSortUserProps: boolean) => (
6+
props: string[]
7+
): string[] => {
8+
const haveKeyProp = props.includes('key');
9+
const haveRefProp = props.includes('ref');
10+
11+
const userPropsOnly = props.filter(oneProp => !isKeyOrRefProps(oneProp));
12+
13+
const sortedProps = shouldSortUserProps
14+
? [...userPropsOnly.sort()] // We use basic lexical order
15+
: [...userPropsOnly];
16+
17+
if (haveRefProp) {
18+
sortedProps.unshift('ref');
19+
}
20+
21+
if (haveKeyProp) {
22+
sortedProps.unshift('key');
23+
}
24+
25+
return sortedProps;
26+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* @flow */
2+
3+
import sortPropsByNames from './sortPropsByNames';
4+
5+
test('sortPropsByNames should always move the `key` and `ref` keys first', () => {
6+
const fixtures = ['c', 'key', 'a', 'ref', 'b'];
7+
8+
expect(sortPropsByNames(false)(fixtures)).toEqual([
9+
'key',
10+
'ref',
11+
'c',
12+
'a',
13+
'b',
14+
]);
15+
});
16+
17+
test('sortPropsByNames should always sort the props and keep `key` and `ref` keys first', () => {
18+
const fixtures = ['c', 'key', 'a', 'ref', 'b'];
19+
20+
expect(sortPropsByNames(true)(fixtures)).toEqual([
21+
'key',
22+
'ref',
23+
'a',
24+
'b',
25+
'c',
26+
]);
27+
});

0 commit comments

Comments
 (0)