literal.js 754 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. function isLiteral(node, value) {
  3. if (node?.type !== 'Literal') {
  4. return false;
  5. }
  6. if (value === null) {
  7. return node.raw === 'null';
  8. }
  9. return node.value === value;
  10. }
  11. const isStringLiteral = node => node?.type === 'Literal' && typeof node.value === 'string';
  12. const isNumberLiteral = node => node.type === 'Literal' && typeof node.value === 'number';
  13. const isRegexLiteral = node => node.type === 'Literal' && Boolean(node.regex);
  14. // eslint-disable-next-line unicorn/no-null
  15. const isNullLiteral = node => isLiteral(node, null);
  16. const isBigIntLiteral = node => node.type === 'Literal' && Boolean(node.bigint);
  17. module.exports = {
  18. isLiteral,
  19. isStringLiteral,
  20. isNumberLiteral,
  21. isBigIntLiteral,
  22. isNullLiteral,
  23. isRegexLiteral,
  24. };