objectLiteralShorthandRule.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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 tsutils_1 = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("..");
  23. var OPTION_NEVER = "never";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. return this.applyWithFunction(sourceFile, this.ruleArguments.indexOf(OPTION_NEVER) === -1
  31. ? enforceShorthandWalker
  32. : disallowShorthandWalker);
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "object-literal-shorthand",
  37. description: "Enforces/disallows use of ES6 object literal shorthand.",
  38. hasFix: true,
  39. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n If the 'never' option is provided, any shorthand object literal syntax will cause a failure."], ["\n If the \\'never\\' option is provided, any shorthand object literal syntax will cause a failure."]))),
  40. options: {
  41. type: "string",
  42. enum: [OPTION_NEVER],
  43. },
  44. optionExamples: [true, [true, OPTION_NEVER]],
  45. type: "style",
  46. typescriptOnly: false,
  47. };
  48. /* tslint:enable:object-literal-sort-keys */
  49. Rule.LONGHAND_PROPERTY = "Expected property shorthand in object literal ";
  50. Rule.LONGHAND_METHOD = "Expected method shorthand in object literal ";
  51. Rule.SHORTHAND_ASSIGNMENT = "Shorthand property assignments have been disallowed.";
  52. return Rule;
  53. }(Lint.Rules.AbstractRule));
  54. exports.Rule = Rule;
  55. function disallowShorthandWalker(ctx) {
  56. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  57. if (tsutils_1.isShorthandPropertyAssignment(node)) {
  58. ctx.addFailureAtNode(node.name, Rule.SHORTHAND_ASSIGNMENT, Lint.Replacement.appendText(node.getStart(ctx.sourceFile), node.name.text + ": "));
  59. }
  60. else if (tsutils_1.isMethodDeclaration(node) && node.parent.kind === ts.SyntaxKind.ObjectLiteralExpression) {
  61. ctx.addFailureAtNode(node.name, Rule.SHORTHAND_ASSIGNMENT, fixShorthandMethodDeclaration(node, ctx.sourceFile));
  62. }
  63. return ts.forEachChild(node, cb);
  64. });
  65. }
  66. function enforceShorthandWalker(ctx) {
  67. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  68. if (tsutils_1.isPropertyAssignment(node)) {
  69. if (node.name.kind === ts.SyntaxKind.Identifier &&
  70. tsutils_1.isIdentifier(node.initializer) &&
  71. node.name.text === node.initializer.text) {
  72. ctx.addFailureAtNode(node, Rule.LONGHAND_PROPERTY + "('{" + node.name.text + "}').", Lint.Replacement.deleteFromTo(node.name.end, node.end));
  73. }
  74. else if (tsutils_1.isFunctionExpression(node.initializer) &&
  75. // allow named function expressions
  76. node.initializer.name === undefined) {
  77. var _a = handleLonghandMethod(node.name, node.initializer, ctx.sourceFile), name = _a[0], fix = _a[1];
  78. ctx.addFailure(node.getStart(ctx.sourceFile), tsutils_1.getChildOfKind(node.initializer, ts.SyntaxKind.OpenParenToken, ctx.sourceFile).pos, Rule.LONGHAND_METHOD + "('{" + name + "() {...}}').", fix);
  79. }
  80. }
  81. return ts.forEachChild(node, cb);
  82. });
  83. }
  84. function fixShorthandMethodDeclaration(node, sourceFile) {
  85. var isGenerator = node.asteriskToken !== undefined;
  86. var isAsync = tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword);
  87. return Lint.Replacement.replaceFromTo(node.getStart(sourceFile), node.name.end, node.name.getText(sourceFile) + ":" + (isAsync ? " async" : "") + " function" + (isGenerator ? "*" : ""));
  88. }
  89. function handleLonghandMethod(name, initializer, sourceFile) {
  90. var nameStart = name.getStart(sourceFile);
  91. var fix = Lint.Replacement.deleteFromTo(name.end, tsutils_1.getChildOfKind(initializer, ts.SyntaxKind.OpenParenToken).pos);
  92. var prefix = "";
  93. if (initializer.asteriskToken !== undefined) {
  94. prefix = "*";
  95. }
  96. if (tsutils_1.hasModifier(initializer.modifiers, ts.SyntaxKind.AsyncKeyword)) {
  97. prefix = "async " + prefix;
  98. }
  99. if (prefix !== "") {
  100. fix = [fix, Lint.Replacement.appendText(nameStart, prefix)];
  101. }
  102. return [prefix + sourceFile.text.substring(nameStart, name.end), fix];
  103. }
  104. var templateObject_1;