Skip to content

Commit

Permalink
fix: migrate types
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOGo committed Feb 16, 2023
1 parent 10f5616 commit e1c491f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
31 changes: 22 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { Declaration, Root, AtRule } from 'stylelint/node_modules/postcss';
import stylelint, { Plugin, PluginContext, PostcssResult } from 'stylelint';
import stylelint, { PostcssResult, Rule } from 'stylelint';
import shortCSS from 'shortcss';
import list from 'shortcss/lib/list';
import cssValues from 'css-values';

import {
validProperties,
validOptions,
expectedTypes,
customExpected,
expected,
getTypes,
getIgnoredVariablesOrFunctions,
Expand All @@ -25,6 +27,7 @@ import defaults, {
const { utils } = stylelint;
const messages = utils.ruleMessages(ruleName, {
expected,
customExpected,
failedToFix,
});
/**
Expand Down Expand Up @@ -122,6 +125,11 @@ type CSSPropertyName = string | RegExpString;
*/
type PrimaryOptions = CSSPropertyName | CSSPropertyName[];

type RuleContext = {
fix?: boolean | undefined;
newline?: string | undefined;
};

/**
* Stylelint declaration strict value rule function.
*
Expand All @@ -132,17 +140,13 @@ type PrimaryOptions = CSSPropertyName | CSSPropertyName[];
*
* @returns Returns a PostCSS Plugin.
*/
type StylelintPlugin<P = unknown, S = unknown> = Plugin<P, S> & {
/**
* @see: https://stylelint.io/developer-guide/plugins/#allow-primary-option-arrays
*/
primaryOptionArray?: boolean;
};
type StylelintPlugin<P = unknown, S = unknown> = Rule<P, S>;

const ruleFunction: StylelintPlugin<PrimaryOptions, SecondaryOptions> =
(
properties: PrimaryOptions,
options: SecondaryOptions,
context: PluginContext = {}
context: RuleContext = {}
) =>
(root: Root, result: PostcssResult) => {
// fix #142
Expand Down Expand Up @@ -463,7 +467,14 @@ const ruleFunction: StylelintPlugin<PrimaryOptions, SecondaryOptions> =
node,
line: start!.line,
column: start!.column + nodeProp.length + raws.between!.length,
message: messages.expected(types, value, nodeProp, message),
message: message
? messages.customExpected(
expectedTypes(types),
value,
nodeProp,
message
)
: messages.expected(expectedTypes(types), value, nodeProp),
} as any);
}

Expand All @@ -476,6 +487,8 @@ const ruleFunction: StylelintPlugin<PrimaryOptions, SecondaryOptions> =
};

ruleFunction.primaryOptionArray = true;
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;

const declarationStrictValuePlugin = stylelint.createPlugin(
ruleName,
Expand Down
62 changes: 44 additions & 18 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,10 @@ type ExpectedTypes = Array<ExpectedType>;
*
* @internal
* @param types - Either `variable`, `function` and/or `keyword`.
* @param value - The CSS declaration's value.
* @param property - The CSS declaration's property.
* @param customMessage - A custom message to be delivered upon error interpolated with `${types}`, `${value}` and `${property}`.
*
* @returns Returns an expected message for stylelint report.
* @returns Returns an expected types message for stylelint report.
*/
export function expected(
types: ExpectedType | ExpectedTypes,
value: string,
property: string,
customMessage = ''
): string {
export function expectedTypes(types: ExpectedType | ExpectedTypes): string {
let typesMessage: string;

if (Array.isArray(types)) {
Expand All @@ -214,18 +206,52 @@ export function expected(
typesMessage = types;
}

if (typeof customMessage === 'string' && customMessage.length) {
/* eslint-disable no-template-curly-in-string */
return customMessage
.replace('${types}', typesMessage)
.replace('${value}', value)
.replace('${property}', property);
/* eslint-enable no-template-curly-in-string */
}
return typesMessage;
}

/**
* Build expected message for stylelint report.
*
* @internal
* @param typesMessage - An expected types message for stylelint report.
* @param value - The CSS declaration's value.
* @param property - The CSS declaration's property.
*
* @returns Returns an expected message for stylelint report.
*/
export function expected(
typesMessage: string,
value: string,
property: string
): string {
return `Expected ${typesMessage} for "${value}" of "${property}"`;
}

/**
* Build custom expected message for stylelint report.
*
* @internal
* @param typesMessage - An expected types message for stylelint report.
* @param value - The CSS declaration's value.
* @param property - The CSS declaration's property.
* @param customMessage - A custom message to be delivered upon error interpolated with `${types}`, `${value}` and `${property}`.
*
* @returns Returns a custom expected message for stylelint report.
*/
export function customExpected(
typesMessage: string,
value: string,
property: string,
customMessage: string
): string {
/* eslint-disable no-template-curly-in-string */
return customMessage
.replace('${types}', typesMessage)
.replace('${value}', value)
.replace('${property}', property);
/* eslint-enable no-template-curly-in-string */
}

/**
* Build failed-to-fix message for stylelint report.
*
Expand Down

0 comments on commit e1c491f

Please sign in to comment.