prefer-object-literal.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/S2428
  22. const nodes_1 = require("../utils/nodes");
  23. const equivalence_1 = require("../utils/equivalence");
  24. const docs_url_1 = require("../utils/docs-url");
  25. const rule = {
  26. meta: {
  27. messages: {
  28. declarePropertiesInsideObject: 'Declare one or more properties of this object inside of the object literal syntax instead of using separate statements.',
  29. },
  30. schema: [],
  31. type: 'suggestion',
  32. docs: {
  33. description: 'Object literal syntax should be used',
  34. recommended: 'error',
  35. url: (0, docs_url_1.default)(__filename),
  36. },
  37. },
  38. create(context) {
  39. return {
  40. BlockStatement: (node) => checkObjectInitialization(node.body, context),
  41. Program: (node) => {
  42. const statements = node.body.filter((statement) => !(0, nodes_1.isModuleDeclaration)(statement));
  43. checkObjectInitialization(statements, context);
  44. },
  45. };
  46. },
  47. };
  48. function checkObjectInitialization(statements, context) {
  49. let index = 0;
  50. while (index < statements.length - 1) {
  51. const objectDeclaration = getObjectDeclaration(statements[index]);
  52. // eslint-disable-next-line sonarjs/no-collapsible-if
  53. if (objectDeclaration && (0, nodes_1.isIdentifier)(objectDeclaration.id)) {
  54. if (isPropertyAssignment(statements[index + 1], objectDeclaration.id, context.getSourceCode())) {
  55. context.report({ messageId: 'declarePropertiesInsideObject', node: objectDeclaration });
  56. }
  57. }
  58. index++;
  59. }
  60. }
  61. function getObjectDeclaration(statement) {
  62. if ((0, nodes_1.isVariableDeclaration)(statement)) {
  63. return statement.declarations.find(declaration => !!declaration.init && isEmptyObjectExpression(declaration.init));
  64. }
  65. return undefined;
  66. }
  67. function isEmptyObjectExpression(expression) {
  68. return (0, nodes_1.isObjectExpression)(expression) && expression.properties.length === 0;
  69. }
  70. function isPropertyAssignment(statement, objectIdentifier, sourceCode) {
  71. if ((0, nodes_1.isExpressionStatement)(statement) && (0, nodes_1.isAssignmentExpression)(statement.expression)) {
  72. const { left, right } = statement.expression;
  73. if ((0, nodes_1.isMemberExpression)(left)) {
  74. return (!left.computed &&
  75. isSingleLineExpression(right, sourceCode) &&
  76. (0, equivalence_1.areEquivalent)(left.object, objectIdentifier, sourceCode));
  77. }
  78. }
  79. return false;
  80. }
  81. function isSingleLineExpression(expression, sourceCode) {
  82. const first = sourceCode.getFirstToken(expression).loc;
  83. const last = sourceCode.getLastToken(expression).loc;
  84. return first.start.line === last.end.line;
  85. }
  86. module.exports = rule;
  87. //# sourceMappingURL=prefer-object-literal.js.map