Skip to content

Commit 42402da

Browse files
helixbassGeoffreyBooth
authored andcommitted
Code AST (#5155)
* adding tests * updated grammar * tests * fixes from code review
1 parent 42622b1 commit 42402da

File tree

7 files changed

+651
-92
lines changed

7 files changed

+651
-92
lines changed

lib/coffeescript/grammar.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/nodes.js

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

lib/coffeescript/parser.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/grammar.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ grammar =
341341
Param: [
342342
o 'ParamVar', -> new Param $1
343343
o 'ParamVar ...', -> new Param $1, null, on
344-
o '... ParamVar', -> new Param $2, null, on
344+
o '... ParamVar', -> new Param $2, null, postfix: no
345345
o 'ParamVar = Expression', -> new Param $1, $3
346346
o '...', -> new Expansion
347347
]

src/nodes.coffee

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,6 +3262,8 @@ exports.Code = class Code extends Base
32623262
if node instanceof For and node.isAwait()
32633263
@isAsync = yes
32643264

3265+
@propagateLhs()
3266+
32653267
children: ['params', 'body']
32663268

32673269
isStatement: -> @isMethod
@@ -3551,6 +3553,38 @@ exports.Code = class Code extends Base
35513553

35523554
seenSuper
35533555

3556+
propagateLhs: ->
3557+
for {name} in @params when name instanceof Arr or name instanceof Obj
3558+
name.propagateLhs yes
3559+
3560+
astType: ->
3561+
if @bound
3562+
'ArrowFunctionExpression'
3563+
else
3564+
'FunctionExpression'
3565+
3566+
paramForAst: (param) ->
3567+
return param if param instanceof Expansion
3568+
{name, value, splat} = param
3569+
if splat
3570+
new Splat name, lhs: yes, postfix: splat.postfix
3571+
.withLocationDataFrom name
3572+
else if value?
3573+
new Assign name, value, null, param: yes
3574+
.withLocationDataFrom locationData: mergeLocationData name.locationData, value.locationData
3575+
else
3576+
name
3577+
3578+
astProperties: (o) ->
3579+
return
3580+
params: @paramForAst(param).ast(o) for param in @params
3581+
body: @body.ast o
3582+
generator: !!@isGenerator
3583+
async: !!@isAsync
3584+
# We never generate named functions, so specify `id` as `null`, which
3585+
# matches the Babel AST for anonymous function expressions/arrow functions
3586+
id: null
3587+
35543588
#### Param
35553589

35563590
# A parameter in a function definition. Beyond a typical JavaScript parameter,
@@ -3661,7 +3695,7 @@ exports.Param = class Param extends Base
36613695
# A splat, either as a parameter to a function, an argument to a call,
36623696
# or as part of a destructuring assignment.
36633697
exports.Splat = class Splat extends Base
3664-
constructor: (name, {@postfix = true} = {}) ->
3698+
constructor: (name, {@lhs, @postfix = true} = {}) ->
36653699
super()
36663700
@name = if name.compile then name else new Literal name
36673701

@@ -4015,6 +4049,8 @@ exports.Op = class Op extends Base
40154049
super idt, @constructor.name + ' ' + @operator
40164050

40174051
astType: ->
4052+
return 'AwaitExpression' if @isAwait()
4053+
return 'YieldExpression' if @isYield()
40184054
switch @operator
40194055
when '||', '&&', '?' then 'LogicalExpression'
40204056
when '++', '--' then 'UpdateExpression'
@@ -4027,10 +4063,21 @@ exports.Op = class Op extends Base
40274063
secondAst = @second?.ast o
40284064
switch
40294065
when @isUnary()
4030-
return
4031-
argument: firstAst
4066+
argument =
4067+
if @isYield() and @first.unwrap().value is ''
4068+
null
4069+
else
4070+
firstAst
4071+
return {argument} if @isAwait()
4072+
return {
4073+
argument
4074+
delegate: @operator is 'yield*'
4075+
} if @isYield()
4076+
return {
4077+
argument
40324078
operator: @originalOperator
40334079
prefix: !@flip
4080+
}
40344081
else
40354082
return
40364083
left: firstAst

0 commit comments

Comments
 (0)