From 083ee70de5143f2ad3a92ed8cc8b9988e93f2b27 Mon Sep 17 00:00:00 2001 From: Karol Majewski Date: Sun, 17 Feb 2019 18:08:02 +0100 Subject: [PATCH] docs(test(no-object-literal-type-assertion): Add code examples --- .../noObjectLiteralTypeAssertion.examples.ts | 50 +++++++++++++++++++ src/rules/noObjectLiteralTypeAssertionRule.ts | 3 ++ 2 files changed, 53 insertions(+) create mode 100644 src/rules/code-examples/noObjectLiteralTypeAssertion.examples.ts diff --git a/src/rules/code-examples/noObjectLiteralTypeAssertion.examples.ts b/src/rules/code-examples/noObjectLiteralTypeAssertion.examples.ts new file mode 100644 index 00000000000..0e87223bd21 --- /dev/null +++ b/src/rules/code-examples/noObjectLiteralTypeAssertion.examples.ts @@ -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 = {}; + `, + }, + { + 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"] } + `, + pass: Lint.Utils.dedent` + bar({} as Foo) + `, + }, +]; diff --git a/src/rules/noObjectLiteralTypeAssertionRule.ts b/src/rules/noObjectLiteralTypeAssertionRule.ts index a115028c63f..28594c8d759 100644 --- a/src/rules/noObjectLiteralTypeAssertionRule.ts +++ b/src/rules/noObjectLiteralTypeAssertionRule.ts @@ -24,6 +24,8 @@ import * as ts from "typescript"; import * as Lint from "../index"; +import { codeExamples } from "./code-examples/noObjectLiteralTypeAssertion.examples"; + const OPTION_ALLOW_ARGUMENTS = "allow-arguments"; interface Options { @@ -57,6 +59,7 @@ export class Rule extends Lint.Rules.AbstractRule { optionExamples: [true, [true, OPTION_ALLOW_ARGUMENTS]], type: "functionality", typescriptOnly: true, + codeExamples, }; /* tslint:enable:object-literal-sort-keys */