Skip to content

Commit c5755ca

Browse files
vhfmagljharb
authored andcommitted
Fix getLiteralPropValue for TS-specific node types
1 parent 9d81b2d commit c5755ca

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

__tests__/helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ export function extractProp(code, prop = 'foo') {
3737
const { attributes: props } = node;
3838
return getProp(props, prop);
3939
}
40+
41+
export const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;

__tests__/src/getPropLiteralValue-test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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, describeIfNotBabylon, changePlugins } from '../helper';
55
import { getLiteralPropValue } from '../../src/getPropValue';
66

77
describe('getLiteralPropValue', () => {
@@ -465,4 +465,38 @@ describe('getLiteralPropValue', () => {
465465
assert.deepEqual(expected, actual);
466466
});
467467
});
468+
469+
describeIfNotBabylon('Typescript', () => {
470+
beforeEach(() => {
471+
changePlugins(pls => [...pls, 'typescript']);
472+
});
473+
474+
it('should return string representation of variable identifier wrapped in a Typescript non-null assertion', () => {
475+
const prop = extractProp('<div foo={bar!} />');
476+
477+
const expected = null;
478+
const actual = getLiteralPropValue(prop);
479+
480+
assert.equal(expected, actual);
481+
});
482+
483+
it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', () => {
484+
const prop = extractProp('<div foo={(bar!)!} />');
485+
486+
const expected = null;
487+
const actual = getLiteralPropValue(prop);
488+
489+
assert.equal(expected, actual);
490+
});
491+
492+
it('should return string representation of variable identifier wrapped in a Typescript type coercion', () => {
493+
changePlugins(pls => [...pls, 'typescript']);
494+
const prop = extractProp('<div foo={bar as any} />');
495+
496+
const expected = null;
497+
const actual = getLiteralPropValue(prop);
498+
499+
assert.equal(expected, actual);
500+
});
501+
});
468502
});

__tests__/src/getPropValue-test.js

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

7-
const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;
8-
97
describe('getPropValue', () => {
108
it('should export a function', () => {
119
const expected = 'function';

src/values/expressions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ const LITERAL_TYPES = Object.assign({}, TYPES, {
123123
},
124124
BindExpression: noop,
125125
SpreadElement: noop,
126+
TSNonNullExpression: noop,
127+
TSAsExpression: noop,
126128
});
127129

128130
/**

0 commit comments

Comments
 (0)