Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(api): a released const that gained readonly properties keeps its promise

yhhc18b04a33ab6
diff --git a/desktop b/desktop
index a28f4bde9..227965fda 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit a28f4bde95c4e16f35632846ac7017d334609b12
+Subproject commit 227965fda43e69863e9864a963294533bbd0d67f
diff --git a/sdk/tests/api-baseline-rules.test.ts b/sdk/tests/api-baseline-rules.test.ts
index 66e9346c6..01a1d855b 100644
--- a/sdk/tests/api-baseline-rules.test.ts
+++ b/sdk/tests/api-baseline-rules.test.ts
@@ -10,7 +10,7 @@
* union that lost a member must still fail.
*/
import { describe, it, expect } from 'vitest';
-import { isAdditiveMembers, isWidenedUnion } from '../../tools/lib/apiSnapshot.mjs';
+import { isAddedConstMembers, isAdditiveMembers, isWidenedUnion } from '../../tools/lib/apiSnapshot.mjs';
describe('api baseline: what counts as additive', () => {
const A = "'texture' | 'material' | 'font'";
@@ -48,3 +48,27 @@ describe('api baseline: what counts as additive', () => {
expect(isAdditiveMembers('a: string\nb: number | undefined', 'a: string')).toBe(false);
});
});
+
+describe('api baseline: a const that gained properties', () => {
+ const F = '(button: number) => Binding';
+ const G = '((button: number) => Binding) & { readonly Left: 0; readonly Middle: 1; readonly Right: 2; }';
+
+ it('kept its type and gained readonly properties: additive', () => {
+ expect(isAddedConstMembers(F, G)).toBe(true);
+ const fence = (s: string) => '```\n' + s + '\n```';
+ expect(isAddedConstMembers(fence(F), fence(G))).toBe(true);
+ });
+
+ it('changed the type it kept: not', () => {
+ expect(isAddedConstMembers('(button: string) => Binding', G)).toBe(false);
+ });
+
+ it('took the properties away again: not', () => {
+ expect(isAddedConstMembers(G, F)).toBe(false);
+ });
+
+ it('added a writable or a method-shaped member: not', () => {
+ expect(isAddedConstMembers(F, `(${F}) & { Left: 0; }`)).toBe(false);
+ expect(isAddedConstMembers(F, `(${F}) & {}`)).toBe(false);
+ });
+});
diff --git a/tools/lib/apiSnapshot.mjs b/tools/lib/apiSnapshot.mjs
index fba4ffcfc..45e7768c9 100644
--- a/tools/lib/apiSnapshot.mjs
+++ b/tools/lib/apiSnapshot.mjs
@@ -139,6 +139,23 @@ export function isWidenedUnion(before, after) {
return now.length > was.length && was.every((p) => set.has(p));
}
+/**
+ * A const that kept its released type and gained readonly properties beside it:
+ * `F` became `(F) & { readonly A: 0; ... }`. The value is still everything `F`
+ * was, so every use written against the release still compiles; the addition is
+ * a namespace hung on the same value (`MouseButton(0)` and `MouseButton.Left`).
+ * A changed `F`, or anything but readonly properties on the right, stays a failure.
+ */
+export function isAddedConstMembers(before, after) {
+ const body = (s) => s.split('\n').filter((l) => !l.startsWith('```')).join(' ').trim();
+ const was = body(before);
+ const now = body(after);
+ const prefix = `(${was}) & {`;
+ if (!now.startsWith(prefix) || !now.endsWith('}')) return false;
+ const members = now.slice(prefix.length, -1).split(';').map((m) => m.trim()).filter(Boolean);
+ return members.length > 0 && members.every((m) => /^readonly [A-Za-z_$][\w$]*: [^;{}]+$/.test(m));
+}
+
/**
* Compare one entry against its released self. Only @public carries a promise,
* so only @public produces a failure; @beta is noted because "may adjust" should
@@ -168,6 +185,9 @@ export function baselineFindings(was, now) {
} else if (before.kind === 'function'
&& isAddedOptionalParam(promisedBody(before.body), promisedBody(after.body))) {
notes.push(`${name} — @public function gained an optional parameter`);
+ } else if (before.kind === 'const'
+ && isAddedConstMembers(promisedBody(before.body), promisedBody(after.body))) {
+ notes.push(`${name} — @public const gained readonly properties`);
} else if (before.kind === 'class'
&& isAddedClassMembers(promisedBody(before.body), promisedBody(after.body))) {
notes.push(`${name} — @public class gained a member`);