Skip to content

Commit

Permalink
polish: skip creating extra reference for safely re-used node (#10720)
Browse files Browse the repository at this point in the history
* polish: skip creating extra reference for safely re-used node

* reimplement using scope.maybeGenerateMemoised
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Nov 16, 2019
1 parent d56911b commit 6c7f829
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 11 deletions.
Expand Up @@ -16,14 +16,15 @@ export default declare((api, { loose = false }) => {
return;
}

const ref = scope.generateUidIdentifierBasedOnNode(node.left);
scope.push({ id: ref });

const assignment = t.assignmentExpression(
"=",
t.cloneNode(ref),
node.left,
);
let ref = scope.maybeGenerateMemoised(node.left);
let assignment;
// skip creating extra reference when `left` is static
if (ref === null) {
ref = node.left;
assignment = t.cloneNode(node.left);
} else {
assignment = t.assignmentExpression("=", ref, node.left);
}

path.replaceWith(
t.conditionalExpression(
Expand Down
@@ -1 +1 @@
function foo(foo, bar = foo ?? "bar") {}
function foo(foo, qux = foo.bar ?? "qux") {}
@@ -1,3 +1,3 @@
function foo(foo, bar = (_foo = foo) !== null && _foo !== void 0 ? _foo : "bar") {
var _foo;
function foo(foo, qux = (_foo$bar = foo.bar) !== null && _foo$bar !== void 0 ? _foo$bar : "qux") {
var _foo$bar;
}
@@ -0,0 +1 @@
function foo(foo, bar = foo ?? "bar") {}
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-nullish-coalescing-operator"]
}
@@ -0,0 +1 @@
function foo(foo, bar = foo !== null && foo !== void 0 ? foo : "bar") {}
@@ -0,0 +1,3 @@
function foo() {
var foo = this ?? {};
}
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-nullish-coalescing-operator"]
}
@@ -0,0 +1,3 @@
function foo() {
var foo = this !== null && this !== void 0 ? this : {};
}

1 comment on commit 6c7f829

@williamgoodhew
Copy link

@williamgoodhew williamgoodhew commented on 6c7f829 Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting the following error when trying to run my react-native ios app.

`
Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(SyntaxError: /Users/williamgoodhew/projects/athalens/Athalens/node_modules/react-native/Libraries/Components/Switch/Switch.js: Support for the experimental syntax 'nullishCoalescingOperator' isn't currently enabled (169:52):

�[0m �[90m 167 | �[39m {�[33m...�[39mprops}�[0m
�[0m �[90m 168 | �[39m {�[33m...�[39mplatformProps}�[0m
�[0m�[31m�[1m>�[22m�[39m�[90m 169 | �[39m accessibilityRole�[33m=�[39m{props�[33m.�[39maccessibilityRole �[33m?�[39m�[33m?�[39m �[32m'button'�[39m}�[0m
�[0m �[90m | �[39m �[31m�[1m^�[22m�[39m�[0m
�[0m �[90m 170 | �[39m onChange�[33m=�[39m{�[36mthis�[39m�[33m.�[39m_handleChange}�[0m
�[0m �[90m 171 | �[39m onResponderTerminationRequest�[33m=�[39m{returnsFalse}�[0m
�[0m �[90m 172 | �[39m onStartShouldSetResponder�[33m=�[39m{returnsTrue}�[0m

Add @babel/plugin-proposal-nullish-coalescing-operator (https://git.io/vb4Se) to the 'plugins' section of your Babel config to enable transformation. (null))

__38-[RCTCxxBridge loadSource:onProgress:]_block_invoke.213
RCTCxxBridge.mm:414
invocation function for block in attemptAsynchronousLoadOfBundleAtURL(NSURL*, void (RCTLoadingProgress*) block_pointer, void (NSError*, RCTSource*) block_pointer)
__80-[RCTMultipartDataTask URLSession:streamTask:didBecomeInputStream:outputStream:]_block_invoke
-[RCTMultipartStreamReader emitChunk:headers:callback:done:]
-[RCTMultipartStreamReader readAllPartsWithCompletionCallback:progressCallback:]
-[RCTMultipartDataTask URLSession:streamTask:didBecomeInputStream:outputStream:]
_CFNetworkHTTPConnectionCacheSetLimit
NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK
-[NSBlockOperation main]
NSOPERATION_IS_INVOKING_MAIN
-[NSOperation start]
NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION
__NSOQSchedule_f
_dispatch_block_async_invoke2
_dispatch_client_callout
_dispatch_continuation_pop
_dispatch_async_redirect_invoke
_dispatch_root_queue_drain
_dispatch_worker_thread2
_pthread_wqthread
start_wqthread
`

my babelrc looks like this.

{ "presets": ["module:metro-react-native-babel-preset"], "plugins": ["@babel/plugin-proposal-nullish-coalescing-operator"] }

I am using:
react-native 0.59.9
react 16.8.3

devdependencies are
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
"babel-jest": "23.4.2",
"babel-preset-react-native": "5.0.2",

Please sign in to comment.