ts-morph
ts-morph copied to clipboard
Replacing JsxSelfClosingElement with text failed
Describe the bug
Version: 18.0.0
I tried to replace a JsxSelfClosingElement with text, but it failed with following error:
error log
/project/node_modules/ts-morph/dist/ts-morph.js:39
throw new common.errors.InvalidOperationError("Cannot get the next when at the end of the iterator.");
^
ManipulationError: Manipulation error: Cannot get the next when at the end of the iterator.
-- Details --
Path: /project/foo.tsx
Text: "const F = () => {\n return <div>\n hello\n world\n </div>\n}\n"
Stack: Error: Cannot get the next when at the end of the iterator.
at AdvancedIterator.next (/project/node_modules/ts-morph/dist/ts-morph.js:39:19)
at RangeParentHandler.handleNode (/project/node_modules/ts-morph/dist/ts-morph.js:1683:78)
at ParentFinderReplacementNodeHandler.handleNode (/project/node_modules/ts-morph/dist/ts-morph.js:1567:36)
at NodeHandlerHelper.handleForValues (/project/node_modules/ts-morph/dist/ts-morph.js:1373:21)
at ParentFinderReplacementNodeHandler.handleChildren (/project/node_modules/ts-morph/dist/ts-morph.js:1440:25)
at ParentFinderReplacementNodeHandler.handleNode (/project/node_modules/ts-morph/dist/ts-morph.js:1430:18)
at ParentFinderReplacementNodeHandler.handleNode (/project/node_modules/ts-morph/dist/ts-morph.js:1570:19)
at NodeHandlerHelper.handleForValues (/project/node_modules/ts-morph/dist/ts-morph.js:1373:21)
at ParentFinderReplacementNodeHandler.handleChildren (/project/node_modules/ts-morph/dist/ts-morph.js:1440:25)
at ParentFinderReplacementNodeHandler.handleNode (/project/node_modules/ts-morph/dist/ts-morph.js:1430:18)
at throwError (/project/node_modules/ts-morph/dist/ts-morph.js:2298:19)
at doManipulation (/project/node_modules/ts-morph/dist/ts-morph.js:2296:9)
at insertIntoParentTextRange (/project/node_modules/ts-morph/dist/ts-morph.js:2317:5)
at JsxSelfClosingElement.replaceWithText (/project/node_modules/ts-morph/dist/ts-morph.js:3644:9)
at Object.<anonymous> (/project/src/test.ts:16:6)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module.m._compile (/project/node_modules/ts-node/src/index.ts:1597:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Object.require.extensions.<computed> [as .ts] (/project/node_modules/ts-node/src/index.ts:1600:12)
at Module.load (node:internal/modules/cjs/loader:1117:32) {
filePath: '/project/foo.tsx',
oldText: 'const F = () => {\n return <div>\n hello\n <X />\n </div>\n}\n',
newText: 'const F = () => {\n return <div>\n hello\n world\n </div>\n}\n'
}
To Reproduce
import { Project, SyntaxKind } from 'ts-morph'
const project = new Project()
const sourceFile = project.createSourceFile(
'foo.tsx',
`\
const F = () => {
return <div>
hello
<X />
</div>
}
`
)
const node = sourceFile.getFirstDescendantByKindOrThrow(
SyntaxKind.JsxSelfClosingElement
)
node.replaceWithText('world')
process.stdout.write(sourceFile.getFullText())
Expected behavior
Replace the node with text and output following:
const F = () => {
return <div>
hello
world
</div>
}