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

Namespace unwrap throws error if the namespace ends with comments #808

Closed
ziacik opened this issue Apr 25, 2020 · 1 comment
Closed

Namespace unwrap throws error if the namespace ends with comments #808

ziacik opened this issue Apr 25, 2020 · 1 comment
Labels

Comments

@ziacik
Copy link

ziacik commented Apr 25, 2020

When the namespace only contains comments, or ends with comments, an error is thrown when namespaces#unwrap is called.

The error thrown is

/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:2253
            throw new ManipulationError(sourceFile.getFilePath(), oldFileText, newFileText, message);
            ^

ManipulationError: Manipulation error: Cannot get the next when at the end of the iterator.

-- Details --
Path: /mnt/c/Workspaces/Experimental/ts-morph-error/test.ts
Text: "class Whatever {}"
Stack: Error: Cannot get the next when at the end of the iterator.
    at AdvancedIterator.next (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:39:19)
    at UnwrapParentHandler.handleNode (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:1761:97)
    at ParentFinderReplacementNodeHandler.handleNode (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:1541:36)
    at NodeHandlerHelper.handleForValues (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:1352:21)
    at ParentFinderReplacementNodeHandler.handleChildren (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:1414:25)
    at ParentFinderReplacementNodeHandler.handleNode (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:1404:18)
    at ParentFinderReplacementNodeHandler.handleNode (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:1544:19)
    at doManipulation (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:2237:21)
    at unwrapNode (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:2644:5)
    at NamespaceDeclaration.unwrap (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:9964:13)
    at throwError (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:2253:19)
    at doManipulation (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:2251:9)
    at unwrapNode (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:2644:5)
    at NamespaceDeclaration.unwrap (/mnt/c/Workspaces/Experimental/ts-morph-error/node_modules/ts-morph/dist/ts-morph.js:9964:13)
    at /mnt/c/Workspaces/Experimental/ts-morph-error/index.js:10:45
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/mnt/c/Workspaces/Experimental/ts-morph-error/index.js:10:28)
    at Module._compile (internal/modules/cjs/loader.js:1144:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)
    at Module.load (internal/modules/cjs/loader.js:993:32) {
  filePath: '/mnt/c/Workspaces/Experimental/ts-morph-error/test.ts',
  oldText: 'namespace Some {\n' +
    '        class Whatever {}\n' +
    '        // Some comment.\n' +
    '        /* This one too. */\n' +
    '    }',
  newText: 'class Whatever {}'
}

Version: 7.0.2

To Reproduce

import { Project, ts } from "ts-morph";
const project = new Project();
const sourceFile = project.createSourceFile(
    "test.ts",
    `namespace Some {
        class Whatever {}
        // Some comment.
        /* This one too. */
    }`
);
sourceFile.getNamespaces().forEach(ns => ns.unwrap());

Expected behavior

No error thrown.

@dsherret
Copy link
Owner

@ziacik thanks for reporting! I'll do a release with the fix later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants