Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ast/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
const Declaration = require("./declaration");
const KIND = "parameter";

// eslint-disable-next-line no-unused-vars
const MODIFIER_PUBLIC = 1;
Copy link
Member

@ichiriac ichiriac Apr 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@czosel / @cseufert not sure thats a good approach, flags are better in memory usage but not readable, here I've used a different approch in other classes : https://github.com/glayzzle/php-parser/blob/master/src/ast/classconstant.js#L12

// eslint-disable-next-line no-unused-vars
const MODIFIER_PROTECTED = 2;
// eslint-disable-next-line no-unused-vars
const MODIFIER_PRIVATE = 4;

/**
* Defines a function parameter
* @constructor Parameter
Expand All @@ -17,6 +24,7 @@ const KIND = "parameter";
* @property {boolean} byref
* @property {boolean} variadic
* @property {boolean} nullable
* @property {MODIFIER_PUBLIC|MODIFIER_PROTECTED|MODIFIER_PRIVATE} flags
*/
module.exports = Declaration.extends(KIND, function Parameter(
name,
Expand All @@ -25,6 +33,7 @@ module.exports = Declaration.extends(KIND, function Parameter(
isRef,
isVariadic,
nullable,
flags,
docs,
location
) {
Expand All @@ -34,4 +43,5 @@ module.exports = Declaration.extends(KIND, function Parameter(
this.byref = isRef;
this.variadic = isVariadic;
this.nullable = nullable;
this.flags = flags || 0;
});
27 changes: 26 additions & 1 deletion src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ module.exports = {
this.next();
nullable = true;
}
const flags = this.read_promoted();
types = this.read_types();
if (nullable && !types) {
this.raiseError(
Expand All @@ -225,7 +226,15 @@ module.exports = {
if (this.token == "=") {
value = this.next().read_expr();
}
return node(parameterName, types, value, isRef, isVariadic, nullable);
return node(
parameterName,
types,
value,
isRef,
isVariadic,
nullable,
flags
);
},
read_types() {
const types = [];
Expand All @@ -244,6 +253,22 @@ module.exports = {
return unionType(types);
}
},
read_promoted() {
const MODIFIER_PUBLIC = 1;
const MODIFIER_PROTECTED = 2;
const MODIFIER_PRIVATE = 4;
if (this.token === this.tok.T_PUBLIC) {
this.next();
return MODIFIER_PUBLIC;
} else if (this.token === this.tok.T_PROTECTED) {
this.next();
return MODIFIER_PROTECTED;
} else if (this.token === this.tok.T_PRIVATE) {
this.next();
return MODIFIER_PRIVATE;
}
return 0;
},
/**
* Reads a list of arguments
* ```ebnf
Expand Down
2 changes: 2 additions & 0 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,7 @@ Program {
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"loc": Location {
"end": Position {
Expand Down Expand Up @@ -4107,6 +4108,7 @@ next:
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"loc": Location {
"end": Position {
Expand Down
8 changes: 8 additions & 0 deletions test/snapshot/__snapshots__/arrowfunc.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Program {
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -65,6 +66,7 @@ Program {
"arguments": Array [
Parameter {
"byref": true,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -114,6 +116,7 @@ Program {
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand All @@ -126,6 +129,7 @@ Program {
},
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand All @@ -138,6 +142,7 @@ Program {
},
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -216,6 +221,7 @@ Program {
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -270,6 +276,7 @@ Program {
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -319,6 +326,7 @@ Program {
"arguments": Array [
Parameter {
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down
2 changes: 2 additions & 0 deletions test/snapshot/__snapshots__/byref.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ Program {
"arguments": Array [
Parameter {
"byref": true,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down Expand Up @@ -897,6 +898,7 @@ Program {
"arguments": Array [
Parameter {
"byref": true,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
Expand Down
Loading