HarmonyTopLevelThisParserPlugin.js 1020 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. /** @import JavascriptParser, { Range } from "../javascript/JavascriptParser" */
  9. const PLUGIN_NAME = "HarmonyTopLevelThisParserPlugin";
  10. class HarmonyTopLevelThisParserPlugin {
  11. /**
  12. * Applies the plugin by registering its hooks on the compiler.
  13. * @param {JavascriptParser} parser the parser
  14. * @returns {void}
  15. */
  16. apply(parser) {
  17. parser.hooks.expression.for("this").tap(PLUGIN_NAME, (node) => {
  18. if (!parser.scope.topLevelScope) return;
  19. if (HarmonyExports.isEnabled(parser.state)) {
  20. const dep = new ConstDependency(
  21. "undefined",
  22. /** @type {Range} */ (node.range),
  23. null
  24. );
  25. dep.loc = parser.getLocation(node);
  26. parser.state.module.addPresentationalDependency(dep);
  27. return true;
  28. }
  29. });
  30. }
  31. }
  32. module.exports = HarmonyTopLevelThisParserPlugin;