quotemarkRule.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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_SINGLE = "single";
  24. var OPTION_DOUBLE = "double";
  25. var OPTION_JSX_SINGLE = "jsx-single";
  26. var OPTION_JSX_DOUBLE = "jsx-double";
  27. var OPTION_AVOID_TEMPLATE = "avoid-template";
  28. var OPTION_AVOID_ESCAPE = "avoid-escape";
  29. var Rule = /** @class */ (function (_super) {
  30. tslib_1.__extends(Rule, _super);
  31. function Rule() {
  32. return _super !== null && _super.apply(this, arguments) || this;
  33. }
  34. /* tslint:enable:object-literal-sort-keys */
  35. Rule.FAILURE_STRING = function (actual, expected) {
  36. return actual + " should be " + expected;
  37. };
  38. Rule.prototype.apply = function (sourceFile) {
  39. var args = this.ruleArguments;
  40. var quoteMark = getQuotemarkPreference(args) === OPTION_SINGLE ? "'" : '"';
  41. return this.applyWithFunction(sourceFile, walk, {
  42. avoidEscape: hasArg(OPTION_AVOID_ESCAPE),
  43. avoidTemplate: hasArg(OPTION_AVOID_TEMPLATE),
  44. jsxQuoteMark: hasArg(OPTION_JSX_SINGLE) ? "'" : hasArg(OPTION_JSX_DOUBLE) ? '"' : quoteMark,
  45. quoteMark: quoteMark,
  46. });
  47. function hasArg(name) {
  48. return args.indexOf(name) !== -1;
  49. }
  50. };
  51. /* tslint:disable:object-literal-sort-keys */
  52. Rule.metadata = {
  53. ruleName: "quotemark",
  54. description: "Requires single or double quotes for string literals.",
  55. hasFix: true,
  56. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Five arguments may be optionally provided:\n\n * `\"", "\"` enforces single quotes.\n * `\"", "\"` enforces double quotes.\n * `\"", "\"` enforces single quotes for JSX attributes.\n * `\"", "\"` enforces double quotes for JSX attributes.\n * `\"", "\"` forbids single-line untagged template strings that do not contain string interpolations.\n * `\"", "\"` allows you to use the \"other\" quotemark in cases where escaping would normally be required.\n For example, `[true, \"", "\", \"", "\"]` would not report a failure on the string literal\n `'Hello \"World\"'`."], ["\n Five arguments may be optionally provided:\n\n * \\`\"", "\"\\` enforces single quotes.\n * \\`\"", "\"\\` enforces double quotes.\n * \\`\"", "\"\\` enforces single quotes for JSX attributes.\n * \\`\"", "\"\\` enforces double quotes for JSX attributes.\n * \\`\"", "\"\\` forbids single-line untagged template strings that do not contain string interpolations.\n * \\`\"", "\"\\` allows you to use the \"other\" quotemark in cases where escaping would normally be required.\n For example, \\`[true, \"", "\", \"", "\"]\\` would not report a failure on the string literal\n \\`'Hello \"World\"'\\`."])), OPTION_SINGLE, OPTION_DOUBLE, OPTION_JSX_SINGLE, OPTION_JSX_DOUBLE, OPTION_AVOID_TEMPLATE, OPTION_AVOID_ESCAPE, OPTION_DOUBLE, OPTION_AVOID_ESCAPE),
  57. options: {
  58. type: "array",
  59. items: {
  60. type: "string",
  61. enum: [OPTION_SINGLE, OPTION_DOUBLE, OPTION_JSX_SINGLE, OPTION_JSX_DOUBLE, OPTION_AVOID_ESCAPE, OPTION_AVOID_TEMPLATE],
  62. },
  63. minLength: 0,
  64. maxLength: 5,
  65. },
  66. optionExamples: [
  67. [true, OPTION_SINGLE, OPTION_AVOID_ESCAPE, OPTION_AVOID_TEMPLATE],
  68. [true, OPTION_SINGLE, OPTION_JSX_DOUBLE],
  69. ],
  70. type: "style",
  71. typescriptOnly: false,
  72. };
  73. return Rule;
  74. }(Lint.Rules.AbstractRule));
  75. exports.Rule = Rule;
  76. function walk(ctx) {
  77. var sourceFile = ctx.sourceFile, options = ctx.options;
  78. ts.forEachChild(sourceFile, function cb(node) {
  79. if (tsutils_1.isStringLiteral(node)
  80. || options.avoidTemplate && tsutils_1.isNoSubstitutionTemplateLiteral(node)
  81. && node.parent.kind !== ts.SyntaxKind.TaggedTemplateExpression
  82. && tsutils_1.isSameLine(sourceFile, node.getStart(sourceFile), node.end)) {
  83. var expectedQuoteMark = node.parent.kind === ts.SyntaxKind.JsxAttribute ? options.jsxQuoteMark : options.quoteMark;
  84. var actualQuoteMark = sourceFile.text[node.end - 1];
  85. if (actualQuoteMark === expectedQuoteMark) {
  86. return;
  87. }
  88. var fixQuoteMark = expectedQuoteMark;
  89. var needsQuoteEscapes = node.text.includes(expectedQuoteMark);
  90. if (needsQuoteEscapes && options.avoidEscape) {
  91. if (node.kind === ts.SyntaxKind.StringLiteral) {
  92. return;
  93. }
  94. // If expecting double quotes, fix a template `a "quote"` to `a 'quote'` anyway,
  95. // always preferring *some* quote mark over a template.
  96. fixQuoteMark = expectedQuoteMark === '"' ? "'" : '"';
  97. if (node.text.includes(fixQuoteMark)) {
  98. return;
  99. }
  100. }
  101. var start = node.getStart(sourceFile);
  102. var text = sourceFile.text.substring(start + 1, node.end - 1);
  103. if (needsQuoteEscapes) {
  104. text = text.replace(new RegExp(fixQuoteMark, "g"), "\\" + fixQuoteMark);
  105. }
  106. text = text.replace(new RegExp("\\\\" + actualQuoteMark, "g"), actualQuoteMark);
  107. return ctx.addFailure(start, node.end, Rule.FAILURE_STRING(actualQuoteMark, fixQuoteMark), new Lint.Replacement(start, node.end - start, fixQuoteMark + text + fixQuoteMark));
  108. }
  109. ts.forEachChild(node, cb);
  110. });
  111. }
  112. function getQuotemarkPreference(args) {
  113. for (var _i = 0, args_1 = args; _i < args_1.length; _i++) {
  114. var arg = args_1[_i];
  115. if (arg === OPTION_SINGLE || arg === OPTION_DOUBLE) {
  116. return arg;
  117. }
  118. }
  119. return undefined;
  120. }
  121. var templateObject_1;