BasicEvaluatedExpression.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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} Node */
  7. /** @typedef {import("./JavascriptParser").Range} Range */
  8. /** @typedef {import("./JavascriptParser").VariableInfo} VariableInfo */
  9. /** @typedef {import("./JavascriptParser").Members} Members */
  10. /** @typedef {import("./JavascriptParser").MembersOptionals} MembersOptionals */
  11. /** @typedef {import("./JavascriptParser").MemberRanges} MemberRanges */
  12. const TypeUnknown = 0;
  13. const TypeUndefined = 1;
  14. const TypeNull = 2;
  15. const TypeString = 3;
  16. const TypeNumber = 4;
  17. const TypeBoolean = 5;
  18. const TypeRegExp = 6;
  19. const TypeConditional = 7;
  20. const TypeArray = 8;
  21. const TypeConstArray = 9;
  22. const TypeIdentifier = 10;
  23. const TypeWrapped = 11;
  24. const TypeTemplateString = 12;
  25. const TypeBigInt = 13;
  26. /** @typedef {() => Members} GetMembers */
  27. /** @typedef {() => MembersOptionals} GetMembersOptionals */
  28. /** @typedef {() => MemberRanges} GetMemberRanges */
  29. class BasicEvaluatedExpression {
  30. constructor() {
  31. this.type = TypeUnknown;
  32. /** @type {Range | undefined} */
  33. this.range = undefined;
  34. /** @type {boolean} */
  35. this.falsy = false;
  36. /** @type {boolean} */
  37. this.truthy = false;
  38. /** @type {boolean | undefined} */
  39. this.nullish = undefined;
  40. /** @type {boolean} */
  41. this.sideEffects = true;
  42. /** @type {boolean | undefined} */
  43. this.bool = undefined;
  44. /** @type {number | undefined} */
  45. this.number = undefined;
  46. /** @type {bigint | undefined} */
  47. this.bigint = undefined;
  48. /** @type {RegExp | undefined} */
  49. this.regExp = undefined;
  50. /** @type {string | undefined} */
  51. this.string = undefined;
  52. /** @type {BasicEvaluatedExpression[] | undefined} */
  53. this.quasis = undefined;
  54. /** @type {BasicEvaluatedExpression[] | undefined} */
  55. this.parts = undefined;
  56. /** @type {EXPECTED_ANY[] | undefined} */
  57. this.array = undefined;
  58. /** @type {BasicEvaluatedExpression[] | undefined} */
  59. this.items = undefined;
  60. /** @type {BasicEvaluatedExpression[] | undefined} */
  61. this.options = undefined;
  62. /** @type {BasicEvaluatedExpression | undefined | null} */
  63. this.prefix = undefined;
  64. /** @type {BasicEvaluatedExpression | undefined | null} */
  65. this.postfix = undefined;
  66. /** @type {BasicEvaluatedExpression[] | undefined} */
  67. this.wrappedInnerExpressions = undefined;
  68. /** @type {string | VariableInfo | undefined} */
  69. this.identifier = undefined;
  70. /** @type {string | VariableInfo | undefined} */
  71. this.rootInfo = undefined;
  72. /** @type {GetMembers | undefined} */
  73. this.getMembers = undefined;
  74. /** @type {GetMembersOptionals | undefined} */
  75. this.getMembersOptionals = undefined;
  76. /** @type {GetMemberRanges | undefined} */
  77. this.getMemberRanges = undefined;
  78. /** @type {Node | undefined} */
  79. this.expression = undefined;
  80. }
  81. isUnknown() {
  82. return this.type === TypeUnknown;
  83. }
  84. isNull() {
  85. return this.type === TypeNull;
  86. }
  87. isUndefined() {
  88. return this.type === TypeUndefined;
  89. }
  90. isString() {
  91. return this.type === TypeString;
  92. }
  93. isNumber() {
  94. return this.type === TypeNumber;
  95. }
  96. isBigInt() {
  97. return this.type === TypeBigInt;
  98. }
  99. isBoolean() {
  100. return this.type === TypeBoolean;
  101. }
  102. isRegExp() {
  103. return this.type === TypeRegExp;
  104. }
  105. isConditional() {
  106. return this.type === TypeConditional;
  107. }
  108. isArray() {
  109. return this.type === TypeArray;
  110. }
  111. isConstArray() {
  112. return this.type === TypeConstArray;
  113. }
  114. isIdentifier() {
  115. return this.type === TypeIdentifier;
  116. }
  117. isWrapped() {
  118. return this.type === TypeWrapped;
  119. }
  120. isTemplateString() {
  121. return this.type === TypeTemplateString;
  122. }
  123. /**
  124. * Is expression a primitive or an object type value?
  125. * @returns {boolean | undefined} true: primitive type, false: object type, undefined: unknown/runtime-defined
  126. */
  127. isPrimitiveType() {
  128. switch (this.type) {
  129. case TypeUndefined:
  130. case TypeNull:
  131. case TypeString:
  132. case TypeNumber:
  133. case TypeBoolean:
  134. case TypeBigInt:
  135. case TypeWrapped:
  136. case TypeTemplateString:
  137. return true;
  138. case TypeRegExp:
  139. case TypeArray:
  140. case TypeConstArray:
  141. return false;
  142. default:
  143. return undefined;
  144. }
  145. }
  146. /**
  147. * Is expression a runtime or compile-time value?
  148. * @returns {boolean} true: compile time value, false: runtime value
  149. */
  150. isCompileTimeValue() {
  151. switch (this.type) {
  152. case TypeUndefined:
  153. case TypeNull:
  154. case TypeString:
  155. case TypeNumber:
  156. case TypeBoolean:
  157. case TypeRegExp:
  158. case TypeConstArray:
  159. case TypeBigInt:
  160. return true;
  161. default:
  162. return false;
  163. }
  164. }
  165. /**
  166. * Gets the compile-time value of the expression
  167. * @returns {undefined | null | string | number | boolean | RegExp | EXPECTED_ANY[] | bigint} the javascript value
  168. */
  169. asCompileTimeValue() {
  170. switch (this.type) {
  171. case TypeUndefined:
  172. return;
  173. case TypeNull:
  174. return null;
  175. case TypeString:
  176. return this.string;
  177. case TypeNumber:
  178. return this.number;
  179. case TypeBoolean:
  180. return this.bool;
  181. case TypeRegExp:
  182. return this.regExp;
  183. case TypeConstArray:
  184. return this.array;
  185. case TypeBigInt:
  186. return this.bigint;
  187. default:
  188. throw new Error(
  189. "asCompileTimeValue must only be called for compile-time values"
  190. );
  191. }
  192. }
  193. isTruthy() {
  194. return this.truthy;
  195. }
  196. isFalsy() {
  197. return this.falsy;
  198. }
  199. isNullish() {
  200. return this.nullish;
  201. }
  202. /**
  203. * Can this expression have side effects?
  204. * @returns {boolean} false: never has side effects
  205. */
  206. couldHaveSideEffects() {
  207. return this.sideEffects;
  208. }
  209. /**
  210. * Creates a boolean representation of this evaluated expression.
  211. * @returns {boolean | undefined} true: truthy, false: falsy, undefined: unknown
  212. */
  213. asBool() {
  214. if (this.truthy) return true;
  215. if (this.falsy || this.nullish) return false;
  216. if (this.isBoolean()) return this.bool;
  217. if (this.isNull()) return false;
  218. if (this.isUndefined()) return false;
  219. if (this.isString()) return this.string !== "";
  220. if (this.isNumber()) return this.number !== 0;
  221. if (this.isBigInt()) return this.bigint !== BigInt(0);
  222. if (this.isRegExp()) return true;
  223. if (this.isArray()) return true;
  224. if (this.isConstArray()) return true;
  225. if (this.isWrapped()) {
  226. return (this.prefix && this.prefix.asBool()) ||
  227. (this.postfix && this.postfix.asBool())
  228. ? true
  229. : undefined;
  230. }
  231. if (this.isTemplateString()) {
  232. const str = this.asString();
  233. if (typeof str === "string") return str !== "";
  234. }
  235. }
  236. /**
  237. * Creates a nullish coalescing representation of this evaluated expression.
  238. * @returns {boolean | undefined} true: nullish, false: not nullish, undefined: unknown
  239. */
  240. asNullish() {
  241. const nullish = this.isNullish();
  242. if (nullish === true || this.isNull() || this.isUndefined()) return true;
  243. if (nullish === false) return false;
  244. if (this.isTruthy()) return false;
  245. if (this.isBoolean()) return false;
  246. if (this.isString()) return false;
  247. if (this.isNumber()) return false;
  248. if (this.isBigInt()) return false;
  249. if (this.isRegExp()) return false;
  250. if (this.isArray()) return false;
  251. if (this.isConstArray()) return false;
  252. if (this.isTemplateString()) return false;
  253. if (this.isRegExp()) return false;
  254. }
  255. /**
  256. * Creates a string representation of this evaluated expression.
  257. * @returns {string | undefined} the string representation or undefined if not possible
  258. */
  259. asString() {
  260. if (this.isBoolean()) return `${this.bool}`;
  261. if (this.isNull()) return "null";
  262. if (this.isUndefined()) return "undefined";
  263. if (this.isString()) return this.string;
  264. if (this.isNumber()) return `${this.number}`;
  265. if (this.isBigInt()) return `${this.bigint}`;
  266. if (this.isRegExp()) return `${this.regExp}`;
  267. if (this.isArray()) {
  268. /** @type {string[]} */
  269. const array = [];
  270. for (const item of /** @type {BasicEvaluatedExpression[]} */ (
  271. this.items
  272. )) {
  273. const itemStr = item.asString();
  274. if (itemStr === undefined) return;
  275. array.push(itemStr);
  276. }
  277. return `${array}`;
  278. }
  279. if (this.isConstArray()) return `${this.array}`;
  280. if (this.isTemplateString()) {
  281. let str = "";
  282. for (const part of /** @type {BasicEvaluatedExpression[]} */ (
  283. this.parts
  284. )) {
  285. const partStr = part.asString();
  286. if (partStr === undefined) return;
  287. str += partStr;
  288. }
  289. return str;
  290. }
  291. }
  292. /**
  293. * @param {string} string value
  294. * @returns {BasicEvaluatedExpression} basic evaluated expression
  295. */
  296. setString(string) {
  297. this.type = TypeString;
  298. this.string = string;
  299. this.sideEffects = false;
  300. return this;
  301. }
  302. setUndefined() {
  303. this.type = TypeUndefined;
  304. this.sideEffects = false;
  305. return this;
  306. }
  307. setNull() {
  308. this.type = TypeNull;
  309. this.sideEffects = false;
  310. return this;
  311. }
  312. /**
  313. * Set's the value of this expression to a number
  314. * @param {number} number number to set
  315. * @returns {this} this
  316. */
  317. setNumber(number) {
  318. this.type = TypeNumber;
  319. this.number = number;
  320. this.sideEffects = false;
  321. return this;
  322. }
  323. /**
  324. * Set's the value of this expression to a BigInt
  325. * @param {bigint} bigint bigint to set
  326. * @returns {this} this
  327. */
  328. setBigInt(bigint) {
  329. this.type = TypeBigInt;
  330. this.bigint = bigint;
  331. this.sideEffects = false;
  332. return this;
  333. }
  334. /**
  335. * Set's the value of this expression to a boolean
  336. * @param {boolean} bool boolean to set
  337. * @returns {this} this
  338. */
  339. setBoolean(bool) {
  340. this.type = TypeBoolean;
  341. this.bool = bool;
  342. this.sideEffects = false;
  343. return this;
  344. }
  345. /**
  346. * Set's the value of this expression to a regular expression
  347. * @param {RegExp} regExp regular expression to set
  348. * @returns {this} this
  349. */
  350. setRegExp(regExp) {
  351. this.type = TypeRegExp;
  352. this.regExp = regExp;
  353. this.sideEffects = false;
  354. return this;
  355. }
  356. /**
  357. * Set's the value of this expression to a particular identifier and its members.
  358. * @param {string | VariableInfo} identifier identifier to set
  359. * @param {string | VariableInfo} rootInfo root info
  360. * @param {GetMembers} getMembers members
  361. * @param {GetMembersOptionals=} getMembersOptionals optional members
  362. * @param {GetMemberRanges=} getMemberRanges ranges of progressively increasing sub-expressions
  363. * @returns {this} this
  364. */
  365. setIdentifier(
  366. identifier,
  367. rootInfo,
  368. getMembers,
  369. getMembersOptionals,
  370. getMemberRanges
  371. ) {
  372. this.type = TypeIdentifier;
  373. this.identifier = identifier;
  374. this.rootInfo = rootInfo;
  375. this.getMembers = getMembers;
  376. this.getMembersOptionals = getMembersOptionals;
  377. this.getMemberRanges = getMemberRanges;
  378. this.sideEffects = true;
  379. return this;
  380. }
  381. /**
  382. * Wraps an array of expressions with a prefix and postfix expression.
  383. * @param {BasicEvaluatedExpression | null | undefined} prefix Expression to be added before the innerExpressions
  384. * @param {BasicEvaluatedExpression | null | undefined} postfix Expression to be added after the innerExpressions
  385. * @param {BasicEvaluatedExpression[] | undefined} innerExpressions Expressions to be wrapped
  386. * @returns {this} this
  387. */
  388. setWrapped(prefix, postfix, innerExpressions) {
  389. this.type = TypeWrapped;
  390. this.prefix = prefix;
  391. this.postfix = postfix;
  392. this.wrappedInnerExpressions = innerExpressions;
  393. this.sideEffects = true;
  394. return this;
  395. }
  396. /**
  397. * Stores the options of a conditional expression.
  398. * @param {BasicEvaluatedExpression[]} options optional (consequent/alternate) expressions to be set
  399. * @returns {this} this
  400. */
  401. setOptions(options) {
  402. this.type = TypeConditional;
  403. this.options = options;
  404. this.sideEffects = true;
  405. return this;
  406. }
  407. /**
  408. * Adds options to a conditional expression.
  409. * @param {BasicEvaluatedExpression[]} options optional (consequent/alternate) expressions to be added
  410. * @returns {this} this
  411. */
  412. addOptions(options) {
  413. if (!this.options) {
  414. this.type = TypeConditional;
  415. this.options = [];
  416. this.sideEffects = true;
  417. }
  418. for (const item of options) {
  419. this.options.push(item);
  420. }
  421. return this;
  422. }
  423. /**
  424. * Set's the value of this expression to an array of expressions.
  425. * @param {BasicEvaluatedExpression[]} items expressions to set
  426. * @returns {this} this
  427. */
  428. setItems(items) {
  429. this.type = TypeArray;
  430. this.items = items;
  431. this.sideEffects = items.some((i) => i.couldHaveSideEffects());
  432. return this;
  433. }
  434. /**
  435. * Set's the value of this expression to an array of strings.
  436. * @param {string[]} array array to set
  437. * @returns {this} this
  438. */
  439. setArray(array) {
  440. this.type = TypeConstArray;
  441. this.array = array;
  442. this.sideEffects = false;
  443. return this;
  444. }
  445. /**
  446. * Set's the value of this expression to a processed/unprocessed template string. Used
  447. * for evaluating TemplateLiteral expressions in the JavaScript Parser.
  448. * @param {BasicEvaluatedExpression[]} quasis template string quasis
  449. * @param {BasicEvaluatedExpression[]} parts template string parts
  450. * @param {"cooked" | "raw"} kind template string kind
  451. * @returns {this} this
  452. */
  453. setTemplateString(quasis, parts, kind) {
  454. this.type = TypeTemplateString;
  455. this.quasis = quasis;
  456. this.parts = parts;
  457. this.templateStringKind = kind;
  458. this.sideEffects = parts.some((p) => p.sideEffects);
  459. return this;
  460. }
  461. setTruthy() {
  462. this.falsy = false;
  463. this.truthy = true;
  464. this.nullish = false;
  465. return this;
  466. }
  467. setFalsy() {
  468. this.falsy = true;
  469. this.truthy = false;
  470. return this;
  471. }
  472. /**
  473. * Set's the value of the expression to nullish.
  474. * @param {boolean} value true, if the expression is nullish
  475. * @returns {this} this
  476. */
  477. setNullish(value) {
  478. this.nullish = value;
  479. if (value) return this.setFalsy();
  480. return this;
  481. }
  482. /**
  483. * Set's the range for the expression.
  484. * @param {Range} range range to set
  485. * @returns {this} this
  486. */
  487. setRange(range) {
  488. this.range = range;
  489. return this;
  490. }
  491. /**
  492. * Set whether or not the expression has side effects.
  493. * @param {boolean} sideEffects true, if the expression has side effects
  494. * @returns {this} this
  495. */
  496. setSideEffects(sideEffects = true) {
  497. this.sideEffects = sideEffects;
  498. return this;
  499. }
  500. /**
  501. * Set the expression node for the expression.
  502. * @param {Node | undefined} expression expression
  503. * @returns {this} this
  504. */
  505. setExpression(expression) {
  506. this.expression = expression;
  507. return this;
  508. }
  509. }
  510. /**
  511. * @param {string} flags regexp flags
  512. * @returns {boolean} is valid flags
  513. */
  514. BasicEvaluatedExpression.isValidRegExpFlags = (flags) => {
  515. const len = flags.length;
  516. if (len === 0) return true;
  517. if (len > 4) return false;
  518. // cspell:word gimy
  519. let remaining = 0b0000; // bit per RegExp flag: gimy
  520. for (let i = 0; i < len; i++) {
  521. switch (flags.charCodeAt(i)) {
  522. case 103 /* g */:
  523. if (remaining & 0b1000) return false;
  524. remaining |= 0b1000;
  525. break;
  526. case 105 /* i */:
  527. if (remaining & 0b0100) return false;
  528. remaining |= 0b0100;
  529. break;
  530. case 109 /* m */:
  531. if (remaining & 0b0010) return false;
  532. remaining |= 0b0010;
  533. break;
  534. case 121 /* y */:
  535. if (remaining & 0b0001) return false;
  536. remaining |= 0b0001;
  537. break;
  538. default:
  539. return false;
  540. }
  541. }
  542. return true;
  543. };
  544. module.exports = BasicEvaluatedExpression;