Skip to content

Conversation

@tkfoss
Copy link
Contributor

@tkfoss tkfoss commented Jan 23, 2025

No description provided.

@tkfoss tkfoss requested review from a team and codegen-team as code owners January 23, 2025 21:32
@tkfoss tkfoss requested review from tawsifkamal and removed request for a team January 23, 2025 21:32
@linear
Copy link

linear bot commented Jan 23, 2025

CG-8705 Implement `Namespace` as TS graph node type

Namespace is a typescript-specific object that are initialized with the following example syntax

namespace MyNamespace {}  // not exported

export namespace MyOtherNamespace{}  // exported and defined in the same line
export MyNamespace;  // exports the namespace exported in line 1

Part 1: Parse namespaces

  • Implement a new Graph node type Namespace. This class should extend TSSymbol, HasBlock and HasName. This should be added to the graph during parse - it should be in the symbol map.
  • Make sure to parse this in export.py (search for "CG-8705" in the file)
  • Add tests
  • Add basic set of APIs that makes sense for a namespace object to have

Part 2: Add type resolution on namespaces

  • Resolve imports and exports of namespaces
  • Resolve symbols within namespaces
  • Add tests for these cases with varying complexity (example below)
// mathOperations.ts
export namespace MathOperations {
    export const add = (a: number, b: number): number => a + b;
    export const subtract = (a: number, b: number): number => a - b;
    export const multiply = (a: number, b: number): number => a * b;
    export const divide = (a: number, b: number): number => {
        if (b === 0) {
            throw new Error("Division by zero is not allowed.");
        }
        return a / b;
    };
}
// app.ts
import { MathOperations } from './mathOperations';

const a = 10;
const b = 5;

console.log(`Addition: ${MathOperations.add(a, b)}`);
console.log(`Subtraction: ${MathOperations.subtract(a, b)}`);
console.log(`Multiplication: ${MathOperations.multiply(a, b)}`);
console.log(`Division: ${MathOperations.divide(a, b)}`);

Part 3: More complex cases

  • Support wildcards (likely involves implementing valid_import_names)
  • Support nested namespaces

// mathOperations.ts
export namespace MathOperations {
    // Interface for generic mathematical operations
    export interface Operation {
        execute(a: number, b: number): number;
    }

    // Class implementing the Operation interface for addition
    export class Addition implements Operation {
        execute(a: number, b: number): number {
            return a + b;
        }
    }

    // Class implementing the Operation interface for multiplication
    export class Multiplication implements Operation {
        execute(a: number, b: number): number {
            return a * b;
        }
    }

    // Function overloading
    export function calculate(operation: "add", a: number, b: number): number;
    export function calculate(operation: "multiply", a: number, b: number): number;
    export function calculate(operation: string, a: number, b: number): number {
        switch (operation) {
            case "add":
                return a + b;
            case "multiply":
                return a * b;
            default:
                throw new Error(`Operation "${operation}" is not supported.`);
        }
    }

    // Nested namespace for advanced operations
    export namespace Advanced {
        export const power = (base: number, exponent: number): number =>
            Math.pow(base, exponent);

        export const squareRoot = (x: number): number => {
            if (x < 0) {
                throw new Error("Cannot calculate square root of a negative number.");
            }
            return Math.sqrt(x);
        };
    }
}
// app.ts
import { MathOperations } from './mathOperations';

const a = 10;
const b = 5;

// Using classes
const addition = new MathOperations.Addition();
const multiplication = new MathOperations.Multiplication();

console.log(`Class Addition: ${addition.execute(a, b)}`);
console.log(`Class Multiplication: ${multiplication.execute(a, b)}`);

// Using overloaded function
console.log(`Function Add: ${MathOperations.calculate("add", a, b)}`);
console.log(`Function Multiply: ${MathOperations.calculate("multiply", a, b)}`);

// Using nested namespace
console.log(`Power: ${MathOperations.Advanced.power(a, b)}`);
console.log(`Square Root of 25: ${MathOperations.Advanced.squareRoot(25)}`);

try {
    console.log(`Square Root of -1: ${MathOperations.Advanced.squareRoot(-1)}`);
} catch (error) {
    console.error(error.message);
}

@CLAassistant
Copy link

CLAassistant commented Jan 30, 2025

CLA assistant check
All committers have signed the CLA.

@tkfoss tkfoss changed the title (WIP) feat: namespace APIs [CG-8705] feat: Implement Namespace as TS graph node type Mar 14, 2025
bagel897
bagel897 previously approved these changes Mar 19, 2025
@tkfoss tkfoss enabled auto-merge (squash) March 19, 2025 19:18
@tkfoss tkfoss dismissed bagel897’s stale review March 19, 2025 19:18

all adressed

@tkfoss tkfoss disabled auto-merge March 19, 2025 19:20
@tkfoss tkfoss merged commit bf06715 into develop Mar 19, 2025
17 of 18 checks passed
@tkfoss tkfoss deleted the tom-cg-8705-implement-namespace-as-ts-graph-node-type branch March 19, 2025 19:29
@github-actions
Copy link
Contributor

🎉 This PR is included in version 0.52.11 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

codegen-sh bot referenced this pull request in Zeeeepa/codegen Apr 17, 2025
Co-authored-by: tomcodegen <kucar.tomislav@gmail.com>
Co-authored-by: tomcodgen <191515280+tomcodgen@users.noreply.github.com>
Zeeeepa referenced this pull request in Zeeeepa/codegen Apr 17, 2025
#40)

Co-authored-by: tomcodegen <kucar.tomislav@gmail.com>
Co-authored-by: tomcodgen <191515280+tomcodgen@users.noreply.github.com>
Zeeeepa referenced this pull request in Zeeeepa/codegen Apr 23, 2025
Original commit by tomcodgen: [CG-8705] feat: Implement Namespace as TS graph node type (#40)

Co-authored-by: tomcodegen <kucar.tomislav@gmail.com>
Co-authored-by: tomcodgen <191515280+tomcodgen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants