Skip to content

Commit

Permalink
refactor: udate acorn
Browse files Browse the repository at this point in the history
refactor: update acorn
  • Loading branch information
alexander-akait authored Oct 31, 2024
2 parents e5391de + db32353 commit 6a6f14f
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 191 deletions.
4 changes: 0 additions & 4 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,6 @@ type RecursiveArrayOrRecord<T> =
| Array<RecursiveArrayOrRecord<T>>
| T;

declare module "acorn-import-attributes" {
export function importAttributesOrAssertions(BaseParser: typeof import("acorn").Parser): typeof import("acorn").Parser;
}

declare module "loader-runner" {
export function getContext(resource: string) : string;
export function runLoaders(options: any, callback: (err: Error | null, result: any) => void): void;
Expand Down
6 changes: 3 additions & 3 deletions lib/dependencies/HarmonyExportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"use strict";

const { getImportAttributes } = require("../javascript/JavascriptParser");
const InnerGraph = require("../optimize/InnerGraph");
const ConstDependency = require("./ConstDependency");
const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
Expand All @@ -13,8 +14,7 @@ const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImporte
const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
const { ExportPresenceModes } = require("./HarmonyImportDependency");
const {
harmonySpecifierTag,
getAttributes
harmonySpecifierTag
} = require("./HarmonyImportDependencyParserPlugin");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");

Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = class HarmonyExportDependencyParserPlugin {
const sideEffectDep = new HarmonyImportSideEffectDependency(
/** @type {string} */ (source),
parser.state.lastHarmonyImportOrder,
getAttributes(statement)
getImportAttributes(statement)
);
sideEffectDep.loc = Object.create(
/** @type {DependencyLocation} */ (statement.loc)
Expand Down
83 changes: 7 additions & 76 deletions lib/dependencies/HarmonyImportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

const HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin");
const { getImportAttributes } = require("../javascript/JavascriptParser");
const InnerGraph = require("../optimize/InnerGraph");
const ConstDependency = require("./ConstDependency");
const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
Expand All @@ -16,11 +17,7 @@ const { ExportPresenceModes } = require("./HarmonyImportDependency");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");

/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */
/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */
/** @typedef {import("estree").Identifier} Identifier */
/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
/** @typedef {import("estree").ImportExpression} ImportExpression */
/** @typedef {import("estree").Literal} Literal */
/** @typedef {import("estree").MemberExpression} MemberExpression */
/** @typedef {import("estree").ObjectExpression} ObjectExpression */
Expand All @@ -30,7 +27,11 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend
/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty} DestructuringAssignmentProperty */
/** @typedef {import("../javascript/JavascriptParser").ExportAllDeclaration} ExportAllDeclaration */
/** @typedef {import("../javascript/JavascriptParser").ExportNamedDeclaration} ExportNamedDeclaration */
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
/** @typedef {import("../javascript/JavascriptParser").ImportDeclaration} ImportDeclaration */
/** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../optimize/InnerGraph").InnerGraph} InnerGraph */
/** @typedef {import("../optimize/InnerGraph").TopLevelSymbol} TopLevelSymbol */
Expand All @@ -48,73 +49,6 @@ const harmonySpecifierTag = Symbol("harmony import");
* @property {Record<string, any> | undefined} assertions
*/

/**
* @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | (ImportExpression & { arguments?: ObjectExpression[] })} node node with assertions
* @returns {ImportAttributes | undefined} import attributes
*/
function getAttributes(node) {
if (
node.type === "ImportExpression" &&
node.arguments &&
node.arguments[0] &&
node.arguments[0].type === "ObjectExpression" &&
node.arguments[0].properties[0] &&
node.arguments[0].properties[0].type === "Property" &&
node.arguments[0].properties[0].value.type === "ObjectExpression" &&
node.arguments[0].properties[0].value.properties
) {
const properties =
/** @type {Property[]} */
(node.arguments[0].properties[0].value.properties);
const result = /** @type {ImportAttributes} */ ({});
for (const property of properties) {
const key =
/** @type {string} */
(
property.key.type === "Identifier"
? property.key.name
: /** @type {Literal} */ (property.key).value
);
result[key] =
/** @type {string} */
(/** @type {Literal} */ (property.value).value);
}
const key =
node.arguments[0].properties[0].key.type === "Identifier"
? node.arguments[0].properties[0].key.name
: /** @type {Literal} */ (node.arguments[0].properties[0].key).value;
if (key === "assert") {
result._isLegacyAssert = true;
}
return result;
}
// TODO remove cast when @types/estree has been updated to import assertions
const isImportAssertion =
/** @type {{ assertions?: ImportAttributeNode[] }} */ (node).assertions !==
undefined;
const attributes = isImportAssertion
? /** @type {{ assertions?: ImportAttributeNode[] }} */ (node).assertions
: /** @type {{ attributes?: ImportAttributeNode[] }} */ (node).attributes;
if (attributes === undefined) {
return;
}
const result = /** @type {ImportAttributes} */ ({});
for (const attribute of attributes) {
const key =
/** @type {string} */
(
attribute.key.type === "Identifier"
? attribute.key.name
: attribute.key.value
);
result[key] = /** @type {string} */ (attribute.value.value);
}
if (isImportAssertion) {
result._isLegacyAssert = true;
}
return result;
}

module.exports = class HarmonyImportDependencyParserPlugin {
/**
* @param {JavascriptParserOptions} options options
Expand Down Expand Up @@ -184,7 +118,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
clearDep.loc = /** @type {DependencyLocation} */ (statement.loc);
parser.state.module.addPresentationalDependency(clearDep);
parser.unsetAsiPosition(/** @type {Range} */ (statement.range)[1]);
const attributes = getAttributes(statement);
const attributes = getImportAttributes(statement);
const sideEffectDep = new HarmonyImportSideEffectDependency(
/** @type {string} */ (source),
parser.state.lastHarmonyImportOrder,
Expand All @@ -204,7 +138,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
source,
ids,
sourceOrder: parser.state.lastHarmonyImportOrder,
assertions: getAttributes(statement)
assertions: getImportAttributes(statement)
});
return true;
}
Expand Down Expand Up @@ -434,6 +368,3 @@ module.exports = class HarmonyImportDependencyParserPlugin {
};

module.exports.harmonySpecifierTag = harmonySpecifierTag;
// TODO remove it in webpack@6 in favor getAttributes
module.exports.getAssertions = getAttributes;
module.exports.getAttributes = getAttributes;
8 changes: 4 additions & 4 deletions lib/dependencies/ImportParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
const CommentCompilationWarning = require("../CommentCompilationWarning");
const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
const { getImportAttributes } = require("../javascript/JavascriptParser");
const ContextDependencyHelpers = require("./ContextDependencyHelpers");
const { getAttributes } = require("./HarmonyImportDependencyParserPlugin");
const ImportContextDependency = require("./ImportContextDependency");
const ImportDependency = require("./ImportDependency");
const ImportEagerDependency = require("./ImportEagerDependency");
const ImportWeakDependency = require("./ImportWeakDependency");

/** @typedef {import("estree").ImportExpression} ImportExpression */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */

class ImportParserPlugin {
Expand Down Expand Up @@ -260,7 +260,7 @@ class ImportParserPlugin {
}

if (param.isString()) {
const attributes = getAttributes(expr);
const attributes = getImportAttributes(expr);

if (mode === "eager") {
const dep = new ImportEagerDependency(
Expand Down Expand Up @@ -323,7 +323,7 @@ class ImportParserPlugin {
typePrefix: "import()",
category: "esm",
referencedExports: exports,
attributes: getAttributes(expr)
attributes: getImportAttributes(expr)
},
parser
);
Expand Down
3 changes: 2 additions & 1 deletion lib/dependencies/SystemPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class SystemPlugin {
/** @type {import("estree").Literal} */
(expr.arguments[0]),
loc: expr.loc,
range: expr.range
range: expr.range,
options: null
});
});
};
Expand Down
Loading

0 comments on commit 6a6f14f

Please sign in to comment.