BasicEvaluatedExpression.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.array = null;
  30. this.items = null;
  31. this.options = null;
  32. this.prefix = null;
  33. this.postfix = null;
  34. }
  35. isNull() {
  36. return this.type === TypeNull;
  37. }
  38. isString() {
  39. return this.type === TypeString;
  40. }
  41. isNumber() {
  42. return this.type === TypeNumber;
  43. }
  44. isBoolean() {
  45. return this.type === TypeBoolean;
  46. }
  47. isRegExp() {
  48. return this.type === TypeRegExp;
  49. }
  50. isConditional() {
  51. return this.type === TypeConditional;
  52. }
  53. isArray() {
  54. return this.type === TypeArray;
  55. }
  56. isConstArray() {
  57. return this.type === TypeConstArray;
  58. }
  59. isIdentifier() {
  60. return this.type === TypeIdentifier;
  61. }
  62. isWrapped() {
  63. return this.type === TypeWrapped;
  64. }
  65. isTemplateString() {
  66. return this.type === TypeTemplateString;
  67. }
  68. isTruthy() {
  69. return this.truthy;
  70. }
  71. isFalsy() {
  72. return this.falsy;
  73. }
  74. asBool() {
  75. if (this.truthy) return true;
  76. if (this.falsy) return false;
  77. if (this.isBoolean()) return this.bool;
  78. if (this.isNull()) return false;
  79. if (this.isString()) return this.string !== "";
  80. if (this.isNumber()) return this.number !== 0;
  81. if (this.isRegExp()) return true;
  82. if (this.isArray()) return true;
  83. if (this.isConstArray()) return true;
  84. if (this.isWrapped()) {
  85. return (this.prefix && this.prefix.asBool()) ||
  86. (this.postfix && this.postfix.asBool())
  87. ? true
  88. : undefined;
  89. }
  90. if (this.isTemplateString()) {
  91. for (const quasi of this.quasis) {
  92. if (quasi.asBool()) return true;
  93. }
  94. // can't tell if string will be empty without executing
  95. }
  96. return undefined;
  97. }
  98. setString(string) {
  99. this.type = TypeString;
  100. this.string = string;
  101. return this;
  102. }
  103. setNull() {
  104. this.type = TypeNull;
  105. return this;
  106. }
  107. setNumber(number) {
  108. this.type = TypeNumber;
  109. this.number = number;
  110. return this;
  111. }
  112. setBoolean(bool) {
  113. this.type = TypeBoolean;
  114. this.bool = bool;
  115. return this;
  116. }
  117. setRegExp(regExp) {
  118. this.type = TypeRegExp;
  119. this.regExp = regExp;
  120. return this;
  121. }
  122. setIdentifier(identifier) {
  123. this.type = TypeIdentifier;
  124. this.identifier = identifier;
  125. return this;
  126. }
  127. setWrapped(prefix, postfix) {
  128. this.type = TypeWrapped;
  129. this.prefix = prefix;
  130. this.postfix = postfix;
  131. return this;
  132. }
  133. setOptions(options) {
  134. this.type = TypeConditional;
  135. this.options = options;
  136. return this;
  137. }
  138. addOptions(options) {
  139. if (!this.options) {
  140. this.type = TypeConditional;
  141. this.options = [];
  142. }
  143. for (const item of options) {
  144. this.options.push(item);
  145. }
  146. return this;
  147. }
  148. setItems(items) {
  149. this.type = TypeArray;
  150. this.items = items;
  151. return this;
  152. }
  153. setArray(array) {
  154. this.type = TypeConstArray;
  155. this.array = array;
  156. return this;
  157. }
  158. setTemplateString(quasis) {
  159. this.type = TypeTemplateString;
  160. this.quasis = quasis;
  161. return this;
  162. }
  163. setTruthy() {
  164. this.falsy = false;
  165. this.truthy = true;
  166. return this;
  167. }
  168. setFalsy() {
  169. this.falsy = true;
  170. this.truthy = false;
  171. return this;
  172. }
  173. setRange(range) {
  174. this.range = range;
  175. return this;
  176. }
  177. }
  178. module.exports = BasicEvaluatedExpression;