Skip to content

WIP: feat(eslint-plugin): [consistent-type-assertion] add allowAsReturn option #1628

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/eslint-plugin/docs/rules/consistent-type-assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ Type assertions are also commonly referred as "type casting" in TypeScript (even
type Options =
| {
assertionStyle: 'as' | 'angle-bracket';
objectLiteralTypeAssertions: 'allow' | 'allow-as-parameter' | 'never';
objectLiteralTypeAssertions?: 'allow' | 'allow-as-parameter' | 'never';
allowAsReturn?: boolean;
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be a separate boolean flag.
It would be better to accept an array of strings, i.e.:

type Options = 
  | {
    assertionStyle: 'as' | 'angle-bracket';
    objectLiteralTypeAssertions?:
      | 'allow'
      | 'allow-as-parameter'
      | 'allow-as-return'
      | 'never'
      | ('allow-as-parameter' | 'allow-as-return')[];
  }
  | {
    assertionStyle: 'never';
  }

}
| {
assertionStyle: 'never';
allowAsReturn?: boolean;
};

const defaultOptions: Options = {
assertionStyle: 'as',
objectLiteralTypeAssertions: 'allow',
allowAsReturn: false,
};
```

Expand All @@ -44,10 +47,15 @@ The compiler will warn for excess properties with this syntax, but not missing _

The const assertion `const x = { foo: 1 } as const`, introduced in TypeScript 3.4, is considered beneficial and is ignored by this option.

### `allowAsReturn`

Allow type assertions on object literals if they are in a return statement.

Examples of **incorrect** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }` (and for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`)

```ts
const x = { ... } as T;
return {} as T;
```

Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }`.
Expand All @@ -69,6 +77,12 @@ new Clazz({ ... } as T);
function foo() { throw { bar: 5 } as Foo }
```

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

```ts
return {} as T;
```

## When Not To Use It

If you do not want to enforce consistent type assertions.
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/rules/consistent-type-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ type OptUnion =
| {
assertionStyle: 'as' | 'angle-bracket';
objectLiteralTypeAssertions?: 'allow' | 'allow-as-parameter' | 'never';
allowAsReturn?: boolean;
}
| {
assertionStyle: 'never';
allowAsReturn?: boolean;
};
type Options = [OptUnion];

Expand Down Expand Up @@ -45,6 +47,7 @@ export default util.createRule<Options, MessageIds>({
assertionStyle: {
enum: ['never'],
},
allowAsReturn: { type: 'boolean' },
},
additionalProperties: false,
required: ['assertionStyle'],
Expand All @@ -58,6 +61,7 @@ export default util.createRule<Options, MessageIds>({
objectLiteralTypeAssertions: {
enum: ['allow', 'allow-as-parameter', 'never'],
},
allowAsReturn: { type: 'boolean' },
},
additionalProperties: false,
required: ['assertionStyle'],
Expand All @@ -70,6 +74,7 @@ export default util.createRule<Options, MessageIds>({
{
assertionStyle: 'as',
objectLiteralTypeAssertions: 'allow',
allowAsReturn: false,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -131,6 +136,14 @@ export default util.createRule<Options, MessageIds>({
return;
}

if (
options.allowAsReturn &&
node.parent &&
node.parent.type === AST_NODE_TYPES.ReturnStatement
) {
return;
}

if (
checkType(node.typeAnnotation) &&
node.expression.type === AST_NODE_TYPES.ObjectExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function foo() { throw <Foo>{ bar: 5 } }
print?.(<Foo>{ bar: 5 })
print?.call(<Foo>{ bar: 5 })
`;
const OBJECT_LITERAL_RETURN = `
return {} as Foo
`;

ruleTester.run('consistent-type-assertions', rule, {
valid: [
Expand Down Expand Up @@ -89,6 +92,16 @@ ruleTester.run('consistent-type-assertions', rule, {
},
],
}),
...batchedSingleLineTests({
code: OBJECT_LITERAL_RETURN,
options: [
{
assertionStyle: 'as',
objectLiteralTypeAssertions: 'never',
allowAsReturn: true,
},
],
}),
...batchedSingleLineTests({
code: OBJECT_LITERAL_ARGUMENT_ANGLE_BRACKET_CASTS,
options: [
Expand Down Expand Up @@ -328,5 +341,21 @@ ruleTester.run('consistent-type-assertions', rule, {
},
],
}),
...batchedSingleLineTests({
code: OBJECT_LITERAL_RETURN,
options: [
{
assertionStyle: 'as',
objectLiteralTypeAssertions: 'never',
allowAsReturn: false,
},
],
errors: [
{
messageId: 'unexpectedObjectTypeAssertion',
line: 2,
},
],
}),
],
});