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-undef-init.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @fileoverview Rule to flag when initializing to undefined
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow initializing variables to `undefined`",
  15. category: "Variables",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-undef-init"
  18. },
  19. schema: [],
  20. fixable: "code"
  21. },
  22. create(context) {
  23. const sourceCode = context.getSourceCode();
  24. return {
  25. VariableDeclarator(node) {
  26. const name = sourceCode.getText(node.id),
  27. init = node.init && node.init.name,
  28. scope = context.getScope(),
  29. undefinedVar = astUtils.getVariableByName(scope, "undefined"),
  30. shadowed = undefinedVar && undefinedVar.defs.length > 0;
  31. if (init === "undefined" && node.parent.kind !== "const" && !shadowed) {
  32. context.report({
  33. node,
  34. message: "It's not necessary to initialize '{{name}}' to undefined.",
  35. data: { name },
  36. fix(fixer) {
  37. if (node.parent.kind === "var") {
  38. return null;
  39. }
  40. if (node.id.type === "ArrayPattern" || node.id.type === "ObjectPattern") {
  41. // Don't fix destructuring assignment to `undefined`.
  42. return null;
  43. }
  44. return fixer.removeRange([node.id.range[1], node.range[1]]);
  45. }
  46. });
  47. }
  48. }
  49. };
  50. }
  51. };