.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / objectLiteralShorthandRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 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;
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("..");
24 var OPTION_VALUE_NEVER = "never";
25 var OPTION_KEY_PROPERTY = "property";
26 var OPTION_KEY_METHOD = "method";
27 var Rule = /** @class */ (function (_super) {
28     tslib_1.__extends(Rule, _super);
29     function Rule() {
30         return _super !== null && _super.apply(this, arguments) || this;
31     }
32     Rule.getLonghandPropertyErrorMessage = function (nodeText) {
33         return "Expected property shorthand in object literal ('" + nodeText + "').";
34     };
35     Rule.getLonghandMethodErrorMessage = function (nodeText) {
36         return "Expected method shorthand in object literal ('" + nodeText + "').";
37     };
38     Rule.getDisallowedShorthandErrorMessage = function (options) {
39         if (options.enforceShorthandMethods && !options.enforceShorthandProperties) {
40             return "Shorthand property assignments have been disallowed.";
41         }
42         else if (!options.enforceShorthandMethods && options.enforceShorthandProperties) {
43             return "Shorthand method assignments have been disallowed.";
44         }
45         return "Shorthand property and method assignments have been disallowed.";
46     };
47     Rule.prototype.apply = function (sourceFile) {
48         return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.ruleArguments));
49     };
50     Rule.prototype.parseOptions = function (options) {
51         if (options.indexOf(OPTION_VALUE_NEVER) !== -1) {
52             return {
53                 enforceShorthandMethods: false,
54                 enforceShorthandProperties: false,
55             };
56         }
57         var optionsObject = options.find(function (el) {
58             return typeof el === "object" &&
59                 (el[OPTION_KEY_PROPERTY] === OPTION_VALUE_NEVER ||
60                     el[OPTION_KEY_METHOD] === OPTION_VALUE_NEVER);
61         });
62         if (optionsObject !== undefined) {
63             return {
64                 enforceShorthandMethods: !(optionsObject[OPTION_KEY_METHOD] === OPTION_VALUE_NEVER),
65                 enforceShorthandProperties: !(optionsObject[OPTION_KEY_PROPERTY] === OPTION_VALUE_NEVER),
66             };
67         }
68         else {
69             return {
70                 enforceShorthandMethods: true,
71                 enforceShorthandProperties: true,
72             };
73         }
74     };
75     /* tslint:disable:object-literal-sort-keys */
76     Rule.metadata = {
77         ruleName: "object-literal-shorthand",
78         description: "Enforces/disallows use of ES6 object literal shorthand.",
79         hasFix: true,
80         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n        `\"always\"` assumed to be default option, thus with no options provided\n        the rule enforces object literal methods and properties shorthands.\n        With `\"never\"` option provided, any shorthand object literal syntax causes an error.\n\n        The rule can be configured in a more granular way.\n        With `{\"property\": \"never\"}` provided (which is equivalent to `{\"property\": \"never\", \"method\": \"always\"}`),\n        the rule only flags property shorthand assignments,\n        and respectively with `{\"method\": \"never\"}` (equivalent to `{\"property\": \"always\", \"method\": \"never\"}`),\n        the rule fails only on method shorthands."], ["\n        \\`\"always\"\\` assumed to be default option, thus with no options provided\n        the rule enforces object literal methods and properties shorthands.\n        With \\`\"never\"\\` option provided, any shorthand object literal syntax causes an error.\n\n        The rule can be configured in a more granular way.\n        With \\`{\"property\": \"never\"}\\` provided (which is equivalent to \\`{\"property\": \"never\", \"method\": \"always\"}\\`),\n        the rule only flags property shorthand assignments,\n        and respectively with \\`{\"method\": \"never\"}\\` (equivalent to \\`{\"property\": \"always\", \"method\": \"never\"}\\`),\n        the rule fails only on method shorthands."]))),
81         options: {
82             oneOf: [
83                 {
84                     type: "string",
85                     enum: [OPTION_VALUE_NEVER],
86                 },
87                 {
88                     type: "object",
89                     properties: (_a = {},
90                         _a[OPTION_KEY_PROPERTY] = {
91                             type: "string",
92                             enum: [OPTION_VALUE_NEVER],
93                         },
94                         _a[OPTION_KEY_METHOD] = {
95                             type: "string",
96                             enum: [OPTION_VALUE_NEVER],
97                         },
98                         _a),
99                     minProperties: 1,
100                     maxProperties: 2,
101                 },
102             ],
103         },
104         optionExamples: [
105             true,
106             [true, OPTION_VALUE_NEVER],
107             [true, (_b = {}, _b[OPTION_KEY_PROPERTY] = OPTION_VALUE_NEVER, _b)],
108         ],
109         type: "style",
110         typescriptOnly: false,
111     };
112     /* tslint:enable:object-literal-sort-keys */
113     Rule.LONGHAND_PROPERTY = "Expected property shorthand in object literal ";
114     Rule.LONGHAND_METHOD = "Expected method shorthand in object literal ";
115     Rule.SHORTHAND_ASSIGNMENT = "Shorthand property assignments have been disallowed.";
116     return Rule;
117 }(Lint.Rules.AbstractRule));
118 exports.Rule = Rule;
119 function walk(ctx) {
120     var _a = ctx.options, enforceShorthandMethods = _a.enforceShorthandMethods, enforceShorthandProperties = _a.enforceShorthandProperties;
121     return ts.forEachChild(ctx.sourceFile, function cb(node) {
122         if (enforceShorthandProperties &&
123             tsutils_1.isPropertyAssignment(node) &&
124             node.name.kind === ts.SyntaxKind.Identifier &&
125             tsutils_1.isIdentifier(node.initializer) &&
126             node.name.text === node.initializer.text) {
127             ctx.addFailureAtNode(node, Rule.getLonghandPropertyErrorMessage("{" + node.name.text + "}"), Lint.Replacement.deleteFromTo(node.name.end, node.end));
128         }
129         else if (enforceShorthandMethods &&
130             tsutils_1.isPropertyAssignment(node) &&
131             tsutils_1.isFunctionExpression(node.initializer) &&
132             // allow named function expressions
133             node.initializer.name === undefined) {
134             var _a = handleLonghandMethod(node.name, node.initializer, ctx.sourceFile), name = _a[0], fix = _a[1];
135             ctx.addFailure(node.getStart(ctx.sourceFile), tsutils_1.getChildOfKind(node.initializer, ts.SyntaxKind.OpenParenToken, ctx.sourceFile).pos, Rule.getLonghandMethodErrorMessage("{" + name + "() {...}}"), fix);
136         }
137         else if (!enforceShorthandProperties && tsutils_1.isShorthandPropertyAssignment(node)) {
138             ctx.addFailureAtNode(node.name, Rule.getDisallowedShorthandErrorMessage(ctx.options), Lint.Replacement.appendText(node.getStart(ctx.sourceFile), node.name.text + ": "));
139         }
140         else if (!enforceShorthandMethods &&
141             tsutils_1.isMethodDeclaration(node) &&
142             node.parent.kind === ts.SyntaxKind.ObjectLiteralExpression) {
143             ctx.addFailureAtNode(node.name, Rule.getDisallowedShorthandErrorMessage(ctx.options), fixShorthandMethodDeclaration(node, ctx.sourceFile));
144         }
145         return ts.forEachChild(node, cb);
146     });
147 }
148 function fixShorthandMethodDeclaration(node, sourceFile) {
149     var isGenerator = node.asteriskToken !== undefined;
150     var isAsync = tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword);
151     return Lint.Replacement.replaceFromTo(node.getStart(sourceFile), node.name.end, node.name.getText(sourceFile) + ":" + (isAsync ? " async" : "") + " function" + (isGenerator ? "*" : ""));
152 }
153 function handleLonghandMethod(name, initializer, sourceFile) {
154     var nameStart = name.getStart(sourceFile);
155     var fix = Lint.Replacement.deleteFromTo(name.end, tsutils_1.getChildOfKind(initializer, ts.SyntaxKind.OpenParenToken).pos);
156     var prefix = "";
157     if (initializer.asteriskToken !== undefined) {
158         prefix = "*";
159     }
160     if (tsutils_1.hasModifier(initializer.modifiers, ts.SyntaxKind.AsyncKeyword)) {
161         prefix = "async " + prefix;
162     }
163     if (prefix !== "") {
164         fix = [fix, Lint.Replacement.appendText(nameStart, prefix)];
165     }
166     return [prefix + sourceFile.text.substring(nameStart, name.end), fix];
167 }
168 var templateObject_1;