This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[no-object-literal-type-assertion] New option: "allow-arguments" (#4521)
- Loading branch information
1 parent
40120a2
commit e080fc4
Showing
6 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/rules/code-examples/noObjectLiteralTypeAssertion.examples.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as Lint from "../../index"; | ||
import { ICodeExample } from "../../language/rule/rule"; | ||
|
||
// tslint:disable: object-literal-sort-keys | ||
export const codeExamples: ICodeExample[] = [ | ||
{ | ||
description: | ||
"Disallow object literals to appear in type assertion expressions (default). Casing to `any` and `unknown` is allowed.", | ||
config: Lint.Utils.dedent` | ||
"rules": { "no-object-literal-type-assertion": true } | ||
`, | ||
pass: Lint.Utils.dedent` | ||
let foo = {} as any; | ||
let foo = {} as unknown; | ||
let foo = {} as any as Foo; | ||
let foo = {} as unknown as Foo; | ||
`, | ||
fail: Lint.Utils.dedent` | ||
let foo = {} as Foo; | ||
let foo = <Foo>{}; | ||
`, | ||
}, | ||
{ | ||
description: "Allow using a type assertion when the object literal is used as an argument.", | ||
config: Lint.Utils.dedent` | ||
"rules": { "no-object-literal-type-assertion": [true, { "allow-arguments": true }] } | ||
`, | ||
pass: Lint.Utils.dedent` | ||
bar({} as Foo) | ||
`, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test/rules/no-object-literal-type-assertion/allow-arguments/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<T> ({}); | ||
~~~~~~~~ [0] | ||
|
||
({}) as T; | ||
~~~~~~~~~ [0] | ||
|
||
<T> x; | ||
|
||
x as T; | ||
|
||
// Allow cast to 'any' | ||
{} as any; | ||
<any> {}; | ||
|
||
// Allow cast to 'unknown' | ||
{} as unknown; | ||
<unknown> {}; | ||
|
||
// Allow object literals to be asserted when used as arguments. | ||
foo({} as T); | ||
foo(<T>{}); | ||
|
||
[0]: Type assertion on object literals is forbidden, use a type annotation instead. |
5 changes: 5 additions & 0 deletions
5
test/rules/no-object-literal-type-assertion/allow-arguments/tslint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"no-object-literal-type-assertion": [true, { "allow-arguments": true }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.