Skip to content

Commit 51e7430

Browse files
committed
fix: single line type ending without semicolon
1 parent 3c1d520 commit 51e7430

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

lib/rules/sort-prop-types.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ module.exports = {
129129
requiredFirst,
130130
callbacksLast,
131131
noSortAlphabetically,
132-
sortShapeProp
132+
sortShapeProp,
133+
checkTypes
133134
);
134135
}
135136

lib/util/propTypesSort.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start a
126126
* @param {Boolean=} callbacksLast whether or not to sort callbacks after everything else.
127127
* @param {Boolean=} noSortAlphabetically whether or not to disable alphabetical sorting of the elements.
128128
* @param {Boolean=} sortShapeProp whether or not to sort propTypes defined in PropTypes.shape.
129+
* @param {Boolean=} checkTypes whether or not sorting of prop type definitions are checked.
129130
* @returns {Object|*|{range, text}} the sort order of the two elements.
130131
*/
131132
function fixPropTypesSort(
@@ -136,7 +137,8 @@ function fixPropTypesSort(
136137
requiredFirst,
137138
callbacksLast,
138139
noSortAlphabetically,
139-
sortShapeProp
140+
sortShapeProp,
141+
checkTypes
140142
) {
141143
function sortInSource(allNodes, source) {
142144
const originalSource = source;
@@ -197,7 +199,8 @@ function fixPropTypesSort(
197199
sortedAttrText = attrSource.slice(sortedAttr.range[0], sortedAttr.range[1]);
198200
}
199201
}
200-
return `${acc.slice(0, commentnodeMap.get(attr).start)}${sortedAttrText}${acc.slice(commentnodeMap.get(attr).end)}`;
202+
const sortedAttrTextVal = !checkTypes || sortedAttrText.endsWith(';') ? sortedAttrText : `${sortedAttrText};`;
203+
return `${acc.slice(0, commentnodeMap.get(attr).start)}${sortedAttrTextVal}${acc.slice(commentnodeMap.get(attr).end)}`;
201204
}, source);
202205
});
203206
return source;

tests/lib/rules/sort-prop-types.js

+50-4
Original file line numberDiff line numberDiff line change
@@ -2325,16 +2325,16 @@ ruleTester.run('sort-prop-types', rule, {
23252325
{
23262326
code: `
23272327
const Foo = (props: {
2328-
zzz: string,
2329-
aaa: string,
2328+
zzz: string;
2329+
aaa: string;
23302330
}) => {
23312331
return null;
23322332
}
23332333
`,
23342334
output: `
23352335
const Foo = (props: {
2336-
aaa: string,
2337-
zzz: string,
2336+
aaa: string;
2337+
zzz: string;
23382338
}) => {
23392339
return null;
23402340
}
@@ -2348,6 +2348,52 @@ ruleTester.run('sort-prop-types', rule, {
23482348
column: 11,
23492349
},
23502350
],
2351+
},
2352+
{
2353+
code: `
2354+
type CustomProps = { onChange: () => void; name: string };
2355+
const Foo = (props: CustomProps) => {
2356+
return null;
2357+
}
2358+
`,
2359+
output: `
2360+
type CustomProps = { name: string; onChange: () => void };
2361+
const Foo = (props: CustomProps) => {
2362+
return null;
2363+
}
2364+
`,
2365+
features: ['types'],
2366+
options: [{ checkTypes: true }],
2367+
errors: [
2368+
{
2369+
messageId: 'propsNotSorted',
2370+
line: 2,
2371+
column: 52,
2372+
},
2373+
],
2374+
},
2375+
{
2376+
code: `
2377+
type CustomProps = { onChange: (event: { target: { name: string; value: string } }) => void; name: string };
2378+
const Foo = (props: CustomProps) => {
2379+
return null;
2380+
}
2381+
`,
2382+
output: `
2383+
type CustomProps = { name: string; onChange: (event: { target: { name: string; value: string } }) => void };
2384+
const Foo = (props: CustomProps) => {
2385+
return null;
2386+
}
2387+
`,
2388+
features: ['types'],
2389+
options: [{ checkTypes: true }],
2390+
errors: [
2391+
{
2392+
messageId: 'propsNotSorted',
2393+
line: 2,
2394+
column: 102,
2395+
},
2396+
],
23512397
}
23522398
)),
23532399
});

0 commit comments

Comments
 (0)