Skip to content

Commit

Permalink
assign class property keys to the correct top level symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jun 21, 2021
1 parent 4a3fe2b commit 4e608c8
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 8 deletions.
13 changes: 10 additions & 3 deletions lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,18 @@ class JavascriptParser extends Parser {
/** @type {SyncBailHook<[IfStatementNode], boolean | void>} */
statementIf: new SyncBailHook(["statement"]),
/** @type {SyncBailHook<[ExpressionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */
classExtendsExpression: new SyncBailHook(["expression", "statement"]),
classExtendsExpression: new SyncBailHook([
"expression",
"classDefinition"
]),
/** @type {SyncBailHook<[MethodDefinitionNode | PropertyDefinitionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */
classBodyElement: new SyncBailHook(["element", "statement"]),
classBodyElement: new SyncBailHook(["element", "classDefinition"]),
/** @type {SyncBailHook<[ExpressionNode, MethodDefinitionNode | PropertyDefinitionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */
classBodyValue: new SyncBailHook(["expression", "element", "statement"]),
classBodyValue: new SyncBailHook([
"expression",
"element",
"classDefinition"
]),
/** @type {HookMap<SyncBailHook<[LabeledStatementNode], boolean | void>>} */
label: new HookMap(() => new SyncBailHook(["statement"])),
/** @type {SyncBailHook<[StatementNode, ImportSource], boolean | void>} */
Expand Down
35 changes: 33 additions & 2 deletions lib/optimize/InnerGraphPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,25 @@ class InnerGraphPlugin {
}
);

parser.hooks.classBodyElement.tap(
"InnerGraphPlugin",
(element, classDefinition) => {
if (!InnerGraph.isEnabled(parser.state)) return;
if (parser.scope.topLevelScope === true) {
const fn = classWithTopLevelSymbol.get(classDefinition);
if (fn) {
InnerGraph.setTopLevelSymbol(parser.state, undefined);
}
}
}
);

parser.hooks.classBodyValue.tap(
"InnerGraphPlugin",
(expression, element, statement) => {
(expression, element, classDefinition) => {
if (!InnerGraph.isEnabled(parser.state)) return;
if (parser.scope.topLevelScope === true) {
const fn = classWithTopLevelSymbol.get(statement);
const fn = classWithTopLevelSymbol.get(classDefinition);
if (fn) {
if (
!element.static ||
Expand All @@ -253,6 +266,24 @@ class InnerGraphPlugin {
)
) {
InnerGraph.setTopLevelSymbol(parser.state, fn);
if (element.type !== "MethodDefinition" && element.static) {
InnerGraph.onUsage(parser.state, usedByExports => {
switch (usedByExports) {
case undefined:
case true:
return;
default: {
const dep = new PureExpressionDependency(
expression.range
);
dep.loc = expression.loc;
dep.usedByExports = usedByExports;
parser.state.module.addDependency(dep);
break;
}
}
});
}
} else {
InnerGraph.setTopLevelSymbol(parser.state, undefined);
}
Expand Down
2 changes: 1 addition & 1 deletion test/TestCasesProdGlobalUsed.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { describeCases } = require("./TestCases.template");

describe("TestCases", () => {
describe("TestCasesProdGlobalUsed", () => {
describeCases({
name: "production with usedExports global",
mode: "production",
Expand Down
22 changes: 22 additions & 0 deletions test/cases/inner-graph/class-dynamic-props/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
it("should not throw when using dynamic properties in unused classes", () => {
require("./unused1");
});

it("should not throw when using dynamic properties in used classes", () => {
const exports = require("./used1");
const x = new exports.Used();
expect(x.a()).toBe("A");
expect(x.b).toBe("B");
expect(x.c).toBe("C");
expect(exports.Used.d()).toBe("D");
expect(exports.Used.e).toBe("E");
expect(exports.Used.f).toBe("F");
const x2 = new exports.Used2();
expect(x2.a()).toBe("A");
expect(x2.b).toBe("B");
expect(x2.c).toBe("C");
expect(exports.Used2.d()).toBe("D");
expect(exports.Used2.e).toBe("E");
expect(exports.Used2.f).toBe("F");
expect(x2.x).toBe("X");
});
16 changes: 16 additions & 0 deletions test/cases/inner-graph/class-dynamic-props/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const a = () => "a";
export const A = "A";
export const b = "b";
export const B = "B";
export const c = "c";
export const C = "C";
export const d = () => "d";
export const D = "D";
export const e = "e";
export const E = "E";
export const f = "f";
export const F = "F";
export class X {
x = "X";
}
export const y = "y";
37 changes: 37 additions & 0 deletions test/cases/inner-graph/class-dynamic-props/unused1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { a, b, c, d, e, f, A, B, C, D, E, F, X } from "./module";

class Unused {
[a()]() {
return A;
}
[b] = B;
get [c]() {
return C;
}
static [d()]() {
return D;
}
static [e] = E;
static get [f]() {
return F;
}
}

class Unused2 extends X {
[a()]() {
return A;
}
[b] = B;
get [c]() {
return C;
}
static [d()]() {
return D;
}
static [e] = E;
static get [f]() {
return F;
}
}

export {};
37 changes: 37 additions & 0 deletions test/cases/inner-graph/class-dynamic-props/used1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { a, b, c, d, e, f, A, B, C, D, E, F, X } from "./module?1";

class Used {
[a()]() {
return A;
}
[b] = B;
get [c]() {
return C;
}
static [d()]() {
return D;
}
static [e] = E;
static get [f]() {
return F;
}
}

class Used2 extends X {
[a()]() {
return A;
}
[b] = B;
get [c]() {
return C;
}
static [d()]() {
return D;
}
static [e] = E;
static get [f]() {
return F;
}
}

export { Used, Used2 };
4 changes: 2 additions & 2 deletions test/cases/inner-graph/simple/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function f3() {
return EXPORT;
}

const f4 = function() {
const f4 = function () {
return EXPORT;
};

Expand Down Expand Up @@ -95,6 +95,6 @@ export function fWithDefault(r = EXPORT4) {
return r;
}

export default (function() {
export default (function () {
return EXPORT;
});

0 comments on commit 4e608c8

Please sign in to comment.