BasicEvaluatedExpression.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. * As compile time value.
  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. * Updates string using the provided string.
  294. * @param {string} string value
  295. * @returns {BasicEvaluatedExpression} basic evaluated expression
  296. */
  297. setString(string) {
  298. this.type = TypeString;
  299. this.string = string;
  300. this.sideEffects = false;
  301. return this;
  302. }
  303. setUndefined() {
  304. this.type = TypeUndefined;
  305. this.sideEffects = false;
  306. return this;
  307. }
  308. setNull() {
  309. this.type = TypeNull;
  310. this.sideEffects = false;
  311. return this;
  312. }
  313. /**
  314. * Set's the value of this expression to a number
  315. * @param {number} number number to set
  316. * @returns {this} this
  317. */
  318. setNumber(number) {
  319. this.type = TypeNumber;
  320. this.number = number;
  321. this.sideEffects = false;
  322. return this;
  323. }
  324. /**
  325. * Set's the value of this expression to a BigInt
  326. * @param {bigint} bigint bigint to set
  327. * @returns {this} this
  328. */
  329. setBigInt(bigint) {
  330. this.type = TypeBigInt;
  331. this.bigint = bigint;
  332. this.sideEffects = false;
  333. return this;
  334. }
  335. /**
  336. * Set's the value of this expression to a boolean
  337. * @param {boolean} bool boolean to set
  338. * @returns {this} this
  339. */
  340. setBoolean(bool) {
  341. this.type = TypeBoolean;
  342. this.bool = bool;
  343. this.sideEffects = false;
  344. return this;
  345. }
  346. /**
  347. * Set's the value of this expression to a regular expression
  348. * @param {RegExp} regExp regular expression to set
  349. * @returns {this} this
  350. */
  351. setRegExp(regExp) {
  352. this.type = TypeRegExp;
  353. this.regExp = regExp;
  354. this.sideEffects = false;
  355. return this;
  356. }
  357. /**
  358. * Set's the value of this expression to a particular identifier and its members.
  359. * @param {string | VariableInfo} identifier identifier to set
  360. * @param {string | VariableInfo} rootInfo root info
  361. * @param {GetMembers} getMembers members
  362. * @param {GetMembersOptionals=} getMembersOptionals optional members
  363. * @param {GetMemberRanges=} getMemberRanges ranges of progressively increasing sub-expressions
  364. * @returns {this} this
  365. */
  366. setIdentifier(
  367. identifier,
  368. rootInfo,
  369. getMembers,
  370. getMembersOptionals,
  371. getMemberRanges
  372. ) {
  373. this.type = TypeIdentifier;
  374. this.identifier = identifier;
  375. this.rootInfo = rootInfo;
  376. this.getMembers = getMembers;
  377. this.getMembersOptionals = getMembersOptionals;
  378. this.getMemberRanges = getMemberRanges;
  379. this.sideEffects = true;
  380. return this;
  381. }
  382. /**
  383. * Wraps an array of expressions with a prefix and postfix expression.
  384. * @param {BasicEvaluatedExpression | null | undefined} prefix Expression to be added before the innerExpressions
  385. * @param {BasicEvaluatedExpression | null | undefined} postfix Expression to be added after the innerExpressions
  386. * @param {BasicEvaluatedExpression[] | undefined} innerExpressions Expressions to be wrapped
  387. * @returns {this} this
  388. */
  389. setWrapped(prefix, postfix, innerExpressions) {
  390. this.type = TypeWrapped;
  391. this.prefix = prefix;
  392. this.postfix = postfix;
  393. this.wrappedInnerExpressions = innerExpressions;
  394. this.sideEffects = true;
  395. return this;
  396. }
  397. /**
  398. * Stores the options of a conditional expression.
  399. * @param {BasicEvaluatedExpression[]} options optional (consequent/alternate) expressions to be set
  400. * @returns {this} this
  401. */
  402. setOptions(options) {
  403. this.type = TypeConditional;
  404. this.options = options;
  405. this.sideEffects = true;
  406. return this;
  407. }
  408. /**
  409. * Adds the provided basic evaluated expression to the basic evaluated expression.
  410. * @param {BasicEvaluatedExpression[]} options optional (consequent/alternate) expressions to be added
  411. * @returns {this} this
  412. */
  413. addOptions(options) {
  414. if (!this.options) {
  415. this.type = TypeConditional;
  416. this.options = [];
  417. this.sideEffects = true;
  418. }
  419. for (const item of options) {
  420. this.options.push(item);
  421. }
  422. return this;
  423. }
  424. /**
  425. * Set's the value of this expression to an array of expressions.
  426. * @param {BasicEvaluatedExpression[]} items expressions to set
  427. * @returns {this} this
  428. */
  429. setItems(items) {
  430. this.type = TypeArray;
  431. this.items = items;
  432. this.sideEffects = items.some((i) => i.couldHaveSideEffects());
  433. return this;
  434. }
  435. /**
  436. * Set's the value of this expression to an array of strings.
  437. * @param {string[]} array array to set
  438. * @returns {this} this
  439. */
  440. setArray(array) {
  441. this.type = TypeConstArray;
  442. this.array = array;
  443. this.sideEffects = false;
  444. return this;
  445. }
  446. /**
  447. * Set's the value of this expression to a processed/unprocessed template string. Used
  448. * for evaluating TemplateLiteral expressions in the JavaScript Parser.
  449. * @param {BasicEvaluatedExpression[]} quasis template string quasis
  450. * @param {BasicEvaluatedExpression[]} parts template string parts
  451. * @param {"cooked" | "raw"} kind template string kind
  452. * @returns {this} this
  453. */
  454. setTemplateString(quasis, parts, kind) {
  455. this.type = TypeTemplateString;
  456. this.quasis = quasis;
  457. this.parts = parts;
  458. this.templateStringKind = kind;
  459. this.sideEffects = parts.some((p) => p.sideEffects);
  460. return this;
  461. }
  462. setTruthy() {
  463. this.falsy = false;
  464. this.truthy = true;
  465. this.nullish = false;
  466. return this;
  467. }
  468. setFalsy() {
  469. this.falsy = true;
  470. this.truthy = false;
  471. return this;
  472. }
  473. /**
  474. * Set's the value of the expression to nullish.
  475. * @param {boolean} value true, if the expression is nullish
  476. * @returns {this} this
  477. */
  478. setNullish(value) {
  479. this.nullish = value;
  480. if (value) return this.setFalsy();
  481. return this;
  482. }
  483. /**
  484. * Set's the range for the expression.
  485. * @param {Range} range range to set
  486. * @returns {this} this
  487. */
  488. setRange(range) {
  489. this.range = range;
  490. return this;
  491. }
  492. /**
  493. * Set whether or not the expression has side effects.
  494. * @param {boolean} sideEffects true, if the expression has side effects
  495. * @returns {this} this
  496. */
  497. setSideEffects(sideEffects = true) {
  498. this.sideEffects = sideEffects;
  499. return this;
  500. }
  501. /**
  502. * Set the expression node for the expression.
  503. * @param {Node | undefined} expression expression
  504. * @returns {this} this
  505. */
  506. setExpression(expression) {
  507. this.expression = expression;
  508. return this;
  509. }
  510. }
  511. /**
  512. * Returns is valid flags.
  513. * @param {string} flags regexp flags
  514. * @returns {boolean} is valid flags
  515. */
  516. BasicEvaluatedExpression.isValidRegExpFlags = (flags) => {
  517. const len = flags.length;
  518. if (len === 0) return true;
  519. if (len > 4) return false;
  520. // cspell:word gimy
  521. let remaining = 0b0000; // bit per RegExp flag: gimy
  522. for (let i = 0; i < len; i++) {
  523. switch (flags.charCodeAt(i)) {
  524. case 103 /* g */:
  525. if (remaining & 0b1000) return false;
  526. remaining |= 0b1000;
  527. break;
  528. case 105 /* i */:
  529. if (remaining & 0b0100) return false;
  530. remaining |= 0b0100;
  531. break;
  532. case 109 /* m */:
  533. if (remaining & 0b0010) return false;
  534. remaining |= 0b0010;
  535. break;
  536. case 121 /* y */:
  537. if (remaining & 0b0001) return false;
  538. remaining |= 0b0001;
  539. break;
  540. default:
  541. return false;
  542. }
  543. }
  544. return true;
  545. };
  546. module.exports = BasicEvaluatedExpression;