Skip to content

Commit 7d1e7e0

Browse files
committed
[Breaking] add ChainExpression; CallExpression now includes arguments
1 parent 0065cb1 commit 7d1e7e0

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

__tests__/src/getPropValue-babelparser-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ describe('getPropValue', () => {
526526
it('should return string representation of callee', () => {
527527
const prop = extractProp('<div foo={bar()} />');
528528

529-
const expected = 'bar';
529+
const expected = 'bar()';
530530
const actual = getPropValue(prop);
531531

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

538-
const expected = 'bar.call';
538+
const expected = 'bar.call()';
539539
const actual = getPropValue(prop);
540540

541541
assert.equal(actual, expected);

__tests__/src/getPropValue-flowparser-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ describe('getPropValue', () => {
390390
it('should return string representation of callee', () => {
391391
const prop = extractProp('<div foo={bar()} />');
392392

393-
const expected = 'bar';
393+
const expected = 'bar()';
394394
const actual = getPropValue(prop);
395395

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

402-
const expected = 'bar.call';
402+
const expected = 'bar.call()';
403403
const actual = getPropValue(prop);
404404

405405
assert.equal(actual, expected);

src/values/expressions/CallExpression.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
export default function extractValueFromCallExpression(value) {
1111
// eslint-disable-next-line global-require
1212
const getValue = require('./index.js').default;
13-
return getValue(value.callee);
13+
const args = Array.isArray(value.arguments) ? value.arguments.map((x) => getValue(x)).join(', ') : '';
14+
return `${getValue(value.callee)}${value.optional ? '?.' : ''}(${args})`;
1415
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Extractor function for a ChainExpression type value node.
3+
* A member expression is accessing a property on an object `obj.property`.
4+
*
5+
* @param - value - AST Value object with type `ChainExpression`
6+
* @returns - The extracted value converted to correct type
7+
* and maintaing `obj?.property` convention.
8+
*/
9+
export default function extractValueFromChainExpression(value) {
10+
// eslint-disable-next-line global-require
11+
const getValue = require('./index.js').default;
12+
return getValue(value.expression);
13+
}

src/values/expressions/MemberExpression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
export default function extractValueFromMemberExpression(value) {
1010
// eslint-disable-next-line global-require
1111
const getValue = require('./index.js').default;
12-
return `${getValue(value.object)}.${getValue(value.property)}`;
12+
return `${getValue(value.object)}${value.optional ? '?.' : '.'}${getValue(value.property)}`;
1313
}

src/values/expressions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import TemplateLiteral from './TemplateLiteral';
66
import FunctionExpression from './FunctionExpression';
77
import LogicalExpression from './LogicalExpression';
88
import MemberExpression from './MemberExpression';
9+
import ChainExpression from './ChainExpression';
910
import OptionalCallExpression from './OptionalCallExpression';
1011
import OptionalMemberExpression from './OptionalMemberExpression';
1112
import CallExpression from './CallExpression';
@@ -32,6 +33,7 @@ const TYPES = {
3233
FunctionExpression,
3334
LogicalExpression,
3435
MemberExpression,
36+
ChainExpression,
3537
OptionalCallExpression,
3638
OptionalMemberExpression,
3739
CallExpression,

0 commit comments

Comments
 (0)