no-sequences.js 4.0 KB

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