HarmonyTopLevelThisParserPlugin.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. const PLUGIN_NAME = "HarmonyTopLevelThisParserPlugin";
  12. class HarmonyTopLevelThisParserPlugin {
  13. /**
  14. * Applies the plugin by registering its hooks on the compiler.
  15. * @param {JavascriptParser} parser the parser
  16. * @returns {void}
  17. */
  18. apply(parser) {
  19. parser.hooks.expression.for("this").tap(PLUGIN_NAME, (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;