Skip to content

Commit 4a1b8ce

Browse files
committed
refactor(types): add initial types to CommonJsExportsParserPlugin
1 parent 9c70c6c commit 4a1b8ce

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

lib/dependencies/CommonJsDependencyHelpers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77

88
const RuntimeGlobals = require("../RuntimeGlobals");
99

10+
/** @typedef {import("../Module")} Module */
11+
/** @typedef {"exports" | "module.exports" | "this" | "Object.defineProperty(exports)" | "Object.defineProperty(module.exports)" | "Object.defineProperty(this)"} CommonJSDependencyBaseKeywords */
12+
13+
/**
14+
* @param {CommonJSDependencyBaseKeywords} depBase commonjs dependency base
15+
* @param {Module} module module
16+
* @param {Set<string>} runtimeRequirements runtime requirements
17+
* @returns {[string, string]} type and base
18+
*/
1019
exports.handleDependencyBase = (depBase, module, runtimeRequirements) => {
1120
let base = undefined;
1221
let type;

lib/dependencies/CommonJsExportsParserPlugin.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ const DynamicExports = require("./DynamicExports");
1616
const HarmonyExports = require("./HarmonyExports");
1717
const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
1818

19+
/** @typedef {import("estree").Super} Super */
20+
/** @typedef {import("estree").CallExpression} CallExpression */
1921
/** @typedef {import("estree").Expression} ExpressionNode */
22+
/** @typedef {import("estree").Expression} Expression */
23+
/** @typedef {import("estree").AssignmentExpression} AssignmentExpression */
24+
2025
/** @typedef {import("../NormalModule")} NormalModule */
2126
/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
2227
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
28+
/** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
2329

30+
/**
31+
* @param {TODO} expr expression
32+
* @returns {Expression} returns the value of property descriptor
33+
*/
2434
const getValueOfPropertyDescription = expr => {
2535
if (expr.type !== "ObjectExpression") return;
2636
for (const property of expr.properties) {
@@ -31,6 +41,15 @@ const getValueOfPropertyDescription = expr => {
3141
}
3242
};
3343

44+
/**
45+
* The purpose of this function is to check whether an expression is a truthy literal or not. This is
46+
* useful when parsing CommonJS exports, because CommonJS modules can export any value, including falsy
47+
* values like `null` and `false`. However, exports should only be created if the exported value is truthy.
48+
*
49+
* @param {Expression} expr expression being checked
50+
* @returns {boolean} true, when the expression is a truthy literal
51+
*
52+
*/
3453
const isTruthyLiteral = expr => {
3554
switch (expr.type) {
3655
case "Literal":
@@ -41,6 +60,14 @@ const isTruthyLiteral = expr => {
4160
return false;
4261
};
4362

63+
/**
64+
* The purpose of this function is to check whether an expression is a falsy literal or not. This is
65+
* useful when parsing CommonJS exports, because CommonJS modules can export any value, including falsy
66+
* values like `null` and `false`. However, exports should only be created if the exported value is truthy.
67+
*
68+
* @param {Expression} expr expression being checked
69+
* @returns {boolean} true, when the expression is a falsy literal
70+
*/
4471
const isFalsyLiteral = expr => {
4572
switch (expr.type) {
4673
case "Literal":
@@ -97,6 +124,13 @@ class CommonJsExportsParserPlugin {
97124
const enableStructuredExports = () => {
98125
DynamicExports.enable(parser.state);
99126
};
127+
128+
/**
129+
* @param {boolean} topLevel true, when the export is on top level
130+
* @param {string[]} members members of the export
131+
* @param {Expression} valueExpr expression for the value
132+
* @returns {void}
133+
*/
100134
const checkNamespace = (topLevel, members, valueExpr) => {
101135
if (!DynamicExports.isEnabled(parser.state)) return;
102136
if (members.length > 0 && members[0] === "__esModule") {
@@ -126,6 +160,13 @@ class CommonJsExportsParserPlugin {
126160
.tap("CommonJsPlugin", evaluateToString("object"));
127161

128162
// exporting //
163+
164+
/**
165+
* @param {AssignmentExpression} expr expression
166+
* @param {CommonJSDependencyBaseKeywords} base commonjs base keywords
167+
* @param {string[]} members members of the export
168+
* @returns {boolean} true, when the expression was handled
169+
*/
129170
const handleAssignExport = (expr, base, members) => {
130171
if (HarmonyExports.isEnabled(parser.state)) return;
131172
// Handle reexporting
@@ -233,6 +274,14 @@ class CommonJsExportsParserPlugin {
233274
});
234275

235276
// Self reference //
277+
278+
/**
279+
* @param { Expression | Super} expr expression
280+
* @param {CommonJSDependencyBaseKeywords} base commonjs base keywords
281+
* @param {string[]} members members of the export
282+
* @param {CallExpression} call call expression
283+
* @returns {boolean} true, when the expression was handled
284+
*/
236285
const handleAccessExport = (expr, base, members, call = undefined) => {
237286
if (HarmonyExports.isEnabled(parser.state)) return;
238287
if (members.length === 0) {

0 commit comments

Comments
 (0)