Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): add option to no-object-literal-type-assertion rule#87

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JamesHenry merged 8 commits intotypescript-eslint:masterfromarmano2:no-object-literal-type-assertion
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,9 +20,11 @@ const z = { ... } as unknown;

## Options

```json
```cjson
{
"@typescript-eslint/no-object-literal-type-assertion": "error"
"@typescript-eslint/no-object-literal-type-assertion": ["error", {
allowAsParameter: false // Allow type assertion in call and new expression, default false
}]
}
```

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,12 @@ const util = require('../util');
// Rule Definition
//------------------------------------------------------------------------------

const defaultOptions = [
{
allowAsParameter: false
}
];

module.exports = {
meta: {
type: 'problem',
Expand All@@ -25,9 +31,24 @@ module.exports = {
unexpectedTypeAssertion:
'Type assertion on object literals is forbidden, use a type annotation instead.'
},
schema: []
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
allowAsParameter: {
type: 'boolean'
}
}
}
]
},
create(context) {
const { allowAsParameter } = util.applyDefault(
defaultOptions,
context.options
)[0];

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
Expand All@@ -52,6 +73,14 @@ module.exports = {

return {
'TSTypeAssertion, TSAsExpression'(node) {
if (
allowAsParameter &&
(node.parent.type === 'NewExpression' ||
node.parent.type === 'CallExpression')
) {
return;
}

if (
checkType(node.typeAnnotation) &&
node.expression.type === 'ObjectExpression'
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,23 @@ ruleTester.run('no-object-literal-type-assertion', rule, {
`const foo = <any> {};`,
// Allow cast to 'unknown'
`const foo = {} as unknown;`,
`const foo = <unknown> {};`
`const foo = <unknown> {};`,
{
code: `print({ bar: 5 } as Foo)`,
options: [
{
allowAsParameter: true
}
]
},
{
code: `new print({ bar: 5 } as Foo)`,
options: [
{
allowAsParameter: true
}
]
}
],
invalid: [
{
Expand DownExpand Up@@ -71,6 +87,26 @@ ruleTester.run('no-object-literal-type-assertion', rule, {
column: 11
}
]
},
{
code: `print({ bar: 5 } as Foo)`,
errors: [
{
messageId: 'unexpectedTypeAssertion',
line: 1,
column: 7
}
]
},
{
code: `new print({ bar: 5 } as Foo)`,
errors: [
{
messageId: 'unexpectedTypeAssertion',
line: 1,
column: 11
}
]
}
]
});

[8]ページ先頭

©2009-2025 Movatter.jp