chore: rename exceptions file

This commit is contained in:
Roman Hotsiy
2020-06-25 16:18:24 +03:00
parent 3555e66918
commit c0c5d34465
5 changed files with 41 additions and 39 deletions

View File

@@ -204,10 +204,12 @@ Possible values are:
* warning * warning
* error * error
#### Exceptions file #### Ignore file
You can exclude some known errors to not show up/break you validation using exceptions file. This file instructs Redocly's linter to ignore the rules contained for specific parts of your API.
Exceptions file should be named `.openapi-cli.exceptions.yaml` and should be placed next to `.redocly.yaml`.
You can exclude some known errors to not show up/break you validation using ignore file.
Ignore file should be named `.redocly.lint-ignore.yaml` and should be placed next to `.redocly.yaml`.
```yaml ```yaml
"filename.yaml": "filename.yaml":
@@ -219,7 +221,7 @@ Exceptions file should be named `.openapi-cli.exceptions.yaml` and should be pla
- "#/excluded/json/pointer" - "#/excluded/json/pointer"
``` ```
Exceptions file can be autogenerated to add all messages as exceptions by running `openapi lint --generate-exceptions` Ignore file can be autogenerated to add all messages to it by running `openapi lint --generate-ignore-file`
#### Built-in Rules #### Built-in Rules

View File

@@ -87,7 +87,7 @@ export async function bundleDocument(opts: {
return { return {
bundle: document.parsed, bundle: document.parsed,
messages: ctx.messages.map((message) => config.addMessageToExceptions(message)), messages: ctx.messages.map((message) => config.addMessageToIgnore(message)),
}; };
} }
} }

View File

@@ -39,8 +39,8 @@ yargs // eslint-disable-line
type: 'number', type: 'number',
default: 100, default: 100,
}) })
.option('generate-exceptions', { .option('generate-ignore-file', {
description: 'Generate exceptions file', description: 'Generate ignore file',
type: 'boolean', type: 'boolean',
}) })
.option('skip-rule', { .option('skip-rule', {
@@ -65,12 +65,12 @@ yargs // eslint-disable-line
const entrypoints = getFallbackEntryPointsOrExit(argv.entrypoints, config); const entrypoints = getFallbackEntryPointsOrExit(argv.entrypoints, config);
if (argv['generate-exceptions']) { if (argv['generate-ignore-file']) {
config.lint.exceptions = {}; // clear ignore config.lint.ignore = {}; // clear ignore
} }
const totals: Totals = { errors: 0, warnings: 0, ignored: 0 }; const totals: Totals = { errors: 0, warnings: 0, ignored: 0 };
let totalExceptions = 0; let totalIgnored = 0;
// TODO: use shared externalRef resolver, blocked by transformers now as they mutate documents // TODO: use shared externalRef resolver, blocked by transformers now as they mutate documents
for (const entryPoint of entrypoints) { for (const entryPoint of entrypoints) {
@@ -86,10 +86,10 @@ yargs // eslint-disable-line
totals.warnings += fileTotals.warnings; totals.warnings += fileTotals.warnings;
totals.ignored += fileTotals.ignored; totals.ignored += fileTotals.ignored;
if (argv['generate-exceptions']) { if (argv['generate-ignore-file']) {
for (let m of results) { for (let m of results) {
config.lint.addException(m); config.lint.addException(m);
totalExceptions++; totalIgnored++;
} }
} else { } else {
formatMessages(results, { formatMessages(results, {
@@ -108,20 +108,20 @@ yargs // eslint-disable-line
} }
} }
if (argv['generate-exceptions']) { if (argv['generate-ignore-file']) {
config.lint.saveExceptions(); config.lint.saveIgnore();
process.stderr.write( process.stderr.write(
`Added ${totalExceptions} ${pluralize( `Added ${totalIgnored} ${pluralize(
'message', 'message',
totalExceptions, totalIgnored,
)} to exceptions file\n\n`, )} to ignore file\n\n`,
); );
} else { } else {
printLintTotals(totals, entrypoints.length); printLintTotals(totals, entrypoints.length);
} }
printUnusedWarnings(config.lint); printUnusedWarnings(config.lint);
process.exit(totals.errors === 0 || argv['generate-exceptions'] ? 0 : 1); process.exit(totals.errors === 0 || argv['generate-ignore-file'] ? 0 : 1);
}, },
) )
.command( .command(
@@ -326,7 +326,7 @@ function printLintTotals(totals: Totals, definitionsCount: number) {
if (totals.errors > 0) { if (totals.errors > 0) {
process.stderr.write( process.stderr.write(
gray(`\nrun with \`--generate-exceptions\` to add all messages to exceptions file\n`), gray(`\nrun with \`--generate-ignore-file\` to add all messages to ignore file\n`),
); );
} }

View File

@@ -17,7 +17,7 @@ import { red, blue } from 'colorette';
import { NodeType } from '../types'; import { NodeType } from '../types';
import { dirname } from 'path'; import { dirname } from 'path';
const EXCEPTIONS_FILE = '.openapi-cli.exceptions.yaml'; const IGNORE_FILE = '.redocly.lint-ignore.yaml';
export type RuleConfig = export type RuleConfig =
| MessageSeverity | MessageSeverity
@@ -75,7 +75,7 @@ export class LintConfig {
plugins: Plugin[]; plugins: Plugin[];
rules: Record<string, RuleConfig>; rules: Record<string, RuleConfig>;
transformers: Record<string, TransformerConfig>; transformers: Record<string, TransformerConfig>;
exceptions: Record<string, Record<string, Set<string>>> = {}; ignore: Record<string, Record<string, Set<string>>> = {};
private _usedRules: Set<string> = new Set(); private _usedRules: Set<string> = new Set();
@@ -105,37 +105,37 @@ export class LintConfig {
this.transformers = merged.transformers; this.transformers = merged.transformers;
const dir = this.configFile ? path.dirname(this.configFile) : process.cwd(); const dir = this.configFile ? path.dirname(this.configFile) : process.cwd();
const exceptionsFile = path.join(dir, EXCEPTIONS_FILE); const ignoreFile = path.join(dir, IGNORE_FILE);
if (fs.existsSync(exceptionsFile)) { if (fs.existsSync(ignoreFile)) {
this.exceptions = yaml.safeLoad(fs.readFileSync(exceptionsFile, 'utf-8')); // TODO: parse errors this.ignore = yaml.safeLoad(fs.readFileSync(ignoreFile, 'utf-8')); // TODO: parse errors
// resolve ignore paths // resolve ignore paths
for (const fileName of Object.keys(this.exceptions)) { for (const fileName of Object.keys(this.ignore)) {
this.exceptions[path.resolve(dirname(exceptionsFile), fileName)] = this.exceptions[fileName]; this.ignore[path.resolve(dirname(ignoreFile), fileName)] = this.ignore[fileName];
for (const ruleId of Object.keys(this.exceptions[fileName])) { for (const ruleId of Object.keys(this.ignore[fileName])) {
this.exceptions[fileName][ruleId] = new Set(this.exceptions[fileName][ruleId]); this.ignore[fileName][ruleId] = new Set(this.ignore[fileName][ruleId]);
} }
delete this.exceptions[fileName]; delete this.ignore[fileName];
} }
} }
} }
saveExceptions() { saveIgnore() {
const dir = this.configFile ? path.dirname(this.configFile) : process.cwd(); const dir = this.configFile ? path.dirname(this.configFile) : process.cwd();
const ignoreFile = path.join(dir, EXCEPTIONS_FILE); const ignoreFile = path.join(dir, IGNORE_FILE);
const mapped: Record<string, any> = {}; const mapped: Record<string, any> = {};
for (const absFileName of Object.keys(this.exceptions)) { for (const absFileName of Object.keys(this.ignore)) {
const ruleExceptions = mapped[path.relative(dir, absFileName)] = this.exceptions[absFileName]; const ignoredRules = mapped[path.relative(dir, absFileName)] = this.ignore[absFileName];
for (const ruleId of Object.keys(ruleExceptions)) { for (const ruleId of Object.keys(ignoredRules)) {
ruleExceptions[ruleId] = Array.from(ruleExceptions[ruleId]) as any; ignoredRules[ruleId] = Array.from(ignoredRules[ruleId]) as any;
} }
} }
fs.writeFileSync(ignoreFile, yaml.safeDump(mapped)); fs.writeFileSync(ignoreFile, yaml.safeDump(mapped));
} }
addException(message: NormalizedReportMessage) { addException(message: NormalizedReportMessage) {
const ignore = this.exceptions; const ignore = this.ignore;
const loc = message.location[0]; const loc = message.location[0];
if (loc.pointer === undefined) return; if (loc.pointer === undefined) return;
@@ -145,11 +145,11 @@ export class LintConfig {
ruleIgnore.add(loc.pointer); ruleIgnore.add(loc.pointer);
} }
addMessageToExceptions(message: NormalizedReportMessage) { addMessageToIgnore(message: NormalizedReportMessage) {
const loc = message.location[0]; const loc = message.location[0];
if (loc.pointer === undefined) return message; if (loc.pointer === undefined) return message;
const fileIgnore = this.exceptions[loc.source.absoluteRef] || {}; const fileIgnore = this.ignore[loc.source.absoluteRef] || {};
const ruleIgnore = fileIgnore[message.ruleId]; const ruleIgnore = fileIgnore[message.ruleId];
const ignored = ruleIgnore && ruleIgnore.has(loc.pointer); const ignored = ruleIgnore && ruleIgnore.has(loc.pointer);
return ignored return ignored

View File

@@ -76,7 +76,7 @@ export async function validateDocument(opts: {
ctx, ctx,
}); });
return ctx.messages.map((message) => config.addMessageToExceptions(message)); return ctx.messages.map((message) => config.addMessageToIgnore(message));
} }
} }
} }