Skip to content

Commit

Permalink
Address nits
Browse files Browse the repository at this point in the history
  • Loading branch information
noanflaherty committed Dec 10, 2024
1 parent 86949d0 commit 9892bf1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
30 changes: 15 additions & 15 deletions generators/python-v2/ast/src/PythonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AstNode } from "./core/AstNode";
import { Comment } from "./Comment";
import { Writer } from "./core/Writer";
import { Reference } from "./Reference";
import { ModulePath } from "./core/types";
import { ImportedName, ModulePath } from "./core/types";
import { StarImport } from "./StarImport";
import { Class } from "./Class";
import { Method } from "./Method";
Expand Down Expand Up @@ -76,29 +76,29 @@ export class PythonFile extends AstNode {
writer: Writer;
uniqueReferences: Map<string, { modulePath: ModulePath; references: Reference[]; referenceNames: Set<string> }>;
}): void {
const reservedNames = this.getReservedNames();
const usedNames = this.getUsedNames();
const references: Reference[] = Array.from(uniqueReferences.values()).flatMap(({ references }) => references);

// Build up a map of refs to their name overrides, keeping track of howmany times we've seen a name as we go.
const completeRefPathsToNameOverrides: Record<string, { name: string; isAlias: boolean }> = {};
const completeRefPathsToNameOverrides: Record<string, ImportedName> = {};
const nameUsageCounts: Record<string, number> = {};

// Initialize counts for reserved names
reservedNames.forEach((name) => {
// Initialize counts for used names
usedNames.forEach((name) => {
nameUsageCounts[name] = 1;
});

references.forEach((reference) => {
const name = reference.alias ?? reference.name;
const refIdentifier = reference.getCompletePath();

// Get current count for this name, accounting for reserved names used by statements in the file
// Get current count for this name, accounting for used names used by statements in the file
const currentCount = nameUsageCounts[name] ?? 0;
nameUsageCounts[name] = currentCount + 1;

// For reserved names or names we've seen before, use an auto-generated alias with a naming convention
// For used names or names we've seen before, use an auto-generated alias with a naming convention
// of a numbered suffix.
if (reservedNames.has(name) || currentCount > 0) {
if (usedNames.has(name) || currentCount > 0) {
completeRefPathsToNameOverrides[refIdentifier] = {
name: `${name}_${currentCount}`,
isAlias: true
Expand All @@ -114,20 +114,20 @@ export class PythonFile extends AstNode {
writer.setRefNameOverrides(completeRefPathsToNameOverrides);
}

private getReservedNames(): Set<string> {
const reservedNames = new Set<string>();
private getUsedNames(): Set<string> {
const usedNames = new Set<string>();

this.statements.forEach((statement) => {
if (statement instanceof Class) {
reservedNames.add(statement.name);
usedNames.add(statement.name);
} else if (statement instanceof Method) {
reservedNames.add(statement.name);
usedNames.add(statement.name);
} else if (statement instanceof Field) {
reservedNames.add(statement.name);
usedNames.add(statement.name);
}
});

return reservedNames;
return usedNames;
}

private deduplicateReferences() {
Expand All @@ -138,7 +138,7 @@ export class PythonFile extends AstNode {
>();
for (const reference of this.references) {
const referenceName = reference.name;
const fullyQualifiedPath = reference.getFullyQualifiedModulePath();
const fullyQualifiedPath = reference.getFullyQualifiedPath();
const existingRefs = uniqueReferences.get(fullyQualifiedPath);

if (existingRefs) {
Expand Down
4 changes: 2 additions & 2 deletions generators/python-v2/ast/src/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export class Reference extends AstNode {
}
}

public getFullyQualifiedModulePath(): string {
public getFullyQualifiedPath(): string {
return this.modulePath.join(".");
}

public getCompletePath(): string {
return `${this.getFullyQualifiedModulePath()}.${this.name}`;
return `${this.getFullyQualifiedPath()}.${this.name}`;
}
}
15 changes: 7 additions & 8 deletions generators/python-v2/ast/src/core/Writer.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { AbstractWriter } from "@fern-api/base-generator";
import { Config } from "@wasm-fmt/ruff_fmt";
import { Reference } from "../Reference";
import { ImportedName } from "./types";

export declare namespace Writer {}

export class Writer extends AbstractWriter {
private completeRefPathsToNameOverrides: Record<string, { name: string; isAlias: boolean }> = {};
private fullyQualifiedPathsToImportedNames: Record<string, ImportedName> = {};

public setRefNameOverrides(
completeRefPathsToNameOverrides: Record<string, { name: string; isAlias: boolean }>
): void {
this.completeRefPathsToNameOverrides = completeRefPathsToNameOverrides;
public setRefNameOverrides(completeRefPathsToNameOverrides: Record<string, ImportedName>): void {
this.fullyQualifiedPathsToImportedNames = completeRefPathsToNameOverrides;
}

public unsetRefNameOverrides(): void {
this.completeRefPathsToNameOverrides = {};
this.fullyQualifiedPathsToImportedNames = {};
}

public getRefNameOverride(reference: Reference): { name: string; isAlias: boolean } {
const explicitNameOverride = this.completeRefPathsToNameOverrides[reference.getCompletePath()];
public getRefNameOverride(reference: Reference): ImportedName {
const explicitNameOverride = this.fullyQualifiedPathsToImportedNames[reference.getCompletePath()];

if (explicitNameOverride) {
return explicitNameOverride;
Expand Down
6 changes: 6 additions & 0 deletions generators/python-v2/ast/src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export type ModulePath = string[] | Readonly<string[]>;

export type AttrPath = string[] | Readonly<string[]>;

export interface ImportedName {
name: string;
isAlias?: boolean;
}

0 comments on commit 9892bf1

Please sign in to comment.