source: imaps-frontend/node_modules/jsx-ast-utils/__tests__/helper.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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