Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [consistent-type-assertions] check type assertion…
… in jsx props (#2653)
  • Loading branch information
yeonjuan committed Oct 11, 2020
1 parent 57aa6c7 commit 393e925
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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,
},
],
},
],
});

0 comments on commit 393e925

Please sign in to comment.