项目原始demo,不改动
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

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