Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle class name in properties and methods #17233

Merged
merged 18 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 78 additions & 33 deletions lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1600,32 +1600,39 @@ class JavascriptParser extends Parser {
}
}
if (classy.body && classy.body.type === "ClassBody") {
for (const classElement of /** @type {TODO} */ (classy.body.body)) {
if (!this.hooks.classBodyElement.call(classElement, classy)) {
if (classElement.computed && classElement.key) {
this.walkExpression(classElement.key);
}
if (classElement.value) {
if (
!this.hooks.classBodyValue.call(
classElement.value,
classElement,
classy
)
) {
const scopeParams = [];
// Add class name in scope for recursive calls
if (classy.id) {
scopeParams.push(classy.id);
}
this.inClassScope(true, scopeParams, () => {
for (const classElement of /** @type {TODO} */ (classy.body.body)) {
if (!this.hooks.classBodyElement.call(classElement, classy)) {
if (classElement.computed && classElement.key) {
this.walkExpression(classElement.key);
}
if (classElement.value) {
if (
!this.hooks.classBodyValue.call(
classElement.value,
classElement,
classy
)
) {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
this.walkExpression(classElement.value);
this.scope.topLevelScope = wasTopLevel;
}
} else if (classElement.type === "StaticBlock") {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
this.walkExpression(classElement.value);
this.walkBlockStatement(classElement);
this.scope.topLevelScope = wasTopLevel;
}
} else if (classElement.type === "StaticBlock") {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
this.walkBlockStatement(classElement);
this.scope.topLevelScope = wasTopLevel;
}
}
}
});
}
}

Expand Down Expand Up @@ -3561,6 +3568,30 @@ class JavascriptParser extends Parser {
this.scope = oldScope;
}

inClassScope(hasThis, params, fn) {
const oldScope = this.scope;
this.scope = {
topLevelScope: oldScope.topLevelScope,
inTry: false,
inShorthand: false,
isStrict: oldScope.isStrict,
isAsmJs: oldScope.isAsmJs,
definitions: oldScope.definitions.createChild()
};

if (hasThis) {
this.undefineVariable("this");
}

this.enterPatterns(params, (ident, pattern) => {
this.defineVariable(ident);
});

fn();

this.scope = oldScope;
}

inFunctionScope(hasThis, params, fn) {
const oldScope = this.scope;
this.scope = {
Expand Down Expand Up @@ -3932,20 +3963,34 @@ class JavascriptParser extends Parser {
return false;
}
const items =
/** @type {(MethodDefinition | PropertyDefinition)[]} */
/** @type {TODO[]} */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix it late, I am already working on types here, we need rewrite some parts of code

(expr.body.body);
return items.every(
item =>
(!item.computed ||
!item.key ||
this.isPure(item.key, item.range[0])) &&
(!item.static ||
!item.value ||
this.isPure(
item.value,
item.key ? item.key.range[1] : item.range[0]
))
);
return items.every(item => {
if (
item.computed &&
item.key &&
!this.isPure(item.key, item.range[0])
) {
return false;
}

if (
item.static &&
item.value &&
!this.isPure(
item.value,
item.key ? item.key.range[1] : item.range[0]
)
) {
return false;
}

if (item.type === "StaticBlock") {
return false;
}

return true;
});
}

case "FunctionDeclaration":
Expand Down
15 changes: 11 additions & 4 deletions lib/optimize/InnerGraphPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ class InnerGraphPlugin {
if (!InnerGraph.isEnabled(parser.state)) return;

if (parser.scope.topLevelScope === true) {
if (statement.type === "ClassDeclaration") {
if (
statement.type === "ClassDeclaration" &&
parser.isPure(statement, statement.range[0])
) {
const name = statement.id ? statement.id.name : "*default*";
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
classWithTopLevelSymbol.set(statement, fn);
Expand All @@ -131,8 +134,9 @@ class InnerGraphPlugin {
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
const decl = statement.declaration;
if (
decl.type === "ClassExpression" ||
decl.type === "ClassDeclaration"
(decl.type === "ClassExpression" ||
decl.type === "ClassDeclaration") &&
parser.isPure(decl, decl.range[0])
) {
classWithTopLevelSymbol.set(decl, fn);
} else if (parser.isPure(decl, statement.range[0])) {
Expand All @@ -157,7 +161,10 @@ class InnerGraphPlugin {
decl.id.type === "Identifier"
) {
const name = decl.id.name;
if (decl.init.type === "ClassExpression") {
if (
decl.init.type === "ClassExpression" &&
parser.isPure(decl.init, decl.id.range[1])
) {
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
classWithTopLevelSymbol.set(decl.init, fn);
} else if (parser.isPure(decl.init, decl.id.range[1])) {
Expand Down
14 changes: 14 additions & 0 deletions test/cases/inner-graph/extend-class/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import B from "./b.js";
import { A1 } from "./dep1";

export default class A extends B {
constructor() {
super();
}
test() {
super.test();

this.b = new B();
this.a1 = new A1();
}
}
10 changes: 10 additions & 0 deletions test/cases/inner-graph/extend-class/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import A from "./a.js";
import { A1 } from "./dep1";

export default class B {
constructor() {}
test() {
this.a = new A();
this.a2 = new A1();
}
}
4 changes: 4 additions & 0 deletions test/cases/inner-graph/extend-class/dep2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class Z {}
export function mixin1(_class) {return _class}
export function mixin2(_class) {return _class}
export function mixin3(_class) {return _class}
export function mixin4(_class) {return _class}
export function getField() { return "test" }

export const exportsInfoForA = __webpack_exports_info__.A.used;
export const exportsInfoForB = __webpack_exports_info__.B.used;
Expand All @@ -15,3 +17,5 @@ export const exportsInfoForZ = __webpack_exports_info__.Z.used;
export const exportsInfoForMixin1 = __webpack_exports_info__.mixin1.used;
export const exportsInfoForMixin2 = __webpack_exports_info__.mixin2.used;
export const exportsInfoForMixin3 = __webpack_exports_info__.mixin3.used;
export const exportsInfoForMixin4 = __webpack_exports_info__.mixin4.used;
export const exportsInfoForgetField = __webpack_exports_info__.getField.used;
8 changes: 6 additions & 2 deletions test/cases/inner-graph/extend-class/dep3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {mixin1, mixin2, mixin3, A, B, C, Y} from "./dep2";
import {mixin1, mixin2, mixin3, getField, A, B, C, Y, mixin4} from "./dep2";

export const A1 = class A1 extends A {
render() {return new E();}
Expand All @@ -12,7 +12,7 @@ export const C1 = class C1 extends mixin2(Y, /*#__PURE__*/ mixin3(C)) {
render() {return new D();}
};

export class Y1 extends mixin2(Y) {
export class Y1 extends /*#__PURE__*/ mixin2(Y) {
constructor() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use mixin1 inside this class, so we can't drop it, because mixin2 is not pure, sounds like we have a bug before

super();

Expand All @@ -22,5 +22,9 @@ export class Y1 extends mixin2(Y) {
render() {return new D();}
}

export class Bar extends /*#__PURE__*/ mixin4(A) {
[/*#__PURE__*/ getField()] = 12;
}

export class E {}
const D = class D {};
5 changes: 4 additions & 1 deletion test/cases/inner-graph/extend-class/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
exportsInfoForZ,
exportsInfoForMixin1,
exportsInfoForMixin2,
exportsInfoForMixin3
exportsInfoForMixin3,
exportsInfoForMixin4
} from "./dep2";

it("should load modules correctly", () => {
require("./module1");
require("./module2");
require("./module3");
});

if (process.env.NODE_ENV === "production") {
Expand All @@ -31,6 +33,7 @@ it("Z used, inner graph can not determine const usage", () => {
it("Pure super expression should be unused, another used", () => {
if (process.env.NODE_ENV === "production") {
expect(exportsInfoForMixin1).toBe(false);
expect(exportsInfoForMixin4).toBe(false);
}

expect(exportsInfoForMixin2).toBe(true);
Expand Down
3 changes: 3 additions & 0 deletions test/cases/inner-graph/extend-class/module3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import A from "./a.js";
let a = new A();
a.test();
5 changes: 5 additions & 0 deletions test/cases/inner-graph/extend-class/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var supportsClassStaticBlock = require("../../../helpers/supportsClassStaticBlock");

module.exports = function (config) {
return supportsClassStaticBlock();
};
96 changes: 95 additions & 1 deletion test/cases/inner-graph/extend-class2/dep-decl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { A, B, getC, getD, getE, getF } from "./dep2?decl";
import { A, B, getC, getD, getE, getF, Foo } from "./dep2?decl";
import { A3, B3, C3, D3, E3, F3 } from "./dep3?decl";

export class A1 extends A {
Expand Down Expand Up @@ -39,6 +39,100 @@ export class F1 extends getF() {
}
}

function foo(instance) {
return new instance()
}

class Bar extends Foo {
static prop = 42;
static a = foo(this).prop;
static b = foo(Bar).prop;
static c = foo(super.Bar).prop;
static inStatic1;
static inStatic2;
static inStatic3;
static {
this.inStatic1 = new Bar().prop;
this.inStatic2 = new super.Bar().prop;
this.inStatic3 = (new this).prop;
}
}

class BarA extends Foo {
static prop = 42;
static a = foo(this).prop;
}

class BarB extends Foo {
static prop = 42;
static b = foo(Bar).prop;
}

class BarC extends Foo {
static prop = 42;
static c = foo(super.Bar).prop;
}

class BarPA extends Foo {
static prop = 42;
static #a = foo(this).prop;
}

class BarPB extends Foo {
static prop = 42;
static #b = foo(Bar).prop;
}

class BarPC extends Foo {
static prop = 42;
static #c = foo(super.Bar).prop;
}

const ExpressionFoo = class Bar extends Foo {
static prop = 42;
static a = foo(this).prop;
static b = foo(Bar).prop;
static c = foo(super.Bar).prop;
static inStatic1;
static inStatic2;
static inStatic3;
static {
this.inStatic1 = new Bar().prop;
this.inStatic2 = new super.Bar().prop;
this.inStatic3 = (new this).prop;
}
}

export class Baz extends Foo {
static prop = 42;
static a = foo(this).prop;
static b = foo(Bar).prop;
static c = foo(super.Bar).prop;
static inStatic1;
static inStatic2;
static inStatic3;
static {
this.inStatic1 = new Bar().prop;
this.inStatic2 = new super.Bar().prop;
this.inStatic3 = (new this).prop;
}
}

export default class DefaultBar extends Foo {
static prop = 42;
static a = foo(this).prop;
static b = foo(Bar).prop;
static c = foo(super.Bar).prop;
static inStatic1;
static inStatic2;
static inStatic3;
static {
this.inStatic1 = new Bar().prop;
this.inStatic2 = new super.Bar().prop;
this.inStatic3 = (new this).prop;
}
}

export class A2 extends A3 {}
export class B2 extends B3 {}
export class C2 extends C3 {}
Expand Down
2 changes: 2 additions & 0 deletions test/cases/inner-graph/extend-class2/dep2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const getC = () => class C {};
export const getD = () => class D {};
export const getE = () => class E {};
export const getF = () => class F {};
export class Foo { static Bar = Foo; }

export const exportsInfoForA = __webpack_exports_info__.A.used;
export const exportsInfoForB = __webpack_exports_info__.B.used;
export const exportsInfoForC = __webpack_exports_info__.getC.used;
export const exportsInfoForD = __webpack_exports_info__.getD.used;
export const exportsInfoForE = __webpack_exports_info__.getE.used;
export const exportsInfoForF = __webpack_exports_info__.getF.used;
export const exportsInfoForFoo = __webpack_exports_info__.Foo.used;
Loading
Loading