Skip to content

Commit

Permalink
refactor: abstract lintoutcome construction
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Feb 7, 2020
1 parent 9079ae4 commit a2b627d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 31 deletions.
56 changes: 56 additions & 0 deletions @commitlint/lint/src/lint-outcome.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
42 changes: 11 additions & 31 deletions @commitlint/lint/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<string, Rule<unknown> | Rule<never>> = new Map(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
});
}

0 comments on commit a2b627d

Please sign in to comment.