Repository navigation
Commit fe36a70
committed
Put typeof and functions in parentheses where their precedence needs them
Why:
`Js_dump` printed `typeof e` without parentheses wherever it stood, and
a function without them except as a callee or the operand of `typeof`.
As the object of a member access or a method call, `typeof x.length`
read the length of `x`, an arrow function took the access into its
body, as in `x => x + 1 | 0.length`, a syntax error, and a `function`
expression at the start of a statement read as a declaration. An arrow
function as an operand of `===` gave a syntax error as well. `typeof`
now takes parentheses above level 13, as `!` does, and a function above
level 2, as a conditional does.
JS modified (typeof as an object):
Example:
let x: unknown = %raw(`"abc"`)
let len = String.length(typeof(x) :> string)
let up = (typeof(x) :> string)->String.toUpperCase
Before (js):
let len = typeof x.length;
let up = typeof x.toUpperCase();
After (js):
let len = (typeof x).length;
let up = (typeof x).toUpperCase();
JS modified (function as an object or an operand):
Example:
@get external arity: ('a => 'b) => int = "length"
@send external callThis: (@this ('self, int) => int, Nullable.t<'s>, int) => int = "call"
let n = arity(x => x + 1)
let same = (x => x + 1) === (y => y + 1)
callThis(@this (_, x) => x + 1, Nullable.null, 1)->ignore
Before (js):
let n = x => x + 1 | 0.length;
let same = x => x + 1 | 0 === y => y + 1 | 0;
function (x) {
return x + 1 | 0;
}.call(null, 1);
After (js):
let n = (x => x + 1 | 0).length;
let same = (x => x + 1 | 0) === (y => y + 1 | 0);
(function (x) {
return x + 1 | 0;
}).call(null, 1);
Review: Where does a function take parentheses now? Where a conditional
does: as an operand, the test of `?:`, the object of a call or member
access, and the operand of `typeof`, `!` or `await`. Arguments,
initializers, the right side of `=` and returned values take none.
Review: Why not in the branches of `?:`? `Cond` prints them at level 3
only to put a nested conditional in parentheses. A function needs none
there and is printed at level 1, so `c ? x => x + 1 | 0 : x => x + 2 | 0`
stays as it was.
Review: Why does `pp_function` add them? It prints a function that only
calls a name as that name, which needs none: `arity(x => u(x))` stays
`u.length`. The `Call`, `Tagged_template` and `Typeof` cases no longer
put a function in parentheses themselves.
Review: Which checked-in outputs change? None.
Signed-off-by: mununki <woonki.moon@gmail.com>1 parent 83cf1de commit fe36a70
4 files changed
Lines changed: 132 additions & 30 deletions
File tree
- changes
- compiler/backend
- tests/tests/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
402 | 405 | | |
403 | | - | |
404 | | - | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
405 | 409 | | |
406 | 410 | | |
407 | 411 | | |
| |||
511 | 515 | | |
512 | 516 | | |
513 | 517 | | |
514 | | - | |
515 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
516 | 521 | | |
517 | 522 | | |
518 | 523 | | |
| |||
660 | 665 | | |
661 | 666 | | |
662 | 667 | | |
663 | | - | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
664 | 673 | | |
665 | 674 | | |
666 | 675 | | |
| |||
689 | 698 | | |
690 | 699 | | |
691 | 700 | | |
692 | | - | |
693 | | - | |
694 | | - | |
695 | | - | |
696 | | - | |
697 | | - | |
698 | | - | |
| 701 | + | |
699 | 702 | | |
700 | 703 | | |
701 | 704 | | |
| |||
726 | 729 | | |
727 | 730 | | |
728 | 731 | | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
| 732 | + | |
736 | 733 | | |
737 | 734 | | |
738 | 735 | | |
| |||
849 | 846 | | |
850 | 847 | | |
851 | 848 | | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
860 | 853 | | |
861 | 854 | | |
862 | 855 | | |
| |||
1058 | 1051 | | |
1059 | 1052 | | |
1060 | 1053 | | |
1061 | | - | |
| 1054 | + | |
| 1055 | + | |
1062 | 1056 | | |
1063 | | - | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
1064 | 1065 | | |
1065 | 1066 | | |
1066 | 1067 | | |
1067 | 1068 | | |
1068 | | - | |
| 1069 | + | |
1069 | 1070 | | |
1070 | 1071 | | |
1071 | 1072 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
0 commit comments