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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix define property in constructor parameter. #89

Merged
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
4 changes: 2 additions & 2 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@

{
name: '@cocos/ccbuild|module-overrides',
resolveId(source, importer): string | null {

Check warning on line 277 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

'importer' is defined but never used
if (moduleOverrides[source]) {
return source;
} else {
Expand Down Expand Up @@ -352,8 +352,6 @@
tsTransformers.push(warningPrinterTransformer(program, config));
}

tsTransformers.push(exportControllerTransformer(program, { context, statsQuery }));

if (inlineEnum) {
const enumData = getEnumData();
if (enumData) {
Expand All @@ -363,6 +361,8 @@
}
}

tsTransformers.push(exportControllerTransformer(program, { context, statsQuery }));

if (mangleProperties) {
const config: Partial<IMinifierOptions> = {};
if (typeof mangleProperties === 'object') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,17 @@
}
}

const parentName = (node.parent as any).name?.escapedText;
let parentName: ts.__String | undefined;
if (ts.isParameter(node)
&& node.parent
&& node.parent.kind === ts.SyntaxKind.Constructor
&& node.parent.parent
&& ts.isClassDeclaration(node.parent.parent)
) {
parentName = node.parent.parent.name?.escapedText;
} else {
parentName = (node.parent as any).name?.escapedText;

Check warning on line 289 in modules/build-engine/src/engine-js/ts-plugins/properties-minifier/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
}
if (!parentName) return isPrivate;

const name = node.name.getText();
Expand Down Expand Up @@ -396,6 +406,14 @@
if (!ret && this._options.mangleGetterSetter) {
ret = ts.isGetAccessor(node) || ts.isSetAccessor(node);
}
if (!ret) {
if (ts.isParameter(node) && node.parent.kind === ts.SyntaxKind.Constructor && node.getChildCount() > 0) {
const modifiers = ts.getModifiers(node);
if (modifiers && modifiers.length > 0) {
ret = modifiers.some((mod: ts.Modifier) => mod.kind === ts.SyntaxKind.PublicKeyword || mod.kind === ts.SyntaxKind.ProtectedKeyword || mod.kind === ts.SyntaxKind.PrivateKeyword);
}
}
}
return ret;
}

Expand Down Expand Up @@ -526,7 +544,7 @@
return false;
}

private isIdentifierInArrayLiteralExpression(node: ts.Node, program: ts.Program): node is ts.Identifier {

Check warning on line 547 in modules/build-engine/src/engine-js/ts-plugins/properties-minifier/index.ts

View workflow job for this annotation

GitHub Actions / test

'program' is defined but never used
if (!ts.isIdentifier(node)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/ccbuild",
"version": "2.3.13",
"version": "2.3.14",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
30 changes: 29 additions & 1 deletion test/build-engine/__snapshots__/engine-js.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,36 @@ function testMangleQuestionProperties() {
console.log((_a$_ccprivate$questio5 = a["_ccprivate$questionTestA"]) == null ? void 0 : _a$_ccprivate$questio5["_ccprivate$questionTestC"]["_ccprivate$myQuestionTest"]);
}
testMangleQuestionProperties();
class DefinePropertiesInConstructorParameters {
constructor(_ccprivate$depthTest = true, _ccprivate$depthWrite = true, _ccprivate$stencilReadMaskFront = 0xffff, _ccprivate$stencilWriteMaskFront = 0xffff) {
this._ccprivate$depthTest = _ccprivate$depthTest;
this._ccprivate$depthWrite = _ccprivate$depthWrite;
this._ccprivate$stencilReadMaskFront = _ccprivate$stencilReadMaskFront;
this._ccprivate$stencilWriteMaskFront = _ccprivate$stencilWriteMaskFront;
}
_ccprivate$test() {
this._ccprivate$depthTest = false;
this._ccprivate$depthWrite = false;
this._ccprivate$stencilReadMaskFront = 0;
this._ccprivate$stencilWriteMaskFront = 0;
}
}
class DefinePropertiesInConstructorParameters2 {
constructor(depthTest = true, _ccprivate$depthWrite = true, stencilReadMaskFront = 0xffff, stencilWriteMaskFront = 0xffff) {
this.depthTest = depthTest;
this._ccprivate$depthWrite = _ccprivate$depthWrite;
this.stencilReadMaskFront = stencilReadMaskFront;
this.stencilWriteMaskFront = stencilWriteMaskFront;
}
test() {
this.depthTest = false;
this._ccprivate$depthWrite = false;
this.stencilReadMaskFront = 0;
this.stencilWriteMaskFront = 0;
}
}

export { ManglePrivatePropertiesTest, ManglePropertyBase, ManglePropertyGrand, MangleTestConstEnum, MangleTestMangleWholeClassIncludingStatic, MangleTestMyBaseEnum, MangleTestMyEnum, MangleTestMyStringEnum, MangleWholeClass, MangleWholeClassBase, MyClassExtendsMangleWholeClass, testMangleQuestionProperties };
export { DefinePropertiesInConstructorParameters, DefinePropertiesInConstructorParameters2, ManglePrivatePropertiesTest, ManglePropertyBase, ManglePropertyGrand, MangleTestConstEnum, MangleTestMangleWholeClassIncludingStatic, MangleTestMyBaseEnum, MangleTestMyEnum, MangleTestMyStringEnum, MangleWholeClass, MangleWholeClassBase, MyClassExtendsMangleWholeClass, testMangleQuestionProperties };
"
`;

Expand Down
19 changes: 19 additions & 0 deletions test/dts-bundler/__snapshots__/dts-bundler.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,25 @@ exports[`bundle dts: cc.d.ts content 1`] = `
export interface IMangleQuestionTestB {
questionTestA: IMangleQuestionTestA | null;
}
/** @mangle */
export class DefinePropertiesInConstructorParameters {
depthTest: boolean;
depthWrite: boolean;
stencilReadMaskFront: number;
stencilWriteMaskFront: number;
constructor(depthTest?: boolean, depthWrite?: boolean, stencilReadMaskFront?: number, stencilWriteMaskFront?: number);
test(): void;
}
export class DefinePropertiesInConstructorParameters2 {
depthTest: boolean;
/** @mangle */
depthWrite: boolean;
stencilReadMaskFront: number;
stencilWriteMaskFront: number;
constructor(depthTest?: boolean, /** @mangle */
depthWrite?: boolean, stencilReadMaskFront?: number, stencilWriteMaskFront?: number);
test(): void;
}
export interface IMangleGrand {
helloGrandDontMangle1(): void;
/** @mangle */
Expand Down
36 changes: 35 additions & 1 deletion test/test-engine-source/cocos/mangle-private-properties-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IMangleGrand, IMangleTest, IWebGLBindingMapping, IWebGLGPUShader, IWebGLGPUTexture, IWebGLGPUTexture2, IWebGLGPUTexture3, IWebGLGPUTexture4, ManglePropertyBase, ManglePropertyGrand, MangleTestMyBaseEnum, MangleWholeClass, MyClassExtendsMangleWholeClass } from './mangle-private-base';

const dontmangle: PropertyDecorator = function (target, propertyKey) {};

Check warning on line 3 in test/test-engine-source/cocos/mangle-private-properties-test.ts

View workflow job for this annotation

GitHub Actions / test

'target' is defined but never used

Check warning on line 3 in test/test-engine-source/cocos/mangle-private-properties-test.ts

View workflow job for this annotation

GitHub Actions / test

'propertyKey' is defined but never used

export * from './mangle-private-base';

Expand Down Expand Up @@ -929,7 +929,7 @@
public _myProp1: number = 123;
public getMyProp1(): number { return this._myProp1; }
public static _timers = [];
public static getTimers(): any[] { return MangleTestMangleWholeClassIncludingStatic._timers; }

Check warning on line 932 in test/test-engine-source/cocos/mangle-private-properties-test.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
}

/** @mangle */
Expand Down Expand Up @@ -967,4 +967,38 @@
console.log(a['questionTestA']?.['questionTestC']['myQuestionTest']);
}

testMangleQuestionProperties();
testMangleQuestionProperties();

/** @mangle */
export class DefinePropertiesInConstructorParameters {
constructor(
public depthTest: boolean = true,
public depthWrite: boolean = true,
public stencilReadMaskFront: number = 0xffff,
public stencilWriteMaskFront: number = 0xffff,
) {}

test (): void {
this.depthTest = false;
this.depthWrite = false;
this.stencilReadMaskFront = 0;
this.stencilWriteMaskFront = 0;
}
}

export class DefinePropertiesInConstructorParameters2 {
constructor(
public depthTest: boolean = true,
/** @mangle */
public depthWrite: boolean = true,
public stencilReadMaskFront: number = 0xffff,
public stencilWriteMaskFront: number = 0xffff,
) {}

test (): void {
this.depthTest = false;
this.depthWrite = false;
this.stencilReadMaskFront = 0;
this.stencilWriteMaskFront = 0;
}
}