Skip to content

Commit

Permalink
Account for TypeCastExpression in the utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebeach committed Jun 30, 2019
1 parent 470539a commit 935415b
Show file tree
Hide file tree
Showing 12 changed files with 1,595 additions and 17 deletions.
44 changes: 41 additions & 3 deletions __tests__/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);

export const fallbackToBabylon = nodeVersion < 6;

const parser = fallbackToBabylon ? require('babylon') : require('@babel/parser');
let parserName;
const babelParser = fallbackToBabylon ? require('babylon') : require('@babel/parser');
const flowParser = require('flow-parser');

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

export function setParserName(name) {
parserName = name;
}

export function changePlugins(pluginOrFn) {
if (Array.isArray(pluginOrFn)) {
plugins = pluginOrFn;
Expand All @@ -25,11 +31,43 @@ beforeEach(() => {
});

function parse(code) {
return parser.parse(code, { plugins });
if (parserName === undefined) {
throw new Error('No parser specified');
}
if (parserName === 'babel') {
try {
return babelParser.parse(code, { plugins });
} catch (_) {
// eslint-disable-next-line no-console
console.warn(`Failed to parse with ${fallbackToBabylon ? 'babylon' : 'Babel'} parser.`);
}
}
if (parserName === 'flow') {
try {
return flowParser.parse(code, { plugins });
} catch (_) {
// eslint-disable-next-line no-console
console.warn('Failed to parse with the Flow parser');
}
}
throw new Error(`The parser ${parserName} is not yet supported for testing.`);
}

export function getOpeningElement(code) {
return parse(code).program.body[0].expression.openingElement;
const parsedCode = parse(code);
let body;
if (parsedCode.program) {
// eslint-disable-next-line prefer-destructuring
body = parsedCode.program.body;
} else {
// eslint-disable-next-line prefer-destructuring
body = parsedCode.body;
}
if (Array.isArray(body) && body[0] != null) {
return body[0].expression.openingElement;
}

return null;
}

export function extractProp(code, prop = 'foo') {
Expand Down
5 changes: 4 additions & 1 deletion __tests__/src/elementType-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-env mocha */
import assert from 'assert';
import { getOpeningElement } from '../helper';
import { getOpeningElement, setParserName } from '../helper';
import elementType from '../../src/elementType';

describe('elementType tests', () => {
beforeEach(() => {
setParserName('babel');
});
it('should export a function', () => {
const expected = 'function';
const actual = typeof elementType;
Expand Down
5 changes: 4 additions & 1 deletion __tests__/src/getProp-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-env mocha */
import assert from 'assert';
import { getOpeningElement } from '../helper';
import { getOpeningElement, setParserName } from '../helper';
import getProp from '../../src/getProp';

describe('getProp', () => {
beforeEach(() => {
setParserName('babel');
});
it('should export a function', () => {
const expected = 'function';
const actual = typeof getProp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/* eslint-env mocha */
/* eslint no-template-curly-in-string: 0 */
import assert from 'assert';
import { extractProp, describeIfNotBabylon, changePlugins } from '../helper';
import {
extractProp,
describeIfNotBabylon,
changePlugins,
setParserName,
} from '../helper';
import { getLiteralPropValue } from '../../src/getPropValue';

describe('getLiteralPropValue', () => {
beforeEach(() => {
setParserName('babel');
});
it('should export a function', () => {
const expected = 'function';
const actual = typeof getLiteralPropValue;
Expand All @@ -26,12 +34,22 @@ describe('getLiteralPropValue', () => {
type: 'JSXExpressionContainer',
},
};

let counter = 0;
// eslint-disable-next-line no-console
const errorOrig = console.error;
// eslint-disable-next-line no-console
console.error = () => {
counter += 1;
};
let value;
assert.doesNotThrow(() => {
getLiteralPropValue(prop);
value = getLiteralPropValue(prop);
}, Error);

assert.equal(null, getLiteralPropValue(prop));
assert.equal(null, value);
assert.equal(counter, 1);
// eslint-disable-next-line no-console
console.error = errorOrig;
});

describe('Null', () => {
Expand Down Expand Up @@ -121,7 +139,7 @@ describe('getLiteralPropValue', () => {

describe('JSXElement', () => {
it('should return null', () => {
const prop = extractProp('<div foo=<bar /> />');
const prop = extractProp('<div foo={<bar />} />');

const expected = null;
const actual = getLiteralPropValue(prop);
Expand Down
Loading

0 comments on commit 935415b

Please sign in to comment.