-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: abstract lintoutcome construction
- Loading branch information
Showing
2 changed files
with
67 additions
and
31 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
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; | ||
} | ||
} |
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