Sha256: 7b3b045691f7f884f148d4bf8369f50cdfdc12df9528dc158b15dfc9d9d46b12

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

/* eslint-env jest */
import getProp from '../src/getProp';

const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);

export const fallbackToBabylon = nodeVersion < 6;

const parser = fallbackToBabylon ? require('babylon') : require('@babel/parser');

const defaultPlugins = ['jsx', 'functionBind', 'estree', 'objectRestSpread', 'optionalChaining'];
let plugins = [...defaultPlugins];

export function changePlugins(pluginOrFn) {
  if (Array.isArray(pluginOrFn)) {
    plugins = pluginOrFn;
  } else if (typeof pluginOrFn === 'function') {
    plugins = pluginOrFn(plugins);
  } else {
    throw new Error('changePlugins argument should be either an array or a function');
  }
}

beforeEach(() => {
  plugins = [...defaultPlugins];
});

function parse(code) {
  return parser.parse(code, { plugins });
}

export function getOpeningElement(code) {
  return parse(code).program.body[0].expression.openingElement;
}

export function extractProp(code, prop = 'foo') {
  const node = getOpeningElement(code);
  const { attributes: props } = node;
  return getProp(props, prop);
}

export const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_publishing_components-17.10.0 node_modules/jsx-ast-utils/__tests__/helper.js
govuk_publishing_components-17.9.0 node_modules/jsx-ast-utils/__tests__/helper.js
govuk_publishing_components-17.8.0 node_modules/jsx-ast-utils/__tests__/helper.js