Skip to content

Commit

Permalink
[New] add nullish coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 12, 2020
1 parent d2496c2 commit 7ed1532
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"plugins": [
["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }],
"transform-object-rest-spread",
// "@babel/plugin-proposal-nullish-coalescing-operator", // TODO: update to babel 7
],
}
9 changes: 5 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
extends: "airbnb-base",
rules: {
no-use-before-define: ["error", { functions: false }]
}
"extends": "airbnb-base",
"parser": "babel-eslint",
"rules": {
"no-use-before-define": ["error", { "functions": false }],
},
}
9 changes: 8 additions & 1 deletion __tests__/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ let parserName;
const babelParser = fallbackToBabylon ? require('babylon') : require('@babel/parser');
const flowParser = require('flow-parser');

const defaultPlugins = ['jsx', 'functionBind', 'estree', 'objectRestSpread', 'optionalChaining'];
const defaultPlugins = [
'jsx',
'functionBind',
'estree',
'objectRestSpread',
'optionalChaining',
// 'nullishCoalescing', // TODO: update to babel 7
];
let plugins = [...defaultPlugins];

export function setParserName(name) {
Expand Down
54 changes: 54 additions & 0 deletions __tests__/src/getPropValue-babelparser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,60 @@ describe('getPropValue', () => {

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

it('should correctly infer result of ?? logical expression based on derived values', () => {
const prop = extractProp('<div foo={bar ?? baz} />');

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

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

it('should correctly infer result of ?? logical expression based on derived values', () => {
const prop = extractProp('<div foo={undefined ?? baz} />');

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

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

it('should return undefined when evaluating `undefined ?? undefined` ', () => {
const prop = extractProp('<div foo={undefined ?? undefined} />');

const expected = undefined;
const actual = getPropValue(prop);

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

it('should return undefined when evaluating `null ?? undefined` ', () => {
const prop = extractProp('<div foo={null ?? undefined} />');

const expected = undefined;
const actual = getPropValue(prop);

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

it('should return undefined when evaluating `undefined ?? null` ', () => {
const prop = extractProp('<div foo={undefined ?? null} />');

const expected = null;
const actual = getPropValue(prop);

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

it('should return null when evaluating `null ?? null` ', () => {
const prop = extractProp('<div foo={null ?? null} />');

const expected = null;
const actual = getPropValue(prop);

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

describe('Member expression', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/values/expressions/LogicalExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ export default function extractValueFromLogicalExpression(value) {
const leftVal = getValue(left);
const rightVal = getValue(right);

return operator === '&&' ? leftVal && rightVal : leftVal || rightVal;
if (operator === '&&') {
return leftVal && rightVal;
}
if (operator === '??') {
// return leftVal ?? rightVal; // TODO: update to babel 7
return (leftVal === null || typeof leftVal === 'undefined') ? rightVal : leftVal;
}
return leftVal || rightVal;
}

0 comments on commit 7ed1532

Please sign in to comment.