HarmonyTopLevelThisParserPlugin.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. const ConstDependency = require("./ConstDependency");
  7. const HarmonyExports = require("./HarmonyExports");
  8. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  9. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  10. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  11. class HarmonyTopLevelThisParserPlugin {
  12. /**
  13. * @param {JavascriptParser} parser the parser
  14. * @returns {void}
  15. */
  16. apply(parser) {
  17. parser.hooks.expression
  18. .for("this")
  19. .tap("HarmonyTopLevelThisParserPlugin", node => {
  20. if (!parser.scope.topLevelScope) return;
  21. if (HarmonyExports.isEnabled(parser.state)) {
  22. const dep = new ConstDependency(
  23. "undefined",
  24. /** @type {Range} */ (node.range),
  25. null
  26. );
  27. dep.loc = /** @type {DependencyLocation} */ (node.loc);
  28. parser.state.module.addPresentationalDependency(dep);
  29. return true;
  30. }
  31. });
  32. }
  33. }
  34. module.exports = HarmonyTopLevelThisParserPlugin;