@tbela99/css-parser 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +318 -34
- package/dist/config.json.js +386 -4
- package/dist/index-umd-web.js +3260 -1563
- package/dist/index.cjs +3259 -1564
- package/dist/index.d.ts +687 -536
- package/dist/lib/ast/expand.js +14 -14
- package/dist/lib/ast/features/calc.js +225 -0
- package/dist/lib/ast/features/index.js +3 -0
- package/dist/lib/ast/features/inlinecssvariables.js +130 -0
- package/dist/lib/ast/features/shorthand.js +46 -0
- package/dist/lib/ast/features/utils/math.js +95 -0
- package/dist/lib/ast/minify.js +401 -372
- package/dist/lib/ast/types.js +101 -0
- package/dist/lib/ast/utils/minifyfeature.js +8 -0
- package/dist/lib/ast/walk.js +37 -9
- package/dist/lib/iterable/set.js +48 -0
- package/dist/lib/iterable/weakmap.js +53 -0
- package/dist/lib/parser/declaration/list.js +18 -4
- package/dist/lib/parser/declaration/map.js +102 -33
- package/dist/lib/parser/declaration/set.js +18 -12
- package/dist/lib/parser/parse.js +661 -421
- package/dist/lib/parser/tokenize.js +82 -46
- package/dist/lib/parser/utils/syntax.js +13 -10
- package/dist/lib/parser/utils/type.js +23 -6
- package/dist/lib/renderer/render.js +253 -84
- package/dist/lib/renderer/sourcemap/lib/encode.js +37 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +58 -0
- package/dist/lib/renderer/utils/color.js +25 -20
- package/dist/node/index.js +30 -15
- package/dist/web/index.js +36 -19
- package/package.json +9 -6
- package/dist/lib/transform.js +0 -24
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var EnumToken;
|
|
2
|
+
(function (EnumToken) {
|
|
3
|
+
EnumToken[EnumToken["CommentTokenType"] = 0] = "CommentTokenType";
|
|
4
|
+
EnumToken[EnumToken["CDOCOMMTokenType"] = 1] = "CDOCOMMTokenType";
|
|
5
|
+
EnumToken[EnumToken["StyleSheetNodeType"] = 2] = "StyleSheetNodeType";
|
|
6
|
+
EnumToken[EnumToken["AtRuleNodeType"] = 3] = "AtRuleNodeType";
|
|
7
|
+
EnumToken[EnumToken["RuleNodeType"] = 4] = "RuleNodeType";
|
|
8
|
+
EnumToken[EnumToken["DeclarationNodeType"] = 5] = "DeclarationNodeType";
|
|
9
|
+
EnumToken[EnumToken["LiteralTokenType"] = 6] = "LiteralTokenType";
|
|
10
|
+
EnumToken[EnumToken["IdenTokenType"] = 7] = "IdenTokenType";
|
|
11
|
+
EnumToken[EnumToken["DashedIdenTokenType"] = 8] = "DashedIdenTokenType";
|
|
12
|
+
EnumToken[EnumToken["CommaTokenType"] = 9] = "CommaTokenType";
|
|
13
|
+
EnumToken[EnumToken["ColonTokenType"] = 10] = "ColonTokenType";
|
|
14
|
+
EnumToken[EnumToken["SemiColonTokenType"] = 11] = "SemiColonTokenType";
|
|
15
|
+
EnumToken[EnumToken["NumberTokenType"] = 12] = "NumberTokenType";
|
|
16
|
+
EnumToken[EnumToken["AtRuleTokenType"] = 13] = "AtRuleTokenType";
|
|
17
|
+
EnumToken[EnumToken["PercentageTokenType"] = 14] = "PercentageTokenType";
|
|
18
|
+
EnumToken[EnumToken["FunctionTokenType"] = 15] = "FunctionTokenType";
|
|
19
|
+
EnumToken[EnumToken["TimelineFunctionTokenType"] = 16] = "TimelineFunctionTokenType";
|
|
20
|
+
EnumToken[EnumToken["TimingFunctionTokenType"] = 17] = "TimingFunctionTokenType";
|
|
21
|
+
EnumToken[EnumToken["UrlFunctionTokenType"] = 18] = "UrlFunctionTokenType";
|
|
22
|
+
EnumToken[EnumToken["ImageFunctionTokenType"] = 19] = "ImageFunctionTokenType";
|
|
23
|
+
EnumToken[EnumToken["StringTokenType"] = 20] = "StringTokenType";
|
|
24
|
+
EnumToken[EnumToken["UnclosedStringTokenType"] = 21] = "UnclosedStringTokenType";
|
|
25
|
+
EnumToken[EnumToken["DimensionTokenType"] = 22] = "DimensionTokenType";
|
|
26
|
+
EnumToken[EnumToken["LengthTokenType"] = 23] = "LengthTokenType";
|
|
27
|
+
EnumToken[EnumToken["AngleTokenType"] = 24] = "AngleTokenType";
|
|
28
|
+
EnumToken[EnumToken["TimeTokenType"] = 25] = "TimeTokenType";
|
|
29
|
+
EnumToken[EnumToken["FrequencyTokenType"] = 26] = "FrequencyTokenType";
|
|
30
|
+
EnumToken[EnumToken["ResolutionTokenType"] = 27] = "ResolutionTokenType";
|
|
31
|
+
EnumToken[EnumToken["HashTokenType"] = 28] = "HashTokenType";
|
|
32
|
+
EnumToken[EnumToken["BlockStartTokenType"] = 29] = "BlockStartTokenType";
|
|
33
|
+
EnumToken[EnumToken["BlockEndTokenType"] = 30] = "BlockEndTokenType";
|
|
34
|
+
EnumToken[EnumToken["AttrStartTokenType"] = 31] = "AttrStartTokenType";
|
|
35
|
+
EnumToken[EnumToken["AttrEndTokenType"] = 32] = "AttrEndTokenType";
|
|
36
|
+
EnumToken[EnumToken["StartParensTokenType"] = 33] = "StartParensTokenType";
|
|
37
|
+
EnumToken[EnumToken["EndParensTokenType"] = 34] = "EndParensTokenType";
|
|
38
|
+
EnumToken[EnumToken["ParensTokenType"] = 35] = "ParensTokenType";
|
|
39
|
+
EnumToken[EnumToken["WhitespaceTokenType"] = 36] = "WhitespaceTokenType";
|
|
40
|
+
EnumToken[EnumToken["IncludeMatchTokenType"] = 37] = "IncludeMatchTokenType";
|
|
41
|
+
EnumToken[EnumToken["DashMatchTokenType"] = 38] = "DashMatchTokenType";
|
|
42
|
+
EnumToken[EnumToken["LtTokenType"] = 39] = "LtTokenType";
|
|
43
|
+
EnumToken[EnumToken["LteTokenType"] = 40] = "LteTokenType";
|
|
44
|
+
EnumToken[EnumToken["GtTokenType"] = 41] = "GtTokenType";
|
|
45
|
+
EnumToken[EnumToken["GteTokenType"] = 42] = "GteTokenType";
|
|
46
|
+
EnumToken[EnumToken["PseudoClassTokenType"] = 43] = "PseudoClassTokenType";
|
|
47
|
+
EnumToken[EnumToken["PseudoClassFuncTokenType"] = 44] = "PseudoClassFuncTokenType";
|
|
48
|
+
EnumToken[EnumToken["DelimTokenType"] = 45] = "DelimTokenType";
|
|
49
|
+
EnumToken[EnumToken["UrlTokenTokenType"] = 46] = "UrlTokenTokenType";
|
|
50
|
+
EnumToken[EnumToken["EOFTokenType"] = 47] = "EOFTokenType";
|
|
51
|
+
EnumToken[EnumToken["ImportantTokenType"] = 48] = "ImportantTokenType";
|
|
52
|
+
EnumToken[EnumToken["ColorTokenType"] = 49] = "ColorTokenType";
|
|
53
|
+
EnumToken[EnumToken["AttrTokenType"] = 50] = "AttrTokenType";
|
|
54
|
+
EnumToken[EnumToken["BadCommentTokenType"] = 51] = "BadCommentTokenType";
|
|
55
|
+
EnumToken[EnumToken["BadCdoTokenType"] = 52] = "BadCdoTokenType";
|
|
56
|
+
EnumToken[EnumToken["BadUrlTokenType"] = 53] = "BadUrlTokenType";
|
|
57
|
+
EnumToken[EnumToken["BadStringTokenType"] = 54] = "BadStringTokenType";
|
|
58
|
+
EnumToken[EnumToken["BinaryExpressionTokenType"] = 55] = "BinaryExpressionTokenType";
|
|
59
|
+
EnumToken[EnumToken["UnaryExpressionTokenType"] = 56] = "UnaryExpressionTokenType";
|
|
60
|
+
/* catch all */
|
|
61
|
+
EnumToken[EnumToken["ListToken"] = 57] = "ListToken";
|
|
62
|
+
/* arithmetic tokens */
|
|
63
|
+
EnumToken[EnumToken["Add"] = 58] = "Add";
|
|
64
|
+
EnumToken[EnumToken["Mul"] = 59] = "Mul";
|
|
65
|
+
EnumToken[EnumToken["Div"] = 60] = "Div";
|
|
66
|
+
EnumToken[EnumToken["Sub"] = 61] = "Sub";
|
|
67
|
+
/* new tokens */
|
|
68
|
+
EnumToken[EnumToken["ColumnCombinatorTokenType"] = 62] = "ColumnCombinatorTokenType";
|
|
69
|
+
EnumToken[EnumToken["ContainMatchTokenType"] = 63] = "ContainMatchTokenType";
|
|
70
|
+
EnumToken[EnumToken["StartMatchTokenType"] = 64] = "StartMatchTokenType";
|
|
71
|
+
EnumToken[EnumToken["EndMatchTokenType"] = 65] = "EndMatchTokenType";
|
|
72
|
+
EnumToken[EnumToken["MatchExpressionTokenType"] = 66] = "MatchExpressionTokenType";
|
|
73
|
+
EnumToken[EnumToken["NameSpaceAttributeTokenType"] = 67] = "NameSpaceAttributeTokenType";
|
|
74
|
+
EnumToken[EnumToken["FractionTokenType"] = 68] = "FractionTokenType";
|
|
75
|
+
/* aliases */
|
|
76
|
+
EnumToken[EnumToken["Time"] = 25] = "Time";
|
|
77
|
+
EnumToken[EnumToken["Iden"] = 7] = "Iden";
|
|
78
|
+
EnumToken[EnumToken["Hash"] = 28] = "Hash";
|
|
79
|
+
EnumToken[EnumToken["Angle"] = 24] = "Angle";
|
|
80
|
+
EnumToken[EnumToken["Color"] = 49] = "Color";
|
|
81
|
+
EnumToken[EnumToken["Comma"] = 9] = "Comma";
|
|
82
|
+
EnumToken[EnumToken["String"] = 20] = "String";
|
|
83
|
+
EnumToken[EnumToken["Length"] = 23] = "Length";
|
|
84
|
+
EnumToken[EnumToken["Number"] = 12] = "Number";
|
|
85
|
+
EnumToken[EnumToken["Perc"] = 14] = "Perc";
|
|
86
|
+
EnumToken[EnumToken["Literal"] = 6] = "Literal";
|
|
87
|
+
EnumToken[EnumToken["Comment"] = 0] = "Comment";
|
|
88
|
+
EnumToken[EnumToken["UrlFunc"] = 18] = "UrlFunc";
|
|
89
|
+
EnumToken[EnumToken["Dimension"] = 22] = "Dimension";
|
|
90
|
+
EnumToken[EnumToken["Frequency"] = 26] = "Frequency";
|
|
91
|
+
EnumToken[EnumToken["Resolution"] = 27] = "Resolution";
|
|
92
|
+
EnumToken[EnumToken["Whitespace"] = 36] = "Whitespace";
|
|
93
|
+
EnumToken[EnumToken["DashedIden"] = 8] = "DashedIden";
|
|
94
|
+
EnumToken[EnumToken["ImageFunc"] = 19] = "ImageFunc";
|
|
95
|
+
EnumToken[EnumToken["CommentNodeType"] = 0] = "CommentNodeType";
|
|
96
|
+
EnumToken[EnumToken["CDOCOMMNodeType"] = 1] = "CDOCOMMNodeType";
|
|
97
|
+
EnumToken[EnumToken["TimingFunction"] = 17] = "TimingFunction";
|
|
98
|
+
EnumToken[EnumToken["TimelineFunction"] = 16] = "TimelineFunction";
|
|
99
|
+
})(EnumToken || (EnumToken = {}));
|
|
100
|
+
|
|
101
|
+
export { EnumToken };
|
package/dist/lib/ast/walk.js
CHANGED
|
@@ -1,17 +1,45 @@
|
|
|
1
|
-
function* walk(node,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
function* walk(node, filter) {
|
|
2
|
+
const parents = [node];
|
|
3
|
+
const root = node;
|
|
4
|
+
const weakMap = new WeakMap;
|
|
5
|
+
while (parents.length > 0) {
|
|
6
|
+
node = parents.shift();
|
|
7
|
+
let option = null;
|
|
8
|
+
if (filter != null) {
|
|
9
|
+
option = filter(node);
|
|
10
|
+
if (option === 'ignore') {
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (option === 'stop') {
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
if (option !== 'children') {
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
yield { node, parent: weakMap.get(node), root };
|
|
21
|
+
}
|
|
22
|
+
if (option !== 'ignore-children' && 'chi' in node) {
|
|
23
|
+
parents.unshift(...node.chi);
|
|
24
|
+
for (const child of node.chi.slice()) {
|
|
25
|
+
weakMap.set(child, node);
|
|
26
|
+
}
|
|
6
27
|
}
|
|
7
28
|
}
|
|
8
29
|
}
|
|
9
|
-
function* walkValues(values
|
|
10
|
-
|
|
30
|
+
function* walkValues(values) {
|
|
31
|
+
const stack = values.slice();
|
|
32
|
+
const weakMap = new WeakMap;
|
|
33
|
+
let value;
|
|
34
|
+
while (stack.length > 0) {
|
|
35
|
+
value = stack.shift();
|
|
11
36
|
// @ts-ignore
|
|
12
|
-
yield { value, parent };
|
|
37
|
+
yield { value, parent: weakMap.get(value) };
|
|
13
38
|
if ('chi' in value) {
|
|
14
|
-
|
|
39
|
+
for (const child of value.chi) {
|
|
40
|
+
weakMap.set(child, value);
|
|
41
|
+
}
|
|
42
|
+
stack.unshift(...value.chi);
|
|
15
43
|
}
|
|
16
44
|
}
|
|
17
45
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class IterableWeakSet {
|
|
2
|
+
#weakset = new WeakSet;
|
|
3
|
+
#set = new Set;
|
|
4
|
+
constructor(iterable) {
|
|
5
|
+
if (iterable) {
|
|
6
|
+
for (const value of iterable) {
|
|
7
|
+
const ref = new WeakRef(value);
|
|
8
|
+
this.#weakset.add(value);
|
|
9
|
+
this.#set.add(ref);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
has(value) {
|
|
14
|
+
return this.#weakset.has(value);
|
|
15
|
+
}
|
|
16
|
+
delete(value) {
|
|
17
|
+
if (this.#weakset.has(value)) {
|
|
18
|
+
for (const ref of this.#set) {
|
|
19
|
+
if (ref.deref() === value) {
|
|
20
|
+
this.#set.delete(ref);
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return this.#weakset.delete(value);
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
add(value) {
|
|
29
|
+
if (!this.#weakset.has(value)) {
|
|
30
|
+
this.#weakset.add(value);
|
|
31
|
+
this.#set.add(new WeakRef(value));
|
|
32
|
+
}
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
*[Symbol.iterator]() {
|
|
36
|
+
for (const ref of new Set(this.#set)) {
|
|
37
|
+
const key = ref.deref();
|
|
38
|
+
if (key != null) {
|
|
39
|
+
yield key;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.#set.delete(ref);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { IterableWeakSet };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
class IterableWeakMap {
|
|
2
|
+
#map;
|
|
3
|
+
#set;
|
|
4
|
+
constructor(iterable) {
|
|
5
|
+
this.#map = new WeakMap;
|
|
6
|
+
this.#set = new Set;
|
|
7
|
+
if (iterable) {
|
|
8
|
+
for (const [key, value] of iterable) {
|
|
9
|
+
const ref = new WeakRef(key);
|
|
10
|
+
this.#set.add(ref);
|
|
11
|
+
this.#map.set(key, value);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
has(key) {
|
|
16
|
+
return this.#map.has(key);
|
|
17
|
+
}
|
|
18
|
+
set(key, value) {
|
|
19
|
+
if (!this.#map.has(key)) {
|
|
20
|
+
this.#set.add(new WeakRef(key));
|
|
21
|
+
}
|
|
22
|
+
this.#map.set(key, value);
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
get(key) {
|
|
26
|
+
return this.#map.get(key);
|
|
27
|
+
}
|
|
28
|
+
delete(key) {
|
|
29
|
+
if (this.#map.has(key)) {
|
|
30
|
+
for (const ref of this.#set) {
|
|
31
|
+
if (ref.deref() === key) {
|
|
32
|
+
this.#set.delete(ref);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return this.#map.delete(key);
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
*[Symbol.iterator]() {
|
|
41
|
+
for (const ref of new Set(this.#set)) {
|
|
42
|
+
const key = ref.deref();
|
|
43
|
+
if (key == null) {
|
|
44
|
+
this.#set.delete(ref);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
yield [key, this.#map.get(key)];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { IterableWeakMap };
|
|
@@ -1,23 +1,37 @@
|
|
|
1
1
|
import { PropertySet } from './set.js';
|
|
2
2
|
import '../../renderer/utils/color.js';
|
|
3
|
-
import {
|
|
3
|
+
import { EnumToken } from '../../ast/types.js';
|
|
4
|
+
import '../../ast/minify.js';
|
|
4
5
|
import { parseString } from '../parse.js';
|
|
6
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
5
7
|
import { getConfig } from '../utils/config.js';
|
|
8
|
+
import { PropertyMap } from './map.js';
|
|
6
9
|
|
|
7
10
|
const config = getConfig();
|
|
8
11
|
class PropertyList {
|
|
12
|
+
options = { removeDuplicateDeclarations: true, computeShorthand: true };
|
|
9
13
|
declarations;
|
|
10
|
-
constructor() {
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
for (const key of Object.keys(this.options)) {
|
|
16
|
+
if (key in options) {
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
this.options[key] = options[key];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
11
21
|
this.declarations = new Map;
|
|
12
22
|
}
|
|
13
23
|
set(nam, value) {
|
|
14
|
-
return this.add({ typ:
|
|
24
|
+
return this.add({ typ: EnumToken.DeclarationNodeType, nam, val: Array.isArray(value) ? value : parseString(String(value)) });
|
|
15
25
|
}
|
|
16
26
|
add(declaration) {
|
|
17
|
-
if (declaration.typ !=
|
|
27
|
+
if (declaration.typ != EnumToken.DeclarationNodeType || !this.options.removeDuplicateDeclarations) {
|
|
18
28
|
this.declarations.set(Number(Math.random().toString().slice(2)).toString(36), declaration);
|
|
19
29
|
return this;
|
|
20
30
|
}
|
|
31
|
+
if (!this.options.computeShorthand) {
|
|
32
|
+
this.declarations.set(declaration.nam, declaration);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
21
35
|
let propertyName = declaration.nam;
|
|
22
36
|
let shortHandType;
|
|
23
37
|
let shorthand;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { eq } from '../utils/eq.js';
|
|
2
2
|
import { renderToken } from '../../renderer/render.js';
|
|
3
3
|
import '../../renderer/utils/color.js';
|
|
4
|
+
import { EnumToken } from '../../ast/types.js';
|
|
5
|
+
import '../../ast/minify.js';
|
|
6
|
+
import { parseString } from '../parse.js';
|
|
4
7
|
import { getConfig } from '../utils/config.js';
|
|
5
8
|
import { matchType } from '../utils/type.js';
|
|
6
|
-
import { parseString } from '../parse.js';
|
|
7
9
|
import { PropertySet } from './set.js';
|
|
10
|
+
import { IterableWeakMap } from '../../iterable/weakmap.js';
|
|
8
11
|
|
|
12
|
+
const cache = new IterableWeakMap();
|
|
9
13
|
const propertiesConfig = getConfig();
|
|
10
14
|
class PropertyMap {
|
|
11
15
|
config;
|
|
@@ -28,21 +32,23 @@ class PropertyMap {
|
|
|
28
32
|
this.declarations.set(declaration.nam, declaration);
|
|
29
33
|
}
|
|
30
34
|
else {
|
|
31
|
-
const separator = this.config.separator
|
|
35
|
+
const separator = this.config.separator != null ? {
|
|
36
|
+
...this.config.separator,
|
|
37
|
+
typ: EnumToken[this.config.separator.typ]
|
|
38
|
+
} : null;
|
|
32
39
|
// expand shorthand
|
|
33
40
|
if (declaration.nam != this.config.shorthand && this.declarations.has(this.config.shorthand)) {
|
|
34
41
|
const tokens = {};
|
|
35
42
|
const values = [];
|
|
36
43
|
// @ts-ignore
|
|
37
44
|
this.declarations.get(this.config.shorthand).val.slice().reduce((acc, curr) => {
|
|
45
|
+
// @ts-ignore
|
|
38
46
|
if (separator != null && separator.typ == curr.typ && eq(separator, curr)) {
|
|
39
47
|
acc.push([]);
|
|
40
48
|
return acc;
|
|
41
49
|
}
|
|
42
|
-
// else {
|
|
43
50
|
// @ts-ignore
|
|
44
51
|
acc.at(-1).push(curr);
|
|
45
|
-
// }
|
|
46
52
|
return acc;
|
|
47
53
|
}, [[]]).
|
|
48
54
|
// @ts-ignore
|
|
@@ -51,7 +57,7 @@ class PropertyMap {
|
|
|
51
57
|
// let current: number = 0;
|
|
52
58
|
const props = this.config.properties[property];
|
|
53
59
|
for (let i = 0; i < acc.length; i++) {
|
|
54
|
-
if (acc[i].typ ==
|
|
60
|
+
if (acc[i].typ == EnumToken.CommentTokenType || acc[i].typ == EnumToken.WhitespaceTokenType) {
|
|
55
61
|
acc.splice(i, 1);
|
|
56
62
|
i--;
|
|
57
63
|
continue;
|
|
@@ -67,18 +73,20 @@ class PropertyMap {
|
|
|
67
73
|
else {
|
|
68
74
|
if (current == tokens[property].length) {
|
|
69
75
|
tokens[property].push([acc[i]]);
|
|
70
|
-
// tokens[property][current].push();
|
|
71
76
|
}
|
|
72
77
|
else {
|
|
73
|
-
tokens[property][current].push({ typ:
|
|
78
|
+
tokens[property][current].push({ typ: EnumToken.WhitespaceTokenType }, acc[i]);
|
|
74
79
|
}
|
|
75
80
|
}
|
|
76
81
|
acc.splice(i, 1);
|
|
77
82
|
i--;
|
|
78
83
|
// @ts-ignore
|
|
79
|
-
if ('prefix' in props && acc[i]?.typ == props.prefix.typ) {
|
|
80
|
-
|
|
81
|
-
|
|
84
|
+
if ('prefix' in props && acc[i]?.typ == EnumToken[props.prefix.typ]) {
|
|
85
|
+
if (eq(acc[i], {
|
|
86
|
+
...this.config.properties[property].prefix,
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
typ: EnumToken[props.prefix.typ]
|
|
89
|
+
})) {
|
|
82
90
|
acc.splice(i, 1);
|
|
83
91
|
i--;
|
|
84
92
|
}
|
|
@@ -111,7 +119,7 @@ class PropertyMap {
|
|
|
111
119
|
tokens[property][current].push(...defaults);
|
|
112
120
|
}
|
|
113
121
|
else {
|
|
114
|
-
tokens[property][current].push({ typ:
|
|
122
|
+
tokens[property][current].push({ typ: EnumToken.WhitespaceTokenType }, ...defaults);
|
|
115
123
|
}
|
|
116
124
|
}
|
|
117
125
|
}
|
|
@@ -122,7 +130,7 @@ class PropertyMap {
|
|
|
122
130
|
if (values.length == 0) {
|
|
123
131
|
this.declarations = Object.entries(tokens).reduce((acc, curr) => {
|
|
124
132
|
acc.set(curr[0], {
|
|
125
|
-
typ:
|
|
133
|
+
typ: EnumToken.DeclarationNodeType,
|
|
126
134
|
nam: curr[0],
|
|
127
135
|
val: curr[1].reduce((acc, curr) => {
|
|
128
136
|
if (acc.length > 0) {
|
|
@@ -186,16 +194,56 @@ class PropertyMap {
|
|
|
186
194
|
requiredCount = this.declarations.size;
|
|
187
195
|
}
|
|
188
196
|
if (!isShorthand || requiredCount < this.requiredCount) {
|
|
197
|
+
if (isShorthand && this.declarations.has(this.config.shorthand)) {
|
|
198
|
+
// console.debug(...this.declarations.values());
|
|
199
|
+
const removeDefaults = (declaration) => {
|
|
200
|
+
// const dec: AstDeclaration = {...declaration};
|
|
201
|
+
const config = this.config.shorthand == declaration.nam ? this.config : this.config.properties[declaration.nam];
|
|
202
|
+
declaration.val = declaration.val.filter((val) => {
|
|
203
|
+
if (!cache.has(val)) {
|
|
204
|
+
cache.set(val, renderToken(val, { minify: true }));
|
|
205
|
+
}
|
|
206
|
+
return !config.default.includes(cache.get(val));
|
|
207
|
+
})
|
|
208
|
+
.filter((val, index, array) => !(index > 0 &&
|
|
209
|
+
val.typ == EnumToken.WhitespaceTokenType &&
|
|
210
|
+
array[index - 1].typ == EnumToken.WhitespaceTokenType));
|
|
211
|
+
if (declaration.val.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
|
|
212
|
+
declaration.val.pop();
|
|
213
|
+
}
|
|
214
|
+
return declaration;
|
|
215
|
+
};
|
|
216
|
+
const values = [...this.declarations.values()].reduce((acc, curr) => {
|
|
217
|
+
if (curr instanceof PropertySet) {
|
|
218
|
+
acc.push(...curr);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
acc.push(curr);
|
|
222
|
+
}
|
|
223
|
+
return acc;
|
|
224
|
+
}, []);
|
|
225
|
+
const filtered = values.map(removeDefaults).filter((x) => x.val.length > 0);
|
|
226
|
+
if (filtered.length == 0 && this.config.default.length > 0) {
|
|
227
|
+
filtered.push({
|
|
228
|
+
typ: EnumToken.DeclarationNodeType,
|
|
229
|
+
nam: this.config.shorthand,
|
|
230
|
+
val: parseString(this.config.default[0])
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return (filtered.length > 0 ? filtered : values)[Symbol.iterator]();
|
|
234
|
+
}
|
|
189
235
|
// @ts-ignore
|
|
190
236
|
iterable = this.declarations.values();
|
|
191
237
|
}
|
|
192
238
|
else {
|
|
193
239
|
let count = 0;
|
|
194
240
|
let match;
|
|
195
|
-
const separator = this.config.separator
|
|
241
|
+
const separator = this.config.separator != null ? {
|
|
242
|
+
...this.config.separator,
|
|
243
|
+
typ: EnumToken[this.config.separator.typ]
|
|
244
|
+
} : null;
|
|
196
245
|
const tokens = {};
|
|
197
246
|
// @ts-ignore
|
|
198
|
-
/* const valid: string[] =*/
|
|
199
247
|
Object.entries(this.config.properties).reduce((acc, curr) => {
|
|
200
248
|
if (!this.declarations.has(curr[0])) {
|
|
201
249
|
if (curr[1].required) {
|
|
@@ -216,14 +264,18 @@ class PropertyMap {
|
|
|
216
264
|
}
|
|
217
265
|
continue;
|
|
218
266
|
}
|
|
219
|
-
if (val.typ ==
|
|
267
|
+
if (val.typ == EnumToken.WhitespaceTokenType || val.typ == EnumToken.CommentTokenType) {
|
|
220
268
|
continue;
|
|
221
269
|
}
|
|
222
|
-
|
|
270
|
+
// @ts-ignore
|
|
271
|
+
if (props.multiple && props.separator != null && EnumToken[props.separator.typ] == val.typ && eq({
|
|
272
|
+
...props.separator,
|
|
273
|
+
typ: EnumToken[props.separator.typ]
|
|
274
|
+
}, val)) {
|
|
223
275
|
continue;
|
|
224
276
|
}
|
|
225
277
|
// @ts-ignore
|
|
226
|
-
match = val.typ ==
|
|
278
|
+
match = val.typ == EnumToken.CommentTokenType || matchType(val, curr[1]);
|
|
227
279
|
if (isShorthand) {
|
|
228
280
|
isShorthand = match;
|
|
229
281
|
}
|
|
@@ -252,12 +304,12 @@ class PropertyMap {
|
|
|
252
304
|
return entry[1].required && !(entry[0] in tokens);
|
|
253
305
|
}) ||
|
|
254
306
|
// @ts-ignore
|
|
255
|
-
!Object.values(tokens).every(v => v.filter(t => t.typ !=
|
|
307
|
+
!Object.values(tokens).every(v => v.filter(t => t.typ != EnumToken.CommentTokenType).length == count)) {
|
|
256
308
|
// @ts-ignore
|
|
257
309
|
iterable = this.declarations.values();
|
|
258
310
|
}
|
|
259
311
|
else {
|
|
260
|
-
|
|
312
|
+
let values = Object.entries(tokens).reduce((acc, curr) => {
|
|
261
313
|
const props = this.config.properties[curr[0]];
|
|
262
314
|
for (let i = 0; i < curr[1].length; i++) {
|
|
263
315
|
if (acc.length == i) {
|
|
@@ -265,7 +317,7 @@ class PropertyMap {
|
|
|
265
317
|
}
|
|
266
318
|
let values = curr[1][i].reduce((acc, curr) => {
|
|
267
319
|
if (acc.length > 0) {
|
|
268
|
-
acc.push({ typ:
|
|
320
|
+
acc.push({ typ: EnumToken.WhitespaceTokenType });
|
|
269
321
|
}
|
|
270
322
|
acc.push(curr);
|
|
271
323
|
return acc;
|
|
@@ -274,11 +326,12 @@ class PropertyMap {
|
|
|
274
326
|
if (props.default.includes(curr[1][i].reduce((acc, curr) => acc + renderToken(curr) + ' ', '').trimEnd())) {
|
|
275
327
|
continue;
|
|
276
328
|
}
|
|
329
|
+
// remove default values
|
|
277
330
|
let doFilterDefault = true;
|
|
278
331
|
if (curr[0] in propertiesConfig.properties) {
|
|
279
332
|
for (let v of values) {
|
|
280
|
-
if (![
|
|
281
|
-
|| (v.typ ==
|
|
333
|
+
if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.IdenTokenType].includes(v.typ)
|
|
334
|
+
|| (v.typ == EnumToken.IdenTokenType && !this.config.properties[curr[0]].default.includes(v.val))) {
|
|
282
335
|
doFilterDefault = false;
|
|
283
336
|
break;
|
|
284
337
|
}
|
|
@@ -286,10 +339,10 @@ class PropertyMap {
|
|
|
286
339
|
}
|
|
287
340
|
// remove default values
|
|
288
341
|
values = values.filter((val) => {
|
|
289
|
-
if (val.typ ==
|
|
342
|
+
if (val.typ == EnumToken.WhitespaceTokenType || val.typ == EnumToken.CommentTokenType) {
|
|
290
343
|
return false;
|
|
291
344
|
}
|
|
292
|
-
return !doFilterDefault || !(val.typ ==
|
|
345
|
+
return !doFilterDefault || !(val.typ == EnumToken.IdenTokenType && props.default.includes(val.val));
|
|
293
346
|
});
|
|
294
347
|
if (values.length > 0) {
|
|
295
348
|
if ('mapping' in props) {
|
|
@@ -298,7 +351,7 @@ class PropertyMap {
|
|
|
298
351
|
let i = values.length;
|
|
299
352
|
while (i--) {
|
|
300
353
|
// @ts-ignore
|
|
301
|
-
if (values[i].typ ==
|
|
354
|
+
if (values[i].typ == EnumToken.IdenTokenType && values[i].val in props.mapping) {
|
|
302
355
|
// @ts-ignore
|
|
303
356
|
values.splice(i, 1, ...parseString(props.mapping[values[i].val]));
|
|
304
357
|
}
|
|
@@ -307,15 +360,20 @@ class PropertyMap {
|
|
|
307
360
|
}
|
|
308
361
|
if ('prefix' in props) {
|
|
309
362
|
// @ts-ignore
|
|
310
|
-
acc[i].push({ ...props.prefix });
|
|
363
|
+
acc[i].push({ ...props.prefix, typ: EnumToken[props.prefix.typ] });
|
|
311
364
|
}
|
|
312
365
|
else if (acc[i].length > 0) {
|
|
313
|
-
acc[i].push({ typ:
|
|
366
|
+
acc[i].push({ typ: EnumToken.WhitespaceTokenType });
|
|
314
367
|
}
|
|
315
368
|
acc[i].push(...values.reduce((acc, curr) => {
|
|
316
369
|
if (acc.length > 0) {
|
|
317
370
|
// @ts-ignore
|
|
318
|
-
acc.push({
|
|
371
|
+
acc.push({
|
|
372
|
+
...((props.separator && {
|
|
373
|
+
...props.separator,
|
|
374
|
+
typ: EnumToken[props.separator.typ]
|
|
375
|
+
}) ?? { typ: EnumToken.WhitespaceTokenType })
|
|
376
|
+
});
|
|
319
377
|
}
|
|
320
378
|
// @ts-ignore
|
|
321
379
|
acc.push(curr);
|
|
@@ -331,7 +389,7 @@ class PropertyMap {
|
|
|
331
389
|
if (curr.length == 0 && this.config.default.length > 0) {
|
|
332
390
|
curr.push(...parseString(this.config.default[0]).reduce((acc, curr) => {
|
|
333
391
|
if (acc.length > 0) {
|
|
334
|
-
acc.push({ typ:
|
|
392
|
+
acc.push({ typ: EnumToken.WhitespaceTokenType });
|
|
335
393
|
}
|
|
336
394
|
acc.push(curr);
|
|
337
395
|
return acc;
|
|
@@ -341,15 +399,26 @@ class PropertyMap {
|
|
|
341
399
|
return acc;
|
|
342
400
|
}, []);
|
|
343
401
|
if (this.config.mapping != null) {
|
|
344
|
-
const val = values.reduce((acc, curr) => acc + renderToken(curr, { removeComments: true }), '');
|
|
402
|
+
const val = values.reduce((acc, curr) => acc + renderToken(curr, { removeComments: true, minify: true }), '');
|
|
345
403
|
if (val in this.config.mapping) {
|
|
346
404
|
values.length = 0;
|
|
347
|
-
|
|
348
|
-
|
|
405
|
+
values.push({
|
|
406
|
+
typ: ['"', "'"].includes(val.charAt(0)) ? EnumToken.StringTokenType : EnumToken.IdenTokenType,
|
|
407
|
+
// @ts-ignore
|
|
408
|
+
val: this.config.mapping[val]
|
|
409
|
+
});
|
|
349
410
|
}
|
|
350
411
|
}
|
|
412
|
+
// @ts-ignore
|
|
413
|
+
if (values.length == 1 &&
|
|
414
|
+
typeof values[0].val == 'string' &&
|
|
415
|
+
this.config.default.includes(values[0].val.toLowerCase()) &&
|
|
416
|
+
this.config.default[0] != values[0].val.toLowerCase()) {
|
|
417
|
+
// @ts-ignore/
|
|
418
|
+
values = parseString(this.config.default[0]);
|
|
419
|
+
}
|
|
351
420
|
iterable = [{
|
|
352
|
-
typ:
|
|
421
|
+
typ: EnumToken.DeclarationNodeType,
|
|
353
422
|
nam: this.config.shorthand,
|
|
354
423
|
val: values
|
|
355
424
|
}][Symbol.iterator]();
|