|
| 1 | +// @ts-check |
| 2 | +/** @import { TSESTree } from '@typescript-eslint/types' */ |
| 3 | +/** @import { BaseComment } from '../src/languages/types.js' */ |
| 4 | +import { expect, test } from 'vitest'; |
| 5 | +import { print } from '../src/index.js'; |
| 6 | +import { acornParse } from './common.js'; |
| 7 | +import ts from '../src/languages/ts/index.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @param {string} value |
| 11 | + * @returns {BaseComment} |
| 12 | + */ |
| 13 | +function line(value) { |
| 14 | + return { type: 'Line', value }; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {string} value |
| 19 | + * @returns {BaseComment} |
| 20 | + */ |
| 21 | +function block(value) { |
| 22 | + return { type: 'Block', value }; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper to get return statement from a simple function |
| 27 | + * @param {TSESTree.Program} ast - Parsed AST |
| 28 | + * @returns {TSESTree.Node} The return statement |
| 29 | + */ |
| 30 | +function get_return_statement(ast) { |
| 31 | + const functionDecl = ast.body[0]; |
| 32 | + // @ts-expect-error accessing function body |
| 33 | + const statements = functionDecl.body.body; |
| 34 | + // Find the return statement (could be first or second depending on function structure) |
| 35 | + return statements.find(/** @param {any} stmt */ (stmt) => stmt.type === 'ReturnStatement'); |
| 36 | +} |
| 37 | + |
| 38 | +test('additional comments are inserted correctly', () => { |
| 39 | + const input = `function example() { |
| 40 | + const x = 1; |
| 41 | + return x; |
| 42 | +}`; |
| 43 | + |
| 44 | + const { ast } = acornParse(input); |
| 45 | + const returnStatement = get_return_statement(ast); |
| 46 | + expect(returnStatement.type).toBe('ReturnStatement'); |
| 47 | + |
| 48 | + const { code } = print( |
| 49 | + ast, |
| 50 | + ts({ |
| 51 | + getLeadingComments: (n) => |
| 52 | + n === returnStatement ? [line(' This is a leading comment')] : undefined, |
| 53 | + getTrailingComments: (n) => |
| 54 | + n === returnStatement ? [block(' This is a trailing comment ')] : undefined |
| 55 | + }) |
| 56 | + ); |
| 57 | + |
| 58 | + expect(code).toContain('// This is a leading comment'); |
| 59 | + expect(code).toContain('/* This is a trailing comment */'); |
| 60 | +}); |
| 61 | + |
| 62 | +test('only leading comments are inserted when specified', () => { |
| 63 | + const input = `function test() { return 42; }`; |
| 64 | + const { ast } = acornParse(input); |
| 65 | + const returnStatement = get_return_statement(ast); |
| 66 | + |
| 67 | + const { code } = print( |
| 68 | + ast, |
| 69 | + ts({ |
| 70 | + getLeadingComments: (n) => (n === returnStatement ? [line(' Leading only ')] : undefined) |
| 71 | + }) |
| 72 | + ); |
| 73 | + |
| 74 | + expect(code).toContain('// Leading only'); |
| 75 | + expect(code).not.toContain('trailing'); |
| 76 | +}); |
| 77 | + |
| 78 | +test('only trailing comments are inserted when specified', () => { |
| 79 | + const input = `function test() { return 42; }`; |
| 80 | + const { ast } = acornParse(input); |
| 81 | + const returnStatement = get_return_statement(ast); |
| 82 | + |
| 83 | + const { code } = print( |
| 84 | + ast, |
| 85 | + ts({ |
| 86 | + getTrailingComments: (n) => (n === returnStatement ? [block(' Trailing only ')] : undefined) |
| 87 | + }) |
| 88 | + ); |
| 89 | + |
| 90 | + expect(code).toContain('/* Trailing only */'); |
| 91 | + expect(code).not.toContain('//'); |
| 92 | +}); |
| 93 | + |
| 94 | +test('additional comments multi-line comments have new line', () => { |
| 95 | + const input = `function example() { |
| 96 | + const x = 1; |
| 97 | + return x; |
| 98 | +}`; |
| 99 | + |
| 100 | + const { ast } = acornParse(input); |
| 101 | + const returnStatement = get_return_statement(ast); |
| 102 | + expect(returnStatement.type).toBe('ReturnStatement'); |
| 103 | + |
| 104 | + const { code } = print( |
| 105 | + ast, |
| 106 | + ts({ |
| 107 | + getLeadingComments: (n) => |
| 108 | + n === returnStatement ? [block('*\n * This is a leading comment\n ')] : undefined |
| 109 | + }) |
| 110 | + ); |
| 111 | + |
| 112 | + expect(code).toMatchInlineSnapshot(` |
| 113 | + "function example() { |
| 114 | + const x = 1; |
| 115 | +
|
| 116 | + /** |
| 117 | + * This is a leading comment |
| 118 | + */ |
| 119 | + return x; |
| 120 | + }" |
| 121 | + `); |
| 122 | +}); |
0 commit comments