Fix: ts-morph incorrectly transforms nodes which begin with whitespace#1592
Fix: ts-morph incorrectly transforms nodes which begin with whitespace#1592ofersadgat wants to merge 1 commit intodsherret:latestfrom
Conversation
|
I'm not sure this is correct and it seems to cause some tests to fail. |
|
@dsherret Hi, I have done some more due diligence here. The code IS incorrect for a specific circumstance: JsxText. The reason for this is that getStart(true) includes comments but excludes leading whitespace. The problem, as you've mentioned elsewhere, is that leading whitespace is actually a part of a JsxText node. Therefore, excluding that when you're doing a transform is incorrect. This leads to the result of continually prepending whitespace to the transformed jsx node. You run into a similar situation with replaceWithText(). This function similarly also doesnt include leading whitespace. This leads to the problem: this will also prepend whitespace which is counter intuitive. Also, afaict there isnt an api around this. So I have added an option into the replaceWithText api to allow replacing the full thing. If there is a prefered alternative change, lmk. |
Fixes: #1591
Note that using
.posmatches what typescript is doing as well:The line:
const updated = visitArrayWorker(nodes, visitor, test, start, count);is calling
visitArrayWorkerwhich callsinnerVisitwhich first calls the transform function followed by the handleTransformation function. So, on the way out typescript is using.posas well.