Skip to content

Commit 140c648

Browse files
committed
made implicit objects close at post-if/for/while when inline (jashkenas/coffeescript#1871)
1 parent c26255b commit 140c648

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

lib/lexer.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ function addImplicitParentheses(tokens){
11431143
}
11441144
}
11451145
function addImplicitBraces(tokens){
1146-
var stack, i, token, tag, start, paren, index, pre, lead, _ref;
1146+
var stack, i, token, tag, start, paren, index, pre, inline, _ref;
11471147
stack = [];
11481148
i = 0;
11491149
while (token = tokens[++i]) {
@@ -1169,7 +1169,7 @@ function addImplicitBraces(tokens){
11691169
continue;
11701170
}
11711171
stack.push(['{']);
1172-
lead = pre.doblock || ((_ref = pre[0]) === 'NEWLINE' || _ref === 'INDENT');
1172+
inline = !pre.doblock && ((_ref = pre[0]) !== 'NEWLINE' && _ref !== 'INDENT');
11731173
while (((_ref = tokens[index - 2]) != null ? _ref[0] : void 8) === 'COMMENT') {
11741174
index -= 2;
11751175
}
@@ -1178,17 +1178,23 @@ function addImplicitBraces(tokens){
11781178
}
11791179
function ok(token, i){
11801180
var tag, t1, _ref;
1181-
if (token[1] === ';' || 'DEDENT' === (tag = token[0])) {
1181+
if (token[1] === ';') {
11821182
return true;
11831183
}
1184-
switch (tag) {
1184+
switch (tag = token[0]) {
11851185
case ',':
11861186
break;
11871187
case 'NEWLINE':
1188-
if (!lead) {
1188+
if (inline) {
11891189
return true;
11901190
}
11911191
break;
1192+
case 'DEDENT':
1193+
return true;
1194+
case 'POST_IF':
1195+
case 'FOR':
1196+
case 'WHILE':
1197+
return inline;
11921198
default:
11931199
return false;
11941200
}

src/ast.co

+2-2
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class exports.Obj extends List
621621
key = Parens key
622622
else key = node
623623
val = chain = Chain base, [Index node.maybeKey()]
624-
val = logic <<< {first: val} if logic
624+
val = logic <<< first: val if logic
625625
items[i] = Prop key, val
626626
base = ref
627627
chain or @carp 'empty slice'
@@ -1108,7 +1108,7 @@ class exports.Assign extends Node
11081108
then node = ({key} = node)val
11091109
else key = node
11101110
node = Var node.name if node instanceof Key
1111-
node = logic <<< {first: node} if logic
1111+
node = logic <<< first: node if logic
11121112
val = Chain rcache||=Var(rite), [Index key.maybeKey()]
11131113
val = Import Obj(), val if splat
11141114
(^@<<<{left: node, right: val, +\void})compile o, LEVEL_PAREN

src/lexer.co

+10-8
Original file line numberDiff line numberDiff line change
@@ -767,20 +767,22 @@ character = if JSON!? then uxxxx else ->
767767
tag = \{ if tag is \INDENT and tokens[i-1]0 is \{
768768
stack.push [tag, i]
769769
continue
770-
paren = tokens[i-1]0 is \)
771-
index = if paren then start.1 else i-1
772-
pre = tokens[index-1]
770+
paren = tokens[i-1]0 is \)
771+
index = if paren then start.1 else i-1
772+
pre = tokens[index-1]
773773
continue unless pre.0 is \: or stack[*-1]?0 is not \{
774774
stack.push [\{]
775-
lead = pre.doblock or pre.0 of <[ NEWLINE INDENT ]>
775+
inline = not pre.doblock and pre.0 not of <[ NEWLINE INDENT ]>
776776
index -= 2 while tokens[index-2]?0 is \COMMENT
777777
tokens.splice index, 0 [\{ \{ tokens[index]2]
778778
detectEnd tokens, ++i+1, ok, go
779779
function ok token, i
780-
return true if token.1 is \; or \DEDENT is tag = token.0
781-
switch tag
782-
case \, then break
783-
case \NEWLINE then return true unless lead
780+
return true if token.1 is \;
781+
switch tag = token.0
782+
case \, then break
783+
case \NEWLINE then return true if inline
784+
case \DEDENT then return true
785+
case \POST_IF \FOR \WHILE then return inline
784786
default return false
785787
t1 = tokens[i+1]?0
786788
t1 is not (if tag is \, then \NEWLINE else \COMMENT) and

test/literal.co

+4-1
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,12 @@ obj =
283283

284284
eq obj.fun().three, 3
285285

286-
# [coffee#1903](https://github.com/jashkenas/coffee-script/issues/1903)
286+
# [coffee#1871](https://github.com/jashkenas/coffee-script/issues/1871),
287+
# [coffee#1903](https://github.com/jashkenas/coffee-script/issues/1903):
288+
# Should close early when inline.
287289
o = p: 0
288290
q: 1
291+
o = q: 1 if false
289292
ok \q not in o
290293

291294

0 commit comments

Comments
 (0)