/
index.d.ts
552 lines (501 loc) · 14.8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
declare module 'stylelint' {
import type * as PostCSS from 'postcss';
import type { GlobbyOptions } from 'globby';
import type { cosmiconfig } from 'cosmiconfig';
import type * as fileEntryCache from 'file-entry-cache';
namespace stylelint {
export type Severity = 'warning' | 'error';
export type ConfigExtends = string | string[];
export type ConfigPlugins = string | string[];
export type ConfigProcessor = string | [string, Object];
export type ConfigProcessors = string | ConfigProcessor[];
export type ConfigIgnoreFiles = string | string[];
export type ConfigRuleSettings<T, O extends Object> =
| null
| undefined
| NonNullable<T>
| [NonNullable<T>]
| [NonNullable<T>, O];
export type ConfigRules = {
[ruleName: string]: ConfigRuleSettings<any, Object>;
};
export type ConfigOverride = Omit<Config, 'overrides'> & {
files: string | string[];
};
export type DisableOptions = {
except?: (string | RegExp)[];
severity?: Severity;
};
export type DisableSettings = ConfigRuleSettings<boolean, DisableOptions>;
export type ResultProcessor = (result: LintResult, file: string | undefined) => LintResult;
export type Config = {
extends?: ConfigExtends;
plugins?: ConfigPlugins;
pluginFunctions?: {
[pluginName: string]: Rule;
};
processors?: ConfigProcessors;
processorFunctions?: Function[];
ignoreFiles?: ConfigIgnoreFiles;
ignorePatterns?: string;
rules?: ConfigRules;
codeProcessors?: CodeProcessor[];
resultProcessors?: ResultProcessor[];
quiet?: boolean;
defaultSeverity?: Severity;
ignoreDisables?: DisableSettings;
reportNeedlessDisables?: DisableSettings;
reportInvalidScopeDisables?: DisableSettings;
reportDescriptionlessDisables?: DisableSettings;
overrides?: ConfigOverride[];
customSyntax?: CustomSyntax;
};
// A meta-type that returns a union over all properties of `T` whose values
// have type `U`.
type PropertyNamesOfType<T, U> = {
[K in keyof T]-?: T[K] extends U ? K : never;
}[keyof T];
export type DisablePropertyName = PropertyNamesOfType<Config, DisableSettings>;
// This type has the same properties as `CosmiconfigResult` from `cosmiconfig`.
export type CosmiconfigResult = {
config: Config;
filepath: string;
isEmpty?: boolean;
} | null;
export type ConfigurationError = Error & { code: 78 };
export type DisabledRange = {
comment: PostCSS.Comment;
start: number;
strictStart: boolean;
end?: number;
strictEnd?: boolean;
rules?: string[];
description?: string;
};
export type DisabledRangeObject = {
[ruleName: string]: DisabledRange[];
};
export type DisabledWarning = { line: number; rule: string };
type FileCache = {
calcHashOfConfig: (config: Config) => void;
hasFileChanged: (absoluteFilepath: string) => boolean;
reconcile: () => void;
destroy: () => void;
removeEntry: (absoluteFilepath: string) => void;
};
export type StylelintPostcssResult = {
ruleSeverities: { [ruleName: string]: Severity };
customMessages: { [ruleName: string]: RuleMessage };
ruleMetadata: { [ruleName: string]: Partial<RuleMeta> };
quiet?: boolean;
disabledRanges: DisabledRangeObject;
disabledWarnings?: DisabledWarning[];
ignored?: boolean;
stylelintError?: boolean;
disableWritingFix?: boolean;
config?: Config;
ruleDisableFix?: boolean;
};
type EmptyResult = {
root: {
nodes?: undefined;
source: {
lang?: undefined;
input: {
file?: string;
};
};
};
messages: PostCSS.Message[];
opts: undefined;
};
export type WarningOptions = PostCSS.WarningOptions & {
stylelintType?: string;
severity?: Severity;
rule?: string;
};
export type PostcssResult = (PostCSS.Result | EmptyResult) & {
stylelint: StylelintPostcssResult;
warn(message: string, options?: WarningOptions): void;
};
export type Formatter = (results: LintResult[], returnValue: LinterResult) => string;
export type FormatterType =
| 'compact'
| 'github'
| 'json'
| 'string'
| 'tap'
| 'unix'
| 'verbose';
export type CustomSyntax = string | PostCSS.Syntax;
export type PluginContext = {
fix?: boolean | undefined;
newline?: string | undefined;
};
// Note: With strict function types enabled, function signatures are checked contravariantly.
// This means that it would not be possible for rule authors to narrow the message function
// parameters to e.g. just `string`. Declaring the type for rule message functions through
// method declarations tricks TypeScript into bivariant signature checking. More details can
// be found here: https://stackoverflow.com/questions/52667959/what-is-the-purpose-of-bivariancehack-in-typescript-types.
// and in the original discussion: https://github.com/stylelint/stylelint/pull/6147#issuecomment-1155337016.
export type RuleMessageFunc = {
bivariance(...args: (string | number | boolean | RegExp)[]): string;
}['bivariance'];
export type RuleMessage = string | RuleMessageFunc;
export type RuleMessages = { [message: string]: RuleMessage };
export type RuleOptionsPossibleFunc = (value: unknown) => boolean;
export type RuleOptionsPossible = boolean | number | string | RuleOptionsPossibleFunc;
export type RuleOptions = {
actual: unknown;
possible?:
| RuleOptionsPossibleFunc
| RuleOptionsPossible[]
| Record<string, RuleOptionsPossible[]>;
optional?: boolean;
};
export type RuleBase<P = any, S = any> = (
primaryOption: P,
secondaryOptions: Record<string, S>,
context: PluginContext,
) => (root: PostCSS.Root, result: PostcssResult) => Promise<void> | void;
export type RuleMeta = {
url: string;
deprecated?: boolean;
fixable?: boolean;
};
export type Rule<P = any, S = any> = RuleBase<P, S> & {
ruleName: string;
messages: RuleMessages;
primaryOptionArray?: boolean;
meta?: RuleMeta;
};
export type Plugin = RuleBase;
export type CodeProcessor = (code: string, file: string | undefined) => string;
export type GetPostcssOptions = {
code?: string;
codeFilename?: string;
filePath?: string;
codeProcessors?: CodeProcessor[];
syntax?: string;
customSyntax?: CustomSyntax;
};
export type GetLintSourceOptions = GetPostcssOptions & {
existingPostcssResult?: PostCSS.Result;
cache?: boolean;
};
export type LinterOptions = {
files?: string | string[];
globbyOptions?: GlobbyOptions;
cache?: boolean;
cacheLocation?: string;
cacheStrategy?: string;
code?: string;
codeFilename?: string;
config?: Config;
configFile?: string;
configBasedir?: string;
/**
* The working directory to resolve files from. Defaults to the
* current working directory.
*/
cwd?: string;
ignoreDisables?: boolean;
ignorePath?: string | string[];
ignorePattern?: string[];
reportDescriptionlessDisables?: boolean;
reportNeedlessDisables?: boolean;
reportInvalidScopeDisables?: boolean;
maxWarnings?: number;
/** @deprecated Use `customSyntax` instead. Using this option will result in an error. */
syntax?: string;
customSyntax?: CustomSyntax;
formatter?: FormatterType | Formatter;
disableDefaultIgnores?: boolean;
fix?: boolean;
allowEmptyInput?: boolean;
quiet?: boolean;
};
export type CssSyntaxError = {
file?: string;
input: {
column: number;
file?: string;
line: number;
source: string;
};
/**
* The line of the inclusive start position of the error.
*/
line: number;
/**
* The column of the inclusive start position of the error.
*/
column: number;
/**
* The line of the exclusive end position of the error.
*/
endLine?: number;
/**
* The column of the exclusive end position of the error.
*/
endColumn?: number;
message: string;
name: string;
reason: string;
source: string;
};
export type Warning = {
/**
* The line of the inclusive start position of the warning.
*/
line: number;
/**
* The column of the inclusive start position of the warning.
*/
column: number;
/**
* The line of the exclusive end position of the warning.
*/
endLine?: number;
/**
* The column of the exclusive end position of the warning.
*/
endColumn?: number;
rule: string;
severity: Severity;
text: string;
stylelintType?: string;
};
export type LintResult = {
source?: string;
deprecations: {
text: string;
reference: string;
}[];
invalidOptionWarnings: {
text: string;
}[];
parseErrors: (PostCSS.Warning & { stylelintType: string })[];
errored?: boolean;
warnings: Warning[];
ignored?: boolean;
/**
* Internal use only. Do not use or rely on this property. It may
* change at any time.
* @internal
*/
_postcssResult?: PostcssResult;
};
export type DisableReportRange = {
rule: string;
start: number;
end?: number;
};
export type RangeType = DisabledRange & { used?: boolean };
export type DisableReportEntry = {
source?: string;
ranges: DisableReportRange[];
};
export type LinterResult = {
/**
* The working directory from which the linter was run when the
* results were generated.
*/
cwd: string;
results: LintResult[];
errored: boolean;
output: any;
maxWarningsExceeded?: {
maxWarnings: number;
foundWarnings: number;
};
reportedDisables: DisableOptionsReport;
descriptionlessDisables?: DisableOptionsReport;
needlessDisables?: DisableOptionsReport;
invalidScopeDisables?: DisableOptionsReport;
/**
* Each rule metadata by name.
*/
ruleMetadata: { [ruleName: string]: Partial<RuleMeta> };
};
export type Problem = {
ruleName: string;
result: PostcssResult;
message: RuleMessage;
messageArgs?: Parameters<RuleMessageFunc> | undefined;
node: PostCSS.Node;
/**
* The inclusive start index of the problem, relative to the node's
* source text.
*/
index?: number;
/**
* The exclusive end index of the problem, relative to the node's
* source text.
*/
endIndex?: number;
/**
* The inclusive start position of the problem, relative to the
* node's source text. If provided, this will be used instead of
* `index`.
*/
start?: {
line: number;
column: number;
};
/**
* The exclusive end position of the problem, relative to the
* node's source text. If provided, this will be used instead of
* `endIndex`.
*/
end?: {
line: number;
column: number;
};
word?: string;
line?: number;
};
export type LonghandSubPropertiesOfShorthandProperties = ReadonlyMap<
| 'animation'
| 'background'
| 'border'
| 'border-block-end'
| 'border-block-start'
| 'border-bottom'
| 'border-color'
| 'border-image'
| 'border-inline-end'
| 'border-inline-start'
| 'border-left'
| 'border-radius'
| 'border-right'
| 'border-style'
| 'border-top'
| 'border-width'
| 'column-rule'
| 'columns'
| 'flex'
| 'flex-flow'
| 'font'
| 'grid'
| 'grid-area'
| 'grid-column'
| 'grid-gap'
| 'grid-row'
| 'grid-template'
| 'list-style'
| 'margin'
| 'mask'
| 'outline'
| 'padding'
| 'text-decoration'
| 'text-emphasis'
| 'transition',
ReadonlySet<string>
>;
export type PublicApi = PostCSS.PluginCreator<PostcssPluginOptions> & {
/**
* Runs stylelint with the given options and returns a Promise that
* resolves to the results.
*/
lint: (options: LinterOptions) => Promise<LinterResult>;
/**
* Available rules.
*/
rules: { [k: string]: Rule };
/**
* Result report formatters by name.
*/
formatters: { [k: string]: Formatter };
/**
* Creates a Stylelint plugin.
*/
createPlugin: (ruleName: string, rule: Rule) => { ruleName: string; rule: Rule };
/**
* Internal use only. Do not use or rely on this method. It may
* change at any time.
* @internal
*/
createLinter: (options: LinterOptions) => InternalApi;
/**
* Resolves the effective configuation for a given file. Resolves to
* `undefined` if no config is found.
* @param filePath - The path to the file to get the config for.
* @param options - The options to use when creating the Stylelint instance.
*/
resolveConfig: (
filePath: string,
options?: Pick<LinterOptions, 'cwd' | 'config' | 'configBasedir' | 'configFile'>,
) => Promise<stylelint.Config | undefined>;
utils: {
/**
* Report a problem.
*
* This function accounts for `disabledRanges` attached to the result.
* That is, if the reported problem is within a disabledRange,
* it is ignored. Otherwise, it is attached to the result as a
* postcss warning.
*
* It also accounts for the rule's severity.
*
* You *must* pass *either* a node or a line number.
*/
report: (problem: Problem) => void;
/**
* Given an object of problem messages, return another
* that provides the same messages postfixed with the rule
* that has been violated.
*/
ruleMessages: <T extends RuleMessages, R extends { [K in keyof T]: T[K] }>(
ruleName: string,
messages: T,
) => R;
/**
* Validate a rule's options.
*
* See existing rules for examples.
*/
validateOptions: (
result: PostcssResult,
ruleName: string,
...optionDescriptions: RuleOptions[]
) => boolean;
/**
* Useful for third-party code (e.g. plugins) to run a PostCSS Root
* against a specific rule and do something with the warnings
*/
checkAgainstRule: <T, O extends Object>(
options: { ruleName: string; ruleSettings: ConfigRuleSettings<T, O>; root: PostCSS.Root },
callback: (warning: PostCSS.Warning) => void,
) => void;
};
reference: {
longhandSubPropertiesOfShorthandProperties: LonghandSubPropertiesOfShorthandProperties;
};
};
/**
* Internal use only. Do not use or rely on this type. It may change at
* any time.
* @internal
*/
export type InternalApi = {
_options: LinterOptions & { cwd: string };
_extendExplorer: ReturnType<typeof cosmiconfig>;
_specifiedConfigCache: Map<Config, Promise<CosmiconfigResult>>;
_postcssResultCache: Map<string, PostCSS.Result>;
_fileCache: FileCache;
_getPostcssResult: (options?: GetPostcssOptions) => Promise<PostCSS.Result>;
_lintSource: (options: GetLintSourceOptions) => Promise<PostcssResult>;
_createStylelintResult: (
postcssResult: PostcssResult,
filePath?: string,
) => Promise<LintResult>;
getConfigForFile: (searchPath?: string, filePath?: string) => Promise<CosmiconfigResult>;
isPathIgnored: (s?: string) => Promise<boolean>;
};
export type DisableOptionsReport = DisableReportEntry[];
export type PostcssPluginOptions = Omit<LinterOptions, 'syntax' | 'customSyntax'> | Config;
}
const stylelint: stylelint.PublicApi;
export = stylelint;
}