webpack/lib/dependencies/HarmonyTopLevelThisParserPl...

40 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
2018-07-30 23:08:51 +08:00
"use strict";
const ConstDependency = require("./ConstDependency");
const HarmonyExports = require("./HarmonyExports");
2023-06-17 02:24:34 +08:00
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "HarmonyTopLevelThisParserPlugin";
class HarmonyTopLevelThisParserPlugin {
2023-06-17 02:24:34 +08:00
/**
* @param {JavascriptParser} parser the parser
* @returns {void}
*/
apply(parser) {
2025-04-23 20:03:37 +08:00
parser.hooks.expression.for("this").tap(PLUGIN_NAME, node => {
if (!parser.scope.topLevelScope) return;
if (HarmonyExports.isEnabled(parser.state)) {
const dep = new ConstDependency(
"undefined",
/** @type {Range} */ (node.range),
null
);
dep.loc = /** @type {DependencyLocation} */ (node.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
}
});
}
}
module.exports = HarmonyTopLevelThisParserPlugin;