|
| 1 | +import { ImportDeclaration } from '@babel/types'; |
| 2 | + |
| 3 | +import { adjustCommentsOnSortedNodes } from '../adjust-comments-on-sorted-nodes'; |
| 4 | +import { getImportNodes } from '../get-import-nodes'; |
| 5 | + |
| 6 | +function leadingComments(node: ImportDeclaration): string[] { |
| 7 | + return node.leadingComments?.map((c) => c.value) ?? []; |
| 8 | +} |
| 9 | + |
| 10 | +function trailingComments(node: ImportDeclaration): string[] { |
| 11 | + return node.trailingComments?.map((c) => c.value) ?? []; |
| 12 | +} |
| 13 | + |
| 14 | +test('it preserves the single leading comment for each import declaration', () => { |
| 15 | + const importNodes = getImportNodes(` |
| 16 | + import {x} from "c"; |
| 17 | + // comment b |
| 18 | + import {y} from "b"; |
| 19 | + // comment a |
| 20 | + import {z} from "a"; |
| 21 | + `); |
| 22 | + expect(importNodes).toHaveLength(3); |
| 23 | + const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; |
| 24 | + adjustCommentsOnSortedNodes(importNodes, finalNodes); |
| 25 | + expect(finalNodes).toHaveLength(3); |
| 26 | + expect(leadingComments(finalNodes[0])).toEqual([' comment a']); |
| 27 | + expect(trailingComments(finalNodes[0])).toEqual([]); |
| 28 | + expect(leadingComments(finalNodes[1])).toEqual([' comment b']); |
| 29 | + expect(trailingComments(finalNodes[1])).toEqual([]); |
| 30 | + expect(leadingComments(finalNodes[2])).toEqual([]); |
| 31 | + expect(trailingComments(finalNodes[2])).toEqual([]); |
| 32 | +}); |
| 33 | + |
| 34 | +test('it preserves multiple leading comments for each import declaration', () => { |
| 35 | + const importNodes = getImportNodes(` |
| 36 | + import {x} from "c"; |
| 37 | + // comment b1 |
| 38 | + // comment b2 |
| 39 | + // comment b3 |
| 40 | + import {y} from "b"; |
| 41 | + // comment a1 |
| 42 | + // comment a2 |
| 43 | + // comment a3 |
| 44 | + import {z} from "a"; |
| 45 | + `); |
| 46 | + expect(importNodes).toHaveLength(3); |
| 47 | + const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; |
| 48 | + adjustCommentsOnSortedNodes(importNodes, finalNodes); |
| 49 | + expect(finalNodes).toHaveLength(3); |
| 50 | + expect(leadingComments(finalNodes[0])).toEqual([ |
| 51 | + ' comment a1', |
| 52 | + ' comment a2', |
| 53 | + ' comment a3', |
| 54 | + ]); |
| 55 | + expect(trailingComments(finalNodes[0])).toEqual([]); |
| 56 | + expect(leadingComments(finalNodes[1])).toEqual([ |
| 57 | + ' comment b1', |
| 58 | + ' comment b2', |
| 59 | + ' comment b3', |
| 60 | + ]); |
| 61 | + expect(trailingComments(finalNodes[1])).toEqual([]); |
| 62 | + expect(leadingComments(finalNodes[2])).toEqual([]); |
| 63 | + expect(trailingComments(finalNodes[2])).toEqual([]); |
| 64 | +}); |
| 65 | + |
| 66 | +test('it does not move comments at before all import declarations', () => { |
| 67 | + const importNodes = getImportNodes(` |
| 68 | + // comment c1 |
| 69 | + // comment c2 |
| 70 | + import {x} from "c"; |
| 71 | + import {y} from "b"; |
| 72 | + import {z} from "a"; |
| 73 | + `); |
| 74 | + expect(importNodes).toHaveLength(3); |
| 75 | + const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; |
| 76 | + adjustCommentsOnSortedNodes(importNodes, finalNodes); |
| 77 | + expect(finalNodes).toHaveLength(3); |
| 78 | + expect(leadingComments(finalNodes[0])).toEqual([ |
| 79 | + ' comment c1', |
| 80 | + ' comment c2', |
| 81 | + ]); |
| 82 | + expect(trailingComments(finalNodes[0])).toEqual([]); |
| 83 | + expect(leadingComments(finalNodes[1])).toEqual([]); |
| 84 | + expect(trailingComments(finalNodes[1])).toEqual([]); |
| 85 | + expect(leadingComments(finalNodes[2])).toEqual([]); |
| 86 | + expect(trailingComments(finalNodes[2])).toEqual([]); |
| 87 | +}); |
| 88 | + |
| 89 | +test('it does not affect comments after all import declarations', () => { |
| 90 | + const importNodes = getImportNodes(` |
| 91 | + import {x} from "c"; |
| 92 | + import {y} from "b"; |
| 93 | + import {z} from "a"; |
| 94 | + // comment final 1 |
| 95 | + // comment final 2 |
| 96 | + `); |
| 97 | + expect(importNodes).toHaveLength(3); |
| 98 | + const finalNodes = [importNodes[2], importNodes[1], importNodes[0]]; |
| 99 | + adjustCommentsOnSortedNodes(importNodes, finalNodes); |
| 100 | + expect(finalNodes).toHaveLength(3); |
| 101 | + expect(leadingComments(finalNodes[0])).toEqual([]); |
| 102 | + expect(trailingComments(finalNodes[0])).toEqual([]); |
| 103 | + expect(leadingComments(finalNodes[1])).toEqual([]); |
| 104 | + expect(trailingComments(finalNodes[1])).toEqual([]); |
| 105 | + expect(leadingComments(finalNodes[2])).toEqual([]); |
| 106 | + expect(trailingComments(finalNodes[2])).toEqual([]); |
| 107 | +}); |
0 commit comments