no-sequences.js 4.7 KB

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