Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass an ObjectLiteralExpression as an initializer to addVariableStatement? #320

Closed
derolf opened this issue Apr 25, 2018 · 4 comments

Comments

@derolf
Copy link

derolf commented Apr 25, 2018

Right now, VariableDeclarationStructure exposes initializer just as a string, but I want to construct something like:

foo: {
  bar: 1,
  zoo: "test"
}
@derolf derolf changed the title How to pass an ObjectLiteralExpression as an initializer to addVariableStatement How to pass an ObjectLiteralExpression as an initializer to addVariableStatement? Apr 25, 2018
@dsherret
Copy link
Owner

Hey @derolf, yeah I've thought about that, but not sure what the best way would be at the moment. I still have to think about it a bit more. This is somewhat related to #148, but not exactly.

What I will do in the short term is improve VariableDeclarationStructure to accept a writer function and that would make doing this a little easier, but still not ideal. I'll get that done soon and comment back here when it's done.

@dsherret
Copy link
Owner

In v11 it's possible to use a writer when creating a variable declaration:

sourceFile.addVariableStatement({
    declarationKind: VariableDeclarationKind.Const,
    declarations: [{
        name: "myVar",
        initializer: writer => {
            writer.inlineBlock(() => {
                writer.writeLine("bar: 1,")
                    .writeLine(`zoo: "test"`);
            });
        }
    }]
});

I haven't tested that, but it should be a little easier to use than before. You could even extract that out to a reusable function and then write something like: initializer: writer => objToString(writer, { bar: 1, zoo: "test" }) and in that function call the code that formats things like above.

Anyway, I'm going to keep looking into this and evaluate how I can make this easier because it definitely is a pain point. I just have to figure out how it would work.

@derolf
Copy link
Author

derolf commented Apr 30, 2018

Hm, that’s kind of hack. Better would be to be able to pass a structured Initializer-Expression.

@dsherret
Copy link
Owner

dsherret commented Oct 13, 2018

Added WriterFunctions class for doing this:

import { WriterFunctions } from "ts-simple-ast";

// ...etc...

sourceFile.addVariableDeclaration({
    name: "myVarDeclaration",
    initializer: WriterFunctions.object({
        key1: 5,
        key2: "undefined",
        key3: writer => writer.quote("someString"),
        key4: undefined
    })
});

Outputs:

let myVarDeclaration = {
    key1: 5,
    key2: undefined,
    key3: "someString",
    key4
};

I'm not going to go with specific methods for doing this because it should be sufficient to use the writer function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants