Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop authored and sokra committed Aug 27, 2020
1 parent bdf4a2b commit 1556341
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
41 changes: 21 additions & 20 deletions lib/dependencies/ImportMetaPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,27 @@ class ImportMetaPlugin {
"ImportMetaPlugin",
toConstantDependency(parser, JSON.stringify("object"))
);
parser.hooks.metaProperty.tap(
"ImportMetaPlugin",
toConstantDependency(parser, "Object()")
);
parser.hooks.metaProperty.tap("ImportMetaPlugin", metaProperty => {
const CriticalDependencyWarning = getCriticalDependencyWarning();
parser.state.module.addWarning(
new ModuleDependencyWarning(
parser.state.module,
new CriticalDependencyWarning(
"Accessing import.meta directly is unsupported (only property access is supported)"
),
metaProperty.loc
)
);
const dep = new ConstDependency("Object()", metaProperty.range);
dep.loc = metaProperty.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
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(
parser.state.module,
new CriticalDependencyWarning(
"Accessing import.meta directly is unsupported (only property access is supported)"
),
metaProperty.loc
)
);
const dep = new ConstDependency("Object()", metaProperty.range);
dep.loc = metaProperty.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
parser.hooks.evaluateTypeof
.for("import.meta")
.tap("ImportMetaPlugin", evaluateToString("object"));
Expand Down
10 changes: 5 additions & 5 deletions lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>>} */
Expand Down Expand Up @@ -963,7 +963,7 @@ class JavascriptParser extends Parser {

return this.callHooksForName(
this.hooks.evaluateIdentifier,
"import.meta",
getRootName(expr),
metaProperty
);
});
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions test/cases/parsing/meta-property/index.js
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 1556341

Please sign in to comment.