no-global-this.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { READ, ReferenceTracker } = require("eslint-utils")
  7. module.exports = {
  8. meta: {
  9. docs: {
  10. description: "disallow the `globalThis` variable",
  11. category: "ES2020",
  12. recommended: false,
  13. url:
  14. "http://mysticatea.github.io/eslint-plugin-es/rules/no-global-this.html",
  15. },
  16. fixable: null,
  17. messages: {
  18. forbidden: "ES2020 '{{name}}' variable is forbidden.",
  19. },
  20. schema: [],
  21. type: "problem",
  22. },
  23. create(context) {
  24. return {
  25. "Program:exit"() {
  26. const tracker = new ReferenceTracker(context.getScope())
  27. for (const { node, path } of tracker.iterateGlobalReferences({
  28. globalThis: { [READ]: true },
  29. })) {
  30. context.report({
  31. node,
  32. messageId: "forbidden",
  33. data: { name: path.join(".") },
  34. })
  35. }
  36. },
  37. }
  38. },
  39. }