4 * Copyright 2018 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
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);
30 return _super !== null && _super.apply(this, arguments) || this;
32 Rule.getLonghandPropertyErrorMessage = function (nodeText) {
33 return "Expected property shorthand in object literal ('" + nodeText + "').";
35 Rule.getLonghandMethodErrorMessage = function (nodeText) {
36 return "Expected method shorthand in object literal ('" + nodeText + "').";
38 Rule.getDisallowedShorthandErrorMessage = function (options) {
39 if (options.enforceShorthandMethods && !options.enforceShorthandProperties) {
40 return "Shorthand property assignments have been disallowed.";
42 else if (!options.enforceShorthandMethods && options.enforceShorthandProperties) {
43 return "Shorthand method assignments have been disallowed.";
45 return "Shorthand property and method assignments have been disallowed.";
47 Rule.prototype.apply = function (sourceFile) {
48 return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.ruleArguments));
50 Rule.prototype.parseOptions = function (options) {
51 if (options.indexOf(OPTION_VALUE_NEVER) !== -1) {
53 enforceShorthandMethods: false,
54 enforceShorthandProperties: false,
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);
62 if (optionsObject !== undefined) {
64 enforceShorthandMethods: !(optionsObject[OPTION_KEY_METHOD] === OPTION_VALUE_NEVER),
65 enforceShorthandProperties: !(optionsObject[OPTION_KEY_PROPERTY] === OPTION_VALUE_NEVER),
70 enforceShorthandMethods: true,
71 enforceShorthandProperties: true,
75 /* tslint:disable:object-literal-sort-keys */
77 ruleName: "object-literal-shorthand",
78 description: "Enforces/disallows use of ES6 object literal shorthand.",
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."]))),
85 enum: [OPTION_VALUE_NEVER],
90 _a[OPTION_KEY_PROPERTY] = {
92 enum: [OPTION_VALUE_NEVER],
94 _a[OPTION_KEY_METHOD] = {
96 enum: [OPTION_VALUE_NEVER],
106 [true, OPTION_VALUE_NEVER],
107 [true, (_b = {}, _b[OPTION_KEY_PROPERTY] = OPTION_VALUE_NEVER, _b)],
110 typescriptOnly: false,
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.";
117 }(Lint.Rules.AbstractRule));
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));
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);
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 + ": "));
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));
145 return ts.forEachChild(node, cb);
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 ? "*" : ""));
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);
157 if (initializer.asteriskToken !== undefined) {
160 if (tsutils_1.hasModifier(initializer.modifiers, ts.SyntaxKind.AsyncKeyword)) {
161 prefix = "async " + prefix;
164 fix = [fix, Lint.Replacement.appendText(nameStart, prefix)];
166 return [prefix + sourceFile.text.substring(nameStart, name.end), fix];
168 var templateObject_1;