refactor: udate acorn

This commit is contained in:
alexander.akait 2024-10-31 00:09:25 +03:00
parent e5391de40b
commit b520904482
4 changed files with 92 additions and 37 deletions

View File

@ -6,6 +6,7 @@
"use strict";
const HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin");
const { LEGACY_ASSERT_ATTRIBUTES } = require("../javascript/JavascriptParser");
const InnerGraph = require("../optimize/InnerGraph");
const ConstDependency = require("./ConstDependency");
const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
@ -55,17 +56,16 @@ const harmonySpecifierTag = Symbol("harmony import");
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
node.options &&
node.options.type === "ObjectExpression" &&
node.options.properties[0] &&
node.options.properties[0].type === "Property" &&
node.options.properties[0].value.type === "ObjectExpression" &&
node.options.properties[0].value.properties
) {
const properties =
/** @type {Property[]} */
(node.arguments[0].properties[0].value.properties);
(node.options.properties[0].value.properties);
const result = /** @type {ImportAttributes} */ ({});
for (const property of properties) {
const key =
@ -80,26 +80,23 @@ function getAttributes(node) {
(/** @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;
node.options.properties[0].key.type === "Identifier"
? node.options.properties[0].key.name
: /** @type {Literal} */ (node.options.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) {
if (node.attributes === undefined) {
return;
}
const result = /** @type {ImportAttributes} */ ({});
for (const attribute of attributes) {
for (const attribute of node.attributes) {
const key =
/** @type {string} */
(
@ -109,7 +106,8 @@ function getAttributes(node) {
);
result[key] = /** @type {string} */ (attribute.value.value);
}
if (isImportAssertion) {
if (node.attributes[LEGACY_ASSERT_ATTRIBUTES]) {
result._isLegacyAssert = true;
}
return result;

View File

@ -5,8 +5,7 @@
"use strict";
const { Parser: AcornParser } = require("acorn");
const { importAttributesOrAssertions } = require("acorn-import-attributes");
const { Parser: AcornParser, tokTypes } = require("acorn");
const { SyncBailHook, HookMap } = require("tapable");
const vm = require("vm");
const Parser = require("../Parser");
@ -113,9 +112,68 @@ const ALLOWED_MEMBER_TYPES_CALL_EXPRESSION = 0b01;
const ALLOWED_MEMBER_TYPES_EXPRESSION = 0b10;
const ALLOWED_MEMBER_TYPES_ALL = 0b11;
// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
const LEGACY_ASSERT_ATTRIBUTES = Symbol("assert");
const parser = AcornParser.extend(importAttributesOrAssertions);
function importAssertions(Parser) {
return class extends Parser {
constructor(...args) {
super(...args);
}
parseWithClause() {
const nodes = [];
const isAssertLegacy = this.value === "assert";
if (
!this.eat(tokTypes._with) &&
isAssertLegacy &&
!this.eat(tokTypes.name)
) {
return nodes;
}
this.expect(tokTypes.braceL);
const attributeKeys = {};
let first = true;
while (!this.eat(tokTypes.braceR)) {
if (!first) {
this.expect(tokTypes.comma);
if (this.afterTrailingComma(tokTypes.braceR)) {
break;
}
} else {
first = false;
}
const attr = this.parseImportAttribute();
const keyName =
attr.key.type === "Identifier" ? attr.key.name : attr.key.value;
if (Object.prototype.hasOwnProperty.call(attributeKeys, keyName)) {
this.raiseRecoverable(
attr.key.start,
`Duplicate attribute key '${keyName}'`
);
}
attributeKeys[keyName] = true;
nodes.push(attr);
}
if (isAssertLegacy) {
nodes[LEGACY_ASSERT_ATTRIBUTES] = true;
}
return nodes;
}
};
}
// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
const parser = AcornParser.extend(importAssertions);
class VariableInfo {
/**
@ -4866,3 +4924,4 @@ module.exports.ALLOWED_MEMBER_TYPES_EXPRESSION =
ALLOWED_MEMBER_TYPES_EXPRESSION;
module.exports.ALLOWED_MEMBER_TYPES_CALL_EXPRESSION =
ALLOWED_MEMBER_TYPES_CALL_EXPRESSION;
module.exports.LEGACY_ASSERT_ATTRIBUTES = LEGACY_ASSERT_ATTRIBUTES;

View File

@ -9,8 +9,7 @@
"@webassemblyjs/ast": "^1.12.1",
"@webassemblyjs/wasm-edit": "^1.12.1",
"@webassemblyjs/wasm-parser": "^1.12.1",
"acorn": "^8.7.1",
"acorn-import-attributes": "^1.9.5",
"acorn": "^8.14.0",
"browserslist": "^4.24.0",
"chrome-trace-event": "^1.0.2",
"enhanced-resolve": "^5.17.1",

View File

@ -1482,11 +1482,6 @@ abbrev@1.0.x:
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"
integrity sha512-LEyx4aLEC3x6T0UguF6YILf+ntvmOaWsVfENmIW0E9H09vKlLDGelMjjSm0jkDHALj8A8quZ/HapKNigzwge+Q==
acorn-import-attributes@^1.9.5:
version "1.9.5"
resolved "https://registry.yarnpkg.com/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz#7eb1557b1ba05ef18b5ed0ec67591bfab04688ef"
integrity sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==
acorn-jsx@^5.3.2:
version "5.3.2"
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"
@ -1497,10 +1492,10 @@ acorn@^7.1.1:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.4.1.tgz#feaed255973d2e77555b83dbc08851a6c63520fa"
integrity sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==
acorn@^8.12.0, acorn@^8.7.1, acorn@^8.8.2:
version "8.12.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.12.1.tgz#71616bdccbe25e27a54439e0046e89ca76df2248"
integrity sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==
acorn@^8.12.0, acorn@^8.14.0, acorn@^8.8.2:
version "8.14.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.14.0.tgz#063e2c70cac5fb4f6467f0b11152e04c682795b0"
integrity sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA==
aggregate-error@^3.0.0:
version "3.1.0"
@ -5035,8 +5030,7 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w==
"prettier-2@npm:prettier@^2", prettier@^2.0.5:
name prettier-2
"prettier-2@npm:prettier@^2":
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
@ -5048,6 +5042,11 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"
prettier@^2.0.5:
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
prettier@^3.2.1:
version "3.3.3"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105"