Remove private field shorthand

There seems to be tentative agreement to remove the private field
shorthand given the added confusion and edge cases involved with the
shorthand.

Refs: https://github.com/tc39/proposal-class-fields/issues/21
This commit is contained in:
Karl Cheng 2017-07-25 11:53:38 +10:00
parent 59ffa6268e
commit 42d5dbe544
12 changed files with 2937 additions and 2266 deletions

View File

@ -331,14 +331,12 @@ export default class ExpressionParser extends LValParser {
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");
}
} else if (
this.hasPlugin("classPrivateProperties") &&
arg.type === "MemberExpression" &&
arg.property.type === "PrivateName"
) {
this.raise(node.start, "Deleting a private field is not allowed");
}
}
@ -747,13 +745,6 @@ export default class ExpressionParser extends LValParser {
this.takeDecorators(node);
return this.parseClass(node, false);
case tt.hash:
if (this.hasPlugin("classPrivateProperties")) {
return this.parseMaybePrivateName();
} else {
throw this.unexpected();
}
case tt._new:
return this.parseNew();

View File

@ -49,7 +49,6 @@ export default class LValParser extends NodeUtils {
if (node) {
switch (node.type) {
case "Identifier":
case "PrivateName":
case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
@ -300,7 +299,6 @@ export default class LValParser extends NodeUtils {
contextDescription: string,
): void {
switch (expr.type) {
case "PrivateName":
case "Identifier":
this.checkReservedWord(expr.name, expr.start, false, true);

View File

@ -1,4 +1,4 @@
{
"throws": "Unexpected token, expected ; (3:9)",
"throws": "Unexpected token (3:3)",
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

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

View File

@ -1,276 +0,0 @@
{
"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"
},
"static": false,
"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

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

View File

@ -1,5 +1,5 @@
{
"throws": "Deleting a private field is not allowed (4:4)",
"throws": "Unexpected token (4:11)",
"plugins": [
"classProperties",
"classPrivateProperties"

View File

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

File diff suppressed because it is too large Load Diff

View File

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