/
index.d.ts
411 lines (365 loc) · 11.1 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
declare module 'stylelint' {
import type * as PostCSS from 'postcss';
import type { GlobbyOptions } from 'globby';
import type { cosmiconfig } from 'cosmiconfig';
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]: Function;
};
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 };
export type StylelintPostcssResult = {
ruleSeverities: { [k: string]: Severity };
customMessages: { [k: string]: any };
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' | 'json' | 'string' | 'tap' | 'unix' | 'verbose';
export type CustomSyntax = string | PostCSS.Syntax;
export type PluginContext = {
fix?: boolean | undefined;
newline?: string | undefined;
};
export type RuleMessages = { [message: string]: string | ((...args: any[]) => string) };
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 Rule<P = any, S = any> = RuleBase<P, S> & {
ruleName: string;
messages: RuleMessages;
primaryOptionArray?: boolean;
};
export type Plugin<P = any, S = any> = RuleBase<P, S>;
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;
};
export type LinterOptions = {
files?: string | string[];
globbyOptions?: GlobbyOptions;
cache?: boolean;
cacheLocation?: 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;
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 = {
column: number;
file?: string;
input: {
column: number;
file?: string;
line: number;
source: string;
};
line: number;
message: string;
name: string;
reason: string;
source: string;
};
export type Warning = {
line: number;
column: 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;
};
export type Problem = {
ruleName: string;
result: PostcssResult;
message: string;
node: PostCSS.Node;
index?: number;
word?: string;
line?: number;
};
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, plugin: Plugin) => { 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
* `null` 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 | null>;
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;
};
};
/**
* 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>;
_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;
}