项目原始demo,不改动
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.
 
 
 
 

103 rindas
3.1 KiB

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