Skip to content

Commit

Permalink
Fix getLiteralPropValue for TS-specific node types
Browse files Browse the repository at this point in the history
  • Loading branch information
vhfmag authored and ljharb committed Apr 22, 2019
1 parent 9d81b2d commit c5755ca
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions __tests__/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export function extractProp(code, prop = 'foo') {
const { attributes: props } = node;
return getProp(props, prop);
}

export const describeIfNotBabylon = fallbackToBabylon ? describe.skip : describe;
36 changes: 35 additions & 1 deletion __tests__/src/getPropLiteralValue-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
/* eslint no-template-curly-in-string: 0 */
import assert from 'assert';
import { extractProp } from '../helper';
import { extractProp, describeIfNotBabylon, changePlugins } from '../helper';
import { getLiteralPropValue } from '../../src/getPropValue';

describe('getLiteralPropValue', () => {
Expand Down Expand Up @@ -465,4 +465,38 @@ describe('getLiteralPropValue', () => {
assert.deepEqual(expected, actual);
});
});

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 = null;
const actual = getLiteralPropValue(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 = null;
const actual = getLiteralPropValue(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 = null;
const actual = getLiteralPropValue(prop);

assert.equal(expected, actual);
});
});
});
4 changes: 1 addition & 3 deletions __tests__/src/getPropValue-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-env mocha */
/* eslint no-template-curly-in-string: 0 */
import assert from 'assert';
import { extractProp, changePlugins, fallbackToBabylon } from '../helper';
import { extractProp, changePlugins, fallbackToBabylon, describeIfNotBabylon } 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
2 changes: 2 additions & 0 deletions src/values/expressions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const LITERAL_TYPES = Object.assign({}, TYPES, {
},
BindExpression: noop,
SpreadElement: noop,
TSNonNullExpression: noop,
TSAsExpression: noop,
});

/**
Expand Down

0 comments on commit c5755ca

Please sign in to comment.