dsherret / dsherret/ts-morph

`DeclarationImport.remove()` removes following new line

Open
#1,483 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 21.0.1

**To Reproduce**

```typescript
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.