Skip to content

Commit

Permalink
fix(placeholders): compile assigned expressions correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
haltcase committed Nov 27, 2017
1 parent fd97388 commit c2eb7e2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
21 changes: 13 additions & 8 deletions dist/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var _util = require("./util");
function transformPlaceholders(t, refs) {
const hoistTargets = [];
refs.forEach(referencePath => {
const wrapper = (0, _util.findWrapper)(referencePath);
let wrapper = (0, _util.findWrapper)(referencePath);

if (wrapper) {
const id = wrapper.scope.generateUidIdentifier('arg');
Expand All @@ -26,16 +26,16 @@ function transformPlaceholders(t, refs) {
let isAssign = false;

if (!caller) {
const decl = referencePath.findParent(_it => {
return _it.isVariableDeclarator();
});

if (!decl) {
throw new _util.PartialError('Placeholders must be used as function arguments or the\n' + 'right side of a variable declaration, ie. `const eq = _ === _`)');
}
var _referencePath$findPa;

const decl = (_referencePath$findPa = referencePath.findParent(_it => {
return _it.isVariableDeclarator();
})) !== null && _referencePath$findPa !== void 0 ? _referencePath$findPa : function (e) {
throw e;
}(new _util.PartialError('Placeholders must be used as function arguments or the\n' + 'right side of a variable declaration, ie. `const eq = _ === _`)'));
isAssign = true;
caller = decl.get('init');
wrapper = (0, _util.findWrapper)(referencePath, true);
}

const id = caller.scope.generateUidIdentifier('arg');
Expand All @@ -45,6 +45,11 @@ function transformPlaceholders(t, refs) {

replacement.setData('_.wasPlaceholder', true);

if (wrapper) {
wrapper.node.params.push(id);
return;
}

if (!isAssign) {
const replacementCallee = (0, _util.findTargetCallee)(replacement);
replacementCallee.setData('_.wasPlaceholder', true);
Expand Down
6 changes: 3 additions & 3 deletions dist/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ function findTargetCaller(path) {
return (_findTargetCallee = findTargetCallee(path)) === null || _findTargetCallee === void 0 ? void 0 : _findTargetCallee.parentPath;
}

function findWrapper(path) {
const callee = findTargetCallee(path);
function findWrapper(path, noCallee) {
const root = noCallee ? path : findTargetCallee(path);
let calls = 0;
let link = callee;
let link = root;

while (link = (_link = link) === null || _link === void 0 ? void 0 : _link.parentPath) {
var _link;
Expand Down
14 changes: 9 additions & 5 deletions src/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function transformPlaceholders (t, refs) {
const hoistTargets = []

refs.forEach(referencePath => {
const wrapper = findWrapper(referencePath)
let wrapper = findWrapper(referencePath)

if (wrapper) {
const id = wrapper.scope.generateUidIdentifier('arg')
Expand All @@ -26,30 +26,34 @@ export default function transformPlaceholders (t, refs) {
let caller = findTargetCaller(referencePath)
let isAssign = false
if (!caller) {
const decl = referencePath.findParent(it.isVariableDeclarator())
if (!decl) {
const decl =
referencePath.findParent(it.isVariableDeclarator()) ??
throw new PartialError(
'Placeholders must be used as function arguments or the\n' +
'right side of a variable declaration, ie. `const eq = _ === _`)'
)
}

isAssign = true
caller = decl.get('init')
wrapper = findWrapper(referencePath, true)
}

const id = caller.scope.generateUidIdentifier('arg')
const [replacement] = referencePath.replaceWith(id)
replacement.setData('_.wasPlaceholder', true)

if (wrapper) {
wrapper.node.params.push(id)
return
}

if (!isAssign) {
const replacementCallee = findTargetCallee(replacement)
replacementCallee.setData('_.wasPlaceholder', true)

hoistTargets.push(caller)
}


const fn = t.arrowFunctionExpression(
[id],
t.blockStatement([
Expand Down
6 changes: 3 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export function findTargetCaller (path) {
return findTargetCallee(path)?.parentPath
}

export function findWrapper (path) {
const callee = findTargetCallee(path)
export function findWrapper (path, noCallee) {
const root = noCallee ? path : findTargetCallee(path)

let calls = 0
let link = callee
let link = root
while ((link = link?.parentPath)) {
if (link.isCallExpression()) calls++
if (calls > 1) break
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ test(
`
)

test(
'_: assigned expressions compile to a single function',
testMacro,
$`
import { _ } from 'partial-application.macro'
const areSameThing = _ === _
const oneMansIsAnothers = _.trash === _.treasure
`,
$`
const areSameThing = (_arg, _arg2) => {
return _arg === _arg2;
};
const oneMansIsAnothers = (_arg3, _arg4) => {
return _arg3.trash === _arg4.treasure;
};
`
)

test(
'_: hoists complex sibling arguments to prevent multiple executions',
testMacro,
Expand Down

0 comments on commit c2eb7e2

Please sign in to comment.