Skip to content
Permalink
Browse files

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 393e92573fbde849369af1d10b9f25299ec92eaf
@@ -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
@@ -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;
}
@@ -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({
@@ -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.
You can’t perform that action at this time.