From a2b627d2351f45d70e1db91f93af87c1f1141c33 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Fri, 7 Feb 2020 18:25:30 +1100 Subject: [PATCH] refactor: abstract lintoutcome construction --- @commitlint/lint/src/lint-outcome.ts | 56 ++++++++++++++++++++++++++++ @commitlint/lint/src/lint.ts | 42 ++++++--------------- 2 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 @commitlint/lint/src/lint-outcome.ts diff --git a/@commitlint/lint/src/lint-outcome.ts b/@commitlint/lint/src/lint-outcome.ts new file mode 100644 index 0000000000..bad4c5dca3 --- /dev/null +++ b/@commitlint/lint/src/lint-outcome.ts @@ -0,0 +1,56 @@ +import { + LintOutcome as LintOutcomeData, + LintRuleOutcome, + Commit +} from '@commitlint/types'; +import {buildCommitMesage} from './commit-message'; + +export interface EmptyInit { + message: string; +} + +export interface ResultsInit { + parsed: Commit; + results: LintRuleOutcome[]; +} + +export class LintOutcome implements LintOutcomeData { + public readonly input: string; + public readonly valid: boolean; + public readonly errors: LintRuleOutcome[]; + public readonly warnings: LintRuleOutcome[]; + + public static empty(init: EmptyInit): LintOutcome { + return new LintOutcome({ + valid: true, + errors: [], + warnings: [], + input: init.message + }); + } + + public static fromResults(init: ResultsInit): LintOutcome { + const errors = init.results.filter( + result => result.level === 2 && !result.valid + ); + const warnings = init.results.filter( + result => result.level === 1 && !result.valid + ); + + const valid = errors.length === 0; + + return { + valid, + errors, + warnings, + input: buildCommitMesage(init.parsed) + }; + } + + private constructor(init: LintOutcomeData) { + this.input = init.input; + this.valid = init.valid; + this.errors = init.errors; + this.warnings = init.warnings; + } +} diff --git a/@commitlint/lint/src/lint.ts b/@commitlint/lint/src/lint.ts index 40aa4b241d..569c799abe 100644 --- a/@commitlint/lint/src/lint.ts +++ b/@commitlint/lint/src/lint.ts @@ -2,16 +2,14 @@ import util from 'util'; import isIgnored from '@commitlint/is-ignored'; import parse from '@commitlint/parse'; import defaultRules from '@commitlint/rules'; -import toPairs from 'lodash/toPairs'; -import {buildCommitMesage} from './commit-message'; import { LintRuleConfig, LintOptions, LintRuleOutcome, Rule, - RuleSeverity, - LintOutcome + RuleSeverity } from '@commitlint/types'; +import {LintOutcome} from './lint-outcome'; export default async function lint( message: string, @@ -27,28 +25,19 @@ export default async function lint( if ( isIgnored(message, {defaults: opts.defaultIgnores, ignores: opts.ignores}) ) { - return { - valid: true, - errors: [], - warnings: [], - input: message - }; + return LintOutcome.empty({message}); } // Parse the commit message const parsed = await parse(message, undefined, opts.parserOpts); + if ( parsed.header === null && parsed.body === null && parsed.footer === null ) { // Commit is empty, skip - return { - valid: true, - errors: [], - warnings: [], - input: message - }; + return LintOutcome.empty({message}); } const allRules: Map | Rule> = new Map( @@ -79,7 +68,7 @@ export default async function lint( ); } - const invalid = toPairs(rulesConfig) + const invalid = Object.entries(rulesConfig) .map(([name, config]) => { if (!Array.isArray(config)) { return new Error( @@ -146,7 +135,7 @@ export default async function lint( } // Validate against all rules - const results = toPairs(rulesConfig) + const results = Object.entries(rulesConfig) .filter(([, [level]]) => level > 0) .map(entry => { const [name, config] = entry; @@ -175,17 +164,8 @@ export default async function lint( }) .filter((result): result is LintRuleOutcome => result !== null); - const errors = results.filter(result => result.level === 2 && !result.valid); - const warnings = results.filter( - result => result.level === 1 && !result.valid - ); - - const valid = errors.length === 0; - - return { - valid, - errors, - warnings, - input: buildCommitMesage(parsed) - }; + return LintOutcome.fromResults({ + results, + parsed + }); }