Skip to content

Commit

Permalink
chore: keep TS typed prop sorting check behind an option
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed Aug 15, 2023
1 parent 1519630 commit 5955ae9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module.exports = {
sortShapeProp: {
type: 'boolean',
},
allowTypescript: {
type: 'boolean',
},
},
additionalProperties: false,
}],
Expand All @@ -64,6 +67,7 @@ module.exports = {
const ignoreCase = configuration.ignoreCase || false;
const noSortAlphabetically = configuration.noSortAlphabetically || false;
const sortShapeProp = configuration.sortShapeProp || false;
const allowTypescript = configuration.allowTypescript || false;

const typeAnnotations = new Map();

Expand Down Expand Up @@ -218,6 +222,9 @@ module.exports = {
}

function handleFunctionComponent(node) {
if (!allowTypescript) {
return;
}
const firstArg = node.params[0].typeAnnotation && node.params[0].typeAnnotation.typeAnnotation;
if (firstArg && firstArg.type === 'TSTypeReference') {
const propType = typeAnnotations.get(firstArg.typeName.name)
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,19 @@ ruleTester.run('sort-prop-types', rule, {
});
`,
options: [{ callbacksLast: true, noSortAlphabetically: true }],
},
{
code: `
type Props = {
zzz: string;
aaa: string;
}
function Foo(props: Props) {
return null;
}
`,
features: ['ts', 'no-babel'],
options: [{ allowTypescript: false }],
}
)),
invalid: parsers.all([].concat(
Expand Down Expand Up @@ -2271,6 +2284,7 @@ ruleTester.run('sort-prop-types', rule, {
}
`,
features: ['ts', 'no-babel'],
options: [{ allowTypescript: true }],
errors: [
{
messageId: 'propsNotSorted',
Expand Down Expand Up @@ -2300,6 +2314,7 @@ ruleTester.run('sort-prop-types', rule, {
}
`,
features: ['ts', 'no-babel'],
options: [{ allowTypescript: true }],
errors: [
{
messageId: 'propsNotSorted',
Expand Down Expand Up @@ -2327,6 +2342,7 @@ ruleTester.run('sort-prop-types', rule, {
}
`,
features: ['ts', 'no-babel'],
options: [{ allowTypescript: true }],
errors: [
{
messageId: 'propsNotSorted',
Expand Down

0 comments on commit 5955ae9

Please sign in to comment.