-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve comments and blank lines at top-of-file (#82)
Fixes #81 - This also reverts my `alpha.4` behavior-change where top-of-file comments always got a newline injected below them. Now we neither inject nor delete 1 blank line below the last top-of-file-comment. - This reverts snapshot changes (around top-of-file blank-lines) as compared to our pre-4.0.0-alpha.4 behavior. - Fixes some cases where we would introduce a newline when combining type and value imports (reference #54) --------- Co-authored-by: Ian VanSchooten <ian.vanschooten@gmail.com>
- Loading branch information
Showing
17 changed files
with
352 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
emptyStatement, | ||
stringLiteral, | ||
type CommentBlock, | ||
type ImportDeclaration, | ||
} from '@babel/types'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { | ||
attachCommentsToOutputNodes, | ||
CommentAssociation, | ||
getCommentRegistryFromImportDeclarations, | ||
testingOnlyExports, | ||
} from '../get-comment-registry'; | ||
|
||
describe('getCommentRegistryFromImportDeclarations', () => { | ||
test('is empty if provided no comments or no first-import', () => { | ||
expect( | ||
getCommentRegistryFromImportDeclarations({ | ||
firstImport: emptyStatement() as any, | ||
outputNodes: [], | ||
}), | ||
).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('attachCommentsToOutputNodes', () => { | ||
test('throws when missing inputs', () => { | ||
expect(() => | ||
attachCommentsToOutputNodes([], [], emptyStatement() as any), | ||
).toThrow( | ||
new Error( | ||
"Fatal Internal Error: Can't attach comments to empty output", | ||
), | ||
); | ||
}); | ||
test('does not inject an EmptyStatement if there are no top-of-file comments', () => { | ||
const firstImport = { | ||
type: 'ImportDeclaration', | ||
specifiers: [], | ||
source: stringLiteral('foo'), | ||
} as ImportDeclaration; | ||
const outputNodes = [firstImport]; | ||
|
||
attachCommentsToOutputNodes([], outputNodes, firstImport); | ||
|
||
expect(outputNodes[0].type).not.toEqual('EmptyStatement'); | ||
}); | ||
test("injects an EmptyStatement if there's a top-of-file comment", () => { | ||
const firstImport = { | ||
type: 'ImportDeclaration', | ||
specifiers: [], | ||
source: stringLiteral('foo'), | ||
} as ImportDeclaration; | ||
const comment = { | ||
type: 'CommentBlock', | ||
value: '@prettier', | ||
} as CommentBlock; | ||
const outputNodes = [firstImport]; | ||
|
||
attachCommentsToOutputNodes( | ||
[ | ||
{ | ||
needsTopOfFileOwner: true, | ||
comment, | ||
ownerIsSpecifier: false, | ||
commentId: testingOnlyExports.nodeId(comment), | ||
owner: firstImport, | ||
association: CommentAssociation.trailing, | ||
processingPriority: 0, | ||
}, | ||
], | ||
outputNodes, | ||
firstImport, | ||
); | ||
|
||
expect(outputNodes[0].type).toEqual('EmptyStatement'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.