From 2d1addf5a4d2ebdb4039aee8d9f751d2c50b50da Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Thu, 6 Apr 2017 00:47:06 -0700 Subject: [PATCH] Allow `get` or `set` parentheses-less function calls when first argument is a string without a colon (so a plain string, not a property accessor) --- lib/coffeescript/lexer.js | 2 +- src/lexer.coffee | 2 +- test/function_invocation.coffee | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/coffeescript/lexer.js b/lib/coffeescript/lexer.js index 3bc2d48c8f..0447d38618 100644 --- a/lib/coffeescript/lexer.js +++ b/lib/coffeescript/lexer.js @@ -241,7 +241,7 @@ if (prev && this.value() === 'from' && (this.seenImport || this.seenExport)) { prev[0] = 'FROM'; } - if (prev && prev.spaced && (ref2 = prev[0], indexOf.call(CALLABLE, ref2) >= 0) && /^[gs]et$/.test(prev[1])) { + if (prev && prev.spaced && (ref2 = prev[0], indexOf.call(CALLABLE, ref2) >= 0) && /^[gs]et$/.test(prev[1]) && /^\S*:\s/.test(this.chunk)) { this.error(`'${prev[1]}' cannot be used as a keyword, or as a function call without parentheses`, prev[2]); } regex = (function() { diff --git a/src/lexer.coffee b/src/lexer.coffee index 3bf22b4b03..2288f8fe15 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -252,7 +252,7 @@ exports.Lexer = class Lexer if prev and @value() is 'from' and (@seenImport or @seenExport) prev[0] = 'FROM' - if prev and prev.spaced and prev[0] in CALLABLE and /^[gs]et$/.test(prev[1]) + if prev and prev.spaced and prev[0] in CALLABLE and /^[gs]et$/.test(prev[1]) and /^\S*:\s/.test(@chunk) @error "'#{prev[1]}' cannot be used as a keyword, or as a function call without parentheses", prev[2] regex = switch quote diff --git a/test/function_invocation.coffee b/test/function_invocation.coffee index 99e1a4b362..9bc9b46497 100644 --- a/test/function_invocation.coffee +++ b/test/function_invocation.coffee @@ -712,15 +712,23 @@ test "get and set can be used as function names when not ambiguous with `get`/`s set = (val) -> val eq 2, get(2) eq 3, set(3) + eq 2, get 2 + eq 3, set 3 eq 'a', get('a') eq 'b', set('b') + eq 'a', get 'a' + eq 'b', set 'b' get = ({val}) -> val set = ({val}) -> val eq 4, get({val: 4}) eq 5, set({val: 5}) + eq 4, get {val: 4} + eq 5, set {val: 5} eq 'c', get({val: 'c'}) eq 'd', set({val: 'd'}) + eq 'c', get {val: 'c'} + eq 'd', set {val: 'd'} test "get and set can be used as variable and property names", -> get = 2