BasicEvaluatedExpression.js 4.5 KB

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