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-console.js 4.2KB

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