objectLiteralKeyQuotesRule.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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("../index");
  23. var OPTION_ALWAYS = "always";
  24. var OPTION_AS_NEEDED = "as-needed";
  25. var OPTION_CONSISTENT = "consistent";
  26. var OPTION_CONSISTENT_AS_NEEDED = "consistent-as-needed";
  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.UNNEEDED_QUOTES = function (name) {
  33. return "Unnecessarily quoted property '" + name + "' found.";
  34. };
  35. Rule.UNQUOTED_PROPERTY = function (name) {
  36. return "Unquoted property '" + name + "' found.";
  37. };
  38. Rule.prototype.apply = function (sourceFile) {
  39. return this.applyWithWalker(new ObjectLiteralKeyQuotesWalker(sourceFile, this.ruleName, {
  40. option: this.ruleArguments.length === 0 ? "always" : this.ruleArguments[0],
  41. }));
  42. };
  43. /* tslint:disable:object-literal-sort-keys */
  44. Rule.metadata = {
  45. ruleName: "object-literal-key-quotes",
  46. description: "Enforces consistent object literal property quote style.",
  47. descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Object literal property names can be defined in two ways: using literals or using strings.\n For example, these two objects are equivalent:\n\n var object1 = {\n property: true\n };\n\n var object2 = {\n \"property\": true\n };\n\n In many cases, it doesn\u2019t matter if you choose to use an identifier instead of a string\n or vice-versa. Even so, you might decide to enforce a consistent style in your code.\n\n This rules lets you enforce consistent quoting of property names. Either they should always\n be quoted (default behavior) or quoted only as needed (\"as-needed\")."], ["\n Object literal property names can be defined in two ways: using literals or using strings.\n For example, these two objects are equivalent:\n\n var object1 = {\n property: true\n };\n\n var object2 = {\n \"property\": true\n };\n\n In many cases, it doesn\u2019t matter if you choose to use an identifier instead of a string\n or vice-versa. Even so, you might decide to enforce a consistent style in your code.\n\n This rules lets you enforce consistent quoting of property names. Either they should always\n be quoted (default behavior) or quoted only as needed (\"as-needed\")."]))),
  48. hasFix: true,
  49. optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Possible settings are:\n\n * `\"", "\"`: Property names should always be quoted. (This is the default.)\n * `\"", "\"`: Only property names which require quotes may be quoted (e.g. those with spaces in them).\n * `\"", "\"`: Property names should either all be quoted or unquoted.\n * `\"", "\"`: If any property name requires quotes, then all properties must be quoted. Otherwise, no\n property names may be quoted.\n\n For ES6, computed property names (`{[name]: value}`) and methods (`{foo() {}}`) never need\n to be quoted."], ["\n Possible settings are:\n\n * \\`\"", "\"\\`: Property names should always be quoted. (This is the default.)\n * \\`\"", "\"\\`: Only property names which require quotes may be quoted (e.g. those with spaces in them).\n * \\`\"", "\"\\`: Property names should either all be quoted or unquoted.\n * \\`\"", "\"\\`: If any property name requires quotes, then all properties must be quoted. Otherwise, no\n property names may be quoted.\n\n For ES6, computed property names (\\`{[name]: value}\\`) and methods (\\`{foo() {}}\\`) never need\n to be quoted."])), OPTION_ALWAYS, OPTION_AS_NEEDED, OPTION_CONSISTENT, OPTION_CONSISTENT_AS_NEEDED),
  50. options: {
  51. type: "string",
  52. enum: [OPTION_ALWAYS, OPTION_AS_NEEDED, OPTION_CONSISTENT, OPTION_CONSISTENT_AS_NEEDED],
  53. },
  54. optionExamples: [[true, OPTION_AS_NEEDED], [true, OPTION_ALWAYS]],
  55. type: "style",
  56. typescriptOnly: false,
  57. };
  58. /* tslint:enable:object-literal-sort-keys */
  59. Rule.INCONSISTENT_PROPERTY = "All property names in this object literal must be consistently quoted or unquoted.";
  60. return Rule;
  61. }(Lint.Rules.AbstractRule));
  62. exports.Rule = Rule;
  63. var ObjectLiteralKeyQuotesWalker = /** @class */ (function (_super) {
  64. tslib_1.__extends(ObjectLiteralKeyQuotesWalker, _super);
  65. function ObjectLiteralKeyQuotesWalker() {
  66. return _super !== null && _super.apply(this, arguments) || this;
  67. }
  68. ObjectLiteralKeyQuotesWalker.prototype.walk = function (sourceFile) {
  69. var _this = this;
  70. var cb = function (node) {
  71. if (tsutils_1.isObjectLiteralExpression(node)) {
  72. var propertyNames = Lint.Utils.mapDefined(node.properties, mapPropertyName);
  73. outer: switch (_this.options.option) {
  74. case "always":
  75. for (var _i = 0, propertyNames_1 = propertyNames; _i < propertyNames_1.length; _i++) {
  76. var name = propertyNames_1[_i];
  77. if (name.kind !== ts.SyntaxKind.StringLiteral) {
  78. _this.reportMissing(name);
  79. }
  80. }
  81. break;
  82. case "as-needed":
  83. for (var _a = 0, propertyNames_2 = propertyNames; _a < propertyNames_2.length; _a++) {
  84. var name = propertyNames_2[_a];
  85. if (name.kind === ts.SyntaxKind.StringLiteral && tsutils_1.isValidPropertyName(name.text)) {
  86. _this.reportUnnecessary(name);
  87. }
  88. }
  89. break;
  90. case "consistent":
  91. if (hasInconsistentQuotes(propertyNames)) {
  92. // No fix -- don't know if they would want to add quotes or remove them.
  93. _this.addFailureAt(node.getStart(_this.sourceFile), 1, Rule.INCONSISTENT_PROPERTY);
  94. }
  95. break;
  96. case "consistent-as-needed":
  97. for (var _b = 0, propertyNames_3 = propertyNames; _b < propertyNames_3.length; _b++) {
  98. var name = propertyNames_3[_b];
  99. if (name.kind === ts.SyntaxKind.StringLiteral && !tsutils_1.isValidPropertyName(name.text)) {
  100. for (var _c = 0, propertyNames_4 = propertyNames; _c < propertyNames_4.length; _c++) {
  101. var propertyName = propertyNames_4[_c];
  102. if (propertyName.kind !== ts.SyntaxKind.StringLiteral) {
  103. _this.reportMissing(propertyName);
  104. }
  105. }
  106. break outer;
  107. }
  108. }
  109. for (var _d = 0, propertyNames_5 = propertyNames; _d < propertyNames_5.length; _d++) {
  110. var name = propertyNames_5[_d];
  111. if (name.kind === ts.SyntaxKind.StringLiteral) {
  112. _this.reportUnnecessary(name);
  113. }
  114. }
  115. }
  116. }
  117. return ts.forEachChild(node, cb);
  118. };
  119. return ts.forEachChild(sourceFile, cb);
  120. };
  121. ObjectLiteralKeyQuotesWalker.prototype.reportMissing = function (node) {
  122. var start = node.getStart(this.sourceFile);
  123. this.addFailure(start, node.end, Rule.UNQUOTED_PROPERTY(node.text), Lint.Replacement.replaceFromTo(start, node.end, "\"" + node.text + "\""));
  124. };
  125. ObjectLiteralKeyQuotesWalker.prototype.reportUnnecessary = function (node) {
  126. this.addFailureAtNode(node, Rule.UNNEEDED_QUOTES(node.text), Lint.Replacement.replaceNode(node, node.text, this.sourceFile));
  127. };
  128. return ObjectLiteralKeyQuotesWalker;
  129. }(Lint.AbstractWalker));
  130. function mapPropertyName(property) {
  131. if (property.kind === ts.SyntaxKind.ShorthandPropertyAssignment ||
  132. property.kind === ts.SyntaxKind.SpreadAssignment ||
  133. property.name.kind === ts.SyntaxKind.ComputedPropertyName) {
  134. return undefined;
  135. }
  136. return property.name;
  137. }
  138. function hasInconsistentQuotes(properties) {
  139. if (properties.length < 2) {
  140. return false;
  141. }
  142. var quoted = properties[0].kind === ts.SyntaxKind.StringLiteral;
  143. for (var i = 1; i < properties.length; ++i) {
  144. if (quoted !== (properties[i].kind === ts.SyntaxKind.StringLiteral)) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. var templateObject_1, templateObject_2;