Skip to content

Commit d928e86

Browse files
refactor: read_class_modifiers
1 parent 06f6d89 commit d928e86

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

src/parser/class.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
*/
1515
read_class_declaration_statement: function() {
1616
const result = this.node("class");
17-
const flag = this.read_class_scope();
17+
const flag = this.read_class_modifiers();
1818
// graceful mode : ignore token & go next
1919
if (this.token !== this.tok.T_CLASS) {
2020
this.error(this.tok.T_CLASS);
@@ -32,23 +32,25 @@ module.exports = {
3232
const body = this.next().read_class_body();
3333
return result(propName, propExtends, propImplements, body, flag);
3434
},
35-
/**
36-
* Read the class visibility
37-
* ```ebnf
38-
* class_scope ::= (T_FINAL | T_ABSTRACT)?
39-
* ```
40-
*/
41-
read_class_scope: function() {
42-
const result = this.token;
43-
if (result == this.tok.T_FINAL) {
35+
36+
read_class_modifiers: function() {
37+
return [0, 0, this.read_class_modifier()];
38+
},
39+
40+
read_class_modifier: function() {
41+
let result = 0;
42+
43+
if (this.token === this.tok.T_ABSTRACT) {
4444
this.next();
45-
return [0, 0, 2];
46-
} else if (result == this.tok.T_ABSTRACT) {
45+
return 1;
46+
} else if (this.token === this.tok.T_FINAL) {
4747
this.next();
48-
return [0, 0, 1];
48+
return 2;
4949
}
50-
return [0, 0, 0];
50+
51+
return result;
5152
},
53+
5254
/**
5355
* Reads a class body
5456
* ```ebnf

test/snapshot/__snapshots__/class.test.js.snap

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,36 @@ Program {
11031103
}
11041104
`;
11051105

1106+
exports[`Test classes abstract and final 1`] = `
1107+
Program {
1108+
"children": Array [
1109+
Class {
1110+
"body": Array [],
1111+
"extends": null,
1112+
"implements": null,
1113+
"isAbstract": false,
1114+
"isAnonymous": false,
1115+
"isFinal": false,
1116+
"kind": "class",
1117+
"name": Identifier {
1118+
"kind": "identifier",
1119+
"name": "foo",
1120+
},
1121+
},
1122+
],
1123+
"errors": Array [
1124+
Error {
1125+
"expected": 187,
1126+
"kind": "error",
1127+
"line": 1,
1128+
"message": "Parse Error : syntax error, unexpected 'final' (T_FINAL), expecting T_CLASS on line 1",
1129+
"token": "'final' (T_FINAL)",
1130+
},
1131+
],
1132+
"kind": "program",
1133+
}
1134+
`;
1135+
11061136
exports[`Test classes class name as identifier 1`] = `
11071137
Program {
11081138
"children": Array [
@@ -1146,3 +1176,33 @@ Program {
11461176
"kind": "program",
11471177
}
11481178
`;
1179+
1180+
exports[`Test classes final and abstract 1`] = `
1181+
Program {
1182+
"children": Array [
1183+
Class {
1184+
"body": Array [],
1185+
"extends": null,
1186+
"implements": null,
1187+
"isAbstract": false,
1188+
"isAnonymous": false,
1189+
"isFinal": false,
1190+
"kind": "class",
1191+
"name": Identifier {
1192+
"kind": "identifier",
1193+
"name": "foo",
1194+
},
1195+
},
1196+
],
1197+
"errors": Array [
1198+
Error {
1199+
"expected": 187,
1200+
"kind": "error",
1201+
"line": 1,
1202+
"message": "Parse Error : syntax error, unexpected 'abstract' (T_ABSTRACT), expecting T_CLASS on line 1",
1203+
"token": "'abstract' (T_ABSTRACT)",
1204+
},
1205+
],
1206+
"kind": "program",
1207+
}
1208+
`;

test/snapshot/class.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,23 @@ describe("Test classes", function() {
122122
expect(parser.parseEval("class Foo {}")).toMatchSnapshot();
123123
});
124124

125-
it('class name as identifier', function() {
126-
expect(parser.parseEval('class A {}')).toMatchSnapshot();
125+
it("class name as identifier", function() {
126+
expect(parser.parseEval("class A {}")).toMatchSnapshot();
127+
});
128+
129+
it("final and abstract", function() {
130+
expect(
131+
parser.parseEval(`final abstract class foo {}`, {
132+
parser: { suppressErrors: true }
133+
})
134+
).toMatchSnapshot();
135+
});
136+
137+
it("abstract and final", function() {
138+
expect(
139+
parser.parseEval(`abstract final class foo {}`, {
140+
parser: { suppressErrors: true }
141+
})
142+
).toMatchSnapshot();
127143
});
128144
});

0 commit comments

Comments
 (0)