Skip to content

Latest commit

 

History

History
172 lines (137 loc) · 3.14 KB

lint.md

File metadata and controls

172 lines (137 loc) · 3.14 KB

@commitlint/lint

Lint a string against commitlint rules

Install

npm install --save @commitlint/lint

Signature

typeRuleLevel=0|1|2;typeRuleCondition='always'|'never';typeRuleOption=any;typePrimitiveRule=[RuleLevel,RuleCondition,RuleOption?];typeAsyncRule=Promise<PrimitiveRule>;typeFunctionRule=()=>PrimitiveRule;typeAsyncFunctionRule=()=>Promise<PrimitiveRule>;typeRule=PrimitiveRule|FunctionRule|AsyncFunctionRule;typeProblem={level: number;valid: boolean;name: string;message: string;}typeReport={valid: boolean;errors: Problem[];warnings: Problem[];}typeOptions={parserOpts?: any;};lint(message: string,rules: {[ruleName: string]: Rule},opts?: Options)=>Promise<Report>;

Basic Examples

Import

importlintfrom"@commitlint/lint";

Usage without config

constreport=awaitlint("foo: bar");console.log(report);// => { valid: true, errors: [], warnings: [] }

Usage with type-enum rules and valid message

constreport=awaitlint("foo: bar",{"type-enum": [1,"always",["foo"]]});console.log(report);// => { valid: true, errors: [], warnings: [] }

Usage with type-enum rules and invalid message

constreport=awaitlint("foo: bar",{"type-enum": [1,"always",["bar"]]});console.log(report);/* => {  valid: false, errors: [], warnings: [  {  level: 1, valid: false, name: 'type-enum', message: 'type must be one of [bar]'  }  ]}*/

Usage with custom parser options

constopts={parserOpts: {headerPattern: /^(\w*)-(\w*)/,headerCorrespondence: ["type","scope"],},};constreport=awaitlint("foo-bar",{"type-enum": [2,"always",["foo"]]},opts,);console.log(report);// => { valid: true, errors: [], warnings: [] }

Load configuration

importloadfrom"@commitlint/load";importlintfrom"@commitlint/lint";constCONFIG={extends: ["@commitlint/config-conventional"],};constopts=awaitload(CONFIG);constreport=awaitlint("foo: bar",opts.rules,opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {},);console.log(report);/* => {  valid: false, errors: [  {  level: 2, valid: false, name: 'type-enum', message: 'type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]'  }  ], warnings: [] }*/

Read git history

importlintfrom"@commitlint/lint";importreadfrom"@commitlint/read";constRULES={"type-enum": [2,"always",["foo"]],};constcommits=awaitread({to: "HEAD",from: "HEAD~2"});console.info(commits.map((commit)=>lint(commit,RULES)));

Simplified last-commit checker

importloadfrom"@commitlint/load";importreadfrom"@commitlint/read";importlintfrom"@commitlint/lint";const{ rules, parserPreset }=load();const[commit]=awaitread({from: "HEAD~1"});constreport=awaitlint(commit,rules,parserPreset ? {parserOpts: parserPreset.parserOpts} : {},);console.log(JSON.stringify(result.valid));
close