Skip to content

Allow computed class properties #5206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/coffeescript/grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

363 changes: 184 additions & 179 deletions lib/coffeescript/parser.js

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions lib/coffeescript/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ grammar =
ObjAssignable: [
o 'SimpleObjAssignable'
o '[ Expression ]', -> new Value new ComputedPropertyName $2
o '@ [ Expression ]', -> new Value LOC(1)(new ThisLiteral $1), [LOC(3)(new ComputedPropertyName($3))], 'this'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly copied this grammar rule from the rule for ThisProperty

o 'AlphaNumeric'
]

Expand Down
6 changes: 5 additions & 1 deletion src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,11 @@ exports.ExecutableClassBody = class ExecutableClassBody extends Base
# The class scope is not available yet, so return the assignment to update later
assign = @externalCtor = new Assign new Value, value
else if not assign.variable.this
name = new (if base.shouldCache() then Index else Access) base
name =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for the invalid JS for non-static computed class properties (extracted from #5205)

if base instanceof ComputedPropertyName
new Index base.value
else
new (if base.shouldCache() then Index else Access) base
prototype = new Access new PropertyName 'prototype'
variable = new Value new ThisLiteral(), [ prototype, name ]

Expand Down
18 changes: 15 additions & 3 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,21 @@ exports.Rewriter = class Rewriter
# The lexer has tagged the opening bracket of an indexing operation call.
# Match it with its paired close.
closeOpenIndexes: ->
startToken = null
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowing static computed class properties/methods was a bit more complex. In addition to the new grammar rule above, in the rewriter here we first want to make sure that the [/] in eg @[b]: ... are not tagged as INDEX_START/INDEX_END since that would be inaccurate and creates an ambiguous grammar

I believe this is safe because there's not a scenario where an actual INDEX_END would be immediately followed by a : token

condition = (token, i) ->
token[0] in [']', 'INDEX_END']

action = (token, i) ->
token[0] = 'INDEX_END'
if @tokens.length >= i and @tokens[i + 1][0] is ':'
startToken[0] = '['
token[0] = ']'
else
token[0] = 'INDEX_END'

@scanTokens (token, i) ->
@detectEnd i + 1, condition, action if token[0] is 'INDEX_START'
if token[0] is 'INDEX_START'
startToken = token
@detectEnd i + 1, condition, action
1

# Match tags in token stream starting at `i` with `pattern`.
Expand Down Expand Up @@ -321,7 +328,12 @@ exports.Rewriter = class Rewriter
if tag is ':'
# Go back to the (implicit) start of the object.
s = switch
when @tag(i - 1) in EXPRESSION_END then start[1]
when @tag(i - 1) in EXPRESSION_END
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, we have to make sure that for an implicit object (whether an actual implicit object or a class body) starting with a static computed property eg @[b]: ->, the start of the implicit object comes before the @

[startTag, startIndex] = start
if startTag is '[' and startIndex > 0 and @tag(startIndex - 1) is '@' and not tokens[startIndex - 1].spaced
startIndex - 1
else
startIndex
when @tag(i - 2) is '@' then i - 2
else i - 1

Expand Down
17 changes: 17 additions & 0 deletions test/classes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1935,3 +1935,20 @@ test "#5085: Bug: @ reference to class not maintained in do block", ->

eq thisFoo, 'foo assigned in class'
eq thisBar, 'foo assigned in class'

test "#5204: Computed class property", ->
foo = 'bar'
class A
[foo]: 'baz'
a = new A()
eq a.bar, 'baz'
eq A::bar, 'baz'

test "#5204: Static computed class property", ->
foo = 'bar'
qux = 'quux'
class A
@[foo]: 'baz'
@[qux]: -> 3
eq A.bar, 'baz'
eq A.quux(), 3
7 changes: 7 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,13 @@ test "invalid object keys", ->
{a=2}
^
'''
assertErrorFormat '''
@[a]: 1
''', '''
[stdin]:1:1: error: invalid object key
@[a]: 1
^^^^
'''

test "invalid destructuring default target", ->
assertErrorFormat '''
Expand Down
3 changes: 3 additions & 0 deletions test/objects.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,6 @@ test "#4579: Postfix for/while/until in first line of implicit object literals",
baz: 1337
arrayEq [4, 3, 2, 1, 0], six.foo.bar
eq 1337, six.foo.baz

test "#5204: not parsed as static property", ->
doesNotThrow -> CoffeeScript.compile "@ [b]: 2"