File: if.ls

package info (click to toggle)
node-livescript 1.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 672 kB
  • sloc: makefile: 45
file content (174 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
t = true

# As implicit argument, nested, and/or one-liner.
eq 1, if yes
  if on
    if no then false else
      if t
        1

ok if 0
  0
else if 0/0
  0/0
else if void
  void
else if null
  null
else true

ok if false then false else if false then false else true
eq 100, Number if false then 300 else 100


# `unless`
eq 1, unless true
  0
else
  1


# Returning if-else.
eq -1, do -> if 1 < 0.5 then 1 else -1


# `else`-less `if` returns `undefined` with falsy condition.
eq void, if 0 then
eq void, do -> if 0 then


# As part of a larger operation.
eq 2, 1 + if false then 10 else 1


# Indented within an assignment.
eq 5, do ->
  a =
    if false
      3
    else
      5
  101
  a


# Unmatched `then` should close implicit calls.
i = 1
if Boolean 1 then ++i
eq i, 2


# Unmatched `else` should close implicit blocks.
eq 2, do -> if 0 then -> 1 else 2


# Outer `then` should skip `else`.
eq 3, if 1 then if 0 then 2 else 3


# With suppressed indentations.
eq 6,
  if 0 then 1 \
       else 2 +
         if 3 then 4 \
         else      5

# With leading `then`.
if 0
then ok false
else
  eq 2, if 1
  then 2
  else 3


# Post-condition should accept trailing non-`if` block.
ok true if ->
ok true if do
   true
ok true if let
   true
ok true if do function f
   true


# [coffee#738](https://github.com/jashkenas/coffee-script/issues/738)
ok if true then -> 1


# [coffee#1026](https://github.com/jashkenas/coffee-script/issues/1026)
throws "Parse error on line 2: Unexpected 'ELSE'", -> LiveScript.ast '''
  if a then b
  else then c
  else then d
'''


eq 2, [if 1 then 2       , 3].0
eq 2, [if 0 then 1 else 2, 3].0


# Compile conditonal expression chains neatly.
eq '''
var r;
r = a
  ? b
  : c
    ? d
    : e();
''' LiveScript.compile '''
r =    if a then b
  else if c then d else e!
''' {+bare,-header}


### Anaphoric `if`
eq '''
var that;
if (1) {
  if (that = 2) {
    if (3) {
      4;
    }
    if (that) {
      5;
    }
  }
}
if ((that = 6) != null) {
  that;
}
''', LiveScript.compile '''
if 1
  if 2
    4 if 3
    5 if that
that if 6?
''', {+bare,-header}

# Object shorthand `that`
eq '''
var that;
if (that = result) {
  ({
    that: that
  });
}
''', LiveScript.compile '{that} if result', {+bare,-header}

# Soaks should not `that`-aware.
a = [0 1]
if 1
  eq 1 a?[that]

# then =>
if false     => ok 0
else if true => ok 1
else if true =>
  ok 0
else
  ok 0

# https://github.com/gkz/LiveScript/issues/1098
f = -> ok false
while (if false then f! or f!) then