Skip to content

Commit

Permalink
feat: Support a default reporter when linting. (#3236)
Browse files Browse the repository at this point in the history
- Make the reporter optional for linting.
- Use the cli-reporter as default.
  • Loading branch information
Jason3S authored Jul 17, 2022
1 parent ebf666d commit ffcaeff
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
9 changes: 5 additions & 4 deletions packages/cspell/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipeAsync, toAsyncIterable, opTap, opMap } from '@cspell/cspell-pipe';
import { opMap, opTap, pipeAsync, toAsyncIterable } from '@cspell/cspell-pipe';
import type { CSpellReporter, RunResult } from '@cspell/cspell-types';
import type { CheckTextInfo, SuggestionsForWordResult, TraceResult } from 'cspell-lib';
import {
Expand All @@ -11,9 +11,10 @@ import {
traceWordsAsync,
} from 'cspell-lib';
import * as path from 'path';
import { getReporter } from './cli-reporter';
import { TimedSuggestionsForWordResult } from './emitters/suggestionsEmitter';
import { LintRequest, runLint } from './lint';
import { BaseOptions, fixLegacy, LegacyOptions, LinterOptions, SuggestionOptions, TraceOptions } from './options';
import { BaseOptions, fixLegacy, LegacyOptions, LinterCliOptions, SuggestionOptions, TraceOptions } from './options';
import { simpleRepl } from './repl';
import { calcFinalConfigInfo, readConfig, readFile } from './util/fileHelper';
import { readStdin } from './util/stdin';
Expand All @@ -24,9 +25,9 @@ export type { TraceResult } from 'cspell-lib';

export type AppError = NodeJS.ErrnoException;

export function lint(fileGlobs: string[], options: LinterOptions, emitters: CSpellReporter): Promise<RunResult> {
export function lint(fileGlobs: string[], options: LinterCliOptions, reporter?: CSpellReporter): Promise<RunResult> {
options = fixLegacy(options);
const cfg = new LintRequest(fileGlobs, options, emitters);
const cfg = new LintRequest(fileGlobs, options, reporter ?? getReporter({ ...options, fileGlobs }));
return runLint(cfg);
}

Expand Down
7 changes: 2 additions & 5 deletions packages/cspell/src/commandLint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Command, Option as CommanderOption } from 'commander';
import * as App from './application';
import { getReporter } from './cli-reporter';
import { LinterCliOptions, LinterOptions } from './options';
import { LinterCliOptions } from './options';
import { DEFAULT_CACHE_LOCATION } from './util/cache';
import { CheckFailed } from './util/errors';

Expand Down Expand Up @@ -112,9 +111,7 @@ export function commandLint(prog: Command): Command {
.arguments('[globs...]')
.action((fileGlobs: string[], options: LinterCliOptions) => {
const { mustFindFiles, fileList } = options;
const cliReporter = getReporter({ ...options, fileGlobs });
const lintOptions: LinterOptions = { ...options, fileLists: fileList };
return App.lint(fileGlobs, lintOptions, cliReporter).then((result) => {
return App.lint(fileGlobs, options).then((result) => {
if (!fileGlobs.length && !result.files && !result.errors && !fileList) {
spellCheckCommand.outputHelp();
throw new CheckFailed('outputHelp', 1);
Expand Down
3 changes: 2 additions & 1 deletion packages/cspell/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '@cspell/cspell-types';
export * from './application';
export type { BaseOptions, LinterOptions as CSpellApplicationOptions, TraceOptions } from './options';
export type { BaseOptions, LinterCliOptions as CSpellApplicationOptions, TraceOptions } from './options';
export { getReporter as getDefaultReporter } from './cli-reporter';
12 changes: 10 additions & 2 deletions packages/cspell/src/lint/LintRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import * as util from '../util/util';

const defaultContextRange = 20;

interface Deprecated {
fileLists?: LinterOptions['fileList'];
}

export class LintRequest {
readonly uniqueFilter: (issue: Issue) => boolean;
readonly locale: string;
Expand All @@ -17,7 +21,11 @@ export class LintRequest {
readonly enableGlobDot: boolean | undefined;
readonly fileLists: string[];

constructor(readonly fileGlobs: string[], readonly options: LinterOptions, readonly reporter: CSpellReporter) {
constructor(
readonly fileGlobs: string[],
readonly options: LinterOptions & Deprecated,
readonly reporter: CSpellReporter
) {
this.root = path.resolve(options.root || process.cwd());
this.configFile = options.config;
this.excludes = calcExcludeGlobInfo(this.root, options.exclude);
Expand All @@ -26,6 +34,6 @@ export class LintRequest {
this.uniqueFilter = options.unique ? util.uniqueFilterFnGenerator((issue: Issue) => issue.text) : () => true;
this.showContext =
options.showContext === true ? defaultContextRange : options.showContext ? options.showContext : 0;
this.fileLists = options.fileLists || [];
this.fileLists = (options.fileList ?? options.fileLists) || [];
}
}
31 changes: 23 additions & 8 deletions packages/cspell/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface LinterOptions extends BaseOptions, Omit<CacheOptions, 'version'
* The files in the lists will be filtered against the glob patterns.
* - an entry of `stdin` means to read the file list from **`stdin`**
*/
fileLists?: string[] | undefined;
fileList?: string[] | undefined;

/**
* Files must be found and processed otherwise it is considered an error.
Expand Down Expand Up @@ -142,21 +142,36 @@ export interface BaseOptions {
defaultConfiguration?: boolean;
}

export interface LinterCliOptions extends Omit<LinterOptions, 'fileLists'> {
export interface LinterCliOptions extends LinterOptions {
/**
* Show legacy output
*/
legacy?: boolean;
summary: boolean;
issues: boolean;
silent: boolean;
mustFindFiles: boolean;
/**
* Show summary at the end
*/
summary?: boolean;
/**
* Show issues
*/
issues?: boolean;
/**
* Run in silent mode.
* @default false
*/
silent?: boolean;
/**
* Show progress
*/
progress?: boolean;
/**
* issues are shown with a relative path to the root or `cwd`
*/
relative?: boolean;
/**
* List of file paths to files that contains a list of files to be spell checked.
* Files must be found or cli will exit with an error.
*/
fileList?: string[];
mustFindFiles?: boolean;
}

export function fixLegacy<T extends BaseOptions>(opts: T & LegacyOptions): Omit<T & LegacyOptions, 'local'> {
Expand Down

0 comments on commit ffcaeff

Please sign in to comment.