Skip to content

Commit 10438d8

Browse files
committed
fix(plugin-sdk): stabilize tuple-derived API baseline ordering
1 parent 85fd4c9 commit 10438d8

2 files changed

Lines changed: 110 additions & 48 deletions

File tree

‎src/plugin-sdk/api-baseline.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
* Tests the plugin SDK public API baseline.
33
*/
44
import path from "node:path";
5+
import ts from "typescript";
56
import { beforeAll, describe, expect, it } from "vitest";
67
import { publicPluginSdkEntrypoints } from "../../scripts/lib/plugin-sdk-entries.mjs";
78
import {
89
computePluginSdkApiBaselineHashFileContent,
10+
formatPluginSdkApiTypeAlias,
911
listPluginSdkApiBaselineEntrypoints,
1012
normalizePluginSdkApiDeclarationText,
1113
normalizePluginSdkApiSourcePath,
@@ -29,6 +31,31 @@ const TEST_ENTRYPOINTS = [
2931
"sqlite-runtime-testing",
3032
] as const;
3133

34+
function createTupleAliasFixture(tuple: string, warmup: string, prewarm: boolean) {
35+
const fileName = "/plugin-sdk-tuple-fixture.ts";
36+
const source = [
37+
"interface Array<T> { [index: number]: T; readonly length: number }",
38+
"interface ReadonlyArray<T> { readonly [index: number]: T; readonly length: number }",
39+
`type Warmup = ${warmup};`,
40+
`const VALUES = ${tuple};`,
41+
"type Value = (typeof VALUES)[number];",
42+
].join("\n");
43+
const sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.ESNext, true);
44+
const options = { noLib: true, target: ts.ScriptTarget.ESNext };
45+
const host = ts.createCompilerHost(options);
46+
host.fileExists = (candidate) => candidate === fileName;
47+
host.getSourceFile = (candidate) => (candidate === fileName ? sourceFile : undefined);
48+
const checker = ts.createProgram([fileName], options, host).getTypeChecker();
49+
const [warmupAlias, declaration] = sourceFile.statements.filter(ts.isTypeAliasDeclaration);
50+
if (!warmupAlias || !declaration) {
51+
throw new Error("Missing tuple fixture type aliases");
52+
}
53+
if (prewarm) {
54+
checker.getTypeAtLocation(warmupAlias);
55+
}
56+
return { checker, declaration };
57+
}
58+
3259
describe("Plugin SDK API baseline", () => {
3360
let rendered: PluginSdkApiBaselineRender;
3461

@@ -88,6 +115,35 @@ describe("Plugin SDK API baseline", () => {
88115
expect(normalizePluginSdkApiSourcePath(repoRoot, sourcePath)).toBe("src/plugin-sdk/core.ts");
89116
});
90117

118+
it.each([
119+
{
120+
tuple: '["first", "middle", "last", "first"] as const',
121+
warmup: '"last"',
122+
expected: '"first" | "middle" | "last"',
123+
},
124+
{
125+
tuple: "[3, 1, 2] as const",
126+
warmup: "1",
127+
expected: "3 | 1 | 2",
128+
},
129+
])("keeps tuple-derived unions stable across unrelated type discovery", (fixture) => {
130+
const baseline = createTupleAliasFixture(fixture.tuple, fixture.warmup, false);
131+
const prewarmed = createTupleAliasFixture(fixture.tuple, fixture.warmup, true);
132+
const unstable = prewarmed.checker.typeToString(
133+
prewarmed.checker.getTypeAtLocation(prewarmed.declaration),
134+
prewarmed.declaration,
135+
ts.TypeFormatFlags.NoTruncation,
136+
);
137+
138+
expect(unstable).not.toBe(fixture.expected);
139+
expect(formatPluginSdkApiTypeAlias(baseline.checker, baseline.declaration)).toBe(
140+
fixture.expected,
141+
);
142+
expect(formatPluginSdkApiTypeAlias(prewarmed.checker, prewarmed.declaration)).toBe(
143+
fixture.expected,
144+
);
145+
});
146+
91147
it("renders complete declarations for the canonical public entrypoint inventory", () => {
92148
expect(listPluginSdkApiBaselineEntrypoints()).toEqual(publicPluginSdkEntrypoints);
93149

‎src/plugin-sdk/api-baseline.ts‎

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ function createCompilerContext(repoRoot: string, entrypoints: readonly string[])
179179
const program = ts.createProgram(fileNames, parsedConfig.options);
180180
return {
181181
checker: program.getTypeChecker(),
182-
printer: ts.createPrinter({
183-
newLine: ts.NewLineKind.LineFeed,
184-
removeComments: true,
185-
}),
182+
printer: ts.createPrinter({ newLine: ts.NewLineKind.LineFeed, removeComments: true }),
186183
program,
187184
};
188185
}
@@ -232,26 +229,18 @@ function inferExportKind(
232229
}
233230
}
234231

235-
if (symbol.flags & ts.SymbolFlags.Function) {
236-
return "function";
237-
}
238-
if (symbol.flags & ts.SymbolFlags.Class) {
239-
return "class";
240-
}
241-
if (symbol.flags & ts.SymbolFlags.Interface) {
242-
return "interface";
243-
}
244-
if (symbol.flags & ts.SymbolFlags.TypeAlias) {
245-
return "type";
246-
}
247-
if (symbol.flags & ts.SymbolFlags.ConstEnum || symbol.flags & ts.SymbolFlags.RegularEnum) {
248-
return "enum";
249-
}
250-
if (symbol.flags & ts.SymbolFlags.Variable) {
251-
return "variable";
252-
}
253-
if (symbol.flags & ts.SymbolFlags.NamespaceModule || symbol.flags & ts.SymbolFlags.ValueModule) {
254-
return "namespace";
232+
for (const [flag, kind] of [
233+
[ts.SymbolFlags.Function, "function"],
234+
[ts.SymbolFlags.Class, "class"],
235+
[ts.SymbolFlags.Interface, "interface"],
236+
[ts.SymbolFlags.TypeAlias, "type"],
237+
[ts.SymbolFlags.ConstEnum | ts.SymbolFlags.RegularEnum, "enum"],
238+
[ts.SymbolFlags.Variable, "variable"],
239+
[ts.SymbolFlags.NamespaceModule | ts.SymbolFlags.ValueModule, "namespace"],
240+
] as const) {
241+
if (symbol.flags & flag) {
242+
return kind;
243+
}
255244
}
256245
return "unknown";
257246
}
@@ -460,6 +449,36 @@ function printTypeParameters(printer: ts.Printer, declaration: ts.TypeAliasDecla
460449
return `<${parameters.join(", ")}>`;
461450
}
462451

452+
/** Render tuple-derived literal unions in declaration order, independent of compiler traversal. */
453+
export function formatPluginSdkApiTypeAlias(
454+
checker: ts.TypeChecker,
455+
declaration: ts.TypeAliasDeclaration,
456+
): string {
457+
const type = checker.getTypeAtLocation(declaration);
458+
if (
459+
type.isUnion() &&
460+
ts.isIndexedAccessTypeNode(declaration.type) &&
461+
declaration.type.indexType.kind === ts.SyntaxKind.NumberKeyword
462+
) {
463+
const tuple = checker.getTypeFromTypeNode(declaration.type.objectType);
464+
const members = checker.isTupleType(tuple)
465+
? [...new Set(checker.getTypeArguments(tuple as ts.TypeReference))]
466+
: [];
467+
if (
468+
members.length === type.types.length &&
469+
members.every(
470+
(member) =>
471+
(member.isStringLiteral() || member.isNumberLiteral()) && type.types.includes(member),
472+
)
473+
) {
474+
return members
475+
.map((member) => checker.typeToString(member, declaration, DECLARATION_TYPE_FORMAT_FLAGS))
476+
.join(" | ");
477+
}
478+
}
479+
return checker.typeToString(type, declaration, DECLARATION_TYPE_FORMAT_FLAGS);
480+
}
481+
463482
function printNode(
464483
repoRoot: string,
465484
checker: ts.TypeChecker,
@@ -504,15 +523,10 @@ function printNode(
504523
}
505524

506525
if (ts.isTypeAliasDeclaration(declaration)) {
507-
const type = checker.getTypeAtLocation(declaration);
508526
const typeParameters = printTypeParameters(printer, declaration);
509527
return normalizePluginSdkApiDeclarationText(
510528
repoRoot,
511-
`export type ${exportName}${typeParameters} = ${checker.typeToString(
512-
type,
513-
declaration,
514-
DECLARATION_TYPE_FORMAT_FLAGS,
515-
)};`,
529+
`export type ${exportName}${typeParameters} = ${formatPluginSdkApiTypeAlias(checker, declaration)};`,
516530
);
517531
}
518532

@@ -545,20 +559,14 @@ function compareDeclarations(
545559
left: ts.Declaration,
546560
right: ts.Declaration,
547561
): number {
548-
const byPath = compareText(
549-
relativePath(repoRoot, left.getSourceFile().fileName),
550-
relativePath(repoRoot, right.getSourceFile().fileName),
562+
return (
563+
compareText(
564+
relativePath(repoRoot, left.getSourceFile().fileName),
565+
relativePath(repoRoot, right.getSourceFile().fileName),
566+
) ||
567+
left.getStart() - right.getStart() ||
568+
left.kind - right.kind
551569
);
552-
if (byPath !== 0) {
553-
return byPath;
554-
}
555-
556-
const byStart = left.getStart() - right.getStart();
557-
if (byStart !== 0) {
558-
return byStart;
559-
}
560-
561-
return left.kind - right.kind;
562570
}
563571

564572
function buildExportSurface(params: {
@@ -593,11 +601,9 @@ function sortExports(left: PluginSdkApiExport, right: PluginSdkApiExport): numbe
593601
unknown: 8,
594602
};
595603

596-
const byKind = kindRank[left.kind] - kindRank[right.kind];
597-
if (byKind !== 0) {
598-
return byKind;
599-
}
600-
return compareText(left.exportName, right.exportName);
604+
return (
605+
kindRank[left.kind] - kindRank[right.kind] || compareText(left.exportName, right.exportName)
606+
);
601607
}
602608

603609
function buildModuleSurface(params: {

0 commit comments

Comments
 (0)