Skip to content

Commit f4590c9

Browse files
committed
Create type enums
1 parent e5cee87 commit f4590c9

File tree

10 files changed

+66
-71
lines changed

10 files changed

+66
-71
lines changed

src/constants/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
export const COMMENT_TYPE = 'comment';
2-
export const DECLARATION_TYPE = 'decl';
3-
export const RULE_TYPE = 'rule';
4-
export const AT_RULE_TYPE = 'atrule';
5-
export const STRING_TYPE = 'string';
6-
export const NUMBER_TYPE = 'number';
7-
export const BOOLEAN_TYPE = 'boolean';
8-
export const FUNCTION_TYPE = 'function';
91
export const KEYFRAMES_NAME = 'keyframes';
102
export const ANIMATION_PROP = 'animation';
113
export const ANIMATION_NAME_PROP = 'animation-name';
@@ -19,6 +11,20 @@ export const VIEW_TRANSITION_REGEXP = /^(::view-transition(?:-(?:new|old|group|i
1911
export const REG_EXP_CHARACTERS_REG_EXP = /[.?*+^$[\]\\(){}|-]/g;
2012
export const LAST_WORD_CHARACTER_REG_EXP = /\w$/;
2113

14+
export enum TYPE {
15+
AT_RULE = 'atrule',
16+
COMMENT = 'comment',
17+
DECLARATION = 'decl',
18+
RULE = 'rule'
19+
}
20+
21+
export enum TYPEOF {
22+
BOOLEAN = 'boolean',
23+
FUNCTION = 'function',
24+
NUMBER = 'number',
25+
STRING = 'string'
26+
}
27+
2228
export enum CONTROL_DIRECTIVE {
2329
IGNORE = 'ignore',
2430
URLS = 'urls',

src/data/store.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import {
1717
PluginStringMap
1818
} from '@types';
1919
import {
20-
BOOLEAN_TYPE,
21-
STRING_TYPE,
22-
NUMBER_TYPE,
23-
FUNCTION_TYPE,
20+
TYPEOF,
2421
REG_EXP_CHARACTERS_REG_EXP,
2522
LAST_WORD_CHARACTER_REG_EXP
2623
} from '@constants';
@@ -106,12 +103,12 @@ const isNotAcceptedStringMap = (stringMap: PluginStringMap[]): boolean => {
106103
const isAcceptedProcessDeclarationPlugins = (plugins: DeclarationPlugin[]): boolean =>
107104
Array.isArray(plugins)
108105
&& plugins.every((plugin: DeclarationPlugin) =>
109-
typeof plugin.name == STRING_TYPE
110-
&& typeof plugin.priority == NUMBER_TYPE
106+
typeof plugin.name == TYPEOF.STRING
107+
&& typeof plugin.priority == TYPEOF.NUMBER
111108
&& Array.isArray(plugin.processors)
112109
&& plugin.processors.every((processor: DeclarationPluginProcessor) =>
113110
processor.expr instanceof RegExp
114-
&& typeof processor.action === FUNCTION_TYPE
111+
&& typeof processor.action === TYPEOF.FUNCTION
115112
)
116113
);
117114

@@ -183,10 +180,10 @@ const normalizeOptions = (options: PluginOptions): PluginOptionsNormalized => {
183180
if (options.source && SourceValuesArray.includes(options.source)) {
184181
returnOptions.source = options.source;
185182
}
186-
if (typeof options.ignorePrefixedRules === BOOLEAN_TYPE) {
183+
if (typeof options.ignorePrefixedRules === TYPEOF.BOOLEAN) {
187184
returnOptions.ignorePrefixedRules = options.ignorePrefixedRules;
188185
}
189-
if (typeof options.greedy === BOOLEAN_TYPE) {
186+
if (typeof options.greedy === TYPEOF.BOOLEAN) {
190187
returnOptions.greedy = options.greedy;
191188
}
192189
if (!isNotStringOrStringArray(options.ltrPrefix)) {
@@ -198,25 +195,25 @@ const normalizeOptions = (options: PluginOptions): PluginOptionsNormalized => {
198195
if (!isNotStringOrStringArray(options.bothPrefix)) {
199196
returnOptions.bothPrefix = options.bothPrefix;
200197
}
201-
if (typeof options.prefixSelectorTransformer === FUNCTION_TYPE) {
198+
if (typeof options.prefixSelectorTransformer === TYPEOF.FUNCTION) {
202199
returnOptions.prefixSelectorTransformer = options.prefixSelectorTransformer;
203200
}
204-
if (typeof options.safeBothPrefix === BOOLEAN_TYPE) {
201+
if (typeof options.safeBothPrefix === TYPEOF.BOOLEAN) {
205202
returnOptions.safeBothPrefix = options.safeBothPrefix;
206203
}
207-
if (typeof options.processUrls === BOOLEAN_TYPE) {
204+
if (typeof options.processUrls === TYPEOF.BOOLEAN) {
208205
returnOptions.processUrls = options.processUrls;
209206
}
210-
if (typeof options.processRuleNames === BOOLEAN_TYPE) {
207+
if (typeof options.processRuleNames === TYPEOF.BOOLEAN) {
211208
returnOptions.processRuleNames = options.processRuleNames;
212209
}
213-
if (typeof options.processKeyFrames === BOOLEAN_TYPE) {
210+
if (typeof options.processKeyFrames === TYPEOF.BOOLEAN) {
214211
returnOptions.processKeyFrames = options.processKeyFrames;
215212
}
216-
if (typeof options.processEnv === BOOLEAN_TYPE) {
213+
if (typeof options.processEnv === TYPEOF.BOOLEAN) {
217214
returnOptions.processEnv = options.processEnv;
218215
}
219-
if (typeof options.useCalc === BOOLEAN_TYPE) {
216+
if (typeof options.useCalc === TYPEOF.BOOLEAN) {
220217
returnOptions.useCalc = options.useCalc;
221218
}
222219
if (!isNotAcceptedStringMap(options.stringMap)) {

src/parsers/atrules.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
Mode
1313
} from '@types';
1414
import {
15-
AT_RULE_TYPE,
16-
RULE_TYPE,
15+
TYPE,
1716
KEYFRAMES_NAME,
1817
CONTROL_DIRECTIVE
1918
} from '@constants';
@@ -36,7 +35,7 @@ export const parseAtRules = (container: Container): void => {
3635

3736
walkContainer(
3837
container,
39-
[ AT_RULE_TYPE, RULE_TYPE ],
38+
[ TYPE.AT_RULE, TYPE.RULE ],
4039
(_comment: Comment, controlDirective: ControlDirective): void => {
4140

4241
if (isIgnoreDirectiveInsideAnIgnoreBlock(controlDirective, controlDirectives)) {
@@ -52,7 +51,7 @@ export const parseAtRules = (container: Container): void => {
5251
return;
5352
}
5453

55-
if (node.type !== AT_RULE_TYPE) return;
54+
if (node.type !== TYPE.AT_RULE) return;
5655

5756
const atRule = node as AtRule;
5857

@@ -87,7 +86,7 @@ export const parseKeyFrames = (css: Root): void => {
8786

8887
walkContainer(
8988
css,
90-
[ AT_RULE_TYPE, RULE_TYPE ],
89+
[ TYPE.AT_RULE, TYPE.RULE ],
9190
(_comment: Comment, controlDirective: ControlDirective): void => {
9291

9392
if (isIgnoreDirectiveInsideAnIgnoreBlock(controlDirective, controlDirectives)) {
@@ -104,7 +103,7 @@ export const parseKeyFrames = (css: Root): void => {
104103
return;
105104
}
106105

107-
if (node.type !== AT_RULE_TYPE) {
106+
if (node.type !== TYPE.AT_RULE) {
108107
return;
109108
}
110109

src/parsers/declarations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
DeclarationHashMap
1212
} from '@types';
1313
import {
14-
DECLARATION_TYPE,
14+
TYPE,
1515
FLIP_PROPERTY_REGEXP,
1616
ANIMATION_PROP,
1717
ANIMATION_NAME_PROP,
@@ -70,7 +70,7 @@ export const parseDeclarations = (
7070
const ruleSafe = ruleFlipped.clone();
7171

7272
const declarationHashMap = Array.prototype.reduce.call(rule.nodes, (obj: DeclarationHashMap, node: Node): DeclarationHashMap => {
73-
if (node.type === DECLARATION_TYPE) {
73+
if (node.type === TYPE.DECLARATION) {
7474
const decl = node as Declaration;
7575
const index = rule.index(decl);
7676
obj[decl.prop] = obj[decl.prop] || { ignore: false, indexes: {} };
@@ -89,7 +89,7 @@ export const parseDeclarations = (
8989

9090
walkContainer(
9191
rule,
92-
[ DECLARATION_TYPE ],
92+
[ TYPE.DECLARATION ],
9393
(comment: Comment, controlDirective: ControlDirective) => {
9494

9595
cleanRuleRawsBefore(comment.next());

src/parsers/rules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Source,
1010
Mode
1111
} from '@types';
12-
import { RULE_TYPE, CONTROL_DIRECTIVE } from '@constants';
12+
import { TYPE, CONTROL_DIRECTIVE } from '@constants';
1313
import { store } from '@data/store';
1414
import {
1515
isIgnoreDirectiveInsideAnIgnoreBlock,
@@ -43,7 +43,7 @@ export const parseRules = (
4343

4444
walkContainer(
4545
container,
46-
[ RULE_TYPE ],
46+
[ TYPE.RULE ],
4747
(comment: Comment, controlDirective: ControlDirective): void => {
4848

4949
if (

src/utilities/clean.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import {
55
AtRule
66
} from 'postcss';
77
import { store } from '@data/store';
8-
import {
9-
COMMENT_TYPE,
10-
RULE_TYPE,
11-
AT_RULE_TYPE,
12-
KEYFRAMES_NAME
13-
} from '@constants';
8+
import { TYPE, KEYFRAMES_NAME } from '@constants';
149
import { Mode, RulesObject } from '@types';
1510
import { vendor } from '@utilities/vendor';
1611
import { ruleHasChildren, cleanRuleRawsBefore } from '@utilities/rules';
@@ -40,10 +35,10 @@ export const clean = (css: Container): void => {
4035
}
4136
css.walk((node: Node): void => {
4237
if (mode === Mode.diff) {
43-
if (node.type === COMMENT_TYPE) {
38+
if (node.type === TYPE.COMMENT) {
4439
node.remove();
4540
} else if (
46-
node.type === AT_RULE_TYPE &&
41+
node.type === TYPE.AT_RULE &&
4742
!processKeyFrames &&
4843
vendor.unprefixed((node as AtRule).name) === KEYFRAMES_NAME
4944
) {
@@ -52,8 +47,8 @@ export const clean = (css: Container): void => {
5247
}
5348
if (
5449
(
55-
node.type === RULE_TYPE ||
56-
node.type === AT_RULE_TYPE
50+
node.type === TYPE.RULE ||
51+
node.type === TYPE.AT_RULE
5752
) &&
5853
!!(node as Container).nodes
5954
) {
@@ -64,7 +59,7 @@ export const clean = (css: Container): void => {
6459
} else {
6560
const prev = node.prev();
6661
if (prev) {
67-
if (prev.type !== COMMENT_TYPE) {
62+
if (prev.type !== TYPE.COMMENT) {
6863
cleanRuleRawsBefore(node);
6964
}
7065
} else {

src/utilities/containers.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { Container, Node, Comment } from 'postcss';
1+
import {
2+
Comment,
3+
Container,
4+
Node,
5+
} from 'postcss';
26
import { ControlDirective } from '@types';
3-
import { COMMENT_TYPE } from '@constants';
7+
import { TYPE } from '@constants';
48
import { getControlDirective } from '@utilities/directives';
59

610
type WalkContainerControlDirectiveCallback = (comment: Comment, controlDirective: ControlDirective) => void;
@@ -15,9 +19,9 @@ export const walkContainer = (
1519

1620
container.each((node: Node): undefined | false => {
1721

18-
if ( node.type !== COMMENT_TYPE && !filter.includes(node.type) ) return;
22+
if ( node.type !== TYPE.COMMENT && !filter.includes(node.type) ) return;
1923

20-
if (node.type === COMMENT_TYPE) {
24+
if (node.type === TYPE.COMMENT) {
2125

2226
const comment = node as Comment;
2327
const controlDirective = getControlDirective(comment);

src/utilities/declarations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
DeclarationHashMap
55
} from '@types';
66
import {
7-
COMMENT_TYPE,
7+
TYPE,
88
RTL_COMMENT_IGNORE_REGEXP,
99
FLIP_PROPERTY_REGEXP
1010
} from '@constants';
@@ -80,7 +80,7 @@ Object.keys(initialValuesData).forEach((value: string): void => {
8080
const appendDeclarationToRule = (decl: Declaration, rule: Rule): void => {
8181
const declClone = decl.clone();
8282
const declPrev = decl.prev();
83-
if (declPrev && declPrev.type === COMMENT_TYPE) {
83+
if (declPrev && declPrev.type === TYPE.COMMENT) {
8484
const commentClone = declPrev.clone();
8585
rule.append(commentClone);
8686
declPrev.remove();

src/utilities/rules.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,27 @@ import {
1111
StringMap,
1212
Mode
1313
} from '@types';
14-
import {
15-
COMMENT_TYPE,
16-
RTL_COMMENT_REGEXP,
17-
DECLARATION_TYPE,
18-
RULE_TYPE,
19-
AT_RULE_TYPE
20-
} from '@constants';
14+
import { TYPE, RTL_COMMENT_REGEXP } from '@constants';
2115
import { store } from '@data/store';
2216
import { addProperSelectorPrefixes } from '@utilities/selectors';
2317

2418
export const ruleHasDeclarations = (rule: Rule): boolean => {
2519
return rule.some(
26-
(node: Node) => node.type === DECLARATION_TYPE
20+
(node: Node) => node.type === TYPE.DECLARATION
2721
);
2822
};
2923

3024
export const ruleHasChildren = (rule: Container): boolean => {
3125
if (!rule.nodes) return false;
3226
return rule.some(
3327
(node: Node) => (
34-
node.type === DECLARATION_TYPE ||
28+
node.type === TYPE.DECLARATION ||
3529
(
36-
node.type === RULE_TYPE &&
30+
node.type === TYPE.RULE &&
3731
ruleHasChildren(node as Rule)
3832
) ||
3933
(
40-
node.type === AT_RULE_TYPE &&
34+
node.type === TYPE.AT_RULE &&
4135
ruleHasChildren(node as AtRule)
4236
)
4337
)
@@ -46,7 +40,7 @@ export const ruleHasChildren = (rule: Container): boolean => {
4640

4741
export const getParentRules = (rule: Rule): Rule[] => {
4842
const rules: Rule[] = [];
49-
while (rule.type === RULE_TYPE) {
43+
while (rule.type === TYPE.RULE) {
5044
rules.push(rule);
5145
rule = rule.parent as Rule;
5246
}
@@ -66,7 +60,7 @@ export const insertRules = (
6660
parentRule = rules.shift();
6761
const innerRule = parent.nodes.find((node: Node): boolean => {
6862
if (
69-
node.type === RULE_TYPE &&
63+
node.type === TYPE.RULE &&
7064
(node as Rule).selector === parentRule.selector
7165
) {
7266
return true;
@@ -183,8 +177,8 @@ export const cleanRuleRawsBefore = (node: Node | undefined, prefix = '\n\n'): vo
183177
if (
184178
node &&
185179
(
186-
node.type === RULE_TYPE ||
187-
node.type === AT_RULE_TYPE
180+
node.type === TYPE.RULE ||
181+
node.type === TYPE.AT_RULE
188182
)
189183
) {
190184
node.raws.before = `${prefix}${
@@ -198,11 +192,11 @@ export const cleanRuleRawsBefore = (node: Node | undefined, prefix = '\n\n'): vo
198192
export const cleanRules = (...rules: (Rule | AtRule)[]): void => {
199193
rules.forEach((rule: Rule | AtRule | undefined | null): void => {
200194
const prev = rule.prev();
201-
if (prev && prev.type !== COMMENT_TYPE) {
195+
if (prev && prev.type !== TYPE.COMMENT) {
202196
cleanRuleRawsBefore(rule);
203197
}
204198
rule.walk((node: Node): void => {
205-
if (node.type === DECLARATION_TYPE) {
199+
if (node.type === TYPE.DECLARATION) {
206200
const decl = node as Declaration;
207201
if (decl.raws && decl.raws.value && RTL_COMMENT_REGEXP.test(decl.raws.value.raw)) {
208202
delete decl.raws.value;

src/utilities/selectors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
HTML_SELECTOR_REGEXP,
55
ROOT_SELECTOR_REGEXP,
66
VIEW_TRANSITION_REGEXP,
7-
STRING_TYPE
7+
TYPEOF
88
} from '@constants';
99
import { store } from '@data/store';
1010

1111
const addPrefix = (prefix: string, selector: string): string => {
1212
if (store.options.prefixSelectorTransformer) {
1313
const transformedSelector = store.options.prefixSelectorTransformer(prefix, selector);
14-
if (transformedSelector && typeof transformedSelector === STRING_TYPE) {
14+
if (transformedSelector && typeof transformedSelector === TYPEOF.STRING) {
1515
return transformedSelector;
1616
}
1717
}

0 commit comments

Comments
 (0)