Skip to content

Commit

Permalink
chore: variable separator
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed Jul 18, 2024
1 parent 51e7430 commit fec5d6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/util/propTypesSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,15 @@ function fixPropTypesSort(
);

const sourceCodeText = getText(context);
let separator = null;
source = nodes.reduceRight((acc, attr, index) => {
const sortedAttr = sortedAttributes[index];
const commentNode = commentnodeMap.get(sortedAttr);
let sortedAttrText = sourceCodeText.slice(commentNode.start, commentNode.end);
const sortedAttrTextLastChar = sortedAttrText[sortedAttrText.length - 1];
if (!separator && [';', ','].some((allowedSep) => sortedAttrTextLastChar === allowedSep)) {
separator = sortedAttrTextLastChar;
}
if (sortShapeProp && isShapeProp(sortedAttr.value)) {
const shape = getShapeProperties(sortedAttr.value);
if (shape) {
Expand All @@ -199,7 +204,7 @@ function fixPropTypesSort(
sortedAttrText = attrSource.slice(sortedAttr.range[0], sortedAttr.range[1]);
}
}
const sortedAttrTextVal = !checkTypes || sortedAttrText.endsWith(';') ? sortedAttrText : `${sortedAttrText};`;
const sortedAttrTextVal = checkTypes && !sortedAttrText.endsWith(separator) ? `${sortedAttrText}${separator}` : sortedAttrText;
return `${acc.slice(0, commentnodeMap.get(attr).start)}${sortedAttrTextVal}${acc.slice(commentnodeMap.get(attr).end)}`;
}, source);
});
Expand Down
20 changes: 16 additions & 4 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,18 @@ ruleTester.run('sort-prop-types', rule, {
`,
features: ['types'],
options: [{ checkTypes: false }],
},
{
code: `
const Foo = (props: {
aaa: string,
zzz: string
}) => {
return null;
}
`,
features: ['types'],
options: [{ checkTypes: true }],
}
)),
invalid: parsers.all([].concat(
Expand Down Expand Up @@ -2325,16 +2337,16 @@ ruleTester.run('sort-prop-types', rule, {
{
code: `
const Foo = (props: {
zzz: string;
aaa: string;
zzz: string,
aaa: string,
}) => {
return null;
}
`,
output: `
const Foo = (props: {
aaa: string;
zzz: string;
aaa: string,
zzz: string,
}) => {
return null;
}
Expand Down

0 comments on commit fec5d6a

Please sign in to comment.