Skip to content

Commit 7bd0a47

Browse files
committed
Add support for Typescript nested expressions and type coercion, test it
1 parent 42a7f76 commit 7bd0a47

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

__tests__/src/getPropValue-test.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,6 @@ describe('getPropValue', () => {
111111
assert.equal(expected, actual);
112112
});
113113

114-
it('should work with a Typescript non-null assertion', () => {
115-
changePlugins(pls => [...pls, 'typescript']);
116-
const prop = extractProp('<div foo={bar!} />');
117-
118-
const expected = 'bar';
119-
const actual = getPropValue(prop);
120-
121-
assert.equal(expected, actual);
122-
});
123-
124114
it('should return undefined when identifier is literally `undefined`', () => {
125115
const prop = extractProp('<div foo={undefined} />');
126116

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

src/values/expressions/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default function extract(value) {
7272
}
7373
let { type } = expression;
7474

75-
if (type === 'TSNonNullExpression') {
75+
while (type === 'TSNonNullExpression' || type === 'TSAsExpression') {
7676
expression = expression.expression;
7777
type = expression.type;
7878
}

0 commit comments

Comments
 (0)