Skip to content

Commit da1cc91

Browse files
akulsr0ljharb
authored andcommitted
[New] sort-prop-types: give errors on TS types
1 parent d73cd51 commit da1cc91

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Added
9+
* [`sort-prop-types`]: give errors on TS types ([#3615][] @akulsr0)
10+
811
### Fixed
912
* [`no-deprecated`]: prevent false positive on commonjs import ([#3614][] @akulsr0)
1013

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

1317
## [7.33.1] - 2023.07.29

lib/rules/sort-prop-types.js

+40
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ module.exports = {
6565
const noSortAlphabetically = configuration.noSortAlphabetically || false;
6666
const sortShapeProp = configuration.sortShapeProp || false;
6767

68+
const typeAnnotations = new Map();
69+
6870
function getKey(node) {
6971
if (node.key && node.key.value) {
7072
return node.key.value;
@@ -215,6 +217,21 @@ module.exports = {
215217
}
216218
}
217219

220+
function handleFunctionComponent(node) {
221+
const firstArg = node.params[0].typeAnnotation && node.params[0].typeAnnotation.typeAnnotation;
222+
if (firstArg && firstArg.type === 'TSTypeReference') {
223+
const propType = typeAnnotations.get(firstArg.typeName.name)
224+
&& typeAnnotations.get(firstArg.typeName.name)[0];
225+
if (propType && propType.members) {
226+
checkSorted(propType.members);
227+
}
228+
} else if (firstArg && firstArg.type === 'TSTypeLiteral') {
229+
if (firstArg.members) {
230+
checkSorted(firstArg.members);
231+
}
232+
}
233+
}
234+
218235
return {
219236
CallExpression(node) {
220237
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
@@ -261,6 +278,29 @@ module.exports = {
261278
}
262279
});
263280
},
281+
282+
TSTypeLiteral(node) {
283+
if (node && node.parent.id) {
284+
const currentNode = [].concat(
285+
typeAnnotations.get(node.parent.id.name) || [],
286+
node
287+
);
288+
typeAnnotations.set(node.parent.id.name, currentNode);
289+
}
290+
},
291+
292+
TSTypeAliasDeclaration(node) {
293+
if (node.typeAnnotation.type === 'TSTypeLiteral' || node.typeAnnotation.type === 'ObjectTypeAnnotation') {
294+
const currentNode = [].concat(
295+
typeAnnotations.get(node.id.name) || [],
296+
node.typeAnnotation
297+
);
298+
typeAnnotations.set(node.id.name, currentNode);
299+
}
300+
},
301+
302+
FunctionDeclaration: handleFunctionComponent,
303+
ArrowFunctionExpression: handleFunctionComponent,
264304
};
265305
},
266306
};

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

+86-1
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,91 @@ ruleTester.run('sort-prop-types', rule, {
22502250
line: 4,
22512251
},
22522252
],
2253-
} : []
2253+
} : [],
2254+
{
2255+
code: `
2256+
type Props = {
2257+
zzz: string;
2258+
aaa: string;
2259+
}
2260+
function Foo(props: Props) {
2261+
return null;
2262+
}
2263+
`,
2264+
output: `
2265+
type Props = {
2266+
aaa: string;
2267+
zzz: string;
2268+
}
2269+
function Foo(props: Props) {
2270+
return null;
2271+
}
2272+
`,
2273+
features: ['ts', 'no-babel'],
2274+
errors: [
2275+
{
2276+
messageId: 'propsNotSorted',
2277+
line: 4,
2278+
column: 11,
2279+
type: 'TSPropertySignature',
2280+
},
2281+
],
2282+
},
2283+
{
2284+
code: `
2285+
type Props = {
2286+
zzz: string;
2287+
aaa: string;
2288+
}
2289+
const Foo = (props: Props) => {
2290+
return null;
2291+
}
2292+
`,
2293+
output: `
2294+
type Props = {
2295+
aaa: string;
2296+
zzz: string;
2297+
}
2298+
const Foo = (props: Props) => {
2299+
return null;
2300+
}
2301+
`,
2302+
features: ['ts', 'no-babel'],
2303+
errors: [
2304+
{
2305+
messageId: 'propsNotSorted',
2306+
line: 4,
2307+
column: 11,
2308+
type: 'TSPropertySignature',
2309+
},
2310+
],
2311+
},
2312+
{
2313+
code: `
2314+
const Foo = (props: {
2315+
zzz: string,
2316+
aaa: string,
2317+
}) => {
2318+
return null;
2319+
}
2320+
`,
2321+
output: `
2322+
const Foo = (props: {
2323+
aaa: string,
2324+
zzz: string,
2325+
}) => {
2326+
return null;
2327+
}
2328+
`,
2329+
features: ['ts', 'no-babel'],
2330+
errors: [
2331+
{
2332+
messageId: 'propsNotSorted',
2333+
line: 4,
2334+
column: 11,
2335+
type: 'TSPropertySignature',
2336+
},
2337+
],
2338+
}
22542339
)),
22552340
});

0 commit comments

Comments
 (0)