no-sequences.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @fileoverview Rule to flag use of comma operator
  3. * @author Brandon Mills
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. //------------------------------------------------------------------------------
  14. // Rule Definition
  15. //------------------------------------------------------------------------------
  16. /** @type {import('../types').Rule.RuleModule} */
  17. module.exports = {
  18. meta: {
  19. type: "suggestion",
  20. docs: {
  21. description: "Disallow comma operators",
  22. recommended: false,
  23. url: "https://eslint.org/docs/latest/rules/no-sequences",
  24. },
  25. schema: [
  26. {
  27. type: "object",
  28. properties: {
  29. allowInParentheses: {
  30. type: "boolean",
  31. },
  32. },
  33. additionalProperties: false,
  34. },
  35. ],
  36. defaultOptions: [
  37. {
  38. allowInParentheses: true,
  39. },
  40. ],
  41. messages: {
  42. unexpectedCommaExpression: "Unexpected use of comma operator.",
  43. },
  44. },
  45. create(context) {
  46. const [{ allowInParentheses }] = context.options;
  47. const sourceCode = context.sourceCode;
  48. /**
  49. * Parts of the grammar that are required to have parens.
  50. */
  51. const parenthesized = {
  52. DoWhileStatement: "test",
  53. IfStatement: "test",
  54. SwitchStatement: "discriminant",
  55. WhileStatement: "test",
  56. WithStatement: "object",
  57. ArrowFunctionExpression: "body",
  58. /*
  59. * Omitting CallExpression - commas are parsed as argument separators
  60. * Omitting NewExpression - commas are parsed as argument separators
  61. * Omitting ForInStatement - parts aren't individually parenthesised
  62. * Omitting ForStatement - parts aren't individually parenthesised
  63. */
  64. };
  65. /**
  66. * Determines whether a node is required by the grammar to be wrapped in
  67. * parens, e.g. the test of an if statement.
  68. * @param {ASTNode} node The AST node
  69. * @returns {boolean} True if parens around node belong to parent node.
  70. */
  71. function requiresExtraParens(node) {
  72. return (
  73. node.parent &&
  74. parenthesized[node.parent.type] &&
  75. node === node.parent[parenthesized[node.parent.type]]
  76. );
  77. }
  78. /**
  79. * Check if a node is wrapped in parens.
  80. * @param {ASTNode} node The AST node
  81. * @returns {boolean} True if the node has a paren on each side.
  82. */
  83. function isParenthesised(node) {
  84. return astUtils.isParenthesised(sourceCode, node);
  85. }
  86. /**
  87. * Check if a node is wrapped in two levels of parens.
  88. * @param {ASTNode} node The AST node
  89. * @returns {boolean} True if two parens surround the node on each side.
  90. */
  91. function isParenthesisedTwice(node) {
  92. const previousToken = sourceCode.getTokenBefore(node, 1),
  93. nextToken = sourceCode.getTokenAfter(node, 1);
  94. return (
  95. isParenthesised(node) &&
  96. previousToken &&
  97. nextToken &&
  98. astUtils.isOpeningParenToken(previousToken) &&
  99. previousToken.range[1] <= node.range[0] &&
  100. astUtils.isClosingParenToken(nextToken) &&
  101. nextToken.range[0] >= node.range[1]
  102. );
  103. }
  104. return {
  105. SequenceExpression(node) {
  106. // Always allow sequences in for statement update
  107. if (
  108. node.parent.type === "ForStatement" &&
  109. (node === node.parent.init || node === node.parent.update)
  110. ) {
  111. return;
  112. }
  113. // Wrapping a sequence in extra parens indicates intent
  114. if (allowInParentheses) {
  115. if (requiresExtraParens(node)) {
  116. if (isParenthesisedTwice(node)) {
  117. return;
  118. }
  119. } else {
  120. if (isParenthesised(node)) {
  121. return;
  122. }
  123. }
  124. }
  125. const firstCommaToken = sourceCode.getTokenAfter(
  126. node.expressions[0],
  127. astUtils.isCommaToken,
  128. );
  129. context.report({
  130. node,
  131. loc: firstCommaToken.loc,
  132. messageId: "unexpectedCommaExpression",
  133. });
  134. },
  135. };
  136. },
  137. };