Skip to content

Commit 9cfcf86

Browse files
helixbassGeoffreyBooth
authored andcommitted
AST: add params to scope (#5252)
* astNode() * no override ast() * add suggested comments * astInitialize() * astAddReturns() * separate step * recognize quoted constructor * add params to scope
1 parent 25c2378 commit 9cfcf86

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,8 +4059,14 @@ exports.Code = class Code extends Base
40594059
for {name} in @params when name instanceof Arr or name instanceof Obj
40604060
name.propagateLhs yes
40614061

4062+
astAddParamsToScope: (o) ->
4063+
for param in @params
4064+
param.eachName (name) ->
4065+
o.scope.add name, 'param'
4066+
40624067
astNode: (o) ->
40634068
@updateOptions o
4069+
@astAddParamsToScope o
40644070
@body.makeReturn null, yes unless @body.isEmpty() or @noReturn
40654071

40664072
super o

test/abstract_syntax_tree.coffee

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,84 @@ test "AST as expected for Code node", ->
25652565
async: no
25662566
id: null
25672567

2568+
testExpression '(a) -> a = 1',
2569+
type: 'FunctionExpression'
2570+
body:
2571+
type: 'BlockStatement'
2572+
body: [
2573+
type: 'ExpressionStatement'
2574+
expression:
2575+
type: 'AssignmentExpression'
2576+
left:
2577+
ID 'a', declaration: no
2578+
]
2579+
2580+
testExpression '(...a) -> a = 1',
2581+
type: 'FunctionExpression'
2582+
body:
2583+
type: 'BlockStatement'
2584+
body: [
2585+
type: 'ExpressionStatement'
2586+
expression:
2587+
type: 'AssignmentExpression'
2588+
left:
2589+
ID 'a', declaration: no
2590+
]
2591+
2592+
testExpression '({a}) -> a = 1',
2593+
type: 'FunctionExpression'
2594+
body:
2595+
type: 'BlockStatement'
2596+
body: [
2597+
type: 'ExpressionStatement'
2598+
expression:
2599+
type: 'AssignmentExpression'
2600+
left:
2601+
ID 'a', declaration: no
2602+
]
2603+
2604+
testExpression '([a]) -> a = 1',
2605+
type: 'FunctionExpression'
2606+
body:
2607+
type: 'BlockStatement'
2608+
body: [
2609+
type: 'ExpressionStatement'
2610+
expression:
2611+
type: 'AssignmentExpression'
2612+
left:
2613+
ID 'a', declaration: no
2614+
]
2615+
2616+
testExpression '(a = 1) -> a = 1',
2617+
type: 'FunctionExpression'
2618+
body:
2619+
type: 'BlockStatement'
2620+
body: [
2621+
type: 'ExpressionStatement'
2622+
expression:
2623+
type: 'AssignmentExpression'
2624+
left:
2625+
ID 'a', declaration: no
2626+
]
2627+
generator: no
2628+
async: no
2629+
id: null
2630+
2631+
testExpression '({a} = 1) -> a = 1',
2632+
type: 'FunctionExpression'
2633+
body:
2634+
type: 'BlockStatement'
2635+
body: [
2636+
type: 'ExpressionStatement'
2637+
expression:
2638+
type: 'AssignmentExpression'
2639+
left:
2640+
ID 'a', declaration: no
2641+
]
2642+
generator: no
2643+
async: no
2644+
id: null
2645+
25682646
test "AST as expected for Splat node", ->
25692647
testExpression '[a...]',
25702648
type: 'ArrayExpression'

0 commit comments

Comments
 (0)