Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 4.7KB

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