helper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* eslint-env jest */
  2. import getProp from '../src/getProp';
  3. const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
  4. export const fallbackToBabylon = nodeVersion < 6;
  5. let parserName;
  6. const babelParser = fallbackToBabylon ? require('babylon') : require('@babel/parser');
  7. const flowParser = require('flow-parser');
  8. const defaultPlugins = [
  9. 'jsx',
  10. 'functionBind',
  11. 'estree',
  12. 'objectRestSpread',
  13. 'optionalChaining',
  14. // 'nullishCoalescing', // TODO: update to babel 7
  15. ];
  16. let plugins = [...defaultPlugins];
  17. let isESM = false;
  18. export function setParserName(name) {
  19. parserName = name;
  20. }
  21. export function changePlugins(pluginOrFn) {
  22. if (Array.isArray(pluginOrFn)) {
  23. plugins = pluginOrFn;
  24. } else if (typeof pluginOrFn === 'function') {
  25. plugins = pluginOrFn(plugins);
  26. } else {
  27. throw new Error('changePlugins argument should be either an array or a function');
  28. }
  29. }
  30. export function setIsESM() {
  31. isESM = true;
  32. }
  33. beforeEach(() => {
  34. plugins = [...defaultPlugins];
  35. isESM = false;
  36. });
  37. function parse(code) {
  38. if (parserName === undefined) {
  39. throw new Error('No parser specified');
  40. }
  41. if (parserName === 'babel') {
  42. try {
  43. return babelParser.parse(code, { plugins, sourceFilename: 'test.js', ...(isESM && { sourceType: 'module' }) });
  44. } catch (_) {
  45. // eslint-disable-next-line no-console
  46. console.warn(`Failed to parse with ${fallbackToBabylon ? 'babylon' : 'Babel'} parser.`);
  47. }
  48. }
  49. if (parserName === 'flow') {
  50. try {
  51. return flowParser.parse(code, { plugins });
  52. } catch (_) {
  53. // eslint-disable-next-line no-console
  54. console.warn('Failed to parse with the Flow parser');
  55. }
  56. }
  57. throw new Error(`The parser ${parserName} is not yet supported for testing.`);
  58. }
  59. export function getOpeningElement(code) {
  60. const parsedCode = parse(code);
  61. let body;
  62. if (parsedCode.program) {
  63. // eslint-disable-next-line prefer-destructuring
  64. body = parsedCode.program.body;
  65. } else {
  66. // eslint-disable-next-line prefer-destructuring
  67. body = parsedCode.body;
  68. }
  69. if (Array.isArray(body) && body[0] != null) {
  70. const [{ expression }] = body;
  71. return expression.type === 'JSXFragment' ? expression.openingFragment : expression.openingElement;
  72. }
  73. return null;
  74. }
  75. export function extractProp(code, prop = 'foo') {
  76. const node = getOpeningElement(code);
  77. const { attributes: props } = node;
  78. return getProp(props, prop);
  79. }
  80. export const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;