Debian Codegraph @

js/src/jit-test/tests/parser/redeclaration.js

raw
1 // Error message for redeclaration should show the position where the variable
2 // was declared.
3
4 const npos = -1;
5
6 function test_one(fun, filename, name,
7 [prevLineNumber, prevColumnNumber],
8 [lineNumber, columnNumber]) {
9 let caught = false;
10 try {
11 fun();
12 } catch (e) {
13 assertEq(e.message.includes("redeclaration"), true);
14 assertEq(e.lineNumber, lineNumber);
15 assertEq(e.columnNumber, columnNumber);
16 let notes = getErrorNotes(e);
17 if (prevLineNumber == npos) {
18 assertEq(notes.length, 0);
19 } else {
20 assertEq(notes.length, 1);
21 let note = notes[0];
22 assertEq(note.message,
23 `Previously declared at line ${prevLineNumber}, column ${prevColumnNumber}`);
24 assertEq(note.lineNumber, prevLineNumber);
25 assertEq(note.columnNumber, prevColumnNumber);
26 if (filename)
27 assertEq(note.fileName, filename);
28 }
29 caught = true;
30 }
31 assertEq(caught, true);
32 }
33
34 function test_parse(source, ...args) {
35 test_one(() => {
36 Reflect.parse(source, { source: "foo.js" });
37 }, "foo.js", ...args);
38 }
39
40 function test_eval(source, ...args) {
41 test_one(() => {
42 eval(source);
43 }, undefined, ...args);
44 }
45
46 function test(...args) {
47 test_parse(...args);
48 test_eval(...args);
49 }
50
51 // let
52
53 test(`
54 let a, a;
55 `, "a", [2, 5], [2, 8]);
56
57 test(`
58 let a;
59 let a;
60 `, "a", [2, 5], [3, 5]);
61
62 test(`
63 let a;
64 const a = 1;
65 `, "a", [2, 5], [3, 7]);
66
67 test(`
68 let a;
69 var a;
70 `, "a", [2, 5], [3, 5]);
71
72 test(`
73 let a;
74 function a() {
75 }
76 `, "a", [2, 5], [3, 10]);
77
78 test(`
79 {
80 let a;
81 function a() {
82 }
83 }
84 `, "a", [3, 7], [4, 12]);
85
86 // const
87
88 test(`
89 const a = 1, a = 2;
90 `, "a", [2, 7], [2, 14]);
91
92 test(`
93 const a = 1;
94 const a = 2;
95 `, "a", [2, 7], [3, 7]);
96
97 test(`
98 const a = 1;
99 let a;
100 `, "a", [2, 7], [3, 5]);
101
102 test(`
103 const a = 1;
104 var a;
105 `, "a", [2, 7], [3, 5]);
106
107 test(`
108 const a = 1;
109 function a() {
110 }
111 `, "a", [2, 7], [3, 10]);
112
113 test(`
114 {
115 const a = 1;
116 function a() {
117 }
118 }
119 `, "a", [3, 9], [4, 12]);
120
121 // var
122
123 test(`
124 var a;
125 let a;
126 `, "a", [2, 5], [3, 5]);
127
128 test(`
129 var a;
130 const a = 1;
131 `, "a", [2, 5], [3, 7]);
132
133 // function
134
135 test(`
136 function a() {};
137 let a;
138 `, "a", [2, 10], [3, 5]);
139
140 test(`
141 function a() {};
142 const a = 1;
143 `, "a", [2, 10], [3, 7]);
144
145 // Annex B lexical function
146
147 test(`
148 {
149 function a() {};
150 let a;
151 }
152 `, "a", [3, 12], [4, 7]);
153
154 test(`
155 {
156 function a() {};
157 const a = 1;
158 }
159 `, "a", [3, 12], [4, 9]);
160
161 // catch parameter
162
163 test(`
164 try {
165 } catch (a) {
166 let a;
167 }
168 `, "a", [3, 10], [4, 7]);
169 test(`
170 try {
171 } catch (a) {
172 const a = 1;
173 }
174 `, "a", [3, 10], [4, 9]);
175
176 test(`
177 try {
178 } catch (a) {
179 function a() {
180 }
181 }
182 `, "a", [3, 10], [4, 12]);
183
184 // parameter
185
186 test(`
187 function f(a) {
188 let a;
189 }
190 `, "a", [2, 12], [3, 7]);
191
192 test(`
193 function f(a) {
194 const a = 1;
195 }
196 `, "a", [2, 12], [3, 9]);
197
198 test(`
199 function f([a]) {
200 let a;
201 }
202 `, "a", [2, 13], [3, 7]);
203
204 test(`
205 function f({a}) {
206 let a;
207 }
208 `, "a", [2, 13], [3, 7]);
209
210 test(`
211 function f(...a) {
212 let a;
213 }
214 `, "a", [2, 15], [3, 7]);
215
216 test(`
217 function f(a=1) {
218 let a;
219 }
220 `, "a", [2, 12], [3, 7]);
221
222 // eval
223 // We don't have position information at runtime.
224 // No note should be shown.
225
226 test_eval(`
227 let a;
228 eval("var a");
229 `, "a", [npos, npos], [1, 5]);