Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-sequences.js 3.9KB

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