Skip to content

Commit b5debad

Browse files
authored
Merge pull request #77 from jessebeach/fix-OptionalObjectExpression
[#70] Support OptionalMemberExpression AST nodes
2 parents a9d4159 + 3bff7c8 commit b5debad

21 files changed

+122
-82
lines changed

__tests__/helper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const parser = require('babylon');
44

55
function parse(code) {
66
return parser.parse(code, {
7-
plugins: ['jsx', 'functionBind', 'estree', 'objectRestSpread'],
7+
plugins: [
8+
'estree',
9+
'functionBind',
10+
'jsx',
11+
'objectRestSpread',
12+
'optionalChaining',
13+
],
814
});
915
}
1016

__tests__/src/getPropLiteralValue-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ describe('getLiteralPropValue', () => {
258258

259259
// -"bar" => NaN
260260
const expected = true;
261-
const actual = isNaN(getLiteralPropValue(prop));
261+
const actual = Number.isNaN(getLiteralPropValue(prop));
262262

263263
assert.equal(expected, actual);
264264
});
@@ -277,7 +277,7 @@ describe('getLiteralPropValue', () => {
277277

278278
// +"bar" => NaN
279279
const expected = true;
280-
const actual = isNaN(getLiteralPropValue(prop));
280+
const actual = Number.isNaN(getLiteralPropValue(prop));
281281

282282
assert.equal(expected, actual);
283283
});
@@ -344,7 +344,7 @@ describe('getLiteralPropValue', () => {
344344

345345
// ++"bar" => NaN
346346
const expected = true;
347-
const actual = isNaN(getLiteralPropValue(prop));
347+
const actual = Number.isNaN(getLiteralPropValue(prop));
348348

349349
assert.equal(expected, actual);
350350
});
@@ -354,7 +354,7 @@ describe('getLiteralPropValue', () => {
354354

355355
// --"bar" => NaN
356356
const expected = true;
357-
const actual = isNaN(getLiteralPropValue(prop));
357+
const actual = Number.isNaN(getLiteralPropValue(prop));
358358

359359
assert.equal(expected, actual);
360360
});
@@ -364,7 +364,7 @@ describe('getLiteralPropValue', () => {
364364

365365
// "bar"++ => NaN
366366
const expected = true;
367-
const actual = isNaN(getLiteralPropValue(prop));
367+
const actual = Number.isNaN(getLiteralPropValue(prop));
368368

369369
assert.equal(expected, actual);
370370
});
@@ -374,7 +374,7 @@ describe('getLiteralPropValue', () => {
374374

375375
// "bar"-- => NaN
376376
const expected = true;
377-
const actual = isNaN(getLiteralPropValue(prop));
377+
const actual = Number.isNaN(getLiteralPropValue(prop));
378378

379379
assert.equal(expected, actual);
380380
});

__tests__/src/getPropValue-test.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,20 @@ describe('getPropValue', () => {
355355

356356
assert.equal(expected, actual);
357357
});
358+
359+
it('should evaluate to a correct representation of member expression with a nullable member', () => {
360+
// This tell will not throw when Babel is upgraded from 6 to 7. Remove
361+
// the throw expectation wrapper at that time.
362+
// eslint-disable-next-line no-undef
363+
expect(() => {
364+
const prop = extractProp('<div foo={bar?.baz} />');
365+
366+
const expected = 'bar?.baz';
367+
const actual = getPropValue(prop);
368+
369+
assert.equal(expected, actual);
370+
}).toThrow();
371+
});
358372
});
359373

360374
describe('Call expression', () => {
@@ -383,7 +397,7 @@ describe('getPropValue', () => {
383397

384398
// -"bar" => NaN
385399
const expected = true;
386-
const actual = isNaN(getPropValue(prop));
400+
const actual = Number.isNaN(getPropValue(prop));
387401

388402
assert.equal(expected, actual);
389403
});
@@ -402,7 +416,7 @@ describe('getPropValue', () => {
402416

403417
// +"bar" => NaN
404418
const expected = true;
405-
const actual = isNaN(getPropValue(prop));
419+
const actual = Number.isNaN(getPropValue(prop));
406420

407421
assert.equal(expected, actual);
408422
});
@@ -469,7 +483,7 @@ describe('getPropValue', () => {
469483

470484
// ++"bar" => NaN
471485
const expected = true;
472-
const actual = isNaN(getPropValue(prop));
486+
const actual = Number.isNaN(getPropValue(prop));
473487

474488
assert.equal(expected, actual);
475489
});
@@ -478,7 +492,7 @@ describe('getPropValue', () => {
478492
const prop = extractProp('<div foo={--bar} />');
479493

480494
const expected = true;
481-
const actual = isNaN(getPropValue(prop));
495+
const actual = Number.isNaN(getPropValue(prop));
482496

483497
assert.equal(expected, actual);
484498
});
@@ -488,7 +502,7 @@ describe('getPropValue', () => {
488502

489503
// "bar"++ => NaN
490504
const expected = true;
491-
const actual = isNaN(getPropValue(prop));
505+
const actual = Number.isNaN(getPropValue(prop));
492506

493507
assert.equal(expected, actual);
494508
});
@@ -497,7 +511,7 @@ describe('getPropValue', () => {
497511
const prop = extractProp('<div foo={bar--} />');
498512

499513
const expected = true;
500-
const actual = isNaN(getPropValue(prop));
514+
const actual = Number.isNaN(getPropValue(prop));
501515

502516
assert.equal(expected, actual);
503517
});

elementType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('./lib').elementType; // eslint-disable-line import/no-unresolved
1+
module.exports = require('./lib').elementType; // eslint-disable-line import/no-unresolved

src/elementType.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export default function elementType(node = {}) {
1919
if (name.type === 'JSXMemberExpression') {
2020
const { object = {}, property = {} } = name;
2121
return resolveMemberExpressions(object, property);
22-
} else if (name.type === 'JSXNamespacedName') {
22+
}
23+
24+
if (name.type === 'JSXNamespacedName') {
2325
return `${name.namespace.name}:${name.name.name}`;
2426
}
2527

src/getProp.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default function getProp(props = [], prop = '', options = DEFAULT_OPTIONS
1818
return false;
1919
}
2020

21-
const currentProp = options.ignoreCase ?
22-
propName(attribute).toUpperCase() :
23-
propName(attribute);
21+
const currentProp = options.ignoreCase
22+
? propName(attribute).toUpperCase()
23+
: propName(attribute);
2424

2525
return propToFind === currentProp;
2626
});

src/hasProp.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default function hasProp(props = [], prop = '', options = DEFAULT_OPTIONS
1818
return !options.spreadStrict;
1919
}
2020

21-
const currentProp = options.ignoreCase ?
22-
propName(attribute).toUpperCase() :
23-
propName(attribute);
21+
const currentProp = options.ignoreCase
22+
? propName(attribute).toUpperCase()
23+
: propName(attribute);
2424

2525
return propToCheck === currentProp;
2626
});

src/values/Literal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export default function extractValueFromLiteral(value) {
1010
const normalizedStringValue = typeof extractedValue === 'string' && extractedValue.toLowerCase();
1111
if (normalizedStringValue === 'true') {
1212
return true;
13-
} else if (normalizedStringValue === 'false') {
13+
}
14+
15+
if (normalizedStringValue === 'false') {
1416
return false;
1517
}
1618

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import getValue from './index';
2-
31
/**
42
* Extractor function for an ArrayExpression type value node.
53
* An array expression is an expression with [] syntax.
64
*
75
* @returns - An array of the extracted elements.
86
*/
97
export default function extractValueFromArrayExpression(value) {
8+
// eslint-disable-next-line global-require
9+
const getValue = require('./index.js').default;
1010
return value.elements.map(element => getValue(element));
1111
}

src/values/expressions/BinaryExpression.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import getValue from './index';
2-
3-
41
/**
52
* Extractor function for a BinaryExpression type value node.
63
* A binary expression has a left and right side separated by an operator
@@ -10,6 +7,8 @@ import getValue from './index';
107
* @returns - The extracted value converted to correct type.
118
*/
129
export default function extractValueFromBinaryExpression(value) {
10+
// eslint-disable-next-line global-require
11+
const getValue = require('./index.js').default;
1312
const { operator, left, right } = value;
1413
const leftVal = getValue(left);
1514
const rightVal = getValue(right);

0 commit comments

Comments
 (0)