UNPKG

4.66 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const TypeUnknown = 0;
9const TypeNull = 1;
10const TypeString = 2;
11const TypeNumber = 3;
12const TypeBoolean = 4;
13const TypeRegExp = 5;
14const TypeConditional = 6;
15const TypeArray = 7;
16const TypeConstArray = 8;
17const TypeIdentifier = 9;
18const TypeWrapped = 10;
19const TypeTemplateString = 11;
20
21class BasicEvaluatedExpression {
22 constructor() {
23 this.type = TypeUnknown;
24 this.range = null;
25 this.falsy = false;
26 this.truthy = false;
27 this.bool = null;
28 this.number = null;
29 this.regExp = null;
30 this.string = null;
31 this.quasis = null;
32 this.parts = null;
33 this.array = null;
34 this.items = null;
35 this.options = null;
36 this.prefix = null;
37 this.postfix = null;
38 this.wrappedInnerExpressions = null;
39 this.expression = null;
40 }
41
42 isNull() {
43 return this.type === TypeNull;
44 }
45
46 isString() {
47 return this.type === TypeString;
48 }
49
50 isNumber() {
51 return this.type === TypeNumber;
52 }
53
54 isBoolean() {
55 return this.type === TypeBoolean;
56 }
57
58 isRegExp() {
59 return this.type === TypeRegExp;
60 }
61
62 isConditional() {
63 return this.type === TypeConditional;
64 }
65
66 isArray() {
67 return this.type === TypeArray;
68 }
69
70 isConstArray() {
71 return this.type === TypeConstArray;
72 }
73
74 isIdentifier() {
75 return this.type === TypeIdentifier;
76 }
77
78 isWrapped() {
79 return this.type === TypeWrapped;
80 }
81
82 isTemplateString() {
83 return this.type === TypeTemplateString;
84 }
85
86 isTruthy() {
87 return this.truthy;
88 }
89
90 isFalsy() {
91 return this.falsy;
92 }
93
94 asBool() {
95 if (this.truthy) return true;
96 if (this.falsy) return false;
97 if (this.isBoolean()) return this.bool;
98 if (this.isNull()) return false;
99 if (this.isString()) return this.string !== "";
100 if (this.isNumber()) return this.number !== 0;
101 if (this.isRegExp()) return true;
102 if (this.isArray()) return true;
103 if (this.isConstArray()) return true;
104 if (this.isWrapped()) {
105 return (this.prefix && this.prefix.asBool()) ||
106 (this.postfix && this.postfix.asBool())
107 ? true
108 : undefined;
109 }
110 if (this.isTemplateString()) {
111 const str = this.asString();
112 if (typeof str === "string") return str !== "";
113 }
114 return undefined;
115 }
116
117 asString() {
118 if (this.isBoolean()) return `${this.bool}`;
119 if (this.isNull()) return "null";
120 if (this.isString()) return this.string;
121 if (this.isNumber()) return `${this.number}`;
122 if (this.isRegExp()) return `${this.regExp}`;
123 if (this.isArray()) {
124 let array = [];
125 for (const item of this.items) {
126 const itemStr = item.asString();
127 if (itemStr === undefined) return undefined;
128 array.push(itemStr);
129 }
130 return `${array}`;
131 }
132 if (this.isConstArray()) return `${this.array}`;
133 if (this.isTemplateString()) {
134 let str = "";
135 for (const part of this.parts) {
136 const partStr = part.asString();
137 if (partStr === undefined) return undefined;
138 str += partStr;
139 }
140 return str;
141 }
142 return undefined;
143 }
144
145 setString(string) {
146 this.type = TypeString;
147 this.string = string;
148 return this;
149 }
150
151 setNull() {
152 this.type = TypeNull;
153 return this;
154 }
155
156 setNumber(number) {
157 this.type = TypeNumber;
158 this.number = number;
159 return this;
160 }
161
162 setBoolean(bool) {
163 this.type = TypeBoolean;
164 this.bool = bool;
165 return this;
166 }
167
168 setRegExp(regExp) {
169 this.type = TypeRegExp;
170 this.regExp = regExp;
171 return this;
172 }
173
174 setIdentifier(identifier) {
175 this.type = TypeIdentifier;
176 this.identifier = identifier;
177 return this;
178 }
179
180 setWrapped(prefix, postfix, innerExpressions) {
181 this.type = TypeWrapped;
182 this.prefix = prefix;
183 this.postfix = postfix;
184 this.wrappedInnerExpressions = innerExpressions;
185 return this;
186 }
187
188 setOptions(options) {
189 this.type = TypeConditional;
190 this.options = options;
191 return this;
192 }
193
194 addOptions(options) {
195 if (!this.options) {
196 this.type = TypeConditional;
197 this.options = [];
198 }
199 for (const item of options) {
200 this.options.push(item);
201 }
202 return this;
203 }
204
205 setItems(items) {
206 this.type = TypeArray;
207 this.items = items;
208 return this;
209 }
210
211 setArray(array) {
212 this.type = TypeConstArray;
213 this.array = array;
214 return this;
215 }
216
217 setTemplateString(quasis, parts, kind) {
218 this.type = TypeTemplateString;
219 this.quasis = quasis;
220 this.parts = parts;
221 this.templateStringKind = kind;
222 return this;
223 }
224
225 setTruthy() {
226 this.falsy = false;
227 this.truthy = true;
228 return this;
229 }
230
231 setFalsy() {
232 this.falsy = true;
233 this.truthy = false;
234 return this;
235 }
236
237 setRange(range) {
238 this.range = range;
239 return this;
240 }
241
242 setExpression(expression) {
243 this.expression = expression;
244 return this;
245 }
246}
247
248module.exports = BasicEvaluatedExpression;