SyntaxList.insertChildText throws error when writing property
See original GitHub issueRepro
const tsSimpleAst = require("ts-simple-ast")
const { stripIndent } = require('common-tags')
const {
default: Project,
SyntaxKind,
} = tsSimpleAst
const project = new Project({useVirtualFileSystem: true})
const file = project.createSourceFile(`test.ts`, stripIndent`
export class Foo {
m1 () { }
}`)
const classDeclaration = file.getFirstDescendantByKindOrThrow(SyntaxKind.ClassDeclaration)
const syntaxList = classDeclaration.getFirstDescendantByKindOrThrow(SyntaxKind.SyntaxList)
syntaxList.insertChildText(0, writer => {
writer.write(`p1 = 1`)
})
console.log(file.getFullText())
Demo
https://runkit.com/lazarljubenovic/5b48dbd8c790e80012b9655c
In my original code I’m getting Error: Error replacing tree! Perhaps a syntax error was inserted (Current: MethodDeclaration -- New: PropertyDeclaration).
but while trying to repro I’ve gotten the other one which I also don’t understand so let’s start from there.
It works if I add a comment // p1 = 1
, so it seems like it’s really something wrong with the syntax. Is this a bad way of adding props? (I need to add a constructor in my original code.)
The error message says that it’s generating
export class Foo {\n p1 = 1\n m1 () { }\n}
which seems fine to me.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
What is the difference between `throw new Error` and `throw ...
The Error object in all browsers support the following two properties: name: The name of the error, or more specifically, the name of...
Read more >How to use throwing properties to catch failures in Swift
Defining throwing properties in Swift. A throwing property can be defined by configuring the get statement with a throws keyword:
Read more >How to throw an error when using a property of an object
In this example, we will first create an empty object using the syntax which was shown in the previous section. After creating an...
Read more >Exception.Source Property (System) - Microsoft Learn
Reflection object. Examples. The following example throws an Exception that sets the Source property in its constructor and then catches the exception and ......
Read more >Cannot Read Property of Undefined in JavaScript - Rollbar
Here's an example of a JavaScript TypeError: Cannot read property of undefined thrown when a property is attempted to be read on an ......
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
This was because
const syntaxList = classDeclaration.getFirstDescendantByKindOrThrow(SyntaxKind.SyntaxList);
will return the first descendant syntax list, which in this case is the one for the modifiers.The library was then accidentally inserting the text into what I call the “child syntax list,” which is the one that contains the body of the class (where the
MethodDeclaration
is). Then the other part of the code was wondering why it found new children in that syntax list instead of the one with the export keyword.Anyway, fixed in 12.5.4.
@cancerberoSgx that would be cool, but would be nice if it just worked in vscode, visual studio, etc. by default 😦
@lazarljubenovic It’s neat that WebStorm does that… I like what JetBrains has done with resharper in visual studio for c#, but I kept running into way too many problems using it for TypeScript so I disabled it for TypeScript a while ago.
@lazarljubenovic thanks for reporting. I’ll look into this on the weekend.
I recommend using
classDeclaration.insertProperty
to insert a property and.insertConstructor
to insert a constructor