6
6
"use strict" ;
7
7
8
8
const HotModuleReplacementPlugin = require ( "../HotModuleReplacementPlugin" ) ;
9
+ const { getImportAttributes } = require ( "../javascript/JavascriptParser" ) ;
9
10
const InnerGraph = require ( "../optimize/InnerGraph" ) ;
10
11
const ConstDependency = require ( "./ConstDependency" ) ;
11
12
const HarmonyAcceptDependency = require ( "./HarmonyAcceptDependency" ) ;
@@ -16,11 +17,7 @@ const { ExportPresenceModes } = require("./HarmonyImportDependency");
16
17
const HarmonyImportSideEffectDependency = require ( "./HarmonyImportSideEffectDependency" ) ;
17
18
const HarmonyImportSpecifierDependency = require ( "./HarmonyImportSpecifierDependency" ) ;
18
19
19
- /** @typedef {import("estree").ExportAllDeclaration } ExportAllDeclaration */
20
- /** @typedef {import("estree").ExportNamedDeclaration } ExportNamedDeclaration */
21
20
/** @typedef {import("estree").Identifier } Identifier */
22
- /** @typedef {import("estree").ImportDeclaration } ImportDeclaration */
23
- /** @typedef {import("estree").ImportExpression } ImportExpression */
24
21
/** @typedef {import("estree").Literal } Literal */
25
22
/** @typedef {import("estree").MemberExpression } MemberExpression */
26
23
/** @typedef {import("estree").ObjectExpression } ObjectExpression */
@@ -30,7 +27,11 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend
30
27
/** @typedef {import("../javascript/BasicEvaluatedExpression") } BasicEvaluatedExpression */
31
28
/** @typedef {import("../javascript/JavascriptParser") } JavascriptParser */
32
29
/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty } DestructuringAssignmentProperty */
30
+ /** @typedef {import("../javascript/JavascriptParser").ExportAllDeclaration } ExportAllDeclaration */
31
+ /** @typedef {import("../javascript/JavascriptParser").ExportNamedDeclaration } ExportNamedDeclaration */
33
32
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes } ImportAttributes */
33
+ /** @typedef {import("../javascript/JavascriptParser").ImportDeclaration } ImportDeclaration */
34
+ /** @typedef {import("../javascript/JavascriptParser").ImportExpression } ImportExpression */
34
35
/** @typedef {import("../javascript/JavascriptParser").Range } Range */
35
36
/** @typedef {import("../optimize/InnerGraph").InnerGraph } InnerGraph */
36
37
/** @typedef {import("../optimize/InnerGraph").TopLevelSymbol } TopLevelSymbol */
@@ -48,73 +49,6 @@ const harmonySpecifierTag = Symbol("harmony import");
48
49
* @property {Record<string, any> | undefined } assertions
49
50
*/
50
51
51
- /**
52
- * @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | (ImportExpression & { arguments?: ObjectExpression[] }) } node node with assertions
53
- * @returns {ImportAttributes | undefined } import attributes
54
- */
55
- function getAttributes ( node ) {
56
- if (
57
- node . type === "ImportExpression" &&
58
- node . arguments &&
59
- node . arguments [ 0 ] &&
60
- node . arguments [ 0 ] . type === "ObjectExpression" &&
61
- node . arguments [ 0 ] . properties [ 0 ] &&
62
- node . arguments [ 0 ] . properties [ 0 ] . type === "Property" &&
63
- node . arguments [ 0 ] . properties [ 0 ] . value . type === "ObjectExpression" &&
64
- node . arguments [ 0 ] . properties [ 0 ] . value . properties
65
- ) {
66
- const properties =
67
- /** @type {Property[] } */
68
- ( node . arguments [ 0 ] . properties [ 0 ] . value . properties ) ;
69
- const result = /** @type {ImportAttributes } */ ( { } ) ;
70
- for ( const property of properties ) {
71
- const key =
72
- /** @type {string } */
73
- (
74
- property . key . type === "Identifier"
75
- ? property . key . name
76
- : /** @type {Literal } */ ( property . key ) . value
77
- ) ;
78
- result [ key ] =
79
- /** @type {string } */
80
- ( /** @type {Literal } */ ( property . value ) . value ) ;
81
- }
82
- const key =
83
- node . arguments [ 0 ] . properties [ 0 ] . key . type === "Identifier"
84
- ? node . arguments [ 0 ] . properties [ 0 ] . key . name
85
- : /** @type {Literal } */ ( node . arguments [ 0 ] . properties [ 0 ] . key ) . value ;
86
- if ( key === "assert" ) {
87
- result . _isLegacyAssert = true ;
88
- }
89
- return result ;
90
- }
91
- // TODO remove cast when @types /estree has been updated to import assertions
92
- const isImportAssertion =
93
- /** @type {{ assertions?: ImportAttributeNode[] } } */ ( node ) . assertions !==
94
- undefined ;
95
- const attributes = isImportAssertion
96
- ? /** @type {{ assertions?: ImportAttributeNode[] } } */ ( node ) . assertions
97
- : /** @type {{ attributes?: ImportAttributeNode[] } } */ ( node ) . attributes ;
98
- if ( attributes === undefined ) {
99
- return ;
100
- }
101
- const result = /** @type {ImportAttributes } */ ( { } ) ;
102
- for ( const attribute of attributes ) {
103
- const key =
104
- /** @type {string } */
105
- (
106
- attribute . key . type === "Identifier"
107
- ? attribute . key . name
108
- : attribute . key . value
109
- ) ;
110
- result [ key ] = /** @type {string } */ ( attribute . value . value ) ;
111
- }
112
- if ( isImportAssertion ) {
113
- result . _isLegacyAssert = true ;
114
- }
115
- return result ;
116
- }
117
-
118
52
module . exports = class HarmonyImportDependencyParserPlugin {
119
53
/**
120
54
* @param {JavascriptParserOptions } options options
@@ -184,7 +118,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
184
118
clearDep . loc = /** @type {DependencyLocation } */ ( statement . loc ) ;
185
119
parser . state . module . addPresentationalDependency ( clearDep ) ;
186
120
parser . unsetAsiPosition ( /** @type {Range } */ ( statement . range ) [ 1 ] ) ;
187
- const attributes = getAttributes ( statement ) ;
121
+ const attributes = getImportAttributes ( statement ) ;
188
122
const sideEffectDep = new HarmonyImportSideEffectDependency (
189
123
/** @type {string } */ ( source ) ,
190
124
parser . state . lastHarmonyImportOrder ,
@@ -204,7 +138,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
204
138
source,
205
139
ids,
206
140
sourceOrder : parser . state . lastHarmonyImportOrder ,
207
- assertions : getAttributes ( statement )
141
+ assertions : getImportAttributes ( statement )
208
142
} ) ;
209
143
return true ;
210
144
}
@@ -434,6 +368,3 @@ module.exports = class HarmonyImportDependencyParserPlugin {
434
368
} ;
435
369
436
370
module . exports . harmonySpecifierTag = harmonySpecifierTag ;
437
- // TODO remove it in webpack@6 in favor getAttributes
438
- module . exports . getAssertions = getAttributes ;
439
- module . exports . getAttributes = getAttributes ;
0 commit comments