Skip to content

Commit 34c79d6

Browse files
v-jiaodiCopilot
andauthored
Update packages/typespec-ts/src/framework/hooks/binder.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent cd22235 commit 34c79d6

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

  • packages/typespec-ts/src/framework/hooks

packages/typespec-ts/src/framework/hooks/binder.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,39 @@ class BinderImp implements Binder {
309309
}
310310
}
311311

312+
// Ensure platform-specific variants (e.g. *-browser.*, *-react-native.*)
313+
// are preserved whenever their base helper is referenced.
314+
const platformSuffixes = ["-browser", "-react-native"];
315+
const helpersWithReferences = Array.from(usedHelperFiles);
316+
for (const helperFile of helpersWithReferences) {
317+
const directory = helperFile.getDirectory();
318+
const filePath = helperFile.getFilePath();
319+
const fileName = path.basename(filePath);
320+
const dotIndex = fileName.indexOf(".");
321+
const baseName =
322+
dotIndex === -1 ? fileName : fileName.substring(0, dotIndex);
323+
const variantPrefix = `${baseName}-`;
324+
325+
for (const sibling of directory.getSourceFiles()) {
326+
if (usedHelperFiles.has(sibling)) {
327+
continue;
328+
}
329+
const siblingPath = sibling.getFilePath();
330+
const siblingName = path.basename(siblingPath);
331+
const siblingDotIndex = siblingName.indexOf(".");
332+
const siblingBaseName =
333+
siblingDotIndex === -1
334+
? siblingName
335+
: siblingName.substring(0, siblingDotIndex);
336+
337+
if (
338+
siblingBaseName.startsWith(variantPrefix) &&
339+
platformSuffixes.some((suffix) => siblingBaseName.endsWith(suffix))
340+
) {
341+
usedHelperFiles.add(sibling);
342+
}
343+
}
344+
}
312345
function isFileUnused(file: SourceFile) {
313346
return !usedHelperFiles.has(file);
314347
}

0 commit comments

Comments
 (0)