项目原始demo,不改动
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.
 
 
 
 

137 Zeilen
4.2 KiB

  1. /**
  2. * @fileoverview Rule to flag use of console object
  3. * @author Nicholas C. Zakas
  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 the use of `console`",
  17. category: "Possible Errors",
  18. recommended: true,
  19. url: "https://eslint.org/docs/rules/no-console"
  20. },
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. allow: {
  26. type: "array",
  27. items: {
  28. type: "string"
  29. },
  30. minItems: 1,
  31. uniqueItems: true
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ],
  37. messages: {
  38. unexpected: "Unexpected console statement."
  39. }
  40. },
  41. create(context) {
  42. const options = context.options[0] || {};
  43. const allowed = options.allow || [];
  44. /**
  45. * Checks whether the given reference is 'console' or not.
  46. *
  47. * @param {eslint-scope.Reference} reference - The reference to check.
  48. * @returns {boolean} `true` if the reference is 'console'.
  49. */
  50. function isConsole(reference) {
  51. const id = reference.identifier;
  52. return id && id.name === "console";
  53. }
  54. /**
  55. * Checks whether the property name of the given MemberExpression node
  56. * is allowed by options or not.
  57. *
  58. * @param {ASTNode} node - The MemberExpression node to check.
  59. * @returns {boolean} `true` if the property name of the node is allowed.
  60. */
  61. function isAllowed(node) {
  62. const propertyName = astUtils.getStaticPropertyName(node);
  63. return propertyName && allowed.indexOf(propertyName) !== -1;
  64. }
  65. /**
  66. * Checks whether the given reference is a member access which is not
  67. * allowed by options or not.
  68. *
  69. * @param {eslint-scope.Reference} reference - The reference to check.
  70. * @returns {boolean} `true` if the reference is a member access which
  71. * is not allowed by options.
  72. */
  73. function isMemberAccessExceptAllowed(reference) {
  74. const node = reference.identifier;
  75. const parent = node.parent;
  76. return (
  77. parent.type === "MemberExpression" &&
  78. parent.object === node &&
  79. !isAllowed(parent)
  80. );
  81. }
  82. /**
  83. * Reports the given reference as a violation.
  84. *
  85. * @param {eslint-scope.Reference} reference - The reference to report.
  86. * @returns {void}
  87. */
  88. function report(reference) {
  89. const node = reference.identifier.parent;
  90. context.report({
  91. node,
  92. loc: node.loc,
  93. messageId: "unexpected"
  94. });
  95. }
  96. return {
  97. "Program:exit"() {
  98. const scope = context.getScope();
  99. const consoleVar = astUtils.getVariableByName(scope, "console");
  100. const shadowed = consoleVar && consoleVar.defs.length > 0;
  101. /*
  102. * 'scope.through' includes all references to undefined
  103. * variables. If the variable 'console' is not defined, it uses
  104. * 'scope.through'.
  105. */
  106. const references = consoleVar
  107. ? consoleVar.references
  108. : scope.through.filter(isConsole);
  109. if (!shadowed) {
  110. references
  111. .filter(isMemberAccessExceptAllowed)
  112. .forEach(report);
  113. }
  114. }
  115. };
  116. }
  117. };