Skip to content

Commit faf5d79

Browse files
committed
docs(core): document parserPreset feature
1 parent c981cbe commit faf5d79

File tree

1 file changed

+97
-7
lines changed

1 file changed

+97
-7
lines changed

docs/reference-api.md

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,98 @@ format({
8989
* **Signature**
9090

9191
```ts
92+
/**
93+
* How to handle violation of rule
94+
* 0 - ignore
95+
* 1 - warn
96+
* 2 - throw
97+
*/
9298
type RuleLevel = 0 | 1 | 2;
99+
100+
/*
101+
* Application of rule
102+
* always - positive
103+
* never - negative
104+
*/
93105
type RuleCondition = 'always' | 'never';
106+
107+
/*
108+
* Additional, optional options to pass to rule
109+
*/
94110
type RuleOption = any;
111+
112+
/**
113+
* Basic complete rule definition
114+
*/
95115
type PrimitiveRule = [RuleLevel, RuleCondition, RuleOption?];
116+
117+
/*
118+
* Async rules are resolved during config lookup.
119+
* They can be used to set up linting rules based on e.g. the project fs
120+
*/
96121
type AsyncRule = Promise<PrimitiveRule>;
122+
123+
/*
124+
* Function rules are executed during config lookup.
125+
* They can be used to set up linting rules based on e.g. the project fs
126+
*/
97127
type FunctionRule = () => PrimitiveRule;
128+
129+
/*
130+
* Async function rules are executed and awaited during config lookup.
131+
* They can be used to set up linting rules based on e.g. the project fs
132+
*/
98133
type AsyncFunctionRule () => Promise<PrimitiveRule>;
134+
135+
/*
136+
* Polymorphic rule struct
137+
*/
99138
type Rule = PrimitiveRule | FunctionRule | AsyncFunctionRule;
100139

140+
/*
141+
* Parser preset for conventional commits
142+
*/
143+
type ParserPreset = {
144+
name: string;
145+
path: string;
146+
opts: any;
147+
};
148+
149+
type Seed = {
150+
/*
151+
* ids resolveable from cwd or configuration file.
152+
* Imported and merged into configuration
153+
* with increasing precedence, with top level config taking the highest.
154+
*/
155+
extends?: string[];
156+
/*
157+
* id resolveable from cwd or configuration file.
158+
* Imported and expanded to {ParserPreset}.
159+
* Top level parserPresets override presets in extended configuration.
160+
*/
161+
parserPreset?: string;
162+
/**
163+
* Initial map of rules to check against
164+
*/
165+
rules?: {[ruleName: string]: Rule};
166+
};
167+
101168
type Config = {
169+
/*
170+
* Relatives path to all extendend configurations.
171+
*/
102172
extends: string[];
173+
/*
174+
* Expanded parser preset, if any
175+
*/
176+
parserPreset?: ParserPreset;
177+
/*
178+
* Merged map of rules to check against
179+
*/
103180
rules: {[ruleName: string]: Rule};
104-
}
181+
};
105182

106-
load(seed: Config = {}) => Promise<Config>;
183+
load(seed: Seed = {}) => Promise<Config>;
107184
```
108185

109186
* **Example**
@@ -121,7 +198,11 @@ load({
121198

122199
load({extends: ['./package']})
123200
.then(config => console.log(config));
124-
// => { extends: [], rules: {} }
201+
// => { extends: ['./package', './package-b'], rules: {} }
202+
203+
load({parserPreset: './parser-preset.js'})
204+
.then(config => console.log(config));
205+
// => { extends: [], rules: {}, parserPreset: {name: './parser-preset.js', path: './parser-preset.js', opts: {}}}
125206
```
126207

127208
### read
@@ -186,7 +267,11 @@ type Report = {
186267
warnings: Problem[];
187268
}
188269

189-
lint(message: string, rules: {[ruleName: string]: Rule}) => Promise<Report>;
270+
type Options = {
271+
parserOpts?: any;
272+
};
273+
274+
lint(message: string, rules: {[ruleName: string]: Rule}, opts?: Options) => Promise<Report>;
190275
```
191276

192277
* **Basic Example**
@@ -213,6 +298,11 @@ lint('foo: bar', {'type-enum': [1, 'always', ['bar']]})
213298
name: 'type-enum',
214299
message: 'type must be one of [bar]' } ] }
215300
*/
301+
302+
const opts = {parserOpts: {headerPattern: /^(\w*)-(\w*)/, headerCorrespondence: ['type', 'scope']}};
303+
304+
lint('foo-bar', {'type-enum': [2, 'always', ['foo']]}, opts).then(report => console.log(report));
305+
// => { valid: true, errors: [], warnings: [] }
216306
```
217307

218308
* **Load configuration**
@@ -225,7 +315,7 @@ const CONFIG = {
225315
};
226316

227317
load(CONFIG)
228-
.then(opts =>lint('foo: bar', opts.rules))
318+
.then(opts => lint('foo: bar', opts.rules, opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}))
229319
.then(report => console.log(report));
230320
/* =>
231321
{ valid: false,
@@ -260,8 +350,8 @@ const {lint, load, read} = require('@commitlint/core');
260350

261351
Promise.all([load(), read({from: 'HEAD~1'})])
262352
.then(tasks => {
263-
const [{rules}, [commit]] = tasks;
264-
return lint(commit, rules);
353+
const [{rules, parserPreset}, [commit]] = tasks;
354+
return lint(commit, rules, parserPreset ? {parserOpts: parserPreset.parserOpts}: {});
265355
})
266356
.then(report => console.log(JSON.stringify(result.valid)));
267357
```

0 commit comments

Comments
 (0)