Skip to content

Commit 393e925

Browse files
authored
fix(eslint-plugin): [consistent-type-assertions] check type assertion in jsx props (typescript-eslint#2653)
1 parent 57aa6c7 commit 393e925

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

packages/eslint-plugin/docs/rules/consistent-type-assertions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ function foo(): T {
7474

7575
Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`.
7676

77-
```ts
77+
```tsx
7878
const x: T = { ... };
7979
const y = { ... } as any;
8080
const z = { ... } as unknown;
8181
foo({ ... } as T);
8282
new Clazz({ ... } as T);
8383
function foo() { throw { bar: 5 } as Foo }
84+
const foo = <Foo props={{ ... } as Bar}/>;
8485
```
8586

8687
## When Not To Use It

packages/eslint-plugin/src/rules/consistent-type-assertions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ export default util.createRule<Options, MessageIds>({
140140
(node.parent.type === AST_NODE_TYPES.NewExpression ||
141141
node.parent.type === AST_NODE_TYPES.CallExpression ||
142142
node.parent.type === AST_NODE_TYPES.ThrowStatement ||
143-
node.parent.type === AST_NODE_TYPES.AssignmentPattern)
143+
node.parent.type === AST_NODE_TYPES.AssignmentPattern ||
144+
node.parent.type === AST_NODE_TYPES.JSXExpressionContainer)
144145
) {
145146
return;
146147
}

packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ ruleTester.run('consistent-type-assertions', rule, {
112112
},
113113
],
114114
},
115+
{
116+
code: 'const bar = <Foo style={{ bar: 5 } as Bar} />;',
117+
parserOptions: {
118+
ecmaFeatures: {
119+
jsx: true,
120+
},
121+
},
122+
options: [
123+
{
124+
assertionStyle: 'as',
125+
objectLiteralTypeAssertions: 'allow-as-parameter',
126+
},
127+
],
128+
},
115129
],
116130
invalid: [
117131
...batchedSingleLineTests({
@@ -342,5 +356,24 @@ ruleTester.run('consistent-type-assertions', rule, {
342356
},
343357
],
344358
}),
359+
{
360+
code: 'const foo = <Foo style={{ bar: 5 } as Bar} />;',
361+
parserOptions: {
362+
ecmaFeatures: {
363+
jsx: true,
364+
},
365+
},
366+
options: [
367+
{
368+
assertionStyle: 'never',
369+
},
370+
],
371+
errors: [
372+
{
373+
messageId: 'never',
374+
line: 1,
375+
},
376+
],
377+
},
345378
],
346379
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy