This commit is contained in:
Ivan Kopeykin 2020-08-16 10:26:14 +03:00 committed by Tobias Koppers
parent bdf4a2b942
commit 1556341c13
4 changed files with 46 additions and 26 deletions

View File

@ -56,11 +56,12 @@ class ImportMetaPlugin {
"ImportMetaPlugin",
toConstantDependency(parser, JSON.stringify("object"))
);
parser.hooks.metaProperty.tap(
"ImportMetaPlugin",
toConstantDependency(parser, "Object()")
);
parser.hooks.metaProperty.tap("ImportMetaPlugin", metaProperty => {
parser.hooks.metaProperty
.for("import.meta")
.tap("ImportMetaPlugin", toConstantDependency(parser, "Object()"));
parser.hooks.metaProperty
.for("import.meta")
.tap("ImportMetaPlugin", metaProperty => {
const CriticalDependencyWarning = getCriticalDependencyWarning();
parser.state.module.addWarning(
new ModuleDependencyWarning(

View File

@ -112,7 +112,7 @@ const getRootName = expression => {
case "ThisExpression":
return "this";
case "MetaProperty":
return "import.meta";
return `${expression.meta.name}.${expression.property.name}`;
default:
return undefined;
}
@ -267,8 +267,8 @@ class JavascriptParser extends Parser {
optionalChaining: new SyncBailHook(["optionalChaining"]),
/** @type {HookMap<SyncBailHook<[ExpressionNode], boolean | void>>} */
new: new HookMap(() => new SyncBailHook(["expression"])),
/** @type {SyncBailHook<[MetaPropertyNode], boolean | void>} */
metaProperty: new SyncBailHook(["metaProperty"]),
/** @type {HookMap<SyncBailHook<[MetaPropertyNode], boolean | void>>} */
metaProperty: new HookMap(() => new SyncBailHook(["metaProperty"])),
/** @type {HookMap<SyncBailHook<[ExpressionNode], boolean | void>>} */
expression: new HookMap(() => new SyncBailHook(["expression"])),
/** @type {HookMap<SyncBailHook<[ExpressionNode, string[]], boolean | void>>} */
@ -963,7 +963,7 @@ class JavascriptParser extends Parser {
return this.callHooksForName(
this.hooks.evaluateIdentifier,
"import.meta",
getRootName(expr),
metaProperty
);
});
@ -2740,7 +2740,7 @@ class JavascriptParser extends Parser {
* @param {MetaPropertyNode} metaProperty meta property
*/
walkMetaProperty(metaProperty) {
this.hooks.metaProperty.call(metaProperty);
this.hooks.metaProperty.for(getRootName(metaProperty)).call(metaProperty);
}
callHooksForExpression(hookMap, expr, ...args) {

View File

@ -0,0 +1,19 @@
class A {
constructor() {
if (new.target === B) {
this.val = 2;
} else {
this.val = 1;
}
}
}
class B extends A {}
it("should respect meta property name", () => {
const b = new B();
const a = new A();
expect(b.val).toBe(2);
expect(a.val).toBe(1);
});

2
types.d.ts vendored
View File

@ -3588,7 +3588,7 @@ declare abstract class JavascriptParser extends Parser {
>;
optionalChaining: SyncBailHook<[ChainExpression], boolean | void>;
new: HookMap<SyncBailHook<[Expression], boolean | void>>;
metaProperty: SyncBailHook<[MetaProperty], boolean | void>;
metaProperty: HookMap<SyncBailHook<[MetaProperty], boolean | void>>;
expression: HookMap<SyncBailHook<[Expression], boolean | void>>;
expressionMemberChain: HookMap<
SyncBailHook<[Expression, string[]], boolean | void>