|
| 1 | +import path from 'node:path'; |
| 2 | +import { expect, test } from 'rstack/test'; |
| 3 | +import { normalizeFmtConfig } from '../../src/fmt/config.ts'; |
| 4 | +import { formatText } from '../../src/fmt/format.ts'; |
| 5 | + |
| 6 | +const rootPath = import.meta.dirname; |
| 7 | + |
| 8 | +test('applies per-file overrides and maps the cursor', async () => { |
| 9 | + const source = 'const value={message:"hello"}'; |
| 10 | + const config = normalizeFmtConfig( |
| 11 | + { |
| 12 | + overrides: [ |
| 13 | + { |
| 14 | + files: '*.ts', |
| 15 | + options: { |
| 16 | + singleQuote: true, |
| 17 | + }, |
| 18 | + }, |
| 19 | + ], |
| 20 | + }, |
| 21 | + rootPath, |
| 22 | + ); |
| 23 | + |
| 24 | + const result = await formatText(source, { |
| 25 | + config, |
| 26 | + cursorOffset: source.indexOf('message'), |
| 27 | + filePath: path.join(rootPath, 'example.ts'), |
| 28 | + }); |
| 29 | + |
| 30 | + expect(result).toEqual({ |
| 31 | + status: 'formatted', |
| 32 | + formatted: "const value = { message: 'hello' };\n", |
| 33 | + cursorOffset: 16, |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +test('returns unsupported when no parser can be inferred', async () => { |
| 38 | + const config = normalizeFmtConfig(undefined, rootPath); |
| 39 | + |
| 40 | + await expect( |
| 41 | + formatText('plain text', { |
| 42 | + config, |
| 43 | + filePath: path.join(rootPath, 'unknown.extension'), |
| 44 | + }), |
| 45 | + ).resolves.toEqual({ |
| 46 | + status: 'skipped', |
| 47 | + reason: 'unsupported', |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +test('uses an explicit parser for unknown file extensions', async () => { |
| 52 | + const config = normalizeFmtConfig({ parser: 'babel' }, rootPath); |
| 53 | + |
| 54 | + const result = await formatText('const value={nested:true}', { |
| 55 | + config, |
| 56 | + filePath: path.join(rootPath, 'unknown.extension'), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(result).toEqual({ |
| 60 | + status: 'formatted', |
| 61 | + formatted: 'const value = { nested: true };\n', |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +test('formats an explicitly provided node_modules file', async () => { |
| 66 | + const config = normalizeFmtConfig(undefined, rootPath); |
| 67 | + |
| 68 | + const result = await formatText('const value={nested:true}', { |
| 69 | + config, |
| 70 | + filePath: path.join(rootPath, 'node_modules', 'example', 'index.js'), |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result).toEqual({ |
| 74 | + status: 'formatted', |
| 75 | + formatted: 'const value = { nested: true };\n', |
| 76 | + }); |
| 77 | +}); |
0 commit comments