Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/cspell/src/cli-reporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as path from 'node:path';

import type { ProgressFileBegin, ProgressFileComplete } from '@cspell/cspell-types';
import { toFileDirURL } from '@cspell/url';
import { Chalk } from 'chalk';
import { afterEach, describe, expect, test, vi } from 'vitest';

Expand Down Expand Up @@ -114,6 +117,36 @@ describe('cli-reporter perf summary', () => {
});
});

describe('cli-reporter issue file names', () => {
afterEach(() => {
vi.restoreAllMocks();
});

const root = path.resolve('project');
const stdinRoot = toFileDirURL(root).href.replace(/^file:/, 'stdin:');
const stdinFile = new URL('src/b.ts', stdinRoot).href;

test.each`
uri | relative | expected
${stdinRoot} | ${true} | ${'stdin'}
${stdinRoot} | ${false} | ${'stdin'}
${stdinFile} | ${true} | ${'src/b.ts'}
${stdinFile} | ${false} | ${stdinFile}
`('issue in $uri relative: $relative', ({ uri, relative, expected }) => {
vi.spyOn(console.stdoutChannel, 'getColorLevel').mockReturnValue(0);
const lines: string[] = [];
vi.spyOn(console.stdoutChannel, 'write').mockImplementation((msg) => lines.push(msg));

const reporter = getReporter(
{ fileGlobs: [], root, relative, issues: true, progress: false },
{ issueTemplate: '$filename' },
);
reporter.issue({ ...genIssue('line'), uri });

expect(lines).toEqual([expected + '\n']);
});
});

function fileBegin(fileNum: number): ProgressFileBegin {
return { type: 'ProgressFileBegin', fileNum, fileCount: 2, filename: `file${fileNum}.txt` };
}
Expand Down
16 changes: 15 additions & 1 deletion packages/cspell/src/cli-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { checkTemplate, formatIssue } from './formatIssue.js';
import type { CSpellReporterConfiguration } from './models.js';
import type { LinterCliOptions } from './options.js';
import type { FinalizedReporter } from './reporters/index.js';
import { STDIN, STDINProtocol } from './util/constants.js';
import type { PerfMeasurement } from './util/perfMeasurements.js';
import { getPerfMeasurements } from './util/perfMeasurements.js';
import type { Table, TableRow } from './util/table.js';
Expand Down Expand Up @@ -84,11 +85,24 @@ function nullEmitter() {

function relativeUriFilename(uri: string, rootURL: URL): string {
const url = toFileURL(uri);
if (isStdinWithoutPath(url)) return STDIN;
const rel = urlRelative(rootURL, url);
if (rel.startsWith('..')) return toFilePathOrHref(url);
return rel;
}

function absoluteUriFilename(uri: string, rootURL: URL): string {
const url = toFileURL(uri, rootURL);
return isStdinWithoutPath(url) ? STDIN : toFilePathOrHref(url);
}

/**
* Plain `stdin` is given the current directory as its location, so it has no file name to show.
*/
function isStdinWithoutPath(url: URL): boolean {
return url.protocol === STDINProtocol && url.pathname.endsWith('/');
}

function reportProgress(io: IO, p: ProgressItem, cwdURL: URL, options: CSpellReporterConfiguration) {
if (p.type === 'ProgressFileComplete') {
return reportProgressFileComplete(io, p, cwdURL, options);
Expand Down Expand Up @@ -234,7 +248,7 @@ export function getReporter(options: ReporterOptions, config?: CSpellReporterCon
function relativeIssue(fn: (i: ReporterIssue) => void): (i: Issue) => void {
const fnFilename = options.relative
? (uri: string) => relativeUriFilename(uri, rootURL)
: (uri: string) => toFilePathOrHref(toFileURL(uri, rootURL));
: (uri: string) => absoluteUriFilename(uri, rootURL);
return (i: Issue) => {
const fullFilename = i.uri ? toFilePathOrHref(toFileURL(i.uri, rootURL)) : '';
const filename = i.uri ? fnFilename(i.uri) : '';
Expand Down