@@ -89,21 +89,98 @@ format({
89
89
* ** Signature**
90
90
91
91
``` ts
92
+ /**
93
+ * How to handle violation of rule
94
+ * 0 - ignore
95
+ * 1 - warn
96
+ * 2 - throw
97
+ */
92
98
type RuleLevel = 0 | 1 | 2 ;
99
+
100
+ /*
101
+ * Application of rule
102
+ * always - positive
103
+ * never - negative
104
+ */
93
105
type RuleCondition = ' always' | ' never' ;
106
+
107
+ /*
108
+ * Additional, optional options to pass to rule
109
+ */
94
110
type RuleOption = any ;
111
+
112
+ /**
113
+ * Basic complete rule definition
114
+ */
95
115
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
+ */
96
121
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
+ */
97
127
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
+ */
98
133
type AsyncFunctionRule () = > Promise <PrimitiveRule >;
134
+
135
+ /*
136
+ * Polymorphic rule struct
137
+ */
99
138
type Rule = PrimitiveRule | FunctionRule | AsyncFunctionRule ;
100
139
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
+
101
168
type Config = {
169
+ /*
170
+ * Relatives path to all extendend configurations.
171
+ */
102
172
extends: string [];
173
+ /*
174
+ * Expanded parser preset, if any
175
+ */
176
+ parserPreset? : ParserPreset ;
177
+ /*
178
+ * Merged map of rules to check against
179
+ */
103
180
rules: {[ruleName : string ]: Rule };
104
- }
181
+ };
105
182
106
- load (seed : Config = {}) => Promise < Config > ;
183
+ load (seed : Seed = {}) => Promise < Config > ;
107
184
```
108
185
109
186
* ** Example**
@@ -121,7 +198,11 @@ load({
121
198
122
199
load ({extends: [' ./package' ]})
123
200
.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: {}}}
125
206
```
126
207
127
208
### read
@@ -186,7 +267,11 @@ type Report = {
186
267
warnings: Problem [];
187
268
}
188
269
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 > ;
190
275
```
191
276
192
277
* ** Basic Example**
@@ -213,6 +298,11 @@ lint('foo: bar', {'type-enum': [1, 'always', ['bar']]})
213
298
name: 'type-enum',
214
299
message: 'type must be one of [bar]' } ] }
215
300
*/
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: [] }
216
306
```
217
307
218
308
* ** Load configuration**
@@ -225,7 +315,7 @@ const CONFIG = {
225
315
};
226
316
227
317
load (CONFIG )
228
- .then (opts => lint (' foo: bar' , opts .rules ))
318
+ .then (opts => lint (' foo: bar' , opts .rules , opts . parserPreset ? {parserOpts : opts . parserPreset . parserOpts } : {} ))
229
319
.then (report => console .log (report));
230
320
/* =>
231
321
{ valid: false,
@@ -260,8 +350,8 @@ const {lint, load, read} = require('@commitlint/core');
260
350
261
351
Promise .all ([load (), read ({from: ' HEAD~1' })])
262
352
.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 } : {} );
265
355
})
266
356
.then (report => console .log (JSON .stringify (result .valid )));
267
357
```
0 commit comments