Skip to content

Commit 2635109

Browse files
helixbassGeoffreyBooth
authored andcommitted
AST: interpolated string is computed key (#5267)
* interpolated string key is computed * lhs test
1 parent a091bb8 commit 2635109

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ exports.ObjectProperty = class ObjectProperty extends Base
26322632
@locationData = key.locationData
26332633

26342634
astProperties: (o) ->
2635-
isComputedPropertyName = @key instanceof Value and @key.base instanceof ComputedPropertyName
2635+
isComputedPropertyName = (@key instanceof Value and @key.base instanceof ComputedPropertyName) or @key.unwrap() instanceof StringWithInterpolations
26362636
keyAst = @key.ast o, LEVEL_LIST
26372637

26382638
return
@@ -3177,7 +3177,7 @@ exports.ClassPrototypeProperty = class ClassPrototypeProperty extends Base
31773177
return
31783178
key: @name.ast o, LEVEL_LIST
31793179
value: @value.ast o, LEVEL_LIST
3180-
computed: @name instanceof ComputedPropertyName
3180+
computed: @name instanceof ComputedPropertyName or @name instanceof StringWithInterpolations
31813181

31823182
#### Import and Export
31833183

@@ -4243,9 +4243,7 @@ exports.Code = class Code extends Base
42434243

42444244
methodAstProperties: (o) ->
42454245
getIsComputed = =>
4246-
if @name instanceof Index
4247-
return no if @name.index instanceof StringWithInterpolations
4248-
return yes
4246+
return yes if @name instanceof Index
42494247
return yes if @name instanceof ComputedPropertyName
42504248
return yes if @name.name instanceof ComputedPropertyName
42514249
no

test/abstract_syntax_tree.coffee

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,23 @@ test "AST as expected for Obj node", ->
15341534
]
15351535
implicit: yes
15361536

1537+
testExpression '"#{a}": 1',
1538+
type: 'ObjectExpression'
1539+
properties: [
1540+
type: 'ObjectProperty'
1541+
key:
1542+
type: 'TemplateLiteral'
1543+
expressions: [
1544+
ID 'a'
1545+
]
1546+
value:
1547+
type: 'NumericLiteral'
1548+
value: 1
1549+
shorthand: no
1550+
computed: yes
1551+
]
1552+
implicit: yes
1553+
15371554
test "AST as expected for Arr node", ->
15381555
testExpression '[]',
15391556
type: 'ArrayExpression'
@@ -1837,6 +1854,9 @@ test "AST as expected for Class node", ->
18371854
@[b] = ->
18381855
"#{c}": ->
18391856
@[d] = 1
1857+
[e]: 2
1858+
"#{f}": 3
1859+
@[g]: 4
18401860
''',
18411861
type: 'ClassDeclaration'
18421862
body:
@@ -1845,7 +1865,16 @@ test "AST as expected for Class node", ->
18451865
computed: yes
18461866
,
18471867
type: 'ClassMethod'
1848-
computed: no
1868+
computed: yes
1869+
,
1870+
type: 'ClassProperty'
1871+
computed: yes
1872+
,
1873+
type: 'ClassPrototypeProperty'
1874+
computed: yes
1875+
,
1876+
type: 'ClassPrototypeProperty'
1877+
computed: yes
18491878
,
18501879
type: 'ClassProperty'
18511880
computed: yes
@@ -2362,6 +2391,19 @@ test "AST as expected for Assign node", ->
23622391
left:
23632392
type: 'ObjectPattern'
23642393

2394+
testExpression '{"#{a}": b} = c',
2395+
left:
2396+
type: 'ObjectPattern'
2397+
properties: [
2398+
type: 'ObjectProperty'
2399+
key:
2400+
type: 'TemplateLiteral'
2401+
expressions: [
2402+
ID 'a'
2403+
]
2404+
computed: yes
2405+
]
2406+
23652407
test "AST as expected for Code node", ->
23662408
testExpression '=>',
23672409
type: 'ArrowFunctionExpression'

0 commit comments

Comments
 (0)