Skip to content

Commit

Permalink
fix: Enable cli option --validate-directives (#3461)
Browse files Browse the repository at this point in the history
* fix: Enable cli option `--validate-directives`
  • Loading branch information
Jason3S committed Aug 21, 2022
1 parent 0adde68 commit 52a5337
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/cspell-lib/api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ declare class DocumentValidator {
* The amount of time in ms to prepare for validation.
*/
get prepTime(): number;
get validateDirectives(): boolean;
checkText(range: SimpleRange, _text: string, scope: string[]): ValidationIssue[];
check(parsedText: ParsedText): ValidationIssue[];
/**
Expand Down
6 changes: 5 additions & 1 deletion packages/cspell-lib/src/textValidation/docValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ export class DocumentValidator {
return this._preparationTime;
}

get validateDirectives(): boolean {
return this.options.validateDirectives ?? this._preparations?.config.validateDirectives ?? false;
}

public checkText(range: SimpleRange, _text: string, scope: string[]): ValidationIssue[] {
const text = this._document.text.slice(range[0], range[1]);
return this.check({ text, range, scope: scope.join(' ') });
Expand Down Expand Up @@ -287,7 +291,7 @@ export class DocumentValidator {
assert(this._ready);
assert(this._preparations, ERROR_NOT_PREPARED);

const validateDirectives = forceCheck || this._preparations.config.validateDirectives;
const validateDirectives = forceCheck || this.validateDirectives;
if (!validateDirectives) return [];

const document = this.document;
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell/src/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Array [
"Options:",
" -c, --config <cspell.json> Configuration file to use. By default cspell",
" looks for cspell.json in the current directory.",
" --validate-directives Validate in-document CSpell directives.",
" --no-validate-directives Do not validate in-document CSpell directives.",
" --no-color Turn off color.",
" --color Force color",
" --no-default-configuration Do not load the default configuration and",
Expand Down Expand Up @@ -474,6 +476,8 @@ Array [
" --no-gitignore Do NOT use .gitignore files.",
" --gitignore-root <path> Prevent searching for .gitignore files past",
" root.",
" --validate-directives Validate in-document CSpell directives.",
" --no-validate-directives Do not validate in-document CSpell directives.",
" --no-color Turn off color.",
" --color Force color.",
" --no-default-configuration Do not load the default configuration and",
Expand Down
3 changes: 2 additions & 1 deletion packages/cspell/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ export type CheckTextResult = CheckTextInfo;
export async function checkText(filename: string, options: BaseOptions & LegacyOptions): Promise<CheckTextResult> {
options = fixLegacy(options);
const fileInfo = await readFileInfo(filename);
const { locale, languageId } = options;
const { locale, languageId, validateDirectives } = options;
const doc = fileInfoToDocument(fileInfo, languageId, locale);
const checkOptions = {
configFile: options.config,
validateDirectives,
};
const settingsFromCommandLine = util.clean({
languageId,
Expand Down
2 changes: 2 additions & 0 deletions packages/cspell/src/commandCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export function commandCheck(prog: Command): Command {
'-c, --config <cspell.json>',
'Configuration file to use. By default cspell looks for cspell.json in the current directory.'
)
.option('--validate-directives', 'Validate in-document CSpell directives.')
.option('--no-validate-directives', 'Do not validate in-document CSpell directives.')
.option('--no-color', 'Turn off color.')
.option('--color', 'Force color')
.addOption(
Expand Down
2 changes: 2 additions & 0 deletions packages/cspell/src/commandLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export function commandLint(prog: Command): Command {
.option('--gitignore', 'Ignore files matching glob patterns found in .gitignore files.')
.option('--no-gitignore', 'Do NOT use .gitignore files.')
.option('--gitignore-root <path>', 'Prevent searching for .gitignore files past root.', collect)
.option('--validate-directives', 'Validate in-document CSpell directives.')
.option('--no-validate-directives', 'Do not validate in-document CSpell directives.')
.option('--no-color', 'Turn off color.')
.option('--color', 'Force color.')
.addOption(
Expand Down
3 changes: 2 additions & 1 deletion packages/cspell/src/lint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export async function runLint(cfg: LintRequest): Promise<RunResult> {
MessageTypes.Info
);
try {
const validateOptions = { generateSuggestions: cfg.options.showSuggestions, numSuggestions: 5 };
const { showSuggestions: generateSuggestions, validateDirectives } = cfg.options;
const validateOptions = { generateSuggestions, numSuggestions: 5, validateDirectives };
const r = await cspell.spellCheckDocument(doc, validateOptions, configInfo.config);
spellResult = r;
result.processed = r.checked;
Expand Down
5 changes: 5 additions & 0 deletions packages/cspell/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ export interface BaseOptions {
* @default true
*/
defaultConfiguration?: boolean;

/**
* Check In-Document CSpell directives for correctness.
*/
validateDirectives?: boolean;
}

export interface LinterCliOptions extends LinterOptions {
Expand Down

0 comments on commit 52a5337

Please sign in to comment.