Skip to content

Commit

Permalink
feat: support for readonly class (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
genintho authored Jan 11, 2023
1 parent 0cbf7c4 commit 30e229e
Show file tree
Hide file tree
Showing 27 changed files with 325 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/ast/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const KIND = "class";
* @property {boolean} isAnonymous
* @property {boolean} isAbstract
* @property {boolean} isFinal
* @property {boolean} isReadonly
* @property {AttrGroup[]} attrGroups
*/
module.exports = Declaration.extends(
Expand Down
1 change: 1 addition & 0 deletions src/ast/declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Declaration = Statement.extends(
Declaration.prototype.parseFlags = function (flags) {
this.isAbstract = flags[2] === 1;
this.isFinal = flags[2] === 2;
this.isReadonly = flags[3] === 1;
if (this.kind !== "class") {
if (flags[0] === -1) {
this.visibility = IS_UNDEFINED;
Expand Down
32 changes: 23 additions & 9 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,35 @@ module.exports = {
},

read_class_modifiers: function () {
return [0, 0, this.read_class_modifier()];
const modifier = this.read_class_modifier({
readonly: 0,
final_or_abstract: 0,
});
return [0, 0, modifier.final_or_abstract, modifier.readonly];
},

read_class_modifier: function () {
const result = 0;

if (this.token === this.tok.T_ABSTRACT) {
read_class_modifier: function (memo) {
if (this.token === this.tok.T_READ_ONLY) {
this.next();
memo.readonly = 1;
memo = this.read_class_modifier(memo);
} else if (
memo.final_or_abstract === 0 &&
this.token === this.tok.T_ABSTRACT
) {
this.next();
return 1;
} else if (this.token === this.tok.T_FINAL) {
memo.final_or_abstract = 1;
memo = this.read_class_modifier(memo);
} else if (
memo.final_or_abstract === 0 &&
this.token === this.tok.T_FINAL
) {
this.next();
return 2;
memo.final_or_abstract = 2;
memo = this.read_class_modifier(memo);
}

return result;
return memo;
},

/*
Expand Down
1 change: 1 addition & 0 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
// optional flags
case this.tok.T_ABSTRACT:
case this.tok.T_FINAL:
case this.tok.T_READ_ONLY:
case this.tok.T_CLASS:
return this.read_class_declaration_statement(attrs);
case this.tok.T_INTERFACE:
Expand Down
9 changes: 9 additions & 0 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": true,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"leadingComments": [
Expand Down Expand Up @@ -1625,6 +1626,7 @@ Program {
"isAbstract": true,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"leadingComments": [
CommentBlock {
Expand Down Expand Up @@ -1713,6 +1715,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"loc": Location {
Expand Down Expand Up @@ -1773,6 +1776,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"loc": Location {
Expand Down Expand Up @@ -1833,6 +1837,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": true,
"kind": "method",
"loc": Location {
Expand Down Expand Up @@ -2823,6 +2828,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"loc": Location {
Expand Down Expand Up @@ -2936,6 +2942,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"loc": Location {
Expand Down Expand Up @@ -5284,6 +5291,7 @@ next:
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"loc": Location {
Expand Down Expand Up @@ -5343,6 +5351,7 @@ next:
"isAbstract": false,
"isAnonymous": true,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"loc": Location {
"end": Position {
Expand Down
19 changes: 19 additions & 0 deletions test/snapshot/__snapshots__/attributes.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Program {
"isAbstract": false,
"isAnonymous": true,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": null,
},
Expand Down Expand Up @@ -287,6 +288,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -340,6 +342,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -462,6 +465,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -538,6 +542,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand Down Expand Up @@ -589,6 +594,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -605,6 +611,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -659,6 +666,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -990,6 +998,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1174,6 +1183,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1420,6 +1430,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1525,6 +1536,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1691,6 +1703,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -1711,6 +1724,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -1731,6 +1745,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -1747,6 +1762,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1775,6 +1791,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"leadingComments": [
Expand All @@ -1799,6 +1816,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"leadingComments": [
CommentLine {
Expand Down Expand Up @@ -1968,6 +1986,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down
3 changes: 3 additions & 0 deletions test/snapshot/__snapshots__/block.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -667,6 +668,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -683,6 +685,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down
6 changes: 6 additions & 0 deletions test/snapshot/__snapshots__/call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -1229,6 +1230,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1339,6 +1341,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -1355,6 +1358,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -1450,6 +1454,7 @@ Program {
"byref": false,
"isAbstract": false,
"isFinal": false,
"isReadonly": false,
"isStatic": false,
"kind": "method",
"name": Identifier {
Expand All @@ -1466,6 +1471,7 @@ Program {
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
Expand Down
Loading

0 comments on commit 30e229e

Please sign in to comment.