Skip to content

Commit

Permalink
[3.8.5] Fix inline enum for UnaryExpression and add unit test. (#68)
Browse files Browse the repository at this point in the history
* Fix inline enum for UnaryExpression and add unit test.

* preventAssignment: true

* Inline enum should not handle the overrode module.

* Update version to 2.2.15

* update dts-bundler snapshot.
  • Loading branch information
dumganhar authored Oct 12, 2024
1 parent 01cb556 commit ac2b20a
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 8 deletions.
1 change: 1 addition & 0 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro

const inlineEnumPlugins = await rpInlineEnum({
scanDir: ps.join(engineRoot, 'cocos'),
moduleOverrides,
// exclude: ['*.jsb.ts'],
// scanPattern: '**/*.{cts,mts,ts,tsx}'
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async function handleOneTsEnum(info: {
resolveValue(argNode.left, (left: string | number): void => {
resolveValue(
argNode.right, (right: string | number): void => {
const exp = `${left}${argNode.operator}${right}`;
const exp = `${node.operator}(${left}${argNode.operator}${right})`;
value = evaluate(exp);
cb(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export interface Options {
* The pattern used to match enum files.
* @default '**\/*.{cts,mts,ts,tsx}'
*/
scanPattern?: string | string[]
scanPattern?: string | string[],

moduleOverrides?: Record<string, string> | null,
}

type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
Expand All @@ -49,5 +51,6 @@ export function resolveOptions(options: Options): OptionsResolved {
scanMode: options.scanMode || 'fs',
scanDir: options.scanDir || process.cwd(),
scanPattern: options.scanPattern || '**/*.{cts,mts,ts,tsx}',
moduleOverrides: options.moduleOverrides || null,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function rpInlineEnum(rawOptions: Options, meta?: any): Promise<rol
exclude: options.exclude,
values: strDefines,
delimiters: ['(?<!\\.)\\b', '\\b(?!\\.)'],
preventAssignment: true,
},
// meta,
);
Expand All @@ -63,12 +64,17 @@ export async function rpInlineEnum(rawOptions: Options, meta?: any): Promise<rol
return filter(source) ? source : null;
},

transform(this, code, id): rollup.TransformResult {
transform(this, code: string, moduleId: string): rollup.TransformResult {
// Don't transform a module that is overrode
if (options.moduleOverrides && (moduleId in options.moduleOverrides)) {
return;
}

let s: MagicString | undefined;

if (id in declarations) {
if (moduleId in declarations) {
s ||= new MagicString(code);
for (const declaration of declarations[id]) {
for (const declaration of declarations[moduleId]) {
const {
range: [start, end],
id,
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.2.14",
"version": "2.2.15",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
12 changes: 12 additions & 0 deletions test/build-engine/__snapshots__/engine-js.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,18 @@ exports[`engine-js inline enum 2`] = `
console.log(1026);
console.log(123213);
console.log(123214);
var TileFlag = exports("TileFlag", {
"HORIZONTAL": 2147483648,
"VERTICAL": 1073741824,
"DIAGONAL": 536870912,
"FLIPPED_ALL": 4026531840,
"FLIPPED_MASK": 268435455
});
console.log(2147483648);
console.log(1073741824);
console.log(536870912);
console.log(4026531840);
console.log(268435455);
var MyEnum2 = exports("MyEnum2", {
"AAABBB": 0,
Expand Down
64 changes: 64 additions & 0 deletions test/dts-bundler/__snapshots__/dts-bundler.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ exports[`bundle dts: cc.d.ts content 1`] = `
RGB_ETC2 = 123213,
RGBA_ETC2 = 123214
}
export enum TileFlag {
/**
* @property HORIZONTAL
* @type {Number}
* @static
*/
HORIZONTAL = 2147483648,
/**
* @property VERTICAL
* @type {Number}
* @static
*/
VERTICAL = 1073741824,
/**
* @property DIAGONAL
* @type {Number}
* @static
*/
DIAGONAL = 536870912,
/**
* @property FLIPPED_ALL
* @type {Number}
* @static
*/
FLIPPED_ALL = 4026531840,
/**
* @property FLIPPED_MASK
* @type {Number}
* @static
*/
FLIPPED_MASK = 268435455
}
export const str = "intrinsic flag is false";
export function testEnv(): string;
export function testInternalExport(): string;
Expand Down Expand Up @@ -183,6 +215,38 @@ exports[`bundle dts: cc.d.ts content 1`] = `
RGB_ETC2 = 123213,
RGBA_ETC2 = 123214
}
export enum TileFlag {
/**
* @property HORIZONTAL
* @type {Number}
* @static
*/
HORIZONTAL = 2147483648,
/**
* @property VERTICAL
* @type {Number}
* @static
*/
VERTICAL = 1073741824,
/**
* @property DIAGONAL
* @type {Number}
* @static
*/
DIAGONAL = 536870912,
/**
* @property FLIPPED_ALL
* @type {Number}
* @static
*/
FLIPPED_ALL = 4026531840,
/**
* @property FLIPPED_MASK
* @type {Number}
* @static
*/
FLIPPED_MASK = 268435455
}
}
}
export { db as c } from "cc";
Expand Down
44 changes: 44 additions & 0 deletions test/test-engine-source/cocos/enums/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,47 @@ console.log(PixelFormat.RGB_ETC1);
console.log(PixelFormat.RGBA_ETC1);
console.log(PixelFormat.RGB_ETC2);
console.log(PixelFormat.RGBA_ETC2);


export enum TileFlag {
/**
* @property HORIZONTAL
* @type {Number}
* @static
*/
HORIZONTAL = 0x80000000,

/**
* @property VERTICAL
* @type {Number}
* @static
*/
VERTICAL = 0x40000000,

/**
* @property DIAGONAL
* @type {Number}
* @static
*/
DIAGONAL = 0x20000000,

/**
* @property FLIPPED_ALL
* @type {Number}
* @static
*/
FLIPPED_ALL = (0x80000000 | 0x40000000 | 0x20000000 | 0x10000000) >>> 0,

/**
* @property FLIPPED_MASK
* @type {Number}
* @static
*/
FLIPPED_MASK = (~(0x80000000 | 0x40000000 | 0x20000000 | 0x10000000)) >>> 0
}

console.log(TileFlag.HORIZONTAL);
console.log(TileFlag.VERTICAL);
console.log(TileFlag.DIAGONAL);
console.log(TileFlag.FLIPPED_ALL);
console.log(TileFlag.FLIPPED_MASK);

0 comments on commit ac2b20a

Please sign in to comment.