Skip to content

Commit

Permalink
Fix static-helper relative path (#2873)
Browse files Browse the repository at this point in the history
* ai-modelclient smoke

* regen

* fix static-helper relative path

* update

* Update binder.ts

* Update binder.test.ts

* Update emitUtil.ts

* Update packages/typespec-ts/src/framework/sample.ts

* update

* Update binder.test.ts

* Update emitUtil.ts

---------

Co-authored-by: Qiaoqiao Zhang <[email protected]>
Co-authored-by: Qiaoqiao Zhang <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent 2a83766 commit b25ffb0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
15 changes: 10 additions & 5 deletions packages/typespec-ts/src/framework/hooks/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
SourceFileSymbol,
StaticHelperMetadata
} from "../load-static-helpers.js";
import path from "path/posix";
import { normalizePath } from "@typespec/compiler";

export interface DeclarationInfo {
name: string;
Expand All @@ -40,7 +42,7 @@ export interface Binder {
sourceFile: SourceFile
): string;
resolveReference(refkey: unknown): string;
resolveAllReferences(): void;
resolveAllReferences(sourceRoot: string): void;
}

const PLACEHOLDER_PREFIX = "__PLACEHOLDER_";
Expand Down Expand Up @@ -222,7 +224,7 @@ class BinderImp implements Binder {
/**
* Applies all tracked imports to their respective source files.
*/
resolveAllReferences(): void {
resolveAllReferences(sourceRoot: string): void {
for (const file of this.project.getSourceFiles()) {
this.resolveDeclarationReferences(file);
this.resolveDependencyReferences(file);
Expand All @@ -234,7 +236,7 @@ class BinderImp implements Binder {
}
}

this.cleanUnreferencedHelpers();
this.cleanUnreferencedHelpers(sourceRoot);
}

private resolveDependencyReferences(file: SourceFile) {
Expand Down Expand Up @@ -296,7 +298,7 @@ class BinderImp implements Binder {
this.references.get(refkey)!.add(sourceFile);
}

private cleanUnreferencedHelpers() {
private cleanUnreferencedHelpers(sourceRoot: string) {
const usedHelperFiles = new Set<SourceFile>();
for (const helper of this.staticHelpers.values()) {
const sourceFile = helper[SourceFileSymbol];
Expand All @@ -314,7 +316,10 @@ class BinderImp implements Binder {
}

this.project
.getSourceFiles("**/static-helpers/**/*.ts")
//normalizae the final path to adapt to different systems
.getSourceFiles(
normalizePath(path.join(sourceRoot, "static-helpers/**/*.ts"))
)
.filter((helperFile) => !usedHelperFiles.has(helperFile))
.forEach((helperFile) => helperFile.delete());
}
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-ts/src/framework/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ sourceFile2.addStatements(`${functionReference}();`);
sourceFile2.addStatements(`let obj: ${modelReference} = { id: 1 };`);

// Apply imports to ensure correct references
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

// Output the generated files
console.log("// test.ts");
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export async function $onEmit(context: EmitContext) {
buildRootIndex(subClient, modularCodeModel, rootIndexFile);
}

binder.resolveAllReferences();
binder.resolveAllReferences(modularSourcesRoot);

for (const file of project.getSourceFiles()) {
file.fixMissingImports({}, { importModuleSpecifierEnding: "js" });
Expand Down
42 changes: 21 additions & 21 deletions packages/typespec-ts/test-next/integration/hooks/binder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, interfaceWithReference, "TestModel");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
assertGetInterfaceDeclaration(sourceFile, "TestModel");
Expand Down Expand Up @@ -106,7 +106,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, interfaceWithReference, "TestModel");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
assertGetInterfaceDeclaration(sourceFile, "TestModel");
Expand Down Expand Up @@ -141,7 +141,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, functionDeclaration, "testFunction");
binder.resolveAllReferences();
binder.resolveAllReferences("");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
const functionDec = assertGetFunctionDeclaration(
Expand Down Expand Up @@ -186,7 +186,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, functionDeclaration, "testFunction");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
const functionDec = assertGetFunctionDeclaration(
Expand Down Expand Up @@ -225,7 +225,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, functionDeclaration, "testFunction");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
const fnDeclaration = assertGetFunctionDeclaration(
Expand Down Expand Up @@ -264,7 +264,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, functionDeclaration, "testFunction");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
const fnDeclaration = assertGetFunctionDeclaration(
Expand Down Expand Up @@ -302,7 +302,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, typeDeclaration, "TestType");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
assertGetTypealiasDeclaration(sourceFile, "TestType");
Expand Down Expand Up @@ -337,7 +337,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration, "TestInterface");
addDeclaration(sourceFile, typeDeclaration, "TestType");
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestInterface");
assertGetTypealiasDeclaration(sourceFile, "TestType");
Expand Down Expand Up @@ -381,7 +381,7 @@ describe("Binder", () => {
`${resolveReference(staticHelpers.buildCsvCollection)}();`
);

binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetImportStatements(sourceFile, "./static-helpers/utils.js");
assertGetStatement(sourceFile, "buildCsvCollection();");
Expand Down Expand Up @@ -435,7 +435,7 @@ describe("Binder", () => {
};

addDeclaration(sourceFile, interfaceDeclaration, model);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

const ifaceDeclaration = assertGetInterfaceDeclaration(
sourceFile,
Expand Down Expand Up @@ -497,7 +497,7 @@ describe("Binder", () => {
};

addDeclaration(sourceFile, interfaceDeclaration, model);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

const ifaceDeclaration = assertGetInterfaceDeclaration(
sourceFile,
Expand Down Expand Up @@ -547,7 +547,7 @@ describe("Binder", () => {
};

addDeclaration(sourceFile, interfaceDeclaration, model);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

const ifaceDeclaration = assertGetInterfaceDeclaration(
sourceFile,
Expand Down Expand Up @@ -586,7 +586,7 @@ describe("Binder", () => {
};

addDeclaration(sourceFile, interfaceDeclaration, model);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestModel");

Expand Down Expand Up @@ -631,7 +631,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclaration1, model1);
addDeclaration(sourceFile, interfaceDeclaration2, model2);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestModel");
assertGetInterfaceDeclaration(sourceFile, "TestModel_1");
Expand Down Expand Up @@ -682,7 +682,7 @@ describe("Binder", () => {

addDeclaration(sourceFile, interfaceDeclarationA, modelA);
addDeclaration(sourceFile, interfaceDeclarationB, modelB);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestModelA");
const modelBInterface = assertGetInterfaceDeclaration(
Expand Down Expand Up @@ -764,7 +764,7 @@ describe("Binder", () => {
addDeclaration(sourceFile2, interfaceDeclaration2, model2);
addDeclaration(sourceFile3, interfaceDeclaration3, model3);

binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

const lasModelInterface = assertGetInterfaceDeclaration(
sourceFile3,
Expand Down Expand Up @@ -840,7 +840,7 @@ describe("Binder", () => {
sourceFile2.addStatements(`${resolveReference(fnObject)}();`);

const binder = useBinder();
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

console.log("// test1.ts");
console.log(sourceFile1.getFullText());
Expand Down Expand Up @@ -890,7 +890,7 @@ describe("Binder", () => {
addDeclaration(sourceFile, interfaceDeclaration, model);
addDeclaration(sourceFile, functionDeclaration, functionModel);

binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestModel");
assertGetFunctionDeclaration(sourceFile, "TestFunction");
Expand Down Expand Up @@ -942,7 +942,7 @@ describe("Binder", () => {
addDeclaration(sourceFile, interfaceDeclarationB, modelB);

const binder = useBinder();
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

const propA = sourceFile.getInterface("ModelA")?.getProperty("propA");
const propB = sourceFile.getInterface("ModelB")?.getProperty("propB");
Expand Down Expand Up @@ -1005,7 +1005,7 @@ describe("Binder", () => {
);

const binder = useBinder();
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

const variableDeclaration = assertGetVariableDeclaration(
sourceFile2,
Expand Down Expand Up @@ -1072,7 +1072,7 @@ describe("Binder", () => {
addDeclaration(sourceFile, interfaceDeclarationB, modelB);

const binder = useBinder();
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");

assertGetInterfaceDeclaration(sourceFile, "TestModel");
const modelBInterface = assertGetInterfaceDeclaration(
Expand Down
12 changes: 6 additions & 6 deletions packages/typespec-ts/test/util/emitUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export async function emitModularModelsFromTypeSpec(
if (mustEmptyDiagnostic && dpgContext.program.diagnostics.length > 0) {
throw dpgContext.program.diagnostics;
}
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");
return modelFile;
}

Expand All @@ -450,7 +450,7 @@ export async function emitModularSerializeUtilsFromTypeSpec(
const binder = useBinder();
dpgContext.rlcOptions!.isModularLibrary = true;
const files = emitTypes(dpgContext, { sourceRoot: "" });
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");
expectDiagnosticEmpty(dpgContext.program.diagnostics);
return files;
}
Expand Down Expand Up @@ -516,7 +516,7 @@ export async function emitModularOperationsFromTypeSpec(
if (mustEmptyDiagnostic && dpgContext.program.diagnostics.length > 0) {
throw dpgContext.program.diagnostics;
}
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");
return res;
}
}
Expand Down Expand Up @@ -575,7 +575,7 @@ export async function emitModularClientContextFromTypeSpec(
dpgContext,
modularCodeModel
);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");
return res;
}
}
Expand Down Expand Up @@ -635,7 +635,7 @@ export async function emitModularClientFromTypeSpec(
dpgContext,
modularCodeModel
);
binder.resolveAllReferences();
binder.resolveAllReferences("/modularPackageFolder/src");
return res;
}
}
Expand All @@ -657,6 +657,6 @@ export async function emitSamplesFromTypeSpec(
...configs
});
const files = await emitSamples(dpgContext);
useBinder().resolveAllReferences();
useBinder().resolveAllReferences("/modularPackageFolder/src");
return files;
}

0 comments on commit b25ffb0

Please sign in to comment.