Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Validate options.processDeclarationPlugins
  • Loading branch information
aleen42 authored and elchininet committed May 21, 2024
commit 3037e0b2ee3db0091cab1062a97200bde1a41b5d
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ All the options are optional, and a default value will be used if any of them is
| stringMap | `PluginStringMap[]` | Check below | An array of strings maps that will be used to make the replacements of the declarations' URLs and to match the names of the rules if `processRuleNames` is `true` |
| greedy | `boolean` | `false` | When greedy is `true`, the matches of `stringMap` will not take into account word boundaries |
| aliases | `Record<string, string>` | `{}` | A strings map to treat some declarations as others |
| processDeclarationPlugins | `Array<{name: string, priority: number, processors: PluginProcessor[]}>` | `[]` | Plugins applied when processing CSS declarations |
| processDeclarationPlugins | `DeclarationPlugin[]` | `[]` | Plugins applied when processing CSS declarations |

---

Expand Down
10 changes: 8 additions & 2 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ export interface PluginStringMap {
replace: strings;
}

export interface PluginProcessor {
export interface DeclarationPluginProcessor {
expr: RegExp;
action: (prop: string, value: string, context: object) => object;
}

export type DeclarationPlugin = {
name: string;
priority: number;
processors: DeclarationPluginProcessor[];
}

export type PrefixSelectorTransformer = (prefix: string, selector: string) => string | void;

export interface PluginOptions {
Expand All @@ -63,7 +69,7 @@ export interface PluginOptions {
stringMap?: PluginStringMap[];
greedy?: boolean;
aliases?: Record<string, string>;
processDeclarationPlugins?: Array<{ name: string, priority: number, processors: PluginProcessor[] }>;
processDeclarationPlugins?: DeclarationPlugin[];
}

export interface PluginOptionsNormalized extends Omit<Required<PluginOptions>, 'stringMap' | 'prefixSelectorTransformer'> {
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const DECLARATION_TYPE = 'decl';
export const RULE_TYPE = 'rule';
export const AT_RULE_TYPE = 'atrule';
export const STRING_TYPE = 'string';
export const NUMBER_TYPE = 'number';
export const BOOLEAN_TYPE = 'boolean';
export const FUNCTION_TYPE = 'function';
export const KEYFRAMES_NAME = 'keyframes';
Expand Down
26 changes: 22 additions & 4 deletions src/data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Rule, AtRule } from 'postcss';
import {
PluginOptions,
PluginOptionsNormalized,
DeclarationPlugin,
DeclarationPluginProcessor,
AtRulesObject,
AtRulesStringMap,
RulesObject,
Expand All @@ -16,6 +18,8 @@ import {
} from '@types';
import {
BOOLEAN_TYPE,
STRING_TYPE,
NUMBER_TYPE,
FUNCTION_TYPE,
REG_EXP_CHARACTERS_REG_EXP,
LAST_WORD_CHARACTER_REG_EXP
Expand Down Expand Up @@ -99,6 +103,18 @@ const isNotAcceptedStringMap = (stringMap: PluginStringMap[]): boolean => {
);
};

const isAcceptedProcessDeclarationPlugins = (plugins: DeclarationPlugin[]): boolean =>
Array.isArray(plugins)
&& plugins.every((plugin: DeclarationPlugin) =>
typeof plugin.name == STRING_TYPE
&& typeof plugin.priority == NUMBER_TYPE
&& Array.isArray(plugin.processors)
&& plugin.processors.every((processor: DeclarationPluginProcessor) =>
({}).toString.call(processor.expr) === '[object RegExp]'
&& typeof processor.action === FUNCTION_TYPE
)
);

const isObjectWithStringKeys = (obj: Record<string, unknown>): boolean =>
!Object.entries(obj).some(
(entry: [string, unknown]): boolean =>
Expand Down Expand Up @@ -215,13 +231,15 @@ const normalizeOptions = (options: PluginOptions): PluginOptionsNormalized => {
}
});
}
if (isAcceptedProcessDeclarationPlugins(options.processDeclarationPlugins)) {
returnOptions.processDeclarationPlugins = options.processDeclarationPlugins.map(plugin => ({
...plugin, directives: {control: {}, value: []},
}));
}
if (options.aliases && isObjectWithStringKeys(options.aliases)) {
returnOptions.aliases = options.aliases;
}
return {
...returnOptions,
processDeclarationPlugins: (options.processDeclarationPlugins || []).map(plugin => ({...plugin, directives: {control: {}, value: []}}))
};
return returnOptions;
};

const initStore = (options: PluginOptions): void => {
Expand Down