Ts-morph: addConstructor throw error when class have an empty comments line

Created on 21 Oct 2019  路  2Comments  路  Source: dsherret/ts-morph

Describe the bug

Version: "3.6.4" (latest)

  1) ClassLikeDeclarationBase
       addConstructor
         class with empty comments:
     Manipulation error: Error replacing tree: Should not have children left over.

-- Details --
Path: /testFile.ts
Text: "class c {\n //\n\n    constructor() {\n    }\n //\n}"
Stack: Error: Error replacing tree: Should not have children left over.
    at RangeParentHandler.handleNode (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/RangeParentHandler.ts:71:19)
    at ParentFinderReplacementNodeHandler.handleNode (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/ParentFinderReplacementNodeHandler.ts:25:36)
    at NodeHandlerHelper.handleForValues (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/NodeHandlerHelper.ts:14:21)
    at ParentFinderReplacementNodeHandler.handleChildren (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/StraightReplacementNodeHandler.ts:43:25)
    at ParentFinderReplacementNodeHandler.handleNode (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/StraightReplacementNodeHandler.ts:27:18)
    at ParentFinderReplacementNodeHandler.handleNode (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/ParentFinderReplacementNodeHandler.ts:28:19)
    at NodeHandlerHelper.handleForValues (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/NodeHandlerHelper.ts:14:21)
    at ParentFinderReplacementNodeHandler.handleChildren (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/StraightReplacementNodeHandler.ts:43:25)
    at ParentFinderReplacementNodeHandler.handleNode (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/StraightReplacementNodeHandler.ts:27:18)
    at ParentFinderReplacementNodeHandler.handleNode (/Users/paranoidjk/github/ts-morph/src/manipulation/nodeHandlers/ParentFinderReplacementNodeHandler.ts:28:19)
    at Object.doManipulation (/Users/paranoidjk/github/ts-morph/src/manipulation/manipulations/doManipulation.ts:18:21)
    at insertIntoBracesOrSourceFile (/Users/paranoidjk/github/ts-morph/src/manipulation/manipulations/insertion.ts:260:5)
    at Object.insertIntoBracesOrSourceFileWithGetChildren (/Users/paranoidjk/github/ts-morph/src/manipulation/manipulations/insertion.ts:313:5)
    at insertChildren (/Users/paranoidjk/github/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts:1165:12)
    at ClassDeclaration.insertConstructors (/Users/paranoidjk/github/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts:670:20)
    at ClassDeclaration.insertConstructor (/Users/paranoidjk/github/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts:664:25)
    at ClassDeclaration.addConstructor (/Users/paranoidjk/github/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts:656:25)
    at doTest (/Users/paranoidjk/github/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts:303:39)
    at Context.it (/Users/paranoidjk/github/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts:313:13)
    at callFn (/Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runnable.js:387:21)
    at Test.Runnable.run (/Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runnable.js:379:7)
    at Runner.runTest (/Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runner.js:535:10)
    at /Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runner.js:653:12
    at next (/Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runner.js:447:14)
    at /Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runner.js:457:7
    at next (/Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runner.js:362:14)
    at Immediate._onImmediate (/Users/paranoidjk/github/ts-morph/node_modules/mocha/lib/runner.js:425:5)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
  Error: Manipulation error: Error replacing tree: Should not have children left over.

To Reproduce

Failed Test Case: https://github.com/paranoidjk/ts-morph/commit/544ada6a66987ea9e9e8421fda01c316366ae1ad

Demo:

import { Project, ts } from "ts-morph";

const { Project, StructureKind } = require("ts-morph");

const project = new Project({});

const classFile = project.createSourceFile("src/MyClass.ts", `
class Foo {
// 
}
`);
const classNode = classFile.getClasses()[0];
classNode.addConstructor({
  statements: "console.log('i am constructor');",
});

bug

Most helpful comment

@paranoidjk thanks so much for filing a bug report with a reproducible example. I'll fix this tonight.

All 2 comments

@paranoidjk thanks so much for filing a bug report with a reproducible example. I'll fix this tonight.

Fixed in 4.3.2 (published now).

Was this page helpful?
0 / 5 - 0 ratings