Skip to content

Commit

Permalink
feat(conditionals): remove if in favour of match
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `match` covers all use-cases of `if`
  • Loading branch information
kollhof committed Feb 26, 2020
1 parent 41fdd66 commit 1399b12
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 149 deletions.
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {code_frame_err} from './errors';

import {transform_assign} from './transform/assign';
import {transform_func} from './transform/func';
import {transform_cond} from './transform/cond';
import {transform_attempt} from './transform/attempt';
import {transform_match} from './transform/match';
import {transform_map} from './transform/map';
Expand Down Expand Up @@ -96,7 +95,6 @@ const block_like = {
};

const control_flow = {
if: transform_cond,
match: transform_match,
attempt: transform_attempt,
pipe: transform_pipe
Expand Down
16 changes: 12 additions & 4 deletions src/transform/__snapshots__/await.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ const a_gen = async function* unfold(curr = 0) {
while (true) {
let _do_result;
if (spam) {
_do_result = await ni(curr);
} else {
_do_result = curr + 1;
ˆmatch_3: {
const ˆvalue_2 = shrub;
if (ˆvalue_2 === spam) {
_do_result = await ni(curr);
break ˆmatch_3;
}
{
_do_result = curr + 1;
break ˆmatch_3;
}
}
const ˆresult_1 = _do_result;
Expand Down
40 changes: 0 additions & 40 deletions src/transform/__snapshots__/cond.test.js.snap

This file was deleted.

10 changes: 6 additions & 4 deletions src/transform/__snapshots__/func.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ const fun5 = c => (d, e) => {
const fun6 = (c, d) => {
const x = 123;
{
if (c + x) {
const ˆvalue_7 = ni;
if (ˆvalue_7 === c + x) {
return d;
} else if (c + 1) {
}
if (ˆvalue_7 === c + 1) {
return d + 1;
} else {
return null;
}
}
};
Expand Down
12 changes: 9 additions & 3 deletions src/transform/__snapshots__/object.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ const obj4 = {
};
const obj5 = {
\\"a\\": 1,
\\"b\\": 123
\\"b\\": 123,
[\`c-d-e\`]: cde
};
const obj6 = {
\\"a\\": a = 123,
\\"b\\": c = 456
};
const obj7 = {
\\"a\\": 123,
\\"b\\": 123 && 123 && 1345,
\\"c\\": (a, b) => 134
};
const obj7 = {
const obj8 = {
\\"a\\": 123,
\\"b\\": 123 && 123 && 1345,
\\"c\\": (a, b) => 134,
Expand All @@ -41,6 +46,7 @@ Object.assign(module.exports, {
obj4,
obj5,
obj6,
obj7
obj7,
obj8
});"
`;
2 changes: 1 addition & 1 deletion src/transform/await.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('compiles await', ()=> {
bar + 123
a_gen = unfold curr=0:
if:
match shrub:
spam: await ni(curr)
else : curr + 1
Expand Down
46 changes: 0 additions & 46 deletions src/transform/cond.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/transform/cond.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/transform/func.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('compiles func', ()=> {
fun6 = fn c, d:
x = 123
if:
match ni:
c + x: d
c + 1: d + 1
Expand Down
12 changes: 7 additions & 5 deletions src/transform/group.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {code_frame_err} from '../errors';


export const transform_group = (node, ctx)=> (
ctx.transform(node.exprs[0])
// TODO: if (node.exprs.length === 1) {
// return ctx.transform(node.exprs[0]);
export const transform_group = (node, {transform})=> (
transform(node.exprs[0])

// if (node.exprs.length === 1) {
// return transform(node.exprs[0]);
// }
// throw code_frame_err('Expected only one expression.', node);

// throw new Error('Expected only one expression.');
);
11 changes: 4 additions & 7 deletions src/transform/js/do-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ function* last_expressions(path) {
}
});
yield * items;
} else if (path.isIfStatement()) {
yield * last_expressions(path.get('consequent'));
yield * last_expressions(path.get('alternate'));

} else /* istanbul ignore else */ if (path.isTryStatement()) {
yield * last_expressions(path.get('block'));
yield * last_expressions(path.get('handler').get('body'));
Expand Down Expand Up @@ -67,9 +63,10 @@ const simple = (body, sl=false)=> {
return stmnt.node.body;
}

if (sl && stmnt.isIfStatement()) {
return body.node;
}
// TODO:
// if (sl && stmnt.isIfStatement()) {
// return body.node;
// }

return simple(stmnt);
}
Expand Down
36 changes: 24 additions & 12 deletions src/transform/object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
isSpreadElement, objectExpression, objectProperty, stringLiteral
objectExpression, objectProperty, stringLiteral, assignmentPattern
} from '@babel/types';


Expand All @@ -9,22 +9,34 @@ export const transform_object = (node, ctx)=> {
};


const str_key = ({value, loc})=> (
{...stringLiteral(value), loc}
);


export const transform_prop = (node, ctx)=> {
const is_str_key = node.key.type === 'string';
const computed = is_str_key;
const shorthand = node.key === node.value;
if (node.key.type === 'spread') {
return ctx.transform(node.key);
}

const is_str_key = node.key.type === 'string';
const is_default_assignment = node.value.type === 'assign';

const key = (is_str_key || node.key.type === 'spread')
const key = (is_str_key)
? ctx.transform(node.key)
// TODO: use wrap()?
: {...stringLiteral(node.key.value), loc: node.key.loc};

if (isSpreadElement(key)) {
return key;
}
: str_key(node.key);

const value = ctx.transform(node.value);
return objectProperty(key, value, computed, shorthand);

const computed = is_str_key;
const shorthand = (node.key === node.value);

return objectProperty(
key,
is_default_assignment
? assignmentPattern(value.left, value.right)
: value,
computed, shorthand
);
};

7 changes: 4 additions & 3 deletions src/transform/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ test('compiles object', ()=> {
obj2 = {a}
obj3 = {a, delete, true, false}
obj4 = {a, b, ...c}
obj5 = {a: 1, b: 123}
obj6 = {a: 123, b: 123 && 123 && 1345, c: fn a, b: 134}
obj7 = {
obj5 = {a: 1, b: 123, \`c-d-e\`: cde }
obj6 = {a=123, b: c=456}
obj7 = {a: 123, b: 123 && 123 && 1345, c: fn a, b: 134}
obj8 = {
a: 123,
b: 123
&& 123
Expand Down

0 comments on commit 1399b12

Please sign in to comment.