Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [consistent-type-assertions] check type assertion in jsx props #2653

Merged
merged 1 commit into from Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -74,13 +74,14 @@ function foo(): T {

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

```ts
```tsx
const x: T = { ... };
const y = { ... } as any;
const z = { ... } as unknown;
foo({ ... } as T);
new Clazz({ ... } as T);
function foo() { throw { bar: 5 } as Foo }
const foo = <Foo props={{ ... } as Bar}/>;
```

## When Not To Use It
Expand Down
Expand Up @@ -140,7 +140,8 @@ export default util.createRule<Options, MessageIds>({
(node.parent.type === AST_NODE_TYPES.NewExpression ||
node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.ThrowStatement ||
node.parent.type === AST_NODE_TYPES.AssignmentPattern)
node.parent.type === AST_NODE_TYPES.AssignmentPattern ||
node.parent.type === AST_NODE_TYPES.JSXExpressionContainer)
) {
return;
}
Expand Down
Expand Up @@ -112,6 +112,20 @@ ruleTester.run('consistent-type-assertions', rule, {
},
],
},
{
code: 'const bar = <Foo style={{ bar: 5 } as Bar} />;',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
options: [
{
assertionStyle: 'as',
objectLiteralTypeAssertions: 'allow-as-parameter',
},
],
},
],
invalid: [
...batchedSingleLineTests({
Expand Down Expand Up @@ -342,5 +356,24 @@ ruleTester.run('consistent-type-assertions', rule, {
},
],
}),
{
code: 'const foo = <Foo style={{ bar: 5 } as Bar} />;',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
options: [
{
assertionStyle: 'never',
},
],
errors: [
{
messageId: 'never',
line: 1,
},
],
},
],
});