Adding nodes to an ArrayLiteralExpression
See original GitHub issueI located a node of kind ‘ArrayLiteralExpression’ via a getFirstChildByKindOrThrow(SyntaxKind.ArrayLiteralExpression) call.
How can I add additional items to the array? I cannot find an interface from ts-simple-ast that represents this array expression.
The actual code I’d like to manipulate is:
@NgModule({
declarations: [
AppComponent],
imports: [
BrowserModule,
AppRoutingModule
, WchNgModule.forRoot(environment)],
providers: [],
entryComponents: [...LAYOUTS],
bootstrap: [AppComponent]
})
I’d like to add a value to the ‘decorations’ property (and managed to locate the array node).
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments...
Read more >Array Literals in Java - GeeksforGeeks
Method 1: Initialization of array elements at the time of creating array object. It is the most commonly used syntax and can only...
Read more >Conditionally adding entries inside Array and object literals
This blog post shows how you can conditionally add elements inside Array literals and properties inside object literals.
Read more >What is array literal notation in javascript and when should ...
array literal notation is where you define a new array using just empty brackets. In your example: var myArray = [];. It is...
Read more >Documentation: 15: 8.15. Arrays - PostgreSQL
To write an array value as a literal constant, enclose the element values within curly braces and separate them by commas. (If you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@CarstenLeue, this is implemented in 0.57.0. I’ll add documentation later.
Nodes of kind
ts.SyntaxKind.ArrayLiteralExpressions
are nowArrayLiteralExpression
. So in the example above, you can do the following assertion (note that when #36 is implemented, the assertion would not be necessary):From there, you can do any of the following:
Let me know if you run into any issues.
Thanks for the compliment! 😃 Yeah, I’ve pulled out my hair many times trying to figure out the raw AST. Let me know if you run into any issues using the library.
Take note that #63 is implemented and available in 0.56.1. It will be slightly annoying to use because it will blow away the previously navigated nodes, but it should be an ok solution for the time being.
I believe the code would be something like (untested):
So after doing this everything you’ve just navigated to won’t work… for example, doing anything on
classDeclaration
,decorator
,arg
,declarationsProp
, etc… will throw an error because these nodes were blown away by thesourceFile.insertText(...)
. You would have to renavigate to them:classDeclaration = sourceFile.getClass("YourClassName")!
That’s an unfortunate side effect of using it and why it would be nice if there were actual methods for doing this.
All that said, I’m going to take a break from working on removing nodes and prioritize this because it’s quite easy to implement. I’ll start work on it tomorrow evening.