no-duplicate-string.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. /*
  3. * eslint-plugin-sonarjs
  4. * Copyright (C) 2018-2021 SonarSource SA
  5. * mailto:info AT sonarsource DOT com
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. // https://sonarsource.github.io/rspec/#/rspec/S1192
  22. const docs_url_1 = require("../utils/docs-url");
  23. const locations_1 = require("../utils/locations");
  24. // Number of times a literal must be duplicated to trigger an issue
  25. const DEFAULT_THRESHOLD = 3;
  26. const MIN_LENGTH = 10;
  27. const NO_SEPARATOR_REGEXP = /^\w*$/;
  28. const EXCLUDED_CONTEXTS = [
  29. 'ImportDeclaration',
  30. 'ImportExpression',
  31. 'JSXAttribute',
  32. 'ExportAllDeclaration',
  33. 'ExportNamedDeclaration',
  34. ];
  35. const message = 'Define a constant instead of duplicating this literal {{times}} times.';
  36. const rule = {
  37. meta: {
  38. messages: {
  39. defineConstant: message,
  40. sonarRuntime: '{{sonarRuntimeData}}',
  41. },
  42. type: 'suggestion',
  43. docs: {
  44. description: 'String literals should not be duplicated',
  45. recommended: 'error',
  46. url: (0, docs_url_1.default)(__filename),
  47. },
  48. schema: [
  49. { type: 'integer', minimum: 2 },
  50. { enum: ['sonar-runtime'] /* internal parameter for rules having secondary locations */ },
  51. ],
  52. },
  53. create(context) {
  54. const literalsByValue = new Map();
  55. const threshold = typeof context.options[0] === 'number' ? context.options[0] : DEFAULT_THRESHOLD;
  56. return {
  57. Literal: (node) => {
  58. const literal = node;
  59. const { parent } = literal;
  60. if (typeof literal.value === 'string' &&
  61. parent &&
  62. !['ExpressionStatement', 'TSLiteralType'].includes(parent.type)) {
  63. const stringContent = literal.value.trim();
  64. if (!isExcludedByUsageContext(context, literal) &&
  65. stringContent.length >= MIN_LENGTH &&
  66. !stringContent.match(NO_SEPARATOR_REGEXP)) {
  67. const sameStringLiterals = literalsByValue.get(stringContent) || [];
  68. sameStringLiterals.push(literal);
  69. literalsByValue.set(stringContent, sameStringLiterals);
  70. }
  71. }
  72. },
  73. 'Program:exit'() {
  74. literalsByValue.forEach(literals => {
  75. if (literals.length >= threshold) {
  76. const [primaryNode, ...secondaryNodes] = literals;
  77. const secondaryIssues = secondaryNodes.map(node => (0, locations_1.issueLocation)(node.loc, node.loc, 'Duplication'));
  78. (0, locations_1.report)(context, {
  79. messageId: 'defineConstant',
  80. node: primaryNode,
  81. data: { times: literals.length.toString() },
  82. }, secondaryIssues, message);
  83. }
  84. });
  85. },
  86. };
  87. },
  88. };
  89. function isExcludedByUsageContext(context, literal) {
  90. const parent = literal.parent;
  91. const parentType = parent.type;
  92. return (EXCLUDED_CONTEXTS.includes(parentType) ||
  93. isRequireContext(parent, context) ||
  94. isObjectPropertyKey(parent, literal));
  95. }
  96. function isRequireContext(parent, context) {
  97. return (parent.type === 'CallExpression' && context.getSourceCode().getText(parent.callee) === 'require');
  98. }
  99. function isObjectPropertyKey(parent, literal) {
  100. return parent.type === 'Property' && parent.key === literal;
  101. }
  102. module.exports = rule;
  103. //# sourceMappingURL=no-duplicate-string.js.map