@@ -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[] } */
110111const 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
183180const 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 */
191188const 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