Skip to content

Commit 9f501a1

Browse files
armano2JamesHenry
authored andcommitted
feat(eslint-plugin): add option to no-object-literal-type-assertion rule (#87)
1 parent 91eedf2 commit 9f501a1

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

packages/eslint-plugin/docs/rules/no-object-literal-type-assertion.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ const z = { ... } as unknown;
2020

2121
## Options
2222

23-
```json
23+
```cjson
2424
{
25-
"@typescript-eslint/no-object-literal-type-assertion": "error"
25+
"@typescript-eslint/no-object-literal-type-assertion": ["error", {
26+
allowAsParameter: false // Allow type assertion in call and new expression, default false
27+
}]
2628
}
2729
```
2830

packages/eslint-plugin/lib/rules/no-object-literal-type-assertion.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const util = require('../util');
1010
// Rule Definition
1111
//------------------------------------------------------------------------------
1212

13+
const defaultOptions = [
14+
{
15+
allowAsParameter: false
16+
}
17+
];
18+
1319
module.exports = {
1420
meta: {
1521
type: 'problem',
@@ -25,9 +31,24 @@ module.exports = {
2531
unexpectedTypeAssertion:
2632
'Type assertion on object literals is forbidden, use a type annotation instead.'
2733
},
28-
schema: []
34+
schema: [
35+
{
36+
type: 'object',
37+
additionalProperties: false,
38+
properties: {
39+
allowAsParameter: {
40+
type: 'boolean'
41+
}
42+
}
43+
}
44+
]
2945
},
3046
create(context) {
47+
const { allowAsParameter } = util.applyDefault(
48+
defaultOptions,
49+
context.options
50+
)[0];
51+
3152
//----------------------------------------------------------------------
3253
// Public
3354
//----------------------------------------------------------------------
@@ -52,6 +73,14 @@ module.exports = {
5273

5374
return {
5475
'TSTypeAssertion, TSAsExpression'(node) {
76+
if (
77+
allowAsParameter &&
78+
(node.parent.type === 'NewExpression' ||
79+
node.parent.type === 'CallExpression')
80+
) {
81+
return;
82+
}
83+
5584
if (
5685
checkType(node.typeAnnotation) &&
5786
node.expression.type === 'ObjectExpression'

packages/eslint-plugin/tests/lib/rules/no-object-literal-type-assertion.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,23 @@ ruleTester.run('no-object-literal-type-assertion', rule, {
3939
`const foo = <any> {};`,
4040
// Allow cast to 'unknown'
4141
`const foo = {} as unknown;`,
42-
`const foo = <unknown> {};`
42+
`const foo = <unknown> {};`,
43+
{
44+
code: `print({ bar: 5 } as Foo)`,
45+
options: [
46+
{
47+
allowAsParameter: true
48+
}
49+
]
50+
},
51+
{
52+
code: `new print({ bar: 5 } as Foo)`,
53+
options: [
54+
{
55+
allowAsParameter: true
56+
}
57+
]
58+
}
4359
],
4460
invalid: [
4561
{
@@ -71,6 +87,26 @@ ruleTester.run('no-object-literal-type-assertion', rule, {
7187
column: 11
7288
}
7389
]
90+
},
91+
{
92+
code: `print({ bar: 5 } as Foo)`,
93+
errors: [
94+
{
95+
messageId: 'unexpectedTypeAssertion',
96+
line: 1,
97+
column: 7
98+
}
99+
]
100+
},
101+
{
102+
code: `new print({ bar: 5 } as Foo)`,
103+
errors: [
104+
{
105+
messageId: 'unexpectedTypeAssertion',
106+
line: 1,
107+
column: 11
108+
}
109+
]
74110
}
75111
]
76112
});

0 commit comments

Comments
 (0)