Skip to content

Commit db32353

Browse files
refactor: code
1 parent 79b8f00 commit db32353

5 files changed

Lines changed: 175 additions & 141 deletions

File tree

lib/dependencies/HarmonyImportDependencyParserPlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ const { ExportPresenceModes } = require("./HarmonyImportDependency");
1717
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
1818
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
1919

20-
/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */
21-
/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */
2220
/** @typedef {import("estree").Identifier} Identifier */
23-
/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
24-
/** @typedef {import("estree").ImportExpression} ImportExpression */
2521
/** @typedef {import("estree").Literal} Literal */
2622
/** @typedef {import("estree").MemberExpression} MemberExpression */
2723
/** @typedef {import("estree").ObjectExpression} ObjectExpression */
@@ -31,7 +27,11 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend
3127
/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
3228
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
3329
/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty} DestructuringAssignmentProperty */
30+
/** @typedef {import("../javascript/JavascriptParser").ExportAllDeclaration} ExportAllDeclaration */
31+
/** @typedef {import("../javascript/JavascriptParser").ExportNamedDeclaration} ExportNamedDeclaration */
3432
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
33+
/** @typedef {import("../javascript/JavascriptParser").ImportDeclaration} ImportDeclaration */
34+
/** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
3535
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
3636
/** @typedef {import("../optimize/InnerGraph").InnerGraph} InnerGraph */
3737
/** @typedef {import("../optimize/InnerGraph").TopLevelSymbol} TopLevelSymbol */

lib/dependencies/ImportParserPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const ImportDependency = require("./ImportDependency");
1515
const ImportEagerDependency = require("./ImportEagerDependency");
1616
const ImportWeakDependency = require("./ImportWeakDependency");
1717

18-
/** @typedef {import("estree").ImportExpression} ImportExpression */
1918
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
2019
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
2120
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
2221
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
2322
/** @typedef {import("../Module").BuildMeta} BuildMeta */
2423
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
24+
/** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
2525
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
2626

2727
class ImportParserPlugin {

lib/dependencies/SystemPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ class SystemPlugin {
125125
/** @type {import("estree").Literal} */
126126
(expr.arguments[0]),
127127
loc: expr.loc,
128-
range: expr.range
128+
range: expr.range,
129+
options: null
129130
});
130131
});
131132
};

lib/javascript/JavascriptParser.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
2626
/** @typedef {import("estree").CallExpression} CallExpression */
2727
/** @typedef {import("estree").BaseCallExpression} BaseCallExpression */
2828
/** @typedef {import("estree").StaticBlock} StaticBlock */
29-
/** @typedef {import("estree").ImportExpression} ImportExpression */
3029
/** @typedef {import("estree").ClassDeclaration} ClassDeclaration */
3130
/** @typedef {import("estree").ForStatement} ForStatement */
3231
/** @typedef {import("estree").SwitchStatement} SwitchStatement */
33-
/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */
3432
/** @typedef {import("estree").ClassExpression} ClassExpression */
3533
/** @typedef {import("estree").Comment} Comment */
3634
/** @typedef {import("estree").ConditionalExpression} ConditionalExpression */
@@ -70,7 +68,6 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
7068
/** @typedef {import("estree").WithStatement} WithStatement */
7169
/** @typedef {import("estree").ThrowStatement} ThrowStatement */
7270
/** @typedef {import("estree").MethodDefinition} MethodDefinition */
73-
/** @typedef {import("estree").ModuleDeclaration} ModuleDeclaration */
7471
/** @typedef {import("estree").NewExpression} NewExpression */
7572
/** @typedef {import("estree").SpreadElement} SpreadElement */
7673
/** @typedef {import("estree").FunctionExpression} FunctionExpression */
@@ -84,9 +81,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
8481
/** @typedef {import("estree").Program} Program */
8582
/** @typedef {import("estree").Directive} Directive */
8683
/** @typedef {import("estree").Statement} Statement */
87-
/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
8884
/** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */
89-
/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */
9085
/** @typedef {import("estree").Super} Super */
9186
/** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpression */
9287
/** @typedef {import("estree").TemplateLiteral} TemplateLiteral */
@@ -104,7 +99,13 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
10499
/** @typedef {function(string, Identifier): void} OnIdent */
105100
/** @typedef {StatementPathItem[]} StatementPath */
106101

107-
/** @typedef {Record<string, string> & { _isLegacyAssert?: boolean }} ImportAttributes */
102+
// TODO remove cast when @types/estree has been updated to import assertions
103+
/** @typedef {import("estree").BaseNode & { type: "ImportAttribute", key: Identifier | Literal, value: Literal }} ImportAttribute */
104+
/** @typedef {import("estree").ImportDeclaration & { attributes?: Array<ImportAttribute> }} ImportDeclaration */
105+
/** @typedef {import("estree").ExportNamedDeclaration & { attributes?: Array<ImportAttribute> }} ExportNamedDeclaration */
106+
/** @typedef {import("estree").ExportAllDeclaration & { attributes?: Array<ImportAttribute> }} ExportAllDeclaration */
107+
/** @typedef {import("estree").ImportExpression & { options?: Expression | null }} ImportExpression */
108+
/** @typedef {ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration | ExportAllDeclaration} ModuleDeclaration */
108109

109110
/** @type {string[]} */
110111
const EMPTY_ARRAY = [];
@@ -122,20 +123,16 @@ const importAssertions = Parser =>
122123
/** @type {typeof AcornParser} */ (
123124
/** @type {unknown} */ (
124125
class extends Parser {
125-
constructor(...args) {
126-
super(...args);
127-
}
128-
129126
parseWithClause() {
130127
const nodes = [];
131128

132129
const isAssertLegacy = this.value === "assert";
133130

134-
if (
135-
!this.eat(tokTypes._with) &&
136-
isAssertLegacy &&
137-
!this.eat(tokTypes.name)
138-
) {
131+
if (isAssertLegacy) {
132+
if (!this.eat(tokTypes.name)) {
133+
return nodes;
134+
}
135+
} else if (!this.eat(tokTypes._with)) {
139136
return nodes;
140137
}
141138

@@ -182,10 +179,10 @@ const importAssertions = Parser =>
182179
// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
183180
const parser = AcornParser.extend(importAssertions);
184181

185-
/** @typedef {{ type: "ImportAttribute", key: Identifier | Literal, value: Literal }} ImportAttribute */
182+
/** @typedef {Record<string, string> & { _isLegacyAssert?: boolean }} ImportAttributes */
186183

187184
/**
188-
* @param {(ImportDeclaration & { attributes: Array<ImportAttribute> }) | (ExportNamedDeclaration & { attributes: Array<ImportAttribute> }) | (ExportAllDeclaration & { attributes: Array<ImportAttribute> }) | (ImportExpression & { options: Expression | null })} node node with assertions
185+
* @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions
189186
* @returns {ImportAttributes | undefined} import attributes
190187
*/
191188
const getImportAttributes = node => {
@@ -195,8 +192,11 @@ const getImportAttributes = node => {
195192
node.options.type === "ObjectExpression" &&
196193
node.options.properties[0] &&
197194
node.options.properties[0].type === "Property" &&
195+
node.options.properties[0].key.type === "Identifier" &&
196+
(node.options.properties[0].key.name === "with" ||
197+
node.options.properties[0].key.name === "assert") &&
198198
node.options.properties[0].value.type === "ObjectExpression" &&
199-
node.options.properties[0].value.properties
199+
node.options.properties[0].value.properties.length > 0
200200
) {
201201
const properties =
202202
/** @type {Property[]} */
@@ -229,8 +229,7 @@ const getImportAttributes = node => {
229229
return;
230230
}
231231

232-
// TODO remove cast when @types/estree has been updated to import assertions
233-
if (node.attributes === undefined) {
232+
if (node.attributes === undefined || node.attributes.length === 0) {
234233
return;
235234
}
236235

0 commit comments

Comments
 (0)