Merge pull request #589 from Qantas94Heavy/fix-private-fields

Add delete check and fix nested class parsing for private fields
This commit is contained in:
Henry Zhu 2017-06-27 11:33:19 -04:00 committed by GitHub
commit 994cde616d
16 changed files with 3731 additions and 7 deletions

View File

@ -252,8 +252,16 @@ export default class ExpressionParser extends LValParser {
if (update) {
this.checkLVal(node.argument, undefined, undefined, "prefix operation");
} else if (this.state.strict && node.operator === "delete" && node.argument.type === "Identifier") {
this.raise(node.start, "Deleting local variable in strict mode");
} else if (this.state.strict && node.operator === "delete") {
const arg = node.argument;
if (arg.type === "Identifier") {
this.raise(node.start, "Deleting local variable in strict mode");
} else if (this.hasPlugin("classPrivateProperties")) {
if (arg.type === "PrivateName" || (arg.type === "MemberExpression" && arg.property.type === "PrivateName")) {
this.raise(node.start, "Deleting a private field is not allowed");
}
}
}
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");

View File

@ -684,7 +684,7 @@ export default class StatementParser extends ExpressionParser {
// class bodies are implicitly strict
const oldStrict = this.state.strict;
this.state.strict = true;
this.state.inClass = true;
this.state.classLevel++;
const state = { hadConstructor: false };
let decorators = [];
@ -731,7 +731,7 @@ export default class StatementParser extends ExpressionParser {
node.body = this.finishNode(classBody, "ClassBody");
this.state.inClass = false;
this.state.classLevel--;
this.state.strict = oldStrict;
}

View File

@ -444,7 +444,7 @@ export default class Tokenizer extends LocationParser {
switch (code) {
case 35: // '#'
if (this.hasPlugin("classPrivateProperties") && this.state.inClass) {
if (this.hasPlugin("classPrivateProperties") && this.state.classLevel > 0) {
++this.state.pos; return this.finishToken(tt.hash);
} else {
this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`);

View File

@ -24,11 +24,12 @@ export default class State {
this.inAsync =
this.inPropertyName =
this.inType =
this.inClass =
this.inClassProperty =
this.noAnonFunctionType =
false;
this.classLevel = 0;
this.labels = [];
this.decorators = [];
@ -84,7 +85,9 @@ export default class State {
noAnonFunctionType: boolean;
inPropertyName: boolean;
inClassProperty: boolean;
inClass: boolean;
// Check whether we are in a (nested) class or not.
classLevel: number;
// Labels in scope.
labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>;

View File

@ -0,0 +1,6 @@
class Foo {
#x;
constructor() {
delete #x.d;
}
}

View File

@ -0,0 +1,275 @@
{
"type": "File",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 10,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 6,
"column": 1
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 5
}
},
"key": {
"type": "Identifier",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 4
},
"identifierName": "x"
},
"name": "x"
},
"value": null
},
{
"type": "ClassMethod",
"start": 20,
"end": 56,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 5,
"column": 3
}
},
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 20,
"end": 31,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 13
},
"identifierName": "constructor"
},
"name": "constructor"
},
"kind": "constructor",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 34,
"end": 56,
"loc": {
"start": {
"line": 3,
"column": 16
},
"end": {
"line": 5,
"column": 3
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 40,
"end": 52,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 16
}
},
"expression": {
"type": "UnaryExpression",
"start": 40,
"end": 51,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 15
}
},
"operator": "delete",
"prefix": true,
"argument": {
"type": "MemberExpression",
"start": 47,
"end": 51,
"loc": {
"start": {
"line": 4,
"column": 11
},
"end": {
"line": 4,
"column": 15
}
},
"object": {
"type": "PrivateName",
"start": 48,
"end": 49,
"loc": {
"start": {
"line": 4,
"column": 12
},
"end": {
"line": 4,
"column": 13
}
},
"name": {
"type": "Identifier",
"start": 48,
"end": 49,
"loc": {
"start": {
"line": 4,
"column": 12
},
"end": {
"line": 4,
"column": 13
},
"identifierName": "x"
},
"name": "x"
}
},
"property": {
"type": "Identifier",
"start": 50,
"end": 51,
"loc": {
"start": {
"line": 4,
"column": 14
},
"end": {
"line": 4,
"column": 15
},
"identifierName": "d"
},
"name": "d"
},
"computed": false
},
"extra": {
"parenthesizedArgument": false
}
}
}
],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,6 @@
class Foo {
#x;
constructor() {
delete this.#x;
}
}

View File

@ -0,0 +1,7 @@
{
"throws": "Deleting a private field is not allowed (4:4)",
"plugins": [
"classProperties",
"classPrivateProperties"
]
}

View File

@ -0,0 +1,6 @@
class Foo {
#x;
constructor() {
delete #x;
}
}

View File

@ -0,0 +1,7 @@
{
"throws": "Deleting a private field is not allowed (4:4)",
"plugins": [
"classProperties",
"classPrivateProperties"
]
}

View File

@ -0,0 +1,3 @@
class Foo {
#2x = y
}

View File

@ -0,0 +1,4 @@
{
"throws": "Identifier directly after number (2:4)",
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,40 @@
class Point {
#x = 1;
#y = 2;
constructor(x = 0, y = 0) {
#x = +x;
#y = +y;
this.foo = class {
#x = 1;
#y = 2;
constructor(x = 0, y = 0) {
#x = +x;
#y = +y;
}
get x() { return #x }
set x(value) { #x = +value }
get y() { return #y }
set y(value) { #y = +value }
equals(p) { return #x === p.#x && #y === p.#y }
toString() { return `Point<${ #x },${ #y }>` }
};
}
get x() { return #x }
set x(value) { #x = +value }
get y() { return #y }
set y(value) { #y = +value }
equals(p) { return #x === p.#x && #y === p.#y }
toString() { return `Point<${ #x },${ #y }>` }
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}