Skip to content

Commit 0d3141e

Browse files
committed
fix(plugin-sdk): stabilize API baseline generation
1 parent 1b4029f commit 0d3141e

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
e0dcff6c108e78cc750a9e43ba72f8345cd488ff6c5988630e223664e862bc57 plugin-sdk-api-baseline.json
2-
950e9d5ebef0d2bac2af2985ae4524a14dcf1baabd7a24060f7b6b278ac7a548 plugin-sdk-api-baseline.jsonl
1+
b2d9c7155f82ff2f54b619b2d751d0bae47ddaae719a8400930e125862244b2d plugin-sdk-api-baseline.json
2+
ec57826b7d9e50fa95959e2f2fd15c324c3d3d571cb3c437c3d8a3ba0d70f6c8 plugin-sdk-api-baseline.jsonl

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

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ function normalizeDeclarationImportSpecifier(repoRoot: string, value: string): s
9898
}
9999

100100
function normalizeDeclarationText(repoRoot: string, value: string): string {
101-
return value.replaceAll(/import\("([^"]+)"\)/g, (match, specifier: string) => {
101+
return value.replaceAll(/import\("([^"]+)"/g, (match, specifier: string) => {
102102
const normalized = normalizeDeclarationImportSpecifier(repoRoot, specifier);
103-
return normalized === specifier ? match : `import("${normalized}")`;
103+
return normalized === specifier ? match : `import("${normalized}"`;
104104
});
105105
}
106106

@@ -116,7 +116,13 @@ function createCompilerContext(repoRoot: string) {
116116
throw new Error(ts.flattenDiagnosticMessageText(configFile.error.messageText, "\n"));
117117
}
118118
const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, repoRoot);
119-
const program = ts.createProgram(parsedConfig.fileNames, parsedConfig.options);
119+
const fileNames = parsedConfig.fileNames.toSorted((left, right) =>
120+
compareText(
121+
relativePath(repoRoot, path.resolve(left)),
122+
relativePath(repoRoot, path.resolve(right)),
123+
),
124+
);
125+
const program = ts.createProgram(fileNames, parsedConfig.options);
120126
return {
121127
checker: program.getTypeChecker(),
122128
printer: ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }),
@@ -199,14 +205,19 @@ function inferExportKind(
199205

200206
function resolveSymbolAndDeclaration(
201207
checker: ts.TypeChecker,
208+
repoRoot: string,
202209
symbol: ts.Symbol,
203210
): {
204211
declaration: ts.Declaration | undefined;
205212
resolvedSymbol: ts.Symbol;
206213
} {
207214
const resolvedSymbol =
208215
symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
209-
const declarations = resolvedSymbol.getDeclarations() ?? symbol.getDeclarations() ?? [];
216+
const declarations = (
217+
resolvedSymbol.getDeclarations() ??
218+
symbol.getDeclarations() ??
219+
[]
220+
).toSorted((left, right) => compareDeclarations(repoRoot, left, right));
210221
const declaration = declarations.find((candidate) => candidate.kind !== ts.SyntaxKind.SourceFile);
211222
return { declaration, resolvedSymbol };
212223
}
@@ -290,6 +301,37 @@ function printNode(
290301
: normalizedText;
291302
}
292303

304+
function compareText(left: string, right: string): number {
305+
if (left < right) {
306+
return -1;
307+
}
308+
if (left > right) {
309+
return 1;
310+
}
311+
return 0;
312+
}
313+
314+
function compareDeclarations(
315+
repoRoot: string,
316+
left: ts.Declaration,
317+
right: ts.Declaration,
318+
): number {
319+
const byPath = compareText(
320+
relativePath(repoRoot, left.getSourceFile().fileName),
321+
relativePath(repoRoot, right.getSourceFile().fileName),
322+
);
323+
if (byPath !== 0) {
324+
return byPath;
325+
}
326+
327+
const byStart = left.getStart() - right.getStart();
328+
if (byStart !== 0) {
329+
return byStart;
330+
}
331+
332+
return left.kind - right.kind;
333+
}
334+
293335
function buildExportSurface(params: {
294336
checker: ts.TypeChecker;
295337
printer: ts.Printer;
@@ -298,7 +340,7 @@ function buildExportSurface(params: {
298340
symbol: ts.Symbol;
299341
}): PluginSdkApiExport {
300342
const { checker, printer, program, repoRoot, symbol } = params;
301-
const { declaration, resolvedSymbol } = resolveSymbolAndDeclaration(checker, symbol);
343+
const { declaration, resolvedSymbol } = resolveSymbolAndDeclaration(checker, repoRoot, symbol);
302344
return {
303345
declaration: declaration ? printNode(repoRoot, checker, printer, declaration) : null,
304346
exportName: symbol.getName(),
@@ -331,7 +373,7 @@ function sortExports(left: PluginSdkApiExport, right: PluginSdkApiExport): numbe
331373
if (byKind !== 0) {
332374
return byKind;
333375
}
334-
return left.exportName.localeCompare(right.exportName);
376+
return compareText(left.exportName, right.exportName);
335377
}
336378

337379
function buildModuleSurface(params: {
@@ -424,7 +466,7 @@ export async function renderPluginSdkApiBaseline(params?: {
424466
entrypoint,
425467
}),
426468
)
427-
.toSorted((left, right) => left.importSpecifier.localeCompare(right.importSpecifier));
469+
.toSorted((left, right) => compareText(left.importSpecifier, right.importSpecifier));
428470

429471
const baseline: PluginSdkApiBaseline = {
430472
generatedBy: GENERATED_BY,

0 commit comments

Comments
 (0)