BasicEvaluatedExpression.js 10 KB

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