Skip to content

Commit

Permalink
Merge pull request #942 from dk00/master
Browse files Browse the repository at this point in the history
Add test cases and fix implicit it in object shorthands (#899)
  • Loading branch information
gkz authored Dec 26, 2016
2 parents 81644ac + 4808d67 commit 7a30758
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/ast.ls
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ SourceNode::to-string = (...args) ->
unparen : THIS
unwrap : THIS
maybe-key : THIS
maybe-var : THIS
expand-slice : THIS
var-name : String
get-accessors : VOID
Expand Down Expand Up @@ -568,6 +569,8 @@ class exports.Key extends Node

assigns: -> it is @name

maybe-var: -> (Var @name) <<< {@line}

var-name: ->
{name} = this
if @reserved or name in <[ arguments eval ]> then "$#name" else name
Expand Down
4 changes: 2 additions & 2 deletions src/grammar.ls
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ bnf =

# The various forms of property.
KeyValue:
o 'Key'
o 'Key' -> $1.maybe-var!
o 'LITERAL' -> Prop L(1,Key $1, $1 not in <[ arguments eval ]>), L 1 Literal $1
o 'Key DOT KeyBase' -> Prop $3, Chain( $1; [L 2 3 Index $3, $2])
o 'Key DOT KeyBase' -> Prop $3, Chain( $1.maybe-var!; [L 2 3 Index $3, $2])
o 'LITERAL DOT KeyBase' -> Prop $3, Chain(L 1 Literal $1; [L 2 3 Index $3, $2])
o '{ Properties OptComma } LABEL' -> Prop L(5, Key $5), L(1, 4, Obj $2 .named $5)
o '[ ArgList OptComma ] LABEL' -> Prop L(5, Key $5), L(1, 4, Arr $2 .named $5)
Expand Down
15 changes: 15 additions & 0 deletions test/function.ls
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ eq '''
});
''', LiveScript.compile '-> @it', {+bare,-header}

#899 Object shorthand `{it}` is `it`
eq '''
(function(it){
return {
it: it
};
});
''', LiveScript.compile '-> {it}', {+bare,-header}

eq '''(function(it){
return {
it: it != null ? it : 'default'
};
});
''', LiveScript.compile '-> {it=\\default}', {+bare,-header}

# Simple functions require no parens when comma-listed.
funs = [->, -> 1, -> it, -> this, null]
Expand Down
10 changes: 10 additions & 0 deletions test/if.ls
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ if 1
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
Expand Down

0 comments on commit 7a30758

Please sign in to comment.