Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing JsxSelfClosingElement with text failed #1409

Open
odiak opened this issue May 10, 2023 · 0 comments
Open

Replacing JsxSelfClosingElement with text failed #1409

odiak opened this issue May 10, 2023 · 0 comments

Comments

@odiak
Copy link
Contributor

odiak commented May 10, 2023

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>
}
odiak added a commit to odiak/ts-morph that referenced this issue May 10, 2023
odiak added a commit to odiak/ts-morph that referenced this issue May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant