stylelint: correct several interfaces (#33666)

* stylelint: correct several interfaces

* stylelint: add rule tester

* stylelint: introduce plugin type

* stylelint: remove rule option

* stylelint: bump major version

* stylelint: compromise and match latest SL version
This commit is contained in:
James Garbutt
2019-03-20 15:44:28 -07:00
committed by Nathan Shively-Sanders
parent 941224addd
commit e9b97bdc54
3 changed files with 141 additions and 33 deletions
+97 -28
View File
@@ -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 <https://github.com/alan-agius4>
// Filips Alpe <https://github.com/filipsalpe>
// James Garbutt <https://github.com/43081j>
// 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<string, any>;
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<Configuration>;
configBasedir: string;
configFile: string;
configOverrides: Partial<Configuration>;
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<LinterResult>;
export function lint(options?: Partial<LinterOptions>): Promise<LinterResult>;
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<T extends {[key: string]: RuleMessageValue}>(
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<void>;
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<RuleTesterResult[]>, context: RuleTesterContext) => void
): (rule: Plugin, schema: RuleTesterSchema) => void;
+6
View File
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"postcss": "7.x.x"
}
}
+38 -5
View File
@@ -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<LinterOptions> = {
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<RuleTesterResult[]>, 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 }
]
});