Skip to content

Commit

Permalink
[New] sort-prop-types: give errors on TS types
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 authored and ljharb committed Aug 9, 2023
1 parent d73cd51 commit da1cc91
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,9 +5,13 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Added
* [`sort-prop-types`]: give errors on TS types ([#3615][] @akulsr0)

### Fixed
* [`no-deprecated`]: prevent false positive on commonjs import ([#3614][] @akulsr0)

[#3615]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3615
[#3614]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3614

## [7.33.1] - 2023.07.29
Expand Down
40 changes: 40 additions & 0 deletions lib/rules/sort-prop-types.js
Expand Up @@ -65,6 +65,8 @@ module.exports = {
const noSortAlphabetically = configuration.noSortAlphabetically || false;
const sortShapeProp = configuration.sortShapeProp || false;

const typeAnnotations = new Map();

function getKey(node) {
if (node.key && node.key.value) {
return node.key.value;
Expand Down Expand Up @@ -215,6 +217,21 @@ module.exports = {
}
}

function handleFunctionComponent(node) {
const firstArg = node.params[0].typeAnnotation && node.params[0].typeAnnotation.typeAnnotation;
if (firstArg && firstArg.type === 'TSTypeReference') {
const propType = typeAnnotations.get(firstArg.typeName.name)
&& typeAnnotations.get(firstArg.typeName.name)[0];
if (propType && propType.members) {
checkSorted(propType.members);

Check warning on line 226 in lib/rules/sort-prop-types.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/sort-prop-types.js#L223-L226

Added lines #L223 - L226 were not covered by tests
}
} else if (firstArg && firstArg.type === 'TSTypeLiteral') {
if (firstArg.members) {
checkSorted(firstArg.members);

Check warning on line 230 in lib/rules/sort-prop-types.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/sort-prop-types.js#L229-L230

Added lines #L229 - L230 were not covered by tests
}
}
}

return {
CallExpression(node) {
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
Expand Down Expand Up @@ -261,6 +278,29 @@ module.exports = {
}
});
},

TSTypeLiteral(node) {
if (node && node.parent.id) {
const currentNode = [].concat(
typeAnnotations.get(node.parent.id.name) || [],

Check warning on line 285 in lib/rules/sort-prop-types.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/sort-prop-types.js#L282-L285

Added lines #L282 - L285 were not covered by tests
node
);
typeAnnotations.set(node.parent.id.name, currentNode);

Check warning on line 288 in lib/rules/sort-prop-types.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/sort-prop-types.js#L288

Added line #L288 was not covered by tests
}
},

TSTypeAliasDeclaration(node) {
if (node.typeAnnotation.type === 'TSTypeLiteral' || node.typeAnnotation.type === 'ObjectTypeAnnotation') {
const currentNode = [].concat(
typeAnnotations.get(node.id.name) || [],

Check warning on line 295 in lib/rules/sort-prop-types.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/sort-prop-types.js#L292-L295

Added lines #L292 - L295 were not covered by tests
node.typeAnnotation
);
typeAnnotations.set(node.id.name, currentNode);

Check warning on line 298 in lib/rules/sort-prop-types.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/sort-prop-types.js#L298

Added line #L298 was not covered by tests
}
},

FunctionDeclaration: handleFunctionComponent,
ArrowFunctionExpression: handleFunctionComponent,
};
},
};
87 changes: 86 additions & 1 deletion tests/lib/rules/sort-prop-types.js
Expand Up @@ -2250,6 +2250,91 @@ ruleTester.run('sort-prop-types', rule, {
line: 4,
},
],
} : []
} : [],
{
code: `
type Props = {
zzz: string;
aaa: string;
}
function Foo(props: Props) {
return null;
}
`,
output: `
type Props = {
aaa: string;
zzz: string;
}
function Foo(props: Props) {
return null;
}
`,
features: ['ts', 'no-babel'],
errors: [
{
messageId: 'propsNotSorted',
line: 4,
column: 11,
type: 'TSPropertySignature',
},
],
},
{
code: `
type Props = {
zzz: string;
aaa: string;
}
const Foo = (props: Props) => {
return null;
}
`,
output: `
type Props = {
aaa: string;
zzz: string;
}
const Foo = (props: Props) => {
return null;
}
`,
features: ['ts', 'no-babel'],
errors: [
{
messageId: 'propsNotSorted',
line: 4,
column: 11,
type: 'TSPropertySignature',
},
],
},
{
code: `
const Foo = (props: {
zzz: string,
aaa: string,
}) => {
return null;
}
`,
output: `
const Foo = (props: {
aaa: string,
zzz: string,
}) => {
return null;
}
`,
features: ['ts', 'no-babel'],
errors: [
{
messageId: 'propsNotSorted',
line: 4,
column: 11,
type: 'TSPropertySignature',
},
],
}
)),
});

0 comments on commit da1cc91

Please sign in to comment.