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,26 +56,27 @@ class ImportMetaPlugin {
"ImportMetaPlugin", "ImportMetaPlugin",
toConstantDependency(parser, JSON.stringify("object")) toConstantDependency(parser, JSON.stringify("object"))
); );
parser.hooks.metaProperty.tap( parser.hooks.metaProperty
"ImportMetaPlugin", .for("import.meta")
toConstantDependency(parser, "Object()") .tap("ImportMetaPlugin", toConstantDependency(parser, "Object()"));
); parser.hooks.metaProperty
parser.hooks.metaProperty.tap("ImportMetaPlugin", metaProperty => { .for("import.meta")
const CriticalDependencyWarning = getCriticalDependencyWarning(); .tap("ImportMetaPlugin", metaProperty => {
parser.state.module.addWarning( const CriticalDependencyWarning = getCriticalDependencyWarning();
new ModuleDependencyWarning( parser.state.module.addWarning(
parser.state.module, new ModuleDependencyWarning(
new CriticalDependencyWarning( parser.state.module,
"Accessing import.meta directly is unsupported (only property access is supported)" new CriticalDependencyWarning(
), "Accessing import.meta directly is unsupported (only property access is supported)"
metaProperty.loc ),
) metaProperty.loc
); )
const dep = new ConstDependency("Object()", metaProperty.range); );
dep.loc = metaProperty.loc; const dep = new ConstDependency("Object()", metaProperty.range);
parser.state.module.addPresentationalDependency(dep); dep.loc = metaProperty.loc;
return true; parser.state.module.addPresentationalDependency(dep);
}); return true;
});
parser.hooks.evaluateTypeof parser.hooks.evaluateTypeof
.for("import.meta") .for("import.meta")
.tap("ImportMetaPlugin", evaluateToString("object")); .tap("ImportMetaPlugin", evaluateToString("object"));

View File

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