question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fixMissingImports breaks when adding to empty named imports

See original GitHub issue

I’m not sure exactly what happened here, but I had:

import { } from "../jsx";

And that caused this:

Error: Cannot get the next when at the end of the iterator.
-- Details --
Path: V:/ts-morph/src/structures/utils/forEachStructureChild.ts
Text: "...dDeclarationStructure, ParameteredNodeStructure, TypeElementMemberedNodeStructure } from \"../base\";\r\nimport { JsxElementStructure, JsxSelfClosingElementStructure, JsxAttributedNodeStructure }{  } from \"../jsx\";\r\nimport { VariableStatementStructure } from \"../statement\";\r\n\r\n// todo: Code generate this...\r\n\r\nexp..."
Stack: Error: Cannot get the next when at the end of the iterator.
    at AdvancedIterator.next (V:\ts-morph\node_modules\ts-morph\dist\utils\AdvancedIterator.js:33:19)
    at RangeHandler.handleNode (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\RangeHandler.js:38:78)
    at NodeHandlerHelper.handleForValues (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\NodeHandlerHelper.js:12:21)
    at RangeHandler.rangeHandlerReplace (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\RangeHandler.js:48:21)
    at RangeHandler.handleNode (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\RangeHandler.js:31:18)
    at NodeHandlerHelper.handleForValues (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\NodeHandlerHelper.js:12:21)
    at RangeHandler.rangeHandlerReplace (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\RangeHandler.js:48:21)
    at RangeHandler.handleNode (V:\ts-morph\node_modules\ts-morph\dist\manipulation\nodeHandlers\RangeHandler.js:31:18)
    at Object.doManipulation (V:\ts-morph\node_modules\ts-morph\dist\manipulation\manipulations\doManipulation.js:9:21)
    at Object.insertIntoTextRange (V:\ts-morph\node_modules\ts-morph\dist\manipulation\manipulations\insertion.js:34:22)
    at Object.doManipulation (V:\ts-morph\node_modules\ts-morph\dist\manipulation\manipulations\doManipulation.js:12:15)
    at Object.insertIntoTextRange (V:\ts-morph\node_modules\ts-morph\dist\manipulation\manipulations\insertion.js:34:22)
    at applyTextChanges (V:\ts-morph\node_modules\ts-morph\dist\compiler\ast\module\SourceFile.js:656:32)
    at SourceFile.fixMissingImports (V:\ts-morph\node_modules\ts-morph\dist\compiler\ast\module\SourceFile.js:636:13)
    at Object.createForEachStructureChild (V:\ts-morph\scripts\generation\createForEachStructureChild.ts:29:31)
    at Object.<anonymous> (V:\ts-morph\scripts\generation\main.ts:46:9)
    at Generator.next (<anonymous>)
    at V:\ts-morph\scripts\generation\main.ts:7:71
    at new Promise (<anonymous>)
    at __awaiter (V:\ts-morph\scripts\generation\main.ts:3:12)

Kind of looks like that might be a bug in the compiler though.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jdfmcommented, Jun 11, 2021

@jdfm - ahhh, so it was just the leading comment in the file?

That seems to have been the case for me at least. It would be interesting to hear from others if this fixes their issues as well.

I think it’s acceptable that comments should not be the very first lines of a file (after calling fixMissingImports()) […] instead that those comments would be pushed down below the imports, i.e. the imports to be inserted at the start of the file.

So, in this case we’re using the comment at the top of the file as a way of noting that we’re basing our implementation on someone else’s. Maybe you’d like to give extra context about a module via a code comment. There could be other use cases where a comment at the top of the file is desirable, like, maybe for licensing purposes, or for linter disabling at the file level, which is usually done with an enriched comment. The fact is, I don’t know if it’s fair to assume that the user wouldn’t want to keep that comment at the top of the file.

[…] but I would at least not expect an error, […]

Agreed.

1reaction
LarsBauercommented, Aug 6, 2020

I get the same error when adding JSDoc to an interface declaration and then call fixMissingImports() on source file. Without JSDoc everything is working fine.

ManipulationError: Manipulation error: Error replacing tree: The children of the old and new trees were expected to have the same count (1:92).

Any idea?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Goland inconsistently formats imports when they are grouped ...
However goland mixes all the imports in one group once it tries to add a new import. Also it is confused with empty...
Read more >
Issues closed in the 1.0 development cycle
PR #4132: Fixed name error bug in function safe_unicode in module py3compat. ... PR #3782: add Importing Notebooks example.
Read more >
IPython Documentation - Read the Docs
The notebook has added separate Edit and Command modes, allowing easier keyboard ... PR #2373: fix missing imports in core.interactiveshell.
Read more >
Upgrading Unity Container (Changelog)
Then, use Resharper to fix missing imports in the entire solution in one action. Upgrading to Unity.Abstractions 4.0.0.0. A few breaking changes have...
Read more >
Imports - ts-morph
Imports · Add/Insert · Remove · Module specifier · Default import · Namespace import · Named imports.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found