BasicEvaluatedExpression.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("estree").Node} EsTreeNode */
  7. /** @typedef {import("./JavascriptParser").VariableInfoInterface} VariableInfoInterface */
  8. const TypeUnknown = 0;
  9. const TypeUndefined = 1;
  10. const TypeNull = 2;
  11. const TypeString = 3;
  12. const TypeNumber = 4;
  13. const TypeBoolean = 5;
  14. const TypeRegExp = 6;
  15. const TypeConditional = 7;
  16. const TypeArray = 8;
  17. const TypeConstArray = 9;
  18. const TypeIdentifier = 10;
  19. const TypeWrapped = 11;
  20. const TypeTemplateString = 12;
  21. const TypeBigInt = 13;
  22. class BasicEvaluatedExpression {
  23. constructor() {
  24. this.type = TypeUnknown;
  25. /** @type {[number, number]} */
  26. this.range = undefined;
  27. /** @type {boolean} */
  28. this.falsy = false;
  29. /** @type {boolean} */
  30. this.truthy = false;
  31. /** @type {boolean | undefined} */
  32. this.nullish = undefined;
  33. /** @type {boolean} */
  34. this.sideEffects = true;
  35. /** @type {boolean | undefined} */
  36. this.bool = undefined;
  37. /** @type {number | undefined} */
  38. this.number = undefined;
  39. /** @type {bigint | undefined} */
  40. this.bigint = undefined;
  41. /** @type {RegExp | undefined} */
  42. this.regExp = undefined;
  43. /** @type {string | undefined} */
  44. this.string = undefined;
  45. /** @type {BasicEvaluatedExpression[] | undefined} */
  46. this.quasis = undefined;
  47. /** @type {BasicEvaluatedExpression[] | undefined} */
  48. this.parts = undefined;
  49. /** @type {any[] | undefined} */
  50. this.array = undefined;
  51. /** @type {BasicEvaluatedExpression[] | undefined} */
  52. this.items = undefined;
  53. /** @type {BasicEvaluatedExpression[] | undefined} */
  54. this.options = undefined;
  55. /** @type {BasicEvaluatedExpression | undefined} */
  56. this.prefix = undefined;
  57. /** @type {BasicEvaluatedExpression | undefined} */
  58. this.postfix = undefined;
  59. this.wrappedInnerExpressions = undefined;
  60. /** @type {string | undefined} */
  61. this.identifier = undefined;
  62. /** @type {VariableInfoInterface} */
  63. this.rootInfo = undefined;
  64. /** @type {() => string[]} */
  65. this.getMembers = undefined;
  66. /** @type {EsTreeNode} */
  67. this.expression = undefined;
  68. }
  69. isUnknown() {
  70. return this.type === TypeUnknown;
  71. }
  72. isNull() {
  73. return this.type === TypeNull;
  74. }
  75. isUndefined() {
  76. return this.type === TypeUndefined;
  77. }
  78. isString() {
  79. return this.type === TypeString;
  80. }
  81. isNumber() {
  82. return this.type === TypeNumber;
  83. }
  84. isBigInt() {
  85. return this.type === TypeBigInt;
  86. }
  87. isBoolean() {
  88. return this.type === TypeBoolean;
  89. }
  90. isRegExp() {
  91. return this.type === TypeRegExp;
  92. }
  93. isConditional() {
  94. return this.type === TypeConditional;
  95. }
  96. isArray() {
  97. return this.type === TypeArray;
  98. }
  99. isConstArray() {
  100. return this.type === TypeConstArray;
  101. }
  102. isIdentifier() {
  103. return this.type === TypeIdentifier;
  104. }
  105. isWrapped() {
  106. return this.type === TypeWrapped;
  107. }
  108. isTemplateString() {
  109. return this.type === TypeTemplateString;
  110. }
  111. /**
  112. * Is expression a primitive or an object type value?
  113. * @returns {boolean | undefined} true: primitive type, false: object type, undefined: unknown/runtime-defined
  114. */
  115. isPrimitiveType() {
  116. switch (this.type) {
  117. case TypeUndefined:
  118. case TypeNull:
  119. case TypeString:
  120. case TypeNumber:
  121. case TypeBoolean:
  122. case TypeBigInt:
  123. case TypeWrapped:
  124. case TypeTemplateString:
  125. return true;
  126. case TypeRegExp:
  127. case TypeArray:
  128. case TypeConstArray:
  129. return false;
  130. default:
  131. return undefined;
  132. }
  133. }
  134. /**
  135. * Is expression a runtime or compile-time value?
  136. * @returns {boolean} true: compile time value, false: runtime value
  137. */
  138. isCompileTimeValue() {
  139. switch (this.type) {
  140. case TypeUndefined:
  141. case TypeNull:
  142. case TypeString:
  143. case TypeNumber:
  144. case TypeBoolean:
  145. case TypeRegExp:
  146. case TypeConstArray:
  147. case TypeBigInt:
  148. return true;
  149. default:
  150. return false;
  151. }
  152. }
  153. /**
  154. * Gets the compile-time value of the expression
  155. * @returns {any} the javascript value
  156. */
  157. asCompileTimeValue() {
  158. switch (this.type) {
  159. case TypeUndefined:
  160. return undefined;
  161. case TypeNull:
  162. return null;
  163. case TypeString:
  164. return this.string;
  165. case TypeNumber:
  166. return this.number;
  167. case TypeBoolean:
  168. return this.bool;
  169. case TypeRegExp:
  170. return this.regExp;
  171. case TypeConstArray:
  172. return this.array;
  173. case TypeBigInt:
  174. return this.bigint;
  175. default:
  176. throw new Error(
  177. "asCompileTimeValue must only be called for compile-time values"
  178. );
  179. }
  180. }
  181. isTruthy() {
  182. return this.truthy;
  183. }
  184. isFalsy() {
  185. return this.falsy;
  186. }
  187. isNullish() {
  188. return this.nullish;
  189. }
  190. /**
  191. * Can this expression have side effects?
  192. * @returns {boolean} false: never has side effects
  193. */
  194. couldHaveSideEffects() {
  195. return this.sideEffects;
  196. }
  197. asBool() {
  198. if (this.truthy) return true;
  199. if (this.falsy || this.nullish) return false;
  200. if (this.isBoolean()) return this.bool;
  201. if (this.isNull()) return false;
  202. if (this.isUndefined()) return false;
  203. if (this.isString()) return this.string !== "";
  204. if (this.isNumber()) return this.number !== 0;
  205. if (this.isBigInt()) return this.bigint !== BigInt(0);
  206. if (this.isRegExp()) return true;
  207. if (this.isArray()) return true;
  208. if (this.isConstArray()) return true;
  209. if (this.isWrapped()) {
  210. return (this.prefix && this.prefix.asBool()) ||
  211. (this.postfix && this.postfix.asBool())
  212. ? true
  213. : undefined;
  214. }
  215. if (this.isTemplateString()) {
  216. const str = this.asString();
  217. if (typeof str === "string") return str !== "";
  218. }
  219. return undefined;
  220. }
  221. asNullish() {
  222. const nullish = this.isNullish();
  223. if (nullish === true || this.isNull() || this.isUndefined()) return true;
  224. if (nullish === false) return false;
  225. if (this.isTruthy()) return false;
  226. if (this.isBoolean()) return false;
  227. if (this.isString()) return false;
  228. if (this.isNumber()) return false;
  229. if (this.isBigInt()) return false;
  230. if (this.isRegExp()) return false;
  231. if (this.isArray()) return false;
  232. if (this.isConstArray()) return false;
  233. if (this.isTemplateString()) return false;
  234. if (this.isRegExp()) return false;
  235. return undefined;
  236. }
  237. asString() {
  238. if (this.isBoolean()) return `${this.bool}`;
  239. if (this.isNull()) return "null";
  240. if (this.isUndefined()) return "undefined";
  241. if (this.isString()) return this.string;
  242. if (this.isNumber()) return `${this.number}`;
  243. if (this.isBigInt()) return `${this.bigint}`;
  244. if (this.isRegExp()) return `${this.regExp}`;
  245. if (this.isArray()) {
  246. let array = [];
  247. for (const item of this.items) {
  248. const itemStr = item.asString();
  249. if (itemStr === undefined) return undefined;
  250. array.push(itemStr);
  251. }
  252. return `${array}`;
  253. }
  254. if (this.isConstArray()) return `${this.array}`;
  255. if (this.isTemplateString()) {
  256. let str = "";
  257. for (const part of this.parts) {
  258. const partStr = part.asString();
  259. if (partStr === undefined) return undefined;
  260. str += partStr;
  261. }
  262. return str;
  263. }
  264. return undefined;
  265. }
  266. setString(string) {
  267. this.type = TypeString;
  268. this.string = string;
  269. this.sideEffects = false;
  270. return this;
  271. }
  272. setUndefined() {
  273. this.type = TypeUndefined;
  274. this.sideEffects = false;
  275. return this;
  276. }
  277. setNull() {
  278. this.type = TypeNull;
  279. this.sideEffects = false;
  280. return this;
  281. }
  282. setNumber(number) {
  283. this.type = TypeNumber;
  284. this.number = number;
  285. this.sideEffects = false;
  286. return this;
  287. }
  288. setBigInt(bigint) {
  289. this.type = TypeBigInt;
  290. this.bigint = bigint;
  291. this.sideEffects = false;
  292. return this;
  293. }
  294. setBoolean(bool) {
  295. this.type = TypeBoolean;
  296. this.bool = bool;
  297. this.sideEffects = false;
  298. return this;
  299. }
  300. setRegExp(regExp) {
  301. this.type = TypeRegExp;
  302. this.regExp = regExp;
  303. this.sideEffects = false;
  304. return this;
  305. }
  306. setIdentifier(identifier, rootInfo, getMembers) {
  307. this.type = TypeIdentifier;
  308. this.identifier = identifier;
  309. this.rootInfo = rootInfo;
  310. this.getMembers = getMembers;
  311. this.sideEffects = true;
  312. return this;
  313. }
  314. setWrapped(prefix, postfix, innerExpressions) {
  315. this.type = TypeWrapped;
  316. this.prefix = prefix;
  317. this.postfix = postfix;
  318. this.wrappedInnerExpressions = innerExpressions;
  319. this.sideEffects = true;
  320. return this;
  321. }
  322. setOptions(options) {
  323. this.type = TypeConditional;
  324. this.options = options;
  325. this.sideEffects = true;
  326. return this;
  327. }
  328. addOptions(options) {
  329. if (!this.options) {
  330. this.type = TypeConditional;
  331. this.options = [];
  332. this.sideEffects = true;
  333. }
  334. for (const item of options) {
  335. this.options.push(item);
  336. }
  337. return this;
  338. }
  339. setItems(items) {
  340. this.type = TypeArray;
  341. this.items = items;
  342. this.sideEffects = items.some(i => i.couldHaveSideEffects());
  343. return this;
  344. }
  345. setArray(array) {
  346. this.type = TypeConstArray;
  347. this.array = array;
  348. this.sideEffects = false;
  349. return this;
  350. }
  351. setTemplateString(quasis, parts, kind) {
  352. this.type = TypeTemplateString;
  353. this.quasis = quasis;
  354. this.parts = parts;
  355. this.templateStringKind = kind;
  356. this.sideEffects = parts.some(p => p.sideEffects);
  357. return this;
  358. }
  359. setTruthy() {
  360. this.falsy = false;
  361. this.truthy = true;
  362. this.nullish = false;
  363. return this;
  364. }
  365. setFalsy() {
  366. this.falsy = true;
  367. this.truthy = false;
  368. return this;
  369. }
  370. setNullish(value) {
  371. this.nullish = value;
  372. if (value) return this.setFalsy();
  373. return this;
  374. }
  375. setRange(range) {
  376. this.range = range;
  377. return this;
  378. }
  379. setSideEffects(sideEffects = true) {
  380. this.sideEffects = sideEffects;
  381. return this;
  382. }
  383. setExpression(expression) {
  384. this.expression = expression;
  385. return this;
  386. }
  387. }
  388. /**
  389. * @param {string} flags regexp flags
  390. * @returns {boolean} is valid flags
  391. */
  392. BasicEvaluatedExpression.isValidRegExpFlags = flags => {
  393. const len = flags.length;
  394. if (len === 0) return true;
  395. if (len > 4) return false;
  396. // cspell:word gimy
  397. let remaining = 0b0000; // bit per RegExp flag: gimy
  398. for (let i = 0; i < len; i++)
  399. switch (flags.charCodeAt(i)) {
  400. case 103 /* g */:
  401. if (remaining & 0b1000) return false;
  402. remaining |= 0b1000;
  403. break;
  404. case 105 /* i */:
  405. if (remaining & 0b0100) return false;
  406. remaining |= 0b0100;
  407. break;
  408. case 109 /* m */:
  409. if (remaining & 0b0010) return false;
  410. remaining |= 0b0010;
  411. break;
  412. case 121 /* y */:
  413. if (remaining & 0b0001) return false;
  414. remaining |= 0b0001;
  415. break;
  416. default:
  417. return false;
  418. }
  419. return true;
  420. };
  421. module.exports = BasicEvaluatedExpression;