diff --git a/types/stylelint/index.d.ts b/types/stylelint/index.d.ts index 7d1f0fccc4..adf5057a07 100644 --- a/types/stylelint/index.d.ts +++ b/types/stylelint/index.d.ts @@ -1,37 +1,56 @@ -// Type definitions for stylelint 9.4 +// Type definitions for stylelint 9.10 // Project: https://github.com/stylelint/stylelint, https://stylelint.io // Definitions by: Alan Agius // Filips Alpe +// James Garbutt // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -export type FormatterType = "json" | "string" | "verbose" | "compact"; +import * as postcss from 'postcss'; -export type SyntaxType = "scss" | "sass" | "less" | "sugarss"; +export type FormatterType = "json" | "string" | "verbose" | "compact" | "unix"; + +export type SyntaxType = "css-in-js" + | "html" + | "less" + | "markdown" + | "sass" + | "scss" + | "sugarss"; + +export interface Configuration { + rules: Record; + extends: string | string[]; + plugins: string[]; + processors: string[]; + ignoreFiles: string|string[]; + defaultSeverity: "warning"|"error"; +} export interface LinterOptions { - code?: string; - codeFilename?: string; - config?: JSON; - configBasedir?: string; - configFile?: string; - configOverrides?: JSON; - cache?: boolean; - cacheLocation?: string; - files?: string | string[]; - fix?: boolean; - formatter?: FormatterType; - ignoreDisables?: boolean; - reportNeedlessDisables?: boolean; - ignorePath?: boolean; - syntax?: SyntaxType; - customSyntax?: string; + cache: boolean; + cacheLocation: string; + code: string; + codeFilename: string; + config: Partial; + configBasedir: string; + configFile: string; + configOverrides: Partial; + customSyntax: string; + disableDefaultIgnores: boolean; + files: string | string[]; + fix: boolean; + formatter: FormatterType; + ignoreDisables: boolean; + ignorePath: string; + maxWarnings: number; + reportNeedlessDisables: boolean; + syntax: SyntaxType; } export interface LinterResult { errored: boolean; output: string; - postcssResults: any[]; results: LintResult[]; } @@ -49,11 +68,12 @@ export namespace formatters { function string(results: LintResult[]): string; function compact(results: LintResult[]): string; function verbose(results: LintResult[]): string; + function unix(results: LintResult[]): string; } -export function lint(options?: LinterOptions): Promise; +export function lint(options?: Partial): Promise; -export type RuleOption = { +export type ValidateOptionsAssertion = { actual: any; possible?: any; optional?: false; @@ -63,25 +83,74 @@ export type RuleOption = { optional: true; }; +export type RuleMessageValue = string | ((...args: any[]) => string); + export namespace utils { function report(violation: { ruleName: string; - result: LintResult; + result: postcss.Result; message: string; - node: any; + node: postcss.Node; index?: number; word?: string; line?: number; }): void; - function ruleMessages(ruleName: string, messages: { [key: string]: any; }): typeof messages; + function ruleMessages( + ruleName: string, + messages: T): T; - function validateOptions(result: LintResult, ruleName: string, ...options: RuleOption[]): boolean; + function validateOptions(result: postcss.Result, ruleName: string, + ...options: ValidateOptionsAssertion[]): boolean; - function checkAgainstRule(options: { ruleName: string; ruleSettings: any; root: any; }, callback: (warning: string) => void): void; + function checkAgainstRule(options: { + ruleName: string; + ruleSettings: any; + root: any; + }, callback: (warning: string) => void): void; } +export type Plugin = (primaryOption: any, secondaryOptions?: object) => + (root: postcss.Root, result: postcss.Result) => void|PromiseLike; + export function createPlugin( ruleName: string, - plugin: (options: RuleOption[]) => (root: any, result: LintResult) => void, + plugin: Plugin ): any; + +export interface RuleTesterResult { + expected: number; + actual: number; + description: string; +} + +export interface RuleTesterTest { + code: string; + description?: string; +} + +export interface RuleTesterTestRejected extends RuleTesterTest { + line?: number; + column?: number; + only?: boolean; + message?: string; +} + +export interface RuleTesterSchema { + ruleName: string; + syntax?: SyntaxType; + config?: any; + accept?: RuleTesterTest[]; + reject?: RuleTesterTestRejected[]; +} + +export interface RuleTesterContext { + comparisonCount: number; + completeAssertionDescription: string; + caseDescription: string; + only?: boolean; +} + +export function createRuleTester( + fn: (result: Promise, context: RuleTesterContext) => void +): (rule: Plugin, schema: RuleTesterSchema) => void; diff --git a/types/stylelint/package.json b/types/stylelint/package.json new file mode 100644 index 0000000000..1e1a719545 --- /dev/null +++ b/types/stylelint/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "postcss": "7.x.x" + } +} diff --git a/types/stylelint/stylelint-tests.ts b/types/stylelint/stylelint-tests.ts index e51d2eb643..fc3dc0e5fd 100644 --- a/types/stylelint/stylelint-tests.ts +++ b/types/stylelint/stylelint-tests.ts @@ -1,6 +1,19 @@ -import { LinterOptions, FormatterType, SyntaxType, lint, LintResult, LinterResult, createPlugin, utils } from "stylelint"; +import { + LinterOptions, + FormatterType, + SyntaxType, + lint, + LintResult, + LinterResult, + createPlugin, + utils, + createRuleTester, + RuleTesterContext, + RuleTesterResult, + Plugin +} from "stylelint"; -const options: LinterOptions = { +const options: Partial = { code: "div { color: red }", files: ["**/**.scss"], formatter: "json", @@ -8,14 +21,13 @@ const options: LinterOptions = { cacheLocation: "./stylelint.cache.json", ignoreDisables: true, reportNeedlessDisables: true, - ignorePath: true, + ignorePath: 'foo', syntax: "scss" }; lint(options).then((x: LinterResult) => { const err: boolean = x.errored; const output: string = x.output; - const postcssResults: any[] = x.postcssResults; const results: LintResult[] = x.results; }); @@ -29,7 +41,7 @@ const messages = utils.ruleMessages(ruleName, { warning: (reason: string) => `This is not allowed because ${reason}`, }); -createPlugin(ruleName, options => { +const testPlugin: Plugin = (options) => { return (root, result) => { const validOptions = utils.validateOptions(result, ruleName, { actual: options }); if (!validOptions) { @@ -52,4 +64,25 @@ createPlugin(ruleName, options => { }); }); }; +}; + +createPlugin(ruleName, testPlugin); + +const tester = createRuleTester( + (result: Promise, context: RuleTesterContext) => { + return; + } +); + +tester(testPlugin, { + ruleName: 'foo', + config: [true, 1], + accept: [ + { code: 'test' }, + { code: 'test2', description: 'testing' } + ], + reject: [ + { code: 'testreject', line: 1, column: 1 }, + { code: 'test2reject', message: 'x', line: 1, column: 1 } + ] });