Skip to content

Commit

Permalink
Merge pull request #72 from vhfmag/support-typescript-non-null-expres…
Browse files Browse the repository at this point in the history
…sion

Add support for Typescript's node types
  • Loading branch information
ljharb committed Apr 18, 2019
2 parents b5debad + 7a610e8 commit 10400da
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
34 changes: 24 additions & 10 deletions __tests__/helper.js
@@ -1,17 +1,31 @@
/* eslint-env jest */
import getProp from '../src/getProp';

const parser = require('babylon');
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: [
'estree',
'functionBind',
'jsx',
'objectRestSpread',
'optionalChaining',
],
});
return parser.parse(code, { plugins });
}

export function getOpeningElement(code) {
Expand Down
52 changes: 46 additions & 6 deletions __tests__/src/getPropValue-test.js
@@ -1,9 +1,11 @@
/* eslint-env mocha */
/* eslint no-template-curly-in-string: 0 */
import assert from 'assert';
import { extractProp } from '../helper';
import { extractProp, changePlugins, fallbackToBabylon } from '../helper';
import getPropValue from '../../src/getPropValue';

const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;

describe('getPropValue', () => {
it('should export a function', () => {
const expected = 'function';
Expand Down Expand Up @@ -357,17 +359,21 @@ describe('getPropValue', () => {
});

it('should evaluate to a correct representation of member expression with a nullable member', () => {
// This tell will not throw when Babel is upgraded from 6 to 7. Remove
// the throw expectation wrapper at that time.
// eslint-disable-next-line no-undef
expect(() => {
const runTest = () => {
const prop = extractProp('<div foo={bar?.baz} />');

const expected = 'bar?.baz';
const actual = getPropValue(prop);

assert.equal(expected, actual);
}).toThrow();
};

if (fallbackToBabylon) {
// eslint-disable-next-line no-undef
expect(runTest).toThrow();
} else {
runTest();
}
});
});

Expand Down Expand Up @@ -875,4 +881,38 @@ describe('getPropValue', () => {
assert.deepEqual(otherExpected, otherActual);
});
});

describeIfNotBabylon('Typescript', () => {
beforeEach(() => {
changePlugins(pls => [...pls, 'typescript']);
});

it('should return string representation of variable identifier wrapped in a Typescript non-null assertion', () => {
const prop = extractProp('<div foo={bar!} />');

const expected = 'bar';
const actual = getPropValue(prop);

assert.equal(expected, actual);
});

it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', () => {
const prop = extractProp('<div foo={(bar!)!} />');

const expected = 'bar';
const actual = getPropValue(prop);

assert.equal(expected, actual);
});

it('should return string representation of variable identifier wrapped in a Typescript type coercion', () => {
changePlugins(pls => [...pls, 'typescript']);
const prop = extractProp('<div foo={bar as any} />');

const expected = 'bar';
const actual = getPropValue(prop);

assert.equal(expected, actual);
});
});
});
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"test:watch": "npm test -- --watch"
},
"devDependencies": {
"@babel/parser": "^7.3.2",
"babel-cli": "^6.14.0",
"babel-core": "^6.14.0",
"babel-eslint": "^7.0.0",
Expand All @@ -26,6 +27,7 @@
"eslint-config-airbnb-base": "^11.1.0",
"eslint-plugin-import": "^2.2.0",
"jest": "^20.0.0",
"jest-cli": "^20.0.4",
"rimraf": "^2.5.2"
},
"engines": {
Expand Down
7 changes: 6 additions & 1 deletion src/values/expressions/index.js
Expand Up @@ -70,7 +70,12 @@ export default function extract(value) {
} else {
expression = value;
}
const { type } = expression;
let { type } = expression;

while (type === 'TSNonNullExpression' || type === 'TSAsExpression') {
expression = expression.expression;
type = expression.type;
}

if (TYPES[type] === undefined) {
throw new Error(errorMessage(type));
Expand Down

0 comments on commit 10400da

Please sign in to comment.