Skip to content

Commit ca154c5

Browse files
fix: correctly parsing string export and import
2 parents e20fd63 + c59a232 commit ca154c5

File tree

11 files changed

+94
-53
lines changed

11 files changed

+94
-53
lines changed

lib/dependencies/CommonJsImportsParserPlugin.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,9 @@ class CommonJsImportsParserPlugin {
590590
data: { context },
591591
next: undefined
592592
});
593+
593594
return new BasicEvaluatedExpression()
594-
.setIdentifier(
595-
/** @type {TODO} */ (ident),
596-
/** @type {TODO} */ (ident),
597-
() => []
598-
)
595+
.setIdentifier(ident, ident, () => [])
599596
.setSideEffects(false)
600597
.setRange(/** @type {Range} */ (expr.range));
601598
});

lib/javascript/JavascriptParser.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
9494
*/
9595
/** @typedef {import("../Parser").ParserState} ParserState */
9696
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
97-
/** @typedef {{declaredScope: ScopeInfo, freeName: string | true, tagInfo: TagInfo | undefined}} VariableInfoInterface */
97+
/** @typedef {{declaredScope: ScopeInfo, freeName: string | true | undefined, tagInfo: TagInfo | undefined}} VariableInfoInterface */
9898
/** @typedef {{ name: string | VariableInfo, rootInfo: string | VariableInfo, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[] }} GetInfoResult */
9999
/** @typedef {Statement | ModuleDeclaration | Expression} StatementPathItem */
100100
/** @typedef {TODO} OnIdent */
@@ -248,7 +248,7 @@ class JavascriptParser extends Parser {
248248
this.hooks = Object.freeze({
249249
/** @type {HookMap<SyncBailHook<[UnaryExpression], BasicEvaluatedExpression | undefined | null>>} */
250250
evaluateTypeof: new HookMap(() => new SyncBailHook(["expression"])),
251-
/** @type {HookMap<SyncBailHook<[Expression | SpreadElement], BasicEvaluatedExpression | undefined | null>>} */
251+
/** @type {HookMap<SyncBailHook<[Expression | SpreadElement | PrivateIdentifier], BasicEvaluatedExpression | undefined | null>>} */
252252
evaluate: new HookMap(() => new SyncBailHook(["expression"])),
253253
/** @type {HookMap<SyncBailHook<[Identifier | ThisExpression | MemberExpression | MetaProperty], BasicEvaluatedExpression | undefined | null>>} */
254254
evaluateIdentifier: new HookMap(() => new SyncBailHook(["expression"])),
@@ -1211,7 +1211,7 @@ class JavascriptParser extends Parser {
12111211
}
12121212
});
12131213
/**
1214-
* @param {string} exprType expression type name
1214+
* @param {"Identifier" | "ThisExpression" | "MemberExpression"} exprType expression type name
12151215
* @param {function(Expression | SpreadElement): GetInfoResult | undefined} getInfo get info
12161216
* @returns {void}
12171217
*/
@@ -1221,9 +1221,10 @@ class JavascriptParser extends Parser {
12211221
/** @type {GetInfoResult | undefined} */
12221222
let cachedInfo;
12231223
this.hooks.evaluate.for(exprType).tap("JavascriptParser", expr => {
1224-
const expression = /** @type {MemberExpression} */ (expr);
1224+
const expression =
1225+
/** @type {Identifier | ThisExpression | MemberExpression} */ (expr);
12251226

1226-
const info = getInfo(expr);
1227+
const info = getInfo(expression);
12271228
if (info !== undefined) {
12281229
return this.callHooksForInfoWithFallback(
12291230
this.hooks.evaluateIdentifier,
@@ -1245,7 +1246,11 @@ class JavascriptParser extends Parser {
12451246
this.hooks.evaluate
12461247
.for(exprType)
12471248
.tap({ name: "JavascriptParser", stage: 100 }, expr => {
1248-
const info = cachedExpression === expr ? cachedInfo : getInfo(expr);
1249+
const expression =
1250+
/** @type {Identifier | ThisExpression | MemberExpression} */
1251+
(expr);
1252+
const info =
1253+
cachedExpression === expression ? cachedInfo : getInfo(expression);
12491254
if (info !== undefined) {
12501255
return new BasicEvaluatedExpression()
12511256
.setIdentifier(
@@ -1255,7 +1260,7 @@ class JavascriptParser extends Parser {
12551260
info.getMembersOptionals,
12561261
info.getMemberRanges
12571262
)
1258-
.setRange(/** @type {Range} */ (expr.range));
1263+
.setRange(/** @type {Range} */ (expression.range));
12591264
}
12601265
});
12611266
this.hooks.finish.tap("JavascriptParser", () => {
@@ -1298,7 +1303,7 @@ class JavascriptParser extends Parser {
12981303

12991304
return this.callHooksForName(
13001305
this.hooks.evaluateIdentifier,
1301-
getRootName(expr),
1306+
getRootName(metaProperty),
13021307
metaProperty
13031308
);
13041309
});
@@ -2365,12 +2370,13 @@ class JavascriptParser extends Parser {
23652370
!this.hooks.importSpecifier.call(
23662371
statement,
23672372
source,
2368-
specifier.imported.name ||
2369-
// eslint-disable-next-line no-warning-comments
2370-
// @ts-ignore
2371-
// Old version of acorn used it
2372-
// TODO drop it in webpack@6
2373-
specifier.imported.value,
2373+
/** @type {Identifier} */
2374+
(specifier.imported).name ||
2375+
/** @type {string} */
2376+
(
2377+
/** @type {Literal} */
2378+
(specifier.imported).value
2379+
),
23742380
name
23752381
)
23762382
) {
@@ -2446,25 +2452,28 @@ class JavascriptParser extends Parser {
24462452
const specifier = statement.specifiers[specifierIndex];
24472453
switch (specifier.type) {
24482454
case "ExportSpecifier": {
2455+
const localName =
2456+
/** @type {Identifier} */ (specifier.local).name ||
2457+
/** @type {string} */ (
2458+
/** @type {Literal} */ (specifier.local).value
2459+
);
24492460
const name =
2450-
specifier.exported.name ||
2451-
// eslint-disable-next-line no-warning-comments
2452-
// @ts-ignore
2453-
// Old version of acorn used it
2454-
// TODO drop it in webpack@6
2455-
specifier.exported.value;
2461+
/** @type {Identifier} */
2462+
(specifier.exported).name ||
2463+
/** @type {string} */
2464+
(/** @type {Literal} */ (specifier.exported).value);
24562465
if (source) {
24572466
this.hooks.exportImportSpecifier.call(
24582467
statement,
24592468
source,
2460-
specifier.local.name,
2469+
localName,
24612470
name,
24622471
specifierIndex
24632472
);
24642473
} else {
24652474
this.hooks.exportSpecifier.call(
24662475
statement,
2467-
specifier.local.name,
2476+
localName,
24682477
name,
24692478
specifierIndex
24702479
);
@@ -2567,7 +2576,12 @@ class JavascriptParser extends Parser {
25672576
*/
25682577
blockPreWalkExportAllDeclaration(statement) {
25692578
const source = /** @type {ImportSource} */ (statement.source.value);
2570-
const name = statement.exported ? statement.exported.name : null;
2579+
const name = statement.exported
2580+
? /** @type {Identifier} */
2581+
(statement.exported).name ||
2582+
/** @type {string} */
2583+
(/** @type {Literal} */ (statement.exported).value)
2584+
: null;
25712585
this.hooks.exportImport.call(statement, source);
25722586
this.hooks.exportImportSpecifier.call(statement, source, null, name, 0);
25732587
}
@@ -4033,7 +4047,7 @@ class JavascriptParser extends Parser {
40334047
}
40344048

40354049
/**
4036-
* @param {Expression | SpreadElement} expression expression node
4050+
* @param {Expression | SpreadElement | PrivateIdentifier} expression expression node
40374051
* @returns {BasicEvaluatedExpression} evaluation result
40384052
*/
40394053
evaluateExpression(expression) {
@@ -4064,7 +4078,7 @@ class JavascriptParser extends Parser {
40644078
case "BinaryExpression":
40654079
if (expression.operator === "+") {
40664080
return (
4067-
this.parseString(expression.left) +
4081+
this.parseString(/** @type {Expression} */ (expression.left)) +
40684082
this.parseString(expression.right)
40694083
);
40704084
}
@@ -4079,13 +4093,16 @@ class JavascriptParser extends Parser {
40794093

40804094
/**
40814095
* @param {Expression} expression expression
4082-
* @returns {TODO} result
4096+
* @returns {{ range: Range, value: string, code: boolean, conditional: TODO }} result
40834097
*/
40844098
parseCalculatedString(expression) {
40854099
switch (expression.type) {
40864100
case "BinaryExpression":
40874101
if (expression.operator === "+") {
4088-
const left = this.parseCalculatedString(expression.left);
4102+
const left = this.parseCalculatedString(
4103+
/** @type {Expression} */
4104+
(expression.left)
4105+
);
40894106
const right = this.parseCalculatedString(expression.right);
40904107
if (left.code) {
40914108
return {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
66
"license": "MIT",
77
"dependencies": {
8-
"@types/estree": "^1.0.5",
8+
"@types/estree": "^1.0.6",
99
"@webassemblyjs/ast": "^1.12.1",
1010
"@webassemblyjs/wasm-edit": "^1.12.1",
1111
"@webassemblyjs/wasm-parser": "^1.12.1",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
let value = 0;
2+
let value2 = 5;
23
const add = () => value++;
34

4-
export { value, add }
5+
export { value, add, value2 as "test name" }

test/cases/parsing/es2022/es2022.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { "\0 add" as add } from './reexport';
1+
import { "\0 add" as add, "string name" as variable } from './reexport';
22

33
export default class Foo {
44
static {
@@ -17,4 +17,8 @@ export default class Foo {
1717
this.#foo();
1818
}
1919
}
20+
21+
static getVar() {
22+
return variable;
23+
}
2024
}

test/cases/parsing/es2022/in.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default class C {
2+
#x;
3+
constructor(x) {
4+
this.#x = x;
5+
}
6+
static getX(obj) {
7+
if (#x in obj) return obj.#x;
8+
9+
return "obj must be an instance of C";
10+
}
11+
}

test/cases/parsing/es2022/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { value, add } from "./counter";
22
import Foo from "./es2022";
3+
import C from "./in";
4+
import { "string name" as alias } from "./name";
35

46
it("should compile and run", () => {
57
new Foo(add);
68
expect(value).toBe(2);
9+
const c = new C(1);
10+
expect(C.getX(c)).toBe(1)
11+
expect(alias).toBe("test")
12+
expect(Foo.getVar()).toBe(5)
713
});

test/cases/parsing/es2022/name.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const variable1 = "test";
2+
3+
export { variable1 as "string name" };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { add as "\0 add" } from "./counter";
1+
export { add as "\0 add", "test name" as "string name" } from "./counter";

types.d.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,10 @@ declare abstract class BasicEvaluatedExpression {
582582
| UpdateExpression
583583
| YieldExpression
584584
| SpreadElement
585+
| PrivateIdentifier
585586
| FunctionDeclaration
586587
| VariableDeclaration
587588
| ClassDeclaration
588-
| PrivateIdentifier
589589
| ExpressionStatement
590590
| BlockStatement
591591
| StaticBlock
@@ -805,10 +805,10 @@ declare abstract class BasicEvaluatedExpression {
805805
| UpdateExpression
806806
| YieldExpression
807807
| SpreadElement
808+
| PrivateIdentifier
808809
| FunctionDeclaration
809810
| VariableDeclaration
810811
| ClassDeclaration
811-
| PrivateIdentifier
812812
| ExpressionStatement
813813
| BlockStatement
814814
| StaticBlock
@@ -5670,6 +5670,7 @@ declare class JavascriptParser extends Parser {
56705670
| UpdateExpression
56715671
| YieldExpression
56725672
| SpreadElement
5673+
| PrivateIdentifier
56735674
],
56745675
undefined | null | BasicEvaluatedExpression
56755676
>
@@ -5732,10 +5733,10 @@ declare class JavascriptParser extends Parser {
57325733
| ThisExpression
57335734
| UpdateExpression
57345735
| YieldExpression
5736+
| PrivateIdentifier
57355737
| FunctionDeclaration
57365738
| VariableDeclaration
57375739
| ClassDeclaration
5738-
| PrivateIdentifier
57395740
),
57405741
number
57415742
],
@@ -6538,9 +6539,15 @@ declare class JavascriptParser extends Parser {
65386539
| UpdateExpression
65396540
| YieldExpression
65406541
| SpreadElement
6542+
| PrivateIdentifier
65416543
): BasicEvaluatedExpression;
65426544
parseString(expression: Expression): string;
6543-
parseCalculatedString(expression: Expression): any;
6545+
parseCalculatedString(expression: Expression): {
6546+
range: [number, number];
6547+
value: string;
6548+
code: boolean;
6549+
conditional: any;
6550+
};
65446551
evaluate(source: string): BasicEvaluatedExpression;
65456552
isPure(
65466553
expr:
@@ -6573,10 +6580,10 @@ declare class JavascriptParser extends Parser {
65736580
| ThisExpression
65746581
| UpdateExpression
65756582
| YieldExpression
6583+
| PrivateIdentifier
65766584
| FunctionDeclaration
65776585
| VariableDeclaration
6578-
| ClassDeclaration
6579-
| PrivateIdentifier,
6586+
| ClassDeclaration,
65806587
commentsStartPos: number
65816588
): boolean;
65826589
getComments(range: [number, number]): Comment[];
@@ -14662,7 +14669,7 @@ declare abstract class VariableInfo {
1466214669
}
1466314670
declare interface VariableInfoInterface {
1466414671
declaredScope: ScopeInfo;
14665-
freeName: string | true;
14672+
freeName?: string | true;
1466614673
tagInfo?: TagInfo;
1466714674
}
1466814675
type WarningFilterItemTypes =

0 commit comments

Comments
 (0)