var-only-at-top-level.js 897 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * @fileoverview Marks all var declarations that are not at the top level
  3. * invalid.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. "use strict";
  10. var helpers = require("../helpers");
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. url:
  15. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/var-only-at-top-level.html",
  16. },
  17. type: "suggestion",
  18. },
  19. create(context) {
  20. return {
  21. VariableDeclaration(node) {
  22. if (node.kind === "var") {
  23. if (helpers.getIsTopLevelScript(context.getAncestors())) {
  24. return;
  25. }
  26. context.report(node, "Unexpected var, use let or const instead.");
  27. }
  28. },
  29. };
  30. },
  31. };