Skip to content

Commit 564db55

Browse files
committed
change the flags processing for property
1 parent b964f3b commit 564db55

10 files changed

Lines changed: 250 additions & 63 deletions

File tree

src/ast/classconstant.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ const ClassConstant = ConstantStatement.extends(
5353
* @return {void}
5454
*/
5555
ClassConstant.prototype.parseFlags = function (flags) {
56-
if (flags[0] === -1) {
56+
const getVis = flags[0][0];
57+
if (getVis === -1) {
5758
this.visibility = IS_UNDEFINED;
58-
} else if (flags[0] === null) {
59+
} else if (getVis === null) {
5960
/* istanbul ignore next */
6061
this.visibility = null;
61-
} else if (flags[0] === 0) {
62+
} else if (getVis === 0) {
6263
this.visibility = IS_PUBLIC;
63-
} else if (flags[0] === 1) {
64+
} else if (getVis === 1) {
6465
this.visibility = IS_PROTECTED;
65-
} else if (flags[0] === 2) {
66+
} else if (getVis === 2) {
6667
this.visibility = IS_PRIVATE;
6768
}
6869
this.final = flags[2] === 2;

src/ast/declaration.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ const IS_PUBLIC = "public";
1313
const IS_PROTECTED = "protected";
1414
const IS_PRIVATE = "private";
1515

16-
const SET_VISIBILITY_MAP = {
17-
0: IS_PUBLIC,
18-
1: IS_PROTECTED,
19-
2: IS_PRIVATE,
20-
};
16+
const VISIBILITY_MAP = [IS_PUBLIC, IS_PROTECTED, IS_PRIVATE];
2117

2218
/**
2319
* A declaration statement (function, class, interface...)
@@ -48,23 +44,17 @@ Declaration.prototype.parseFlags = function (flags) {
4844
this.isFinal = flags[2] === 2;
4945
this.isReadonly = flags[3] === 1;
5046
if (this.kind !== "class") {
51-
if (flags[0] === -1) {
47+
const [getVis, setVis] = flags[0];
48+
if (getVis === -1) {
5249
this.visibility = IS_UNDEFINED;
53-
} else if (flags[0] === null) {
50+
} else if (getVis === null) {
5451
/* istanbul ignore next */
5552
this.visibility = null;
56-
} else if (flags[0] === 0) {
57-
this.visibility = IS_PUBLIC;
58-
} else if (flags[0] === 1) {
59-
this.visibility = IS_PROTECTED;
60-
} else if (flags[0] === 2) {
61-
this.visibility = IS_PRIVATE;
53+
} else {
54+
this.visibility = VISIBILITY_MAP[getVis];
6255
}
6356
this.isStatic = flags[1] === 1;
64-
this.visibilitySet =
65-
flags[4] !== undefined && flags[4] !== -1
66-
? SET_VISIBILITY_MAP[flags[4]]
67-
: null;
57+
this.visibilitySet = setVis !== -1 ? VISIBILITY_MAP[setVis] : null;
6858
}
6959
};
7060

src/ast/propertystatement.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ const IS_PUBLIC = "public";
1313
const IS_PROTECTED = "protected";
1414
const IS_PRIVATE = "private";
1515

16-
const SET_VISIBILITY_MAP = {
17-
0: IS_PUBLIC,
18-
1: IS_PROTECTED,
19-
2: IS_PRIVATE,
20-
};
16+
const VISIBILITY_MAP = [IS_PUBLIC, IS_PROTECTED, IS_PRIVATE];
2117

2218
/**
2319
* Declares a properties into the current scope
@@ -48,25 +44,19 @@ const PropertyStatement = Statement.extends(
4844
* @return {void}
4945
*/
5046
PropertyStatement.prototype.parseFlags = function (flags) {
51-
if (flags[0] === -1) {
47+
const [getVis, setVis] = flags[0];
48+
if (getVis === -1) {
5249
this.visibility = IS_UNDEFINED;
53-
} else if (flags[0] === null) {
50+
} else if (getVis === null) {
5451
this.visibility = null;
55-
} else if (flags[0] === 0) {
56-
this.visibility = IS_PUBLIC;
57-
} else if (flags[0] === 1) {
58-
this.visibility = IS_PROTECTED;
59-
} else if (flags[0] === 2) {
60-
this.visibility = IS_PRIVATE;
52+
} else {
53+
this.visibility = VISIBILITY_MAP[getVis];
6154
}
6255

6356
this.isStatic = flags[1] === 1;
6457
this.isAbstract = flags[2] === 1;
6558
this.isFinal = flags[2] === 2;
66-
this.visibilitySet =
67-
flags[4] !== undefined && flags[4] !== -1
68-
? SET_VISIBILITY_MAP[flags[4]]
69-
: null;
59+
this.visibilitySet = setVis !== -1 ? VISIBILITY_MAP[setVis] : null;
7060
};
7161

7262
module.exports = PropertyStatement;

src/ast/traitalias.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ module.exports = Node.extends(
3232
this.as = as;
3333
this.visibility = IS_UNDEFINED;
3434
if (flags) {
35-
if (flags[0] === 0) {
35+
const getVis = flags[0][0];
36+
if (getVis === 0) {
3637
this.visibility = IS_PUBLIC;
37-
} else if (flags[0] === 1) {
38+
} else if (getVis === 1) {
3839
this.visibility = IS_PROTECTED;
39-
} else if (flags[0] === 2) {
40+
} else if (getVis === 2) {
4041
this.visibility = IS_PRIVATE;
4142
}
4243
}

src/parser/class.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ module.exports = {
115115

116116
// check constant
117117
if (this.token === this.tok.T_CONST) {
118+
if (flags[0][1] !== -1) {
119+
this.raiseError("Cannot use asymmetric visibility on constants");
120+
}
118121
const constants = this.read_constant_list(flags, attrs);
119122
if (this.expect(";")) {
120123
this.next();
@@ -126,7 +129,7 @@ module.exports = {
126129
// jump over T_VAR then land on T_VARIABLE
127130
if (allow_variables && this.token === this.tok.T_VAR) {
128131
this.next().expect(this.tok.T_VARIABLE);
129-
flags[0] = null; // public (as null)
132+
flags[0][0] = null; // public (as null)
130133
flags[1] = 0; // non static var
131134
}
132135

@@ -388,14 +391,14 @@ module.exports = {
388391
/*
389392
* Read member flags
390393
* @return array
391-
* 1st index : -1 => no visibility, 0 => public, 1 => protected, 2 => private
394+
* 1st index : [get, set] visibility tuple
395+
* get/set: -1 => no visibility, 0 => public, 1 => protected, 2 => private
392396
* 2nd index : 0 => instance member, 1 => static member
393397
* 3rd index : 0 => normal, 1 => abstract member, 2 => final member
394398
* 4th index : 0 => no readonly, 1 => readonly
395-
* 5th index : -1 => no set modifier, 0 => public(set), 1 => protected(set), 2 => private(set)
396399
*/
397400
read_member_flags(asInterface) {
398-
const result = [-1, 0, 0, 0, -1];
401+
const result = [[-1, -1], 0, 0, 0];
399402
const seen = new Set();
400403
while (this.is("T_MEMBER_FLAGS")) {
401404
let idx = -1,
@@ -428,19 +431,19 @@ module.exports = {
428431
if (this.expect(")")) {
429432
this.next(); // consume ')'
430433
}
431-
if (seen.has(4)) {
434+
if (seen.has("set")) {
432435
this.error(); // set modifier already defined
433436
} else if (val !== -1) {
434-
seen.add(4);
435-
result[4] = val;
437+
seen.add("set");
438+
result[0][1] = val;
436439
}
437440
continue;
438441
}
439442
if (seen.has(idx)) {
440443
this.error();
441444
} else if (val !== -1) {
442445
seen.add(idx);
443-
result[idx] = val;
446+
result[0][0] = val;
444447
}
445448
continue;
446449
}
@@ -606,6 +609,9 @@ module.exports = {
606609

607610
// check constant
608611
if (this.token === this.tok.T_CONST) {
612+
if (flags[0][1] !== -1) {
613+
this.raiseError("Cannot use asymmetric visibility on constants");
614+
}
609615
const constants = this.read_constant_list(flags, attrs);
610616
if (this.expect(";")) {
611617
this.next();

0 commit comments

Comments
 (0)