@@ -56,9 +56,96 @@ describe("catch", () => {
56
56
expect ( scope . type ) . to . be . equal ( "catch" ) ;
57
57
expect ( scope . variables ) . to . have . length ( 1 ) ;
58
58
expect ( scope . variables [ 0 ] . name ) . to . be . equal ( "e" ) ;
59
+ expect ( scope . variables [ 0 ] . defs ) . to . have . length ( 1 ) ;
60
+ expect ( scope . variables [ 0 ] . defs [ 0 ] . type ) . to . be . equal ( "CatchClause" ) ;
61
+ expect ( scope . variables [ 0 ] . defs [ 0 ] . name . type ) . to . be . equal ( "Identifier" ) ;
62
+ expect ( scope . variables [ 0 ] . defs [ 0 ] . name . name ) . to . be . equal ( "e" ) ;
63
+ expect ( scope . variables [ 0 ] . defs [ 0 ] . node . type ) . to . be . equal ( "CatchClause" ) ;
64
+ expect ( scope . variables [ 0 ] . defs [ 0 ] . parent ) . to . be . equal ( null ) ;
59
65
expect ( scope . isArgumentsMaterialized ( ) ) . to . be . true ;
60
66
expect ( scope . references ) . to . have . length ( 0 ) ;
61
67
} ) ;
68
+
69
+ it ( "param can be a pattern" , ( ) => {
70
+ const ast = espree ( `
71
+ (function () {
72
+ const default_id = 0;
73
+ try {
74
+ } catch ({ message, id = default_id, args: [arg1, arg2] }) {
75
+ }
76
+ }());
77
+ ` ) ;
78
+
79
+ const scopeManager = analyze ( ast , { ecmaVersion : 6 } ) ;
80
+
81
+ expect ( scopeManager . scopes ) . to . have . length ( 5 ) ;
82
+
83
+ const globalScope = scopeManager . scopes [ 0 ] ;
84
+
85
+ expect ( globalScope . type ) . to . be . equal ( "global" ) ;
86
+ expect ( globalScope . variables ) . to . have . length ( 0 ) ;
87
+ expect ( globalScope . references ) . to . have . length ( 0 ) ;
88
+
89
+ const functionScope = scopeManager . scopes [ 1 ] ;
90
+
91
+ expect ( functionScope . type ) . to . be . equal ( "function" ) ;
92
+ expect ( functionScope . variables ) . to . have . length ( 2 ) ;
93
+ expect ( functionScope . variables [ 0 ] . name ) . to . be . equal ( "arguments" ) ;
94
+ expect ( functionScope . variables [ 1 ] . name ) . to . be . equal ( "default_id" ) ;
95
+ expect ( functionScope . references ) . to . have . length ( 1 ) ;
96
+ expect ( functionScope . references [ 0 ] . from ) . to . be . equal ( functionScope ) ;
97
+ expect ( functionScope . references [ 0 ] . resolved ) . to . be . equal ( functionScope . variables [ 1 ] ) ;
98
+
99
+ const tryBlockScope = scopeManager . scopes [ 2 ] ;
100
+
101
+ expect ( tryBlockScope . type ) . to . be . equal ( "block" ) ;
102
+ expect ( tryBlockScope . variables ) . to . have . length ( 0 ) ;
103
+ expect ( tryBlockScope . references ) . to . have . length ( 0 ) ;
104
+
105
+ const catchScope = scopeManager . scopes [ 3 ] ;
106
+
107
+ expect ( catchScope . type ) . to . be . equal ( "catch" ) ;
108
+ expect ( catchScope . variables ) . to . have . length ( 4 ) ;
109
+ expect ( catchScope . variables [ 0 ] . name ) . to . be . equal ( "message" ) ;
110
+ expect ( catchScope . variables [ 0 ] . defs ) . to . have . length ( 1 ) ;
111
+ expect ( catchScope . variables [ 0 ] . defs [ 0 ] . type ) . to . be . equal ( "CatchClause" ) ;
112
+ expect ( catchScope . variables [ 0 ] . defs [ 0 ] . name . type ) . to . be . equal ( "Identifier" ) ;
113
+ expect ( catchScope . variables [ 0 ] . defs [ 0 ] . name . name ) . to . be . equal ( "message" ) ;
114
+ expect ( catchScope . variables [ 0 ] . defs [ 0 ] . node . type ) . to . be . equal ( "CatchClause" ) ;
115
+ expect ( catchScope . variables [ 0 ] . defs [ 0 ] . parent ) . to . be . equal ( null ) ;
116
+ expect ( catchScope . variables [ 1 ] . name ) . to . be . equal ( "id" ) ;
117
+ expect ( catchScope . variables [ 1 ] . defs ) . to . have . length ( 1 ) ;
118
+ expect ( catchScope . variables [ 1 ] . defs [ 0 ] . type ) . to . be . equal ( "CatchClause" ) ;
119
+ expect ( catchScope . variables [ 1 ] . defs [ 0 ] . name . type ) . to . be . equal ( "Identifier" ) ;
120
+ expect ( catchScope . variables [ 1 ] . defs [ 0 ] . name . name ) . to . be . equal ( "id" ) ;
121
+ expect ( catchScope . variables [ 1 ] . defs [ 0 ] . node . type ) . to . be . equal ( "CatchClause" ) ;
122
+ expect ( catchScope . variables [ 1 ] . defs [ 0 ] . parent ) . to . be . equal ( null ) ;
123
+ expect ( catchScope . variables [ 2 ] . name ) . to . be . equal ( "arg1" ) ;
124
+ expect ( catchScope . variables [ 2 ] . defs ) . to . have . length ( 1 ) ;
125
+ expect ( catchScope . variables [ 2 ] . defs [ 0 ] . type ) . to . be . equal ( "CatchClause" ) ;
126
+ expect ( catchScope . variables [ 2 ] . defs [ 0 ] . name . type ) . to . be . equal ( "Identifier" ) ;
127
+ expect ( catchScope . variables [ 2 ] . defs [ 0 ] . name . name ) . to . be . equal ( "arg1" ) ;
128
+ expect ( catchScope . variables [ 2 ] . defs [ 0 ] . node . type ) . to . be . equal ( "CatchClause" ) ;
129
+ expect ( catchScope . variables [ 2 ] . defs [ 0 ] . parent ) . to . be . equal ( null ) ;
130
+ expect ( catchScope . variables [ 3 ] . name ) . to . be . equal ( "arg2" ) ;
131
+ expect ( catchScope . variables [ 3 ] . defs ) . to . have . length ( 1 ) ;
132
+ expect ( catchScope . variables [ 3 ] . defs [ 0 ] . type ) . to . be . equal ( "CatchClause" ) ;
133
+ expect ( catchScope . variables [ 3 ] . defs [ 0 ] . name . type ) . to . be . equal ( "Identifier" ) ;
134
+ expect ( catchScope . variables [ 3 ] . defs [ 0 ] . name . name ) . to . be . equal ( "arg2" ) ;
135
+ expect ( catchScope . variables [ 3 ] . defs [ 0 ] . node . type ) . to . be . equal ( "CatchClause" ) ;
136
+ expect ( catchScope . variables [ 3 ] . defs [ 0 ] . parent ) . to . be . equal ( null ) ;
137
+ expect ( catchScope . references ) . to . have . length ( 2 ) ;
138
+ expect ( catchScope . references [ 0 ] . from ) . to . be . equal ( catchScope ) ;
139
+ expect ( catchScope . references [ 0 ] . resolved ) . to . be . equal ( catchScope . variables [ 1 ] ) ;
140
+ expect ( catchScope . references [ 1 ] . from ) . to . be . equal ( catchScope ) ;
141
+ expect ( catchScope . references [ 1 ] . resolved ) . to . be . equal ( functionScope . variables [ 1 ] ) ;
142
+
143
+ const catchBlockScope = scopeManager . scopes [ 4 ] ;
144
+
145
+ expect ( catchBlockScope . type ) . to . be . equal ( "block" ) ;
146
+ expect ( catchBlockScope . variables ) . to . have . length ( 0 ) ;
147
+ expect ( catchBlockScope . references ) . to . have . length ( 0 ) ;
148
+ } ) ;
62
149
} ) ;
63
150
64
151
// vim: set sw=4 ts=4 et tw=80 :
0 commit comments