.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noObjectLiteralTypeAssertionRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 Palantir Technologies, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var _a, _b, _c;
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var noObjectLiteralTypeAssertion_examples_1 = require("./code-examples/noObjectLiteralTypeAssertion.examples");
25 var OPTION_ALLOW_ARGUMENTS = "allow-arguments";
26 var DEFAULT_OPTIONS = (_a = {},
27     _a[OPTION_ALLOW_ARGUMENTS] = false,
28     _a);
29 var Rule = /** @class */ (function (_super) {
30     tslib_1.__extends(Rule, _super);
31     function Rule() {
32         return _super !== null && _super.apply(this, arguments) || this;
33     }
34     Rule.prototype.apply = function (sourceFile) {
35         return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0]));
36     };
37     /* tslint:disable:object-literal-sort-keys */
38     Rule.metadata = {
39         ruleName: "no-object-literal-type-assertion",
40         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Forbids an object literal to appear in a type assertion expression.\n            Casting to `any` or to `unknown` is still allowed."], ["\n            Forbids an object literal to appear in a type assertion expression.\n            Casting to \\`any\\` or to \\`unknown\\` is still allowed."]))),
41         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            Always prefer `const x: T = { ... };` to `const x = { ... } as T;`.\n            The type assertion in the latter case is either unnecessary or hides an error.\n            The compiler will warn for excess properties with this syntax, but not missing required fields.\n            For example: `const x: { foo: number } = {}` will fail to compile, but\n            `const x = {} as { foo: number }` will succeed.\n            Additionally, the const assertion `const x = { foo: 1 } as const`,\n            introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule."], ["\n            Always prefer \\`const x: T = { ... };\\` to \\`const x = { ... } as T;\\`.\n            The type assertion in the latter case is either unnecessary or hides an error.\n            The compiler will warn for excess properties with this syntax, but not missing required fields.\n            For example: \\`const x: { foo: number } = {}\\` will fail to compile, but\n            \\`const x = {} as { foo: number }\\` will succeed.\n            Additionally, the const assertion \\`const x = { foo: 1 } as const\\`,\n            introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule."]))),
42         optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n            One option may be configured:\n\n            * `", "` allows type assertions to be used on object literals inside call expressions."], ["\n            One option may be configured:\n\n            * \\`", "\\` allows type assertions to be used on object literals inside call expressions."])), OPTION_ALLOW_ARGUMENTS),
43         options: {
44             type: "object",
45             properties: (_b = {},
46                 _b[OPTION_ALLOW_ARGUMENTS] = {
47                     type: "boolean",
48                 },
49                 _b),
50             additionalProperties: false,
51         },
52         optionExamples: [true, [true, (_c = {}, _c[OPTION_ALLOW_ARGUMENTS] = true, _c)]],
53         type: "functionality",
54         typescriptOnly: true,
55         codeExamples: noObjectLiteralTypeAssertion_examples_1.codeExamples,
56     };
57     /* tslint:enable:object-literal-sort-keys */
58     Rule.FAILURE_STRING = "Type assertion on object literals is forbidden, use a type annotation instead.";
59     return Rule;
60 }(Lint.Rules.AbstractRule));
61 exports.Rule = Rule;
62 function walk(ctx) {
63     return ts.forEachChild(ctx.sourceFile, function cb(node) {
64         if (tsutils_1.isAssertionExpression(node) &&
65             node.type.kind !== ts.SyntaxKind.AnyKeyword &&
66             // Allow const assertions, introduced in TS 3.4
67             !(ts.isConstTypeReference !== undefined && ts.isConstTypeReference(node.type)) &&
68             // Compare with UnknownKeyword if using TS 3.0 or above
69             (!!ts.SyntaxKind.UnknownKeyword
70                 ? node.type.kind !== ts.SyntaxKind.UnknownKeyword
71                 : node.type.getText(ctx.sourceFile) !== "unknown") &&
72             tsutils_1.isObjectLiteralExpression(tsutils_1.isParenthesizedExpression(node.expression)
73                 ? node.expression.expression
74                 : node.expression) &&
75             !(ctx.options[OPTION_ALLOW_ARGUMENTS] && isArgument(node))) {
76             ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
77         }
78         return ts.forEachChild(node, cb);
79     });
80 }
81 function isArgument(node) {
82     return ts.isCallLikeExpression(node.parent);
83 }
84 function parseOptions(options) {
85     return tslib_1.__assign({}, DEFAULT_OPTIONS, options);
86 }
87 var templateObject_1, templateObject_2, templateObject_3;