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-redeclare.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @fileoverview Rule to flag when the same variable is declared more then once.
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow variable redeclaration",
  14. category: "Best Practices",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/no-redeclare"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. builtinGlobals: { type: "boolean" }
  23. },
  24. additionalProperties: false
  25. }
  26. ]
  27. },
  28. create(context) {
  29. const options = {
  30. builtinGlobals: Boolean(context.options[0] && context.options[0].builtinGlobals)
  31. };
  32. /**
  33. * Find variables in a given scope and flag redeclared ones.
  34. * @param {Scope} scope - An eslint-scope scope object.
  35. * @returns {void}
  36. * @private
  37. */
  38. function findVariablesInScope(scope) {
  39. scope.variables.forEach(variable => {
  40. const hasBuiltin = options.builtinGlobals && "writeable" in variable;
  41. const count = (hasBuiltin ? 1 : 0) + variable.identifiers.length;
  42. if (count >= 2) {
  43. variable.identifiers.sort((a, b) => a.range[1] - b.range[1]);
  44. for (let i = (hasBuiltin ? 0 : 1), l = variable.identifiers.length; i < l; i++) {
  45. context.report({ node: variable.identifiers[i], message: "'{{a}}' is already defined.", data: { a: variable.name } });
  46. }
  47. }
  48. });
  49. }
  50. /**
  51. * Find variables in the current scope.
  52. * @param {ASTNode} node - The Program node.
  53. * @returns {void}
  54. * @private
  55. */
  56. function checkForGlobal(node) {
  57. const scope = context.getScope(),
  58. parserOptions = context.parserOptions,
  59. ecmaFeatures = parserOptions.ecmaFeatures || {};
  60. // Nodejs env or modules has a special scope.
  61. if (ecmaFeatures.globalReturn || node.sourceType === "module") {
  62. findVariablesInScope(scope.childScopes[0]);
  63. } else {
  64. findVariablesInScope(scope);
  65. }
  66. }
  67. /**
  68. * Find variables in the current scope.
  69. * @returns {void}
  70. * @private
  71. */
  72. function checkForBlock() {
  73. findVariablesInScope(context.getScope());
  74. }
  75. if (context.parserOptions.ecmaVersion >= 6) {
  76. return {
  77. Program: checkForGlobal,
  78. BlockStatement: checkForBlock,
  79. SwitchStatement: checkForBlock
  80. };
  81. }
  82. return {
  83. Program: checkForGlobal,
  84. FunctionDeclaration: checkForBlock,
  85. FunctionExpression: checkForBlock,
  86. ArrowFunctionExpression: checkForBlock
  87. };
  88. }
  89. };