Skip to content

Commit fec5d6a

Browse files
committed
chore: variable separator
1 parent 51e7430 commit fec5d6a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/util/propTypesSort.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,15 @@ function fixPropTypesSort(
185185
);
186186

187187
const sourceCodeText = getText(context);
188+
let separator = null;
188189
source = nodes.reduceRight((acc, attr, index) => {
189190
const sortedAttr = sortedAttributes[index];
190191
const commentNode = commentnodeMap.get(sortedAttr);
191192
let sortedAttrText = sourceCodeText.slice(commentNode.start, commentNode.end);
193+
const sortedAttrTextLastChar = sortedAttrText[sortedAttrText.length - 1];
194+
if (!separator && [';', ','].some((allowedSep) => sortedAttrTextLastChar === allowedSep)) {
195+
separator = sortedAttrTextLastChar;
196+
}
192197
if (sortShapeProp && isShapeProp(sortedAttr.value)) {
193198
const shape = getShapeProperties(sortedAttr.value);
194199
if (shape) {
@@ -199,7 +204,7 @@ function fixPropTypesSort(
199204
sortedAttrText = attrSource.slice(sortedAttr.range[0], sortedAttr.range[1]);
200205
}
201206
}
202-
const sortedAttrTextVal = !checkTypes || sortedAttrText.endsWith(';') ? sortedAttrText : `${sortedAttrText};`;
207+
const sortedAttrTextVal = checkTypes && !sortedAttrText.endsWith(separator) ? `${sortedAttrText}${separator}` : sortedAttrText;
203208
return `${acc.slice(0, commentnodeMap.get(attr).start)}${sortedAttrTextVal}${acc.slice(commentnodeMap.get(attr).end)}`;
204209
}, source);
205210
});

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,18 @@ ruleTester.run('sort-prop-types', rule, {
493493
`,
494494
features: ['types'],
495495
options: [{ checkTypes: false }],
496+
},
497+
{
498+
code: `
499+
const Foo = (props: {
500+
aaa: string,
501+
zzz: string
502+
}) => {
503+
return null;
504+
}
505+
`,
506+
features: ['types'],
507+
options: [{ checkTypes: true }],
496508
}
497509
)),
498510
invalid: parsers.all([].concat(
@@ -2325,16 +2337,16 @@ ruleTester.run('sort-prop-types', rule, {
23252337
{
23262338
code: `
23272339
const Foo = (props: {
2328-
zzz: string;
2329-
aaa: string;
2340+
zzz: string,
2341+
aaa: string,
23302342
}) => {
23312343
return null;
23322344
}
23332345
`,
23342346
output: `
23352347
const Foo = (props: {
2336-
aaa: string;
2337-
zzz: string;
2348+
aaa: string,
2349+
zzz: string,
23382350
}) => {
23392351
return null;
23402352
}

0 commit comments

Comments
 (0)