Skip to content

Commit

Permalink
refactor(types): more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 21, 2023
1 parent e226101 commit d9d64b5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 36 deletions.
48 changes: 31 additions & 17 deletions lib/dependencies/HarmonyImportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend
/** @typedef {import("estree").Identifier} Identifier */
/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
/** @typedef {import("estree").ImportExpression} ImportExpression */
/** @typedef {import("estree").MemberExpression} MemberExpression */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../optimize/InnerGraph").InnerGraph} InnerGraph */
/** @typedef {import("../optimize/InnerGraph").TopLevelSymbol} TopLevelSymbol */
/** @typedef {import("./HarmonyImportDependency")} HarmonyImportDependency */
Expand Down Expand Up @@ -113,19 +117,21 @@ module.exports = class HarmonyImportDependencyParserPlugin {
parser.state.lastHarmonyImportOrder =
(parser.state.lastHarmonyImportOrder || 0) + 1;
const clearDep = new ConstDependency(
parser.isAsiPosition(statement.range[0]) ? ";" : "",
statement.range
parser.isAsiPosition(/** @type {Range} */ (statement.range)[0])
? ";"
: "",
/** @type {Range} */ (statement.range)
);
clearDep.loc = statement.loc;
clearDep.loc = /** @type {DependencyLocation} */ (statement.loc);
parser.state.module.addPresentationalDependency(clearDep);
parser.unsetAsiPosition(statement.range[1]);
parser.unsetAsiPosition(/** @type {Range} */ (statement.range)[1]);
const assertions = getAssertions(statement);
const sideEffectDep = new HarmonyImportSideEffectDependency(
source,
parser.state.lastHarmonyImportOrder,
assertions
);
sideEffectDep.loc = statement.loc;
sideEffectDep.loc = /** @type {DependencyLocation} */ (statement.loc);
parser.state.module.addDependency(sideEffectDep);
return true;
}
Expand Down Expand Up @@ -172,13 +178,15 @@ module.exports = class HarmonyImportDependencyParserPlugin {
settings.sourceOrder,
settings.ids.concat(members).concat([leftPart]),
settings.name,
expression.range,
/** @type {Range} */ (expression.range),
settings.assertions,
"in"
);
dep.directImport = members.length === 0;
dep.asiSafe = !parser.isAsiPosition(expression.range[0]);
dep.loc = expression.loc;
dep.asiSafe = !parser.isAsiPosition(
/** @type {Range} */ (expression.range)[0]
);
dep.loc = /** @type {DependencyLocation} */ (expression.loc);
parser.state.module.addDependency(dep);
InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
return true;
Expand All @@ -193,7 +201,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
settings.sourceOrder,
settings.ids,
settings.name,
expr.range,
/** @type {Range} */ (expr.range),
exportPresenceMode,
settings.assertions,
[]
Expand All @@ -202,8 +210,10 @@ module.exports = class HarmonyImportDependencyParserPlugin {
parser.destructuringAssignmentPropertiesFor(expr);
dep.shorthand = parser.scope.inShorthand;
dep.directImport = true;
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
dep.loc = expr.loc;
dep.asiSafe = !parser.isAsiPosition(
/** @type {Range} */ (expr.range)[0]
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
dep.call = parser.scope.inTaggedTemplateTag;
parser.state.module.addDependency(dep);
InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
Expand Down Expand Up @@ -238,15 +248,17 @@ module.exports = class HarmonyImportDependencyParserPlugin {
settings.sourceOrder,
ids,
settings.name,
expr.range,
/** @type {Range} */ (expr.range),
exportPresenceMode,
settings.assertions,
ranges
);
dep.referencedPropertiesInDestructuring =
parser.destructuringAssignmentPropertiesFor(expr);
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
dep.loc = expr.loc;
dep.asiSafe = !parser.isAsiPosition(
/** @type {Range} */ (expr.range)[0]
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addDependency(dep);
InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
return true;
Expand Down Expand Up @@ -282,18 +294,20 @@ module.exports = class HarmonyImportDependencyParserPlugin {
settings.sourceOrder,
ids,
settings.name,
expr.range,
/** @type {Range} */ (expr.range),
exportPresenceMode,
settings.assertions,
ranges
);
dep.directImport = members.length === 0;
dep.call = true;
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
dep.asiSafe = !parser.isAsiPosition(
/** @type {Range} */ (expr.range)[0]
);
// only in case when we strictly follow the spec we need a special case here
dep.namespaceObjectAsContext =
members.length > 0 && this.strictThisContextOnImports;
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addDependency(dep);
if (args) parser.walkExpressions(args);
InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
Expand Down
1 change: 1 addition & 0 deletions lib/dependencies/HarmonyImportSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
this.range = range;
this.idRanges = idRanges;
this.exportPresenceMode = exportPresenceMode;
/** @type {boolean | undefined} */
this.namespaceObjectAsContext = false;
this.call = undefined;
this.directImport = undefined;
Expand Down
46 changes: 31 additions & 15 deletions lib/dependencies/ImportMetaPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const ConstDependency = require("./ConstDependency");
/** @typedef {import("estree").MemberExpression} MemberExpression */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../javascript/JavascriptParser")} Parser */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */

const getCriticalDependencyWarning = memoize(() =>
require("./CriticalDependencyWarning")
Expand Down Expand Up @@ -64,10 +66,10 @@ class ImportMetaPlugin {
.for("import.meta")
.tap(PLUGIN_NAME, metaProperty => {
const dep = new ConstDependency(
importMetaName,
metaProperty.range
/** @type {string} */ (importMetaName),
/** @type {Range} */ (metaProperty.range)
);
dep.loc = metaProperty.loc;
dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand All @@ -82,6 +84,10 @@ class ImportMetaPlugin {
const importMetaUrl = () =>
JSON.stringify(getUrl(parser.state.module));
const importMetaWebpackVersion = () => JSON.stringify(webpackVersion);
/**
* @param {string[]} members members
* @returns {string} error message
*/
const importMetaUnknownProperty = members =>
`${Template.toNormalComment(
"unsupported import.meta." + members.join(".")
Expand All @@ -106,16 +112,20 @@ class ImportMetaPlugin {
new CriticalDependencyWarning(
"Accessing import.meta directly is unsupported (only property access or destructuring is supported)"
),
metaProperty.loc
/** @type {DependencyLocation} */ (metaProperty.loc)
)
);
const dep = new ConstDependency(
`${
parser.isAsiPosition(metaProperty.range[0]) ? ";" : ""
parser.isAsiPosition(
/** @type {Range} */ (metaProperty.range)[0]
)
? ";"
: ""
}({})`,
metaProperty.range
/** @type {Range} */ (metaProperty.range)
);
dep.loc = metaProperty.loc;
dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
}
Expand All @@ -136,8 +146,11 @@ class ImportMetaPlugin {
break;
}
}
const dep = new ConstDependency(`({${str}})`, metaProperty.range);
dep.loc = metaProperty.loc;
const dep = new ConstDependency(
`({${str}})`,
/** @type {Range} */ (metaProperty.range)
);
dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand All @@ -159,8 +172,11 @@ class ImportMetaPlugin {
parser.hooks.expression
.for("import.meta.url")
.tap(PLUGIN_NAME, expr => {
const dep = new ConstDependency(importMetaUrl(), expr.range);
dep.loc = expr.loc;
const dep = new ConstDependency(
importMetaUrl(),
/** @type {Range} */ (expr.range)
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand All @@ -172,7 +188,7 @@ class ImportMetaPlugin {
.tap(PLUGIN_NAME, expr => {
return new BasicEvaluatedExpression()
.setString(getUrl(parser.state.module))
.setRange(expr.range);
.setRange(/** @type {Range} */ (expr.range));
});

/// import.meta.webpack ///
Expand Down Expand Up @@ -201,9 +217,9 @@ class ImportMetaPlugin {
.tap(PLUGIN_NAME, (expr, members) => {
const dep = new ConstDependency(
importMetaUnknownProperty(members),
expr.range
/** @type {Range} */ (expr.range)
);
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
Expand All @@ -220,7 +236,7 @@ class ImportMetaPlugin {
) {
return new BasicEvaluatedExpression()
.setUndefined()
.setRange(expr.range);
.setRange(/** @type {Range} */ (expr.range));
}
});
};
Expand Down
4 changes: 2 additions & 2 deletions lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ class JavascriptParser extends Parser {
binaryExpression: new SyncBailHook(["binaryExpression"]),
/** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
expression: new HookMap(() => new SyncBailHook(["expression"])),
/** @type {HookMap<SyncBailHook<[Expression, string[], boolean[], Range[]], boolean | void>>} */
/** @type {HookMap<SyncBailHook<[MemberExpression, string[], boolean[], Range[]], boolean | void>>} */
expressionMemberChain: new HookMap(
() =>
new SyncBailHook([
Expand All @@ -402,7 +402,7 @@ class JavascriptParser extends Parser {
"memberRanges"
])
),
/** @type {HookMap<SyncBailHook<[Expression, string[]], boolean | void>>} */
/** @type {HookMap<SyncBailHook<[MemberExpression, string[]], boolean | void>>} */
unhandledExpressionMemberChain: new HookMap(
() => new SyncBailHook(["expression", "members"])
),
Expand Down
4 changes: 2 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5533,12 +5533,12 @@ declare class JavascriptParser extends Parser {
expression: HookMap<SyncBailHook<[Expression], boolean | void>>;
expressionMemberChain: HookMap<
SyncBailHook<
[Expression, string[], boolean[], [number, number][]],
[MemberExpression, string[], boolean[], [number, number][]],
boolean | void
>
>;
unhandledExpressionMemberChain: HookMap<
SyncBailHook<[Expression, string[]], boolean | void>
SyncBailHook<[MemberExpression, string[]], boolean | void>
>;
expressionConditionalOperator: SyncBailHook<
[ConditionalExpression],
Expand Down

0 comments on commit d9d64b5

Please sign in to comment.