Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
docs(no-object-literal-type-assertion): Add code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
karol-majewski committed Feb 17, 2019
1 parent 59b18bc commit d0f19c6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 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 = <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)
`,
},
];
3 changes: 3 additions & 0 deletions src/rules/noObjectLiteralTypeAssertionRule.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 */

Expand Down

0 comments on commit d0f19c6

Please sign in to comment.