-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update
cspell
and use the new CSpellReporter (#380)
- Loading branch information
Showing
118 changed files
with
4,028 additions
and
2,541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import * as core from '@actions/core'; | ||
import { issueCommand } from '@actions/core/lib/command'; | ||
import type { | ||
CSpellReporter, | ||
Issue, | ||
MessageType, | ||
ProgressItem, | ||
RunResult, | ||
ProgressFileComplete, | ||
} from '@cspell/cspell-types'; | ||
import { URI } from 'vscode-uri'; | ||
import * as path from 'path'; | ||
|
||
export interface LintResult { | ||
issues: Issue[]; | ||
result: RunResult; | ||
} | ||
|
||
type LogFn = (message: string) => void; | ||
export interface Logger { | ||
debug: LogFn; | ||
info: LogFn; | ||
warning: LogFn; | ||
error: LogFn; | ||
} | ||
|
||
export interface LintOptions { | ||
root: string; | ||
config?: string; | ||
} | ||
|
||
function nullEmitter(_msg: string) { | ||
/* Do Nothings */ | ||
} | ||
|
||
export type ReportIssueCommand = 'error' | 'warning' | 'none'; | ||
|
||
export class CSpellReporterForGithubAction { | ||
readonly issues: Issue[] = []; | ||
readonly issueCounts = new Map<string, number>(); | ||
readonly result: RunResult = { | ||
files: -1, | ||
filesWithIssues: new Set(), | ||
issues: -1, | ||
errors: -1, | ||
}; | ||
finished: boolean = false; | ||
|
||
constructor(readonly reportIssueCommand: ReportIssueCommand, readonly logger: Logger = core) {} | ||
|
||
_issue(issue: Issue) { | ||
const { issues, issueCounts } = this; | ||
const uri = issue.uri; | ||
uri && issueCounts.set(uri, (issueCounts.get(uri) || 0) + 1); | ||
issues.push(issue); | ||
} | ||
|
||
_info(message: string, _msgType: MessageType) { | ||
this._debug(message); | ||
} | ||
|
||
_debug(message: string) { | ||
nullEmitter(message); | ||
// logger.debug(message); | ||
} | ||
|
||
_progress(progress: ProgressItem | ProgressFileComplete) { | ||
if (!isProgressFileComplete(progress)) { | ||
return; | ||
} | ||
const { issueCounts, logger } = this; | ||
|
||
const issueCount = issueCounts.get(progress.filename) || 0; | ||
const { fileNum, fileCount, filename, elapsedTimeMs } = progress; | ||
const issues = issueCount ? ` issues: ${issueCount}` : ''; | ||
const timeMsg = elapsedTimeMs ? `(${elapsedTimeMs.toFixed(2)}ms)` : '-'; | ||
logger.info(`${fileNum}/${fileCount} ${filename}${issues} ${timeMsg}`); | ||
} | ||
|
||
_error(message: string, error: Error) { | ||
const { logger } = this; | ||
logger.error(`${message} | ||
name: ${error.name} | ||
msg: ${error.message} | ||
stack: | ||
${error.stack} | ||
`); | ||
return; | ||
} | ||
|
||
_result(result: RunResult) { | ||
Object.assign(this.result, result); | ||
this.finished = true; | ||
const command = this.reportIssueCommand; | ||
|
||
if (!['error', 'warning'].includes(command)) { | ||
return; | ||
} | ||
|
||
const cwd = process.cwd(); | ||
|
||
this.issues.forEach((item) => { | ||
// format: ::warning file={name},line={line},col={col}::{message} | ||
issueCommand( | ||
command, | ||
{ | ||
file: relative(cwd, item.uri || ''), | ||
line: item.row, | ||
col: item.col, | ||
}, | ||
`Unknown word (${item.text})` | ||
); | ||
console.warn(`${relative(cwd, item.uri || '')}:${item.row}:${item.col} Unknown word (${item.text})`); | ||
}); | ||
} | ||
|
||
readonly reporter: CSpellReporter = { | ||
debug: (...args) => this._debug(...args), | ||
error: (...args) => this._error(...args), | ||
info: (...args) => this._info(...args), | ||
issue: (...args) => this._issue(...args), | ||
progress: (...args) => this._progress(...args), | ||
result: (...args) => this._result(...args), | ||
}; | ||
} | ||
|
||
function isProgressFileComplete(p: ProgressItem): p is ProgressFileComplete { | ||
return p.type === 'ProgressFileComplete'; | ||
} | ||
|
||
function relative(cwd: string, fileUri: string) { | ||
const fsPath = URI.parse(fileUri).fsPath; | ||
return path.relative(cwd, fsPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,19 @@ | ||
import * as cspellApp from 'cspell'; | ||
import { | ||
Emitters, | ||
MessageType, | ||
Issue, | ||
RunResult, | ||
isProgressFileComplete, | ||
ProgressItem, | ||
ProgressFileComplete, | ||
} from 'cspell'; | ||
|
||
export interface LintResult { | ||
issues: Issue[]; | ||
result: RunResult; | ||
} | ||
|
||
type LogFn = (message: string) => void; | ||
export interface Logger { | ||
debug: LogFn; | ||
info: LogFn; | ||
warning: LogFn; | ||
error: LogFn; | ||
} | ||
import { CSpellReporter } from 'cspell'; | ||
|
||
export interface LintOptions { | ||
root: string; | ||
config?: string; | ||
} | ||
|
||
function nullEmitter(_msg: string) { | ||
/* Do Nothings */ | ||
} | ||
|
||
/** | ||
* | ||
* @param files files or glob patterns to check | ||
* @param root the root directory to scan | ||
* @param logger logger functions. | ||
* Spell check files. | ||
* @param files - files or glob patterns to check | ||
* @param root - the root directory to scan | ||
* @param reporter - reporter to use. | ||
*/ | ||
export async function lint(files: string[], lintOptions: LintOptions, logger: Logger): Promise<LintResult> { | ||
const issues: Issue[] = []; | ||
|
||
const issueCounts = new Map<string, number>(); | ||
|
||
function issue(issue: Issue) { | ||
const uri = issue.uri; | ||
uri && issueCounts.set(uri, (issueCounts.get(uri) || 0) + 1); | ||
issues.push(issue); | ||
} | ||
function info(message: string, _msgType: MessageType) { | ||
debug(message); | ||
} | ||
function debug(message: string) { | ||
nullEmitter(message); | ||
// logger.debug(message); | ||
} | ||
function progress(progress: ProgressItem | ProgressFileComplete) { | ||
if (!isProgressFileComplete(progress)) { | ||
return; | ||
} | ||
|
||
const issueCount = issueCounts.get(progress.filename) || 0; | ||
const { fileNum, fileCount, filename, elapsedTimeMs } = progress; | ||
const issues = issueCount ? ` issues: ${issueCount}` : ''; | ||
const timeMsg = elapsedTimeMs ? `(${elapsedTimeMs.toFixed(2)}ms)` : '-'; | ||
logger.info(`${fileNum}/${fileCount} ${filename}${issues} ${timeMsg}`); | ||
} | ||
|
||
function error(message: string, error: Error) { | ||
logger.error(`${message} | ||
name: ${error.name} | ||
msg: ${error.message} | ||
stack: | ||
${error.stack} | ||
`); | ||
return; | ||
} | ||
|
||
const emitters: Emitters = { | ||
issue, | ||
info, | ||
debug, | ||
error, | ||
progress, | ||
}; | ||
|
||
const options: cspellApp.CSpellApplicationOptions = { ...lintOptions }; | ||
const result = await cspellApp.lint(files, options, emitters); | ||
return { | ||
issues, | ||
result, | ||
}; | ||
export async function lint(files: string[], lintOptions: LintOptions, reporter: CSpellReporter): Promise<void> { | ||
const { root, config } = lintOptions; | ||
const options: cspellApp.CSpellApplicationOptions = { root, config }; | ||
await cspellApp.lint(files, options, reporter); | ||
} |
Oops, something went wrong.