Skip to content

Commit

Permalink
[Breaking] add ChainExpression; CallExpression now includes argum…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
ljharb committed Sep 6, 2020
1 parent 0065cb1 commit 7d1e7e0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions __tests__/src/getPropValue-babelparser-test.js
Expand Up @@ -526,7 +526,7 @@ describe('getPropValue', () => {
it('should return string representation of callee', () => {
const prop = extractProp('<div foo={bar()} />');

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

assert.equal(actual, expected);
Expand All @@ -535,7 +535,7 @@ describe('getPropValue', () => {
it('should return string representation of callee', () => {
const prop = extractProp('<div foo={bar.call()} />');

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

assert.equal(actual, expected);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/src/getPropValue-flowparser-test.js
Expand Up @@ -390,7 +390,7 @@ describe('getPropValue', () => {
it('should return string representation of callee', () => {
const prop = extractProp('<div foo={bar()} />');

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

assert.equal(actual, expected);
Expand All @@ -399,7 +399,7 @@ describe('getPropValue', () => {
it('should return string representation of callee', () => {
const prop = extractProp('<div foo={bar.call()} />');

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

assert.equal(actual, expected);
Expand Down
3 changes: 2 additions & 1 deletion src/values/expressions/CallExpression.js
Expand Up @@ -10,5 +10,6 @@
export default function extractValueFromCallExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('./index.js').default;
return getValue(value.callee);
const args = Array.isArray(value.arguments) ? value.arguments.map((x) => getValue(x)).join(', ') : '';
return `${getValue(value.callee)}${value.optional ? '?.' : ''}(${args})`;
}
13 changes: 13 additions & 0 deletions src/values/expressions/ChainExpression.js
@@ -0,0 +1,13 @@
/**
* Extractor function for a ChainExpression type value node.
* A member expression is accessing a property on an object `obj.property`.
*
* @param - value - AST Value object with type `ChainExpression`
* @returns - The extracted value converted to correct type
* and maintaing `obj?.property` convention.
*/
export default function extractValueFromChainExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('./index.js').default;
return getValue(value.expression);
}
2 changes: 1 addition & 1 deletion src/values/expressions/MemberExpression.js
Expand Up @@ -9,5 +9,5 @@
export default function extractValueFromMemberExpression(value) {
// eslint-disable-next-line global-require
const getValue = require('./index.js').default;
return `${getValue(value.object)}.${getValue(value.property)}`;
return `${getValue(value.object)}${value.optional ? '?.' : '.'}${getValue(value.property)}`;
}
2 changes: 2 additions & 0 deletions src/values/expressions/index.js
Expand Up @@ -6,6 +6,7 @@ import TemplateLiteral from './TemplateLiteral';
import FunctionExpression from './FunctionExpression';
import LogicalExpression from './LogicalExpression';
import MemberExpression from './MemberExpression';
import ChainExpression from './ChainExpression';
import OptionalCallExpression from './OptionalCallExpression';
import OptionalMemberExpression from './OptionalMemberExpression';
import CallExpression from './CallExpression';
Expand All @@ -32,6 +33,7 @@ const TYPES = {
FunctionExpression,
LogicalExpression,
MemberExpression,
ChainExpression,
OptionalCallExpression,
OptionalMemberExpression,
CallExpression,
Expand Down

0 comments on commit 7d1e7e0

Please sign in to comment.