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 */
class HarmonyTopLevelThisParserPlugin {
2023-06-17 02:24:34 +08:00
/**
* @param {JavascriptParser} parser the parser
* @returns {void}
*/
apply(parser) {
2018-02-25 09:00:20 +08:00
parser.hooks.expression
.for("this")
.tap("HarmonyTopLevelThisParserPlugin", node => {
if (!parser.scope.topLevelScope) return;
if (HarmonyExports.isEnabled(parser.state)) {
2023-06-17 02:24:34 +08:00
const dep = new ConstDependency(
"undefined",
/** @type {Range} */ (node.range),
null
);
dep.loc = /** @type {DependencyLocation} */ (node.loc);
parser.state.module.addPresentationalDependency(dep);
2023-06-17 02:49:43 +08:00
return true;
2018-02-25 09:00:20 +08:00
}
});
}
}
module.exports = HarmonyTopLevelThisParserPlugin;