Skip to content

Commit 10400da

Browse files
authored
Merge pull request #72 from vhfmag/support-typescript-non-null-expression
Add support for Typescript's node types
2 parents b5debad + 7a610e8 commit 10400da

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed

__tests__/helper.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
/* eslint-env jest */
12
import getProp from '../src/getProp';
23

3-
const parser = require('babylon');
4+
const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
5+
6+
export const fallbackToBabylon = nodeVersion < 6;
7+
8+
const parser = fallbackToBabylon ? require('babylon') : require('@babel/parser');
9+
10+
const defaultPlugins = ['jsx', 'functionBind', 'estree', 'objectRestSpread', 'optionalChaining'];
11+
let plugins = [...defaultPlugins];
12+
13+
export function changePlugins(pluginOrFn) {
14+
if (Array.isArray(pluginOrFn)) {
15+
plugins = pluginOrFn;
16+
} else if (typeof pluginOrFn === 'function') {
17+
plugins = pluginOrFn(plugins);
18+
} else {
19+
throw new Error('changePlugins argument should be either an array or a function');
20+
}
21+
}
22+
23+
beforeEach(() => {
24+
plugins = [...defaultPlugins];
25+
});
426

527
function parse(code) {
6-
return parser.parse(code, {
7-
plugins: [
8-
'estree',
9-
'functionBind',
10-
'jsx',
11-
'objectRestSpread',
12-
'optionalChaining',
13-
],
14-
});
28+
return parser.parse(code, { plugins });
1529
}
1630

1731
export function getOpeningElement(code) {

__tests__/src/getPropValue-test.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* eslint-env mocha */
22
/* eslint no-template-curly-in-string: 0 */
33
import assert from 'assert';
4-
import { extractProp } from '../helper';
4+
import { extractProp, changePlugins, fallbackToBabylon } from '../helper';
55
import getPropValue from '../../src/getPropValue';
66

7+
const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;
8+
79
describe('getPropValue', () => {
810
it('should export a function', () => {
911
const expected = 'function';
@@ -357,17 +359,21 @@ describe('getPropValue', () => {
357359
});
358360

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

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

369368
assert.equal(expected, actual);
370-
}).toThrow();
369+
};
370+
371+
if (fallbackToBabylon) {
372+
// eslint-disable-next-line no-undef
373+
expect(runTest).toThrow();
374+
} else {
375+
runTest();
376+
}
371377
});
372378
});
373379

@@ -875,4 +881,38 @@ describe('getPropValue', () => {
875881
assert.deepEqual(otherExpected, otherActual);
876882
});
877883
});
884+
885+
describeIfNotBabylon('Typescript', () => {
886+
beforeEach(() => {
887+
changePlugins(pls => [...pls, 'typescript']);
888+
});
889+
890+
it('should return string representation of variable identifier wrapped in a Typescript non-null assertion', () => {
891+
const prop = extractProp('<div foo={bar!} />');
892+
893+
const expected = 'bar';
894+
const actual = getPropValue(prop);
895+
896+
assert.equal(expected, actual);
897+
});
898+
899+
it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', () => {
900+
const prop = extractProp('<div foo={(bar!)!} />');
901+
902+
const expected = 'bar';
903+
const actual = getPropValue(prop);
904+
905+
assert.equal(expected, actual);
906+
});
907+
908+
it('should return string representation of variable identifier wrapped in a Typescript type coercion', () => {
909+
changePlugins(pls => [...pls, 'typescript']);
910+
const prop = extractProp('<div foo={bar as any} />');
911+
912+
const expected = 'bar';
913+
const actual = getPropValue(prop);
914+
915+
assert.equal(expected, actual);
916+
});
917+
});
878918
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"test:watch": "npm test -- --watch"
1515
},
1616
"devDependencies": {
17+
"@babel/parser": "^7.3.2",
1718
"babel-cli": "^6.14.0",
1819
"babel-core": "^6.14.0",
1920
"babel-eslint": "^7.0.0",
@@ -26,6 +27,7 @@
2627
"eslint-config-airbnb-base": "^11.1.0",
2728
"eslint-plugin-import": "^2.2.0",
2829
"jest": "^20.0.0",
30+
"jest-cli": "^20.0.4",
2931
"rimraf": "^2.5.2"
3032
},
3133
"engines": {

src/values/expressions/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ export default function extract(value) {
7070
} else {
7171
expression = value;
7272
}
73-
const { type } = expression;
73+
let { type } = expression;
74+
75+
while (type === 'TSNonNullExpression' || type === 'TSAsExpression') {
76+
expression = expression.expression;
77+
type = expression.type;
78+
}
7479

7580
if (TYPES[type] === undefined) {
7681
throw new Error(errorMessage(type));

0 commit comments

Comments
 (0)