-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathclass.test.js
More file actions
323 lines (302 loc) · 7.22 KB
/
Copy pathclass.test.js
File metadata and controls
323 lines (302 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const parser = require("../main");
describe("Test classes", function () {
it("Implement #183 : static keyword as identifier", function () {
expect(
parser.parseEval(`
class A {
public static function test() {
parent::foo();
self::bar();
static::baz();
A::fooBar();
$this->fooBaz();
}
}
`),
).toMatchSnapshot();
});
it("Implement typed_properties_v2 / php74", function () {
expect(
parser.parseEval(`
class Test {
public ?int $prop = null;
protected static float|string $y;
}
`),
).toMatchSnapshot();
});
it("Implement readonly property", function () {
expect(
parser.parseEval(
`
class User {
public readonly int $uid;
}
`,
{
parser: {
version: "8.1",
},
},
),
).toMatchSnapshot();
});
it("Validate usual declarations", function () {
expect(
parser.parseEval(`
final class foo extends bar implements
bim, bam, boum {
const FOO = "azerty";
public static $var;
public function __construct(array $data = null) {
$this->data = $data;
}
const list = "bar";
public function new($foo = self::list) {
return $this::list;
}
protected $foo;
private $bar;
function foobar() {}
}
abstract class bar {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B, C;
B::bigTalk as public talk;
B::bigTalk as protected talk;
B::bigTalk as private talk;
A::new as list;
list as new;
}
/**
* Some informations
*/
abstract protected function &foo() : bar;
}`),
).toMatchSnapshot();
});
it("Advanced tests", function () {
expect(
parser.parseEval(
`
class foo implements boo {
use A;
use B { foo as bar; }
// comment
/* boo */
/** doc
* data
foo
*/
var $var = true;
final function __construct() { }
private function boo() { }
}
interface boo extends something {
// some doc
const A = 1.5;
/** foo */
protected function foo();
}
trait line {
// some doc
const A = 1.5;
abstract protected function foo();
}
`,
{
parser: { extractDoc: true },
},
),
).toMatchSnapshot();
});
it("Test of silent mode", function () {
expect(
parser.parseEval(
`
class foo {
use A
use B { foo };
}`,
{
parser: { suppressErrors: true },
},
),
).toMatchSnapshot();
});
it("Test js properties", function () {
expect(
parser.parseEval(`
class __proto__ {
static $constructor;
public function constructor() {}
public function __proto__() {
$this->constructor = null;
self::$constructor = null;
}
}`),
).toMatchSnapshot();
});
it("Test promoted class properties php 8", function () {
const ast = parser.parseEval(
`
class __proto__ {
public function constructor(public int $id, private $name, int $c, protected ServerRequestInterface $req) {}
}`,
{
parser: {
version: "8.0",
suppressErrors: true,
},
},
);
expect(ast).toMatchSnapshot();
});
it("Test promoted readonly class properties", function () {
const ast = parser.parseEval(
`
class Bob {
public function __construct(public readonly int $id) {}
}
class Bob2 {
public function __construct(readonly public int $id) {}
}
`,
{
parser: {
version: "8.1",
},
},
);
expect(ast).toMatchSnapshot();
});
it("Test that readonly method parameters are throwing errors", function () {
const ast = parser.parseEval(
`
class Bob {
public function foo(public readonly int $id) {}
}`,
{
parser: {
version: "8.1",
suppressErrors: true,
},
},
);
expect(ast).toMatchSnapshot();
});
it("Test promoted nullable properties php 8", function () {
const ast = parser.parseEval(
`
class __proto__ {
public function constructor(public ?string $maybe, private ?int $opt) {}
}`,
{
parser: {
version: "8.0",
suppressErrors: true,
},
},
);
expect(ast).toMatchSnapshot();
});
describe("readonly class in PHP8.2", function () {
[
"readonly",
"readonly abstract",
"abstract readonly",
"final readonly",
"readonly final",
].forEach(function (token) {
it("should support " + token, function () {
expect(
parser.parseEval(`
${token} class Foo {
}
`),
).toMatchSnapshot();
});
});
});
it("empty", function () {
expect(parser.parseEval("class Foo {}")).toMatchSnapshot();
});
it("class name as identifier", function () {
expect(parser.parseEval("class A {}")).toMatchSnapshot();
});
it("final and abstract", function () {
expect(
parser.parseEval(`final abstract class foo {}`, {
parser: { suppressErrors: true },
}),
).toMatchSnapshot();
});
it("abstract and final", function () {
expect(
parser.parseEval(`abstract final class foo {}`, {
parser: { suppressErrors: true },
}),
).toMatchSnapshot();
});
it("8.4 allow new without parenthesis", () => {
const code = `new People()->name();`;
const test_parser = parser.create({
parser: {
version: "8.4",
},
});
expect(test_parser.parseEval(code)).toMatchSnapshot();
});
it("8.4 new without parenthesis with array lookup", () => {
const code = `new People()[0];`;
const test_parser = parser.create({
parser: {
version: "8.4",
},
});
expect(test_parser.parseEval(code)).toMatchSnapshot();
});
it("new without parenthesis throw errors in PHP < 8.4", () => {
const code = `new People()->name();`;
const test_parser = parser.create({
parser: {
version: "8.3",
},
});
expect(() => {
test_parser.parseEval(code);
}).toThrowErrorMatchingSnapshot();
});
it("knows where a function definiton starts", function () {
const phpCode = `
class b {
// prettier-ignore
public static function a() {}
}
`;
const ast = parser.parseEval(phpCode, {
ast: {
withPositions: true,
withSource: true,
},
});
const funcStart = ast.children[0].body[0].loc.start.offset;
const funcEnd = ast.children[0].body[0].loc.end.offset;
expect(phpCode.substr(funcStart, funcEnd - funcStart)).toEqual(
"public static function a() {}",
);
expect(ast.children[0].body[0].loc.source).toEqual(
"public static function a()",
);
});
it("handles property types with a leading \\", function () {
expect(
parser.parseEval(`
class Foo {
public \\Bar $baz;
}
`),
).toMatchSnapshot();
});
});