-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Be smarter with when to wrap in IIFE (#37)
* refactor `shouldWrapInClosure` to be more based on the owner than the contents * handle `await` expressions * handle JSX usage better * clean up commented code * update CHANGELOG
- Loading branch information
1 parent
0c90e0d
commit 111cfa7
Showing
18 changed files
with
223 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { map } from '../../../../src/inline-loops.macro'; | ||
|
||
function getStuff(array) { | ||
return [array, map(array, (v) => v * 2)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function getStuff(array) { | ||
const _length = array.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _v; _key < _length; ++_key) { | ||
_v = array[_key]; | ||
_results[_key] = _v * 2; | ||
} | ||
return [array, _results]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { log } from './log'; | ||
function logItems(items) { | ||
(() => { | ||
const _fn = (_item) => { | ||
if (!this.position) { | ||
return; | ||
} | ||
log(_item); | ||
}; | ||
for (let _key = 0, _length = items.length, _item; _key < _length; ++_key) { | ||
_item = items[_key]; | ||
_fn(_item, _key, items); | ||
const _fn = (_item) => { | ||
if (!this.position) { | ||
return; | ||
} | ||
})(); | ||
log(_item); | ||
}; | ||
for (let _key = 0, _length = items.length, _item; _key < _length; ++_key) { | ||
_item = items[_key]; | ||
_fn(_item, _key, items); | ||
} | ||
} |
20 changes: 9 additions & 11 deletions
20
__tests__/__fixtures__/complex/for-each-right-hoisted/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { log } from './log'; | ||
function logItems(items) { | ||
(() => { | ||
const _fn = (_item) => { | ||
if (!this.position) { | ||
return; | ||
} | ||
log(_item); | ||
}; | ||
for (let _key = items.length, _item; --_key >= 0; ) { | ||
_item = items[_key]; | ||
_fn(_item, _key, items); | ||
const _fn = (_item) => { | ||
if (!this.position) { | ||
return; | ||
} | ||
})(); | ||
log(_item); | ||
}; | ||
for (let _key = items.length, _item; --_key >= 0; ) { | ||
_item = items[_key]; | ||
_fn(_item, _key, items); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
__tests__/__fixtures__/complex/if-statement-object/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { mapObject } from '../../../../src/inline-loops.macro'; | ||
|
||
function getStuff(object, foo) { | ||
if (foo === 'bar') { | ||
const state = { | ||
...state, | ||
foo, | ||
}; | ||
|
||
return mapObject(object, (v) => v * 2); | ||
} | ||
|
||
return object; | ||
} |
18 changes: 18 additions & 0 deletions
18
__tests__/__fixtures__/complex/if-statement-object/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function getStuff(object, foo) { | ||
if (foo === 'bar') { | ||
const state = { | ||
...state, | ||
foo, | ||
}; | ||
return (() => { | ||
const _results = {}; | ||
let _v; | ||
for (const _key in object) { | ||
_v = object[_key]; | ||
_results[_key] = _v * 2; | ||
} | ||
return _results; | ||
})(); | ||
} | ||
return object; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
function getStuff(array, foo) { | ||
if (foo === 'bar') { | ||
const _length = array.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _v; _key < _length; ++_key) { | ||
_v = array[_key]; | ||
_results[_key] = _v * 2; | ||
} | ||
return _results; | ||
return (() => { | ||
const _length = array.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _v; _key < _length; ++_key) { | ||
_v = array[_key]; | ||
_results[_key] = _v * 2; | ||
} | ||
return _results; | ||
})(); | ||
} | ||
return array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
|
||
import { map } from '../../../../src/inline-loops.macro'; | ||
|
||
function List(props) { | ||
return ( | ||
<ul> | ||
{props.items && | ||
map(props.items, (item) => <li key={item.id}>{item.value}</li>)} | ||
</ul> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
function List(props) { | ||
return /*#__PURE__*/ React.createElement( | ||
'ul', | ||
null, | ||
props.items && | ||
(() => { | ||
const _length = props.items.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _item; _key < _length; ++_key) { | ||
_item = props.items[_key]; | ||
_results[_key] = /*#__PURE__*/ React.createElement( | ||
'li', | ||
{ | ||
key: _item.id, | ||
}, | ||
_item.value, | ||
); | ||
} | ||
return _results; | ||
})(), | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
__tests__/__fixtures__/complex/logical-expression-nested/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { reduce } from '../../../../src/inline-loops.macro'; | ||
|
||
function getStuff(array, override) { | ||
return override || reduce(array, (a, v) => a + v * 2, 0) || 1; | ||
} |
14 changes: 14 additions & 0 deletions
14
__tests__/__fixtures__/complex/logical-expression-nested/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function getStuff(array, override) { | ||
return ( | ||
override || | ||
(() => { | ||
let _a = 0; | ||
for (let _key = 0, _length = array.length, _v; _key < _length; ++_key) { | ||
_v = array[_key]; | ||
_a = _a + _v * 2; | ||
} | ||
return _a; | ||
})() || | ||
1 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { map } from '../../../../src/inline-loops.macro'; | ||
|
||
function getStuff(array) { | ||
return { | ||
array, | ||
doubled: map(array, (v) => v * 2), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function getStuff(array) { | ||
const _length = array.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _v; _key < _length; ++_key) { | ||
_v = array[_key]; | ||
_results[_key] = _v * 2; | ||
} | ||
return { | ||
array, | ||
doubled: _results, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { reduce } from '../../../../src/inline-loops.macro'; | ||
|
||
function getStuff(array, foo) { | ||
return foo ? (reduce(array, (a, v) => a + v * 2, 0) ? 'stuff' : null) : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function getStuff(array, foo) { | ||
return foo | ||
? (() => { | ||
let _a = 0; | ||
for (let _key = 0, _length = array.length, _v; _key < _length; ++_key) { | ||
_v = array[_key]; | ||
_a = _a + _v * 2; | ||
} | ||
return _a; | ||
})() | ||
? 'stuff' | ||
: null | ||
: null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
function foo(array) { | ||
return (() => { | ||
const _fn = function (_value) { | ||
return this && this.foo ? _value : null; | ||
}; | ||
const _length = array.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _value; _key < _length; ++_key) { | ||
_value = array[_key]; | ||
_results[_key] = _fn(_value, _key, array); | ||
} | ||
return _results; | ||
})(); | ||
const _fn = function (_value) { | ||
return this && this.foo ? _value : null; | ||
}; | ||
const _length = array.length; | ||
const _results = Array(_length); | ||
for (let _key = 0, _value; _key < _length; ++_key) { | ||
_value = array[_key]; | ||
_results[_key] = _fn(_value, _key, array); | ||
} | ||
return _results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters