ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

Show how to add factory generated statements

Open fivethreeo opened this issue 4 years ago • 3 comments

Is this the way to do it?


  import { Project, ts, printNode } from "ts-morph";

  const tsVariableStatement = ts.factory.createVariableStatement(
    [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
    ts.factory.createVariableDeclarationList(
      [ts.factory.createVariableDeclaration(
        ts.factory.createIdentifier("resolvers"),
        undefined,
        ts.factory.createTypeReferenceNode(
          ts.factory.createIdentifier("Resolvers"),
          undefined
        ),
        ts.factory.createObjectLiteralExpression(
          [ts.factory.createPropertyAssignment(
            ts.factory.createIdentifier("Profile"),
            ts.factory.createObjectLiteralExpression(
              [ts.factory.createPropertyAssignment(
                ts.factory.createIdentifier("name"),
                ts.factory.createArrowFunction(
                  undefined,
                  undefined,
                  [ts.factory.createParameterDeclaration(
                    undefined,
                    undefined,
                    undefined,
                    ts.factory.createIdentifier("user"),
                    undefined,
                    undefined,
                    undefined
                  )],
                  undefined,
                  ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
                  ts.factory.createTemplateExpression(
                    ts.factory.createTemplateHead(
                      "",
                      ""
                    ),
                    [
                      ts.factory.createTemplateSpan(
                        ts.factory.createPropertyAccessExpression(
                          ts.factory.createIdentifier("user"),
                          ts.factory.createIdentifier("firstName")
                        ),
                        ts.factory.createTemplateMiddle(
                          " ",
                          " "
                        )
                      ),
                      ts.factory.createTemplateSpan(
                        ts.factory.createPropertyAccessExpression(
                          ts.factory.createIdentifier("user"),
                          ts.factory.createIdentifier("lastName")
                        ),
                        ts.factory.createTemplateTail(
                          "",
                          ""
                        )
                      )
                    ]
                  )
                )
              )],
              true
            )
          )],
          true
        )
      )],
      ts.NodeFlags.Const
    )
  )
  sourceFile.addStatements([writer =>
    writer.write(printNode( tsVariableStatement))
  );

fivethreeo avatar Aug 24 '21 22:08 fivethreeo

Any answers? I've successfully created the node, but cannot add using file.addVariableStatement(tsVariableStatement); 😞

bombillazo avatar Apr 21 '22 21:04 bombillazo

@fivethreeo Did you ever figure out?

SagnikPradhan avatar Jul 30 '22 03:07 SagnikPradhan

This is how far I got.

fivethreeo avatar Jul 31 '22 12:07 fivethreeo