Skip to content

Commit 1849f0d

Browse files
Fix #4852, @get @set implicit object (#4867)
* Test cases for #4852, get/set oddities * Fix soak before accessor call to `get` or `set` * Fixes #4852: More get/set cases; cleanup style * Check for tokens' existence before referencing them
1 parent d86597d commit 1849f0d

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

lib/coffeescript/lexer.js

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lexer.coffee

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,17 @@ exports.Lexer = class Lexer
192192
# what CoffeeScript would normally interpret as calls to functions named
193193
# `get` or `set`, i.e. `get({foo: function () {}})`.
194194
else if tag is 'PROPERTY' and prev
195-
if prev.spaced and prev[0] in CALLABLE and /^[gs]et$/.test(prev[1]) and @tokens[@tokens.length - 2][0] isnt '.'
196-
@error "'#{prev[1]}' cannot be used as a keyword, or as a function call without parentheses", prev[2]
197-
else
195+
if prev.spaced and prev[0] in CALLABLE and /^[gs]et$/.test(prev[1]) and
196+
@tokens.length > 1 and @tokens[@tokens.length - 2][0] not in ['.', '?.', '@']
197+
@error "'#{prev[1]}' cannot be used as a keyword, or as a function call
198+
without parentheses", prev[2]
199+
else if @tokens.length > 2
198200
prevprev = @tokens[@tokens.length - 2]
199-
if prev[0] in ['@', 'THIS'] and prevprev and prevprev.spaced and /^[gs]et$/.test(prevprev[1]) and
200-
@tokens[@tokens.length - 3][0] not in ['.', '@']
201-
@error "'#{prevprev[1]}' cannot be used as a keyword, or as a function call without parentheses", prevprev[2]
201+
if prev[0] in ['@', 'THIS'] and prevprev and prevprev.spaced and
202+
/^[gs]et$/.test(prevprev[1]) and
203+
@tokens[@tokens.length - 3][0] not in ['.', '?.', '@']
204+
@error "'#{prevprev[1]}' cannot be used as a keyword, or as a
205+
function call without parentheses", prevprev[2]
202206

203207
if tag is 'IDENTIFIER' and id in RESERVED
204208
@error "reserved word '#{id}'", length: id.length

test/function_invocation.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ test "#4524: functions named get or set can be used without parentheses when att
823823

824824
eq 12, obj.get 10
825825
eq 13, obj.set 10
826+
eq 12, obj?.get 10
827+
eq 13, obj?.set 10
826828

827829
eq 14, a.get 10
828830
eq 15, a.set 10
@@ -853,13 +855,32 @@ test "#4836: functions named get or set can be used without parentheses when att
853855

854856
eq 12, this.get 10
855857
eq 13, this.set 10
858+
eq 12, this?.get 10
859+
eq 13, this?.set 10
856860
eq 6, this.get @a
857861
eq 7, this.set @a
862+
eq 6, this?.get @a
863+
eq 7, this?.set @a
858864

859865
eq 12, @get 10
860866
eq 13, @set 10
867+
eq 12, @?.get 10
868+
eq 13, @?.set 10
861869
eq 6, @get @a
862870
eq 7, @set @a
871+
eq 6, @?.get @a
872+
eq 7, @?.set @a
873+
874+
test "#4852: functions named get or set can be used without parentheses when attached to this or @, with an argument of an implicit object", ->
875+
@get = ({ x }) -> x + 2
876+
@set = ({ x }) -> x + 3
877+
878+
eq 12, @get x: 10
879+
eq 13, @set x: 10
880+
eq 12, @?.get x: 10
881+
eq 13, @?.set x: 10
882+
eq 12, this?.get x: 10
883+
eq 13, this?.set x: 10
863884

864885
test "#4473: variable scope in chained calls", ->
865886
obj =

0 commit comments

Comments
 (0)