1

I am a beginner at TS manipulation with ts-morph.

I have a template of existing source file, and i wanted to continue building up the template within the constructor space within the main class.

cdk-gen-stack.ts

export class CdkGenStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    
    //to insert code below
    
  }
}

ts-morph.ts

import * as tsmorph from "ts-morph";

const project = new tsmorph.Project({
    tsConfigFilePath: "tsconfig.json",
});
const sourceFile = project.getSourceFile("lib/cdk-gen-stack.ts");

const mainClasses = sourceFile?.getClass("CdkGenStack");

const mainConstructor = mainClasses?.getConstructors();

sourceFile?.save();

I am stuck as I am not sure what I could do after getting the constructor from the main class. What I want to achieve for example within the main template within the main class and constructor:

cdk-gen-stack.ts (morphed)

export class CdkGenStack extends cdk.Stack {
    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        // I want to start inserting something here, for example:
        const table = new dynamodb.Table(this, 'Table', {
            partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }
        });

    }
}

I have spent a lot of time researching online and trying to get any existing methods I can use with the constructor. There are addMethods and addVariable for classes, for example mainClasses?.addMember("test"); or:

mainClasses?.set({
    ctors: [{ 
        statements: [
            "test"
        ]
    }]
});

However these will insert them outside the constructor. What I want is to insert it within the given constructor that already exists in the class.

1 Answer 1

1

You can use .addStatements on the constructor node:

const mainConstructors = clazz.getConstructors();
mainConstructors[0].addStatements('console.log(123);');

Check the documentation for more examples and the API.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks Kelvin, I checked the documentation and tried. The problem is that getConstructors doesn't have the method available to use to addStatements. Your code throws an error "Property 'addStatements' does not exist on type 'number[]'.". I tried with mainConstructor?.addStatements('console.log(123);') earlier but also "Property 'addStatements' does not exist on type 'ConstructorDeclaration[]'."
I got the different 'add' or 'insert' methods available in the documentation, but those are for source file or class mainly, I can't be more specific referring to the constructor section as the methods are not available.
@unacorn Weird, the documentation says that Statements can be added, inserted, or removed from nodes with a body (ex. functions, methods, namespaces, source files) so I'd assume constructors too. Make sure you're actually operating on a constructor node. E.g. mainConstructor in your example is an array of constructors it seems.
sorry just to clarify "Make sure you're actually operating on a constructor node" > does that mean to specify mainConstructor[0]? If yes yah I tried that too (as well as without). the node as array throws error "Property 'addStatements' does not exist on type 'number[]'.
That's a TypeScript error. It thinks your mainConstructors is an array of numbers? Not sure what's going on there, might be an issue with typings. Not sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.