ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

`DeclarationImport.remove()` removes following new line

Open yangwooseong opened this issue 2 years ago • 0 comments

Describe the bug

Version: 21.0.1

To Reproduce

const project = new Project({
  manipulationSettings: {
    indentationText: IndentationText.TwoSpaces,
    newLineKind: NewLineKind.LineFeed,
    quoteKind: QuoteKind.Single,
    usePrefixAndSuffixTextForRename: true,
    useTrailingCommas: false,
  },
})

const removeImportDeclarationWithoutImport = (sourceFile: SourceFile) => {
  sourceFile
    .getDescendantsOfKind(SyntaxKind.ImportDeclaration)
    .filter((v) => v.getDescendantsOfKind(SyntaxKind.ImportClause).length === 0)
    .forEach((v) => {
      v.remove()
    })
}

//input.tsx
import foo from 'bar'
import 'react'

const baz = 1

//output.tsx (after applying removeImportDeclarationWithoutImport function)
import foo from 'bar'
const baz = 1

Expected behavior

I wanted to remove all declaration imports which have no import clauses, but after removing ImportDeclaration node new line is removed as well. The new line should be preserved after removing node.

yangwooseong avatar Dec 13 '23 04:12 yangwooseong