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");
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);
30 return _super !== null && _super.apply(this, arguments) || this;
32 Rule.UNNEEDED_QUOTES = function (name) {
33 return "Unnecessarily quoted property '" + name + "' found.";
35 Rule.UNQUOTED_PROPERTY = function (name) {
36 return "Unquoted property '" + name + "' found.";
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],
43 /* tslint:disable:object-literal-sort-keys */
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\")."]))),
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),
52 enum: [OPTION_ALWAYS, OPTION_AS_NEEDED, OPTION_CONSISTENT, OPTION_CONSISTENT_AS_NEEDED],
54 optionExamples: [[true, OPTION_AS_NEEDED], [true, OPTION_ALWAYS]],
56 typescriptOnly: false,
58 /* tslint:enable:object-literal-sort-keys */
59 Rule.INCONSISTENT_PROPERTY = "All property names in this object literal must be consistently quoted or unquoted.";
61 }(Lint.Rules.AbstractRule));
63 var ObjectLiteralKeyQuotesWalker = /** @class */ (function (_super) {
64 tslib_1.__extends(ObjectLiteralKeyQuotesWalker, _super);
65 function ObjectLiteralKeyQuotesWalker() {
66 return _super !== null && _super.apply(this, arguments) || this;
68 ObjectLiteralKeyQuotesWalker.prototype.walk = function (sourceFile) {
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) {
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);
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 &&
86 tsutils_1.isValidPropertyName(name.text)) {
87 _this.reportUnnecessary(name);
92 if (hasInconsistentQuotes(propertyNames)) {
93 // No fix -- don't know if they would want to add quotes or remove them.
94 _this.addFailureAt(node.getStart(_this.sourceFile), 1, Rule.INCONSISTENT_PROPERTY);
97 case "consistent-as-needed":
98 for (var _b = 0, propertyNames_3 = propertyNames; _b < propertyNames_3.length; _b++) {
99 var name = propertyNames_3[_b];
100 if (name.kind === ts.SyntaxKind.StringLiteral &&
101 !tsutils_1.isValidPropertyName(name.text)) {
102 for (var _c = 0, propertyNames_4 = propertyNames; _c < propertyNames_4.length; _c++) {
103 var propertyName = propertyNames_4[_c];
104 if (propertyName.kind !== ts.SyntaxKind.StringLiteral) {
105 _this.reportMissing(propertyName);
111 for (var _d = 0, propertyNames_5 = propertyNames; _d < propertyNames_5.length; _d++) {
112 var name = propertyNames_5[_d];
113 if (name.kind === ts.SyntaxKind.StringLiteral) {
114 _this.reportUnnecessary(name);
119 return ts.forEachChild(node, cb);
121 return ts.forEachChild(sourceFile, cb);
123 ObjectLiteralKeyQuotesWalker.prototype.reportMissing = function (node) {
124 var start = node.getStart(this.sourceFile);
125 this.addFailure(start, node.end, Rule.UNQUOTED_PROPERTY(node.text), Lint.Replacement.replaceFromTo(start, node.end, "\"" + node.text + "\""));
127 ObjectLiteralKeyQuotesWalker.prototype.reportUnnecessary = function (node) {
128 this.addFailureAtNode(node, Rule.UNNEEDED_QUOTES(node.text), Lint.Replacement.replaceNode(node, node.text, this.sourceFile));
130 return ObjectLiteralKeyQuotesWalker;
131 }(Lint.AbstractWalker));
132 function mapPropertyName(property) {
133 if (property.kind === ts.SyntaxKind.ShorthandPropertyAssignment ||
134 property.kind === ts.SyntaxKind.SpreadAssignment ||
135 property.name.kind === ts.SyntaxKind.ComputedPropertyName) {
138 return property.name;
140 function hasInconsistentQuotes(properties) {
141 if (properties.length < 2) {
144 var quoted = properties[0].kind === ts.SyntaxKind.StringLiteral;
145 for (var i = 1; i < properties.length; ++i) {
146 if (quoted !== (properties[i].kind === ts.SyntaxKind.StringLiteral)) {
152 var templateObject_1, templateObject_2;