dsherret / dsherret/ts-morph

Replacing JsxSelfClosingElement with text failed

Open
#1,409 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**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

\n hello\n world\n
\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. (/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. [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
\n hello\n \n
\n}\n',
newText: 'const F = () => {\n return
\n hello\n world\n
\n}\n'
}
```

**To Reproduce**

```ts
import { Project, SyntaxKind } from 'ts-morph'

const project = new Project()
const sourceFile = project.createSourceFile(
'foo.tsx',
`\
const F = () => {
return


hello


}
`
)
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


hello
world

}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.