Skip to content

Commit

Permalink
made implicit objects close at post-if/for/while when inline (jashken…
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Dec 15, 2011
1 parent c26255b commit 140c648
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
16 changes: 11 additions & 5 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ function addImplicitParentheses(tokens){
}
}
function addImplicitBraces(tokens){
var stack, i, token, tag, start, paren, index, pre, lead, _ref;
var stack, i, token, tag, start, paren, index, pre, inline, _ref;
stack = [];
i = 0;
while (token = tokens[++i]) {
Expand All @@ -1169,7 +1169,7 @@ function addImplicitBraces(tokens){
continue;
}
stack.push(['{']);
lead = pre.doblock || ((_ref = pre[0]) === 'NEWLINE' || _ref === 'INDENT');
inline = !pre.doblock && ((_ref = pre[0]) !== 'NEWLINE' && _ref !== 'INDENT');
while (((_ref = tokens[index - 2]) != null ? _ref[0] : void 8) === 'COMMENT') {
index -= 2;
}
Expand All @@ -1178,17 +1178,23 @@ function addImplicitBraces(tokens){
}
function ok(token, i){
var tag, t1, _ref;
if (token[1] === ';' || 'DEDENT' === (tag = token[0])) {
if (token[1] === ';') {
return true;
}
switch (tag) {
switch (tag = token[0]) {
case ',':
break;
case 'NEWLINE':
if (!lead) {
if (inline) {
return true;
}
break;
case 'DEDENT':
return true;
case 'POST_IF':
case 'FOR':
case 'WHILE':
return inline;
default:
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ class exports.Obj extends List
key = Parens key
else key = node
val = chain = Chain base, [Index node.maybeKey()]
val = logic <<< {first: val} if logic
val = logic <<< first: val if logic
items[i] = Prop key, val
base = ref
chain or @carp 'empty slice'
Expand Down Expand Up @@ -1108,7 +1108,7 @@ class exports.Assign extends Node
then node = ({key} = node)val
else key = node
node = Var node.name if node instanceof Key
node = logic <<< {first: node} if logic
node = logic <<< first: node if logic
val = Chain rcache||=Var(rite), [Index key.maybeKey()]
val = Import Obj(), val if splat
(^@<<<{left: node, right: val, +\void})compile o, LEVEL_PAREN
Expand Down
18 changes: 10 additions & 8 deletions src/lexer.co
Original file line number Diff line number Diff line change
Expand Up @@ -767,20 +767,22 @@ character = if JSON!? then uxxxx else ->
tag = \{ if tag is \INDENT and tokens[i-1]0 is \{
stack.push [tag, i]
continue
paren = tokens[i-1]0 is \)
index = if paren then start.1 else i-1
pre = tokens[index-1]
paren = tokens[i-1]0 is \)
index = if paren then start.1 else i-1
pre = tokens[index-1]
continue unless pre.0 is \: or stack[*-1]?0 is not \{
stack.push [\{]
lead = pre.doblock or pre.0 of <[ NEWLINE INDENT ]>
inline = not pre.doblock and pre.0 not of <[ NEWLINE INDENT ]>
index -= 2 while tokens[index-2]?0 is \COMMENT
tokens.splice index, 0 [\{ \{ tokens[index]2]
detectEnd tokens, ++i+1, ok, go
function ok token, i
return true if token.1 is \; or \DEDENT is tag = token.0
switch tag
case \, then break
case \NEWLINE then return true unless lead
return true if token.1 is \;
switch tag = token.0
case \, then break
case \NEWLINE then return true if inline
case \DEDENT then return true
case \POST_IF \FOR \WHILE then return inline
default return false
t1 = tokens[i+1]?0
t1 is not (if tag is \, then \NEWLINE else \COMMENT) and
Expand Down
5 changes: 4 additions & 1 deletion test/literal.co
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,12 @@ obj =

eq obj.fun().three, 3

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


Expand Down

0 comments on commit 140c648

Please sign in to comment.