diff --git a/ast/spec.md b/ast/spec.md index 3c49c16a70..f457645452 100644 --- a/ast/spec.md +++ b/ast/spec.md @@ -3,6 +3,7 @@ These are the core Babylon AST node types. - [Node objects](#node-objects) - [Changes](#changes) - [Identifier](#identifier) +- [PrivateName](#privatename) - [Literals](#literals) - [RegExpLiteral](#regexpliteral) - [NullLiteral](#nullliteral) @@ -90,6 +91,7 @@ These are the core Babylon AST node types. - [ClassBody](#classbody) - [ClassMethod](#classmethod) - [ClassProperty](#classproperty) + - [ClassPrivateProperty](#classprivateproperty) - [ClassDeclaration](#classdeclaration) - [ClassExpression](#classexpression) - [MetaProperty](#metaproperty) @@ -166,6 +168,18 @@ interface Identifier <: Expression, Pattern { An identifier. Note that an identifier may be an expression or a destructuring pattern. + +# PrivateName + +```js +interface PrivateName <: Expression, Pattern { + type: "PrivateName"; + name: Identifier; +} +``` +A Private Name Identifier. + + # Literals ```js @@ -1015,7 +1029,7 @@ interface Class <: Node { ```js interface ClassBody <: Node { type: "ClassBody"; - body: [ ClassMethod | ClassProperty ]; + body: [ ClassMethod | ClassProperty | ClassPrivateProperty ]; } ``` @@ -1043,6 +1057,16 @@ interface ClassProperty <: Node { } ``` +## ClassPrivateProperty + +```js +interface ClassPrivateProperty <: Node { + type: "ClassPrivateProperty"; + key: Identifier; + value: Expression; +} +``` + ## ClassDeclaration ```js diff --git a/src/parser/expression.js b/src/parser/expression.js index 5d08afd31b..d2189e6c40 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -99,7 +99,6 @@ export default class ExpressionParser extends LValParser { parseMaybeAssign(noIn?: ?boolean, refShorthandDefaultPos?: ?Pos, afterLeftParse?: Function, refNeedsArrowPos?: ?Pos): N.Expression { const startPos = this.state.start; const startLoc = this.state.startLoc; - if (this.match(tt._yield) && this.state.inGenerator) { let left = this.parseYield(); if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc); @@ -297,7 +296,7 @@ export default class ExpressionParser extends LValParser { } else if (this.eat(tt.dot)) { const node = this.startNodeAt(startPos, startLoc); node.object = base; - node.property = this.parseIdentifier(true); + node.property = this.hasPlugin("classPrivateProperties") ? this.parseMaybePrivateName() : this.parseIdentifier(true); node.computed = false; base = this.finishNode(node, "MemberExpression"); } else if (this.eat(tt.bracketL)) { @@ -525,6 +524,13 @@ 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 { + this.unexpected(); + } + case tt._new: return this.parseNew(); @@ -547,6 +553,18 @@ export default class ExpressionParser extends LValParser { } } + parseMaybePrivateName(): N.PrivateName | N.Identifier { + const isPrivate = this.eat(tt.hash); + + if (isPrivate) { + const node = this.startNode(); + node.name = this.parseIdentifier(true); + return this.finishNode(node, "PrivateName"); + } else { + return this.parseIdentifier(true); + } + } + parseFunctionExpression(): N.FunctionExpression | N.MetaProperty { const node = this.startNode(); const meta = this.parseIdentifier(true); diff --git a/src/parser/lval.js b/src/parser/lval.js index 8576198b61..d3fcd02900 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -26,6 +26,7 @@ export default class LValParser extends NodeUtils { if (node) { switch (node.type) { case "Identifier": + case "PrivateName": case "ObjectPattern": case "ArrayPattern": case "AssignmentPattern": @@ -227,6 +228,7 @@ export default class LValParser extends NodeUtils { checkClashes: ?{ [key: string]: boolean }, contextDescription: string): void { switch (expr.type) { + case "PrivateName": case "Identifier": this.checkReservedWord(expr.name, expr.start, false, true); diff --git a/src/parser/statement.js b/src/parser/statement.js index c77f435001..24a693a5d2 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -650,6 +650,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; let hadConstructor = false; let decorators = []; @@ -680,6 +681,13 @@ export default class StatementParser extends ExpressionParser { decorators = []; } + if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) { // Private property + this.next(); + this.parsePropertyName(method); + classBody.body.push(this.parsePrivateClassProperty(method)); + continue; + } + method.static = false; if (this.match(tt.name) && this.state.value === "static") { const key = this.parseIdentifier(true); // eats 'static' @@ -774,9 +782,26 @@ export default class StatementParser extends ExpressionParser { node.body = this.finishNode(classBody, "ClassBody"); + this.state.inClass = false; this.state.strict = oldStrict; + } + parsePrivateClassProperty(node: N.ClassPrivateProperty): N.ClassPrivateProperty { + this.state.inClassProperty = true; + + if (this.match(tt.eq)) { + this.next(); + node.value = this.parseMaybeAssign(); + } else { + node.value = null; + } + this.semicolon(); + this.state.inClassProperty = false; + return this.finishNode(node, "ClassPrivateProperty"); + } + + parseClassProperty(node: N.ClassProperty): N.ClassProperty { const hasPlugin = this.hasPlugin("classProperties"); const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled."; diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index 42273d664c..7727032fcc 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -405,8 +405,17 @@ export default class Tokenizer extends LocationParser { getTokenFromCode(code: number): void { switch (code) { + + case 35: // '#' + if (this.hasPlugin("classPrivateProperties") && this.state.inClass) { + ++this.state.pos; return this.finishToken(tt.hash); + } else { + this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`); + } + // The interpretation of a dot depends on whether it is followed // by a digit or another two dots. + case 46: // '.' return this.readToken_dot(); diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index 8fe08ac001..cb1fedf74c 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -24,6 +24,7 @@ export default class State { this.inAsync = this.inPropertyName = this.inType = + this.inClass = this.inClassProperty = this.noAnonFunctionType = false; @@ -81,6 +82,7 @@ export default class State { noAnonFunctionType: boolean; inPropertyName: boolean; inClassProperty: boolean; + inClass: boolean; // Labels in scope. labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>; diff --git a/src/tokenizer/types.js b/src/tokenizer/types.js index e6552249e6..48d075d2b1 100644 --- a/src/tokenizer/types.js +++ b/src/tokenizer/types.js @@ -108,6 +108,7 @@ export const types: { [name: string]: TokenType } = { backQuote: new TokenType("`", { startsExpr }), dollarBraceL: new TokenType("${", { beforeExpr, startsExpr }), at: new TokenType("@"), + hash: new TokenType("#"), // Operators. These carry several kinds of properties to help the // parser use them properly (the presence of these properties is diff --git a/src/types.js b/src/types.js index 50e8bd95d3..5e8013b5de 100644 --- a/src/types.js +++ b/src/types.js @@ -62,6 +62,13 @@ export type Identifier = PatternBase & { __clone(): Identifier; }; +export type PrivateName = PatternBase & { + type: "PrivateName"; + name: string; + + __clone(): Identifier; +}; + // Literals export type Literal = RegExpLiteral | NullLiteral | StringLiteral | BooleanLiteral | NumericLiteral; @@ -334,7 +341,7 @@ export type ObjectExpression = NodeBase & { properties: $ReadOnlyArray; }; -export type ObjectOrClassMember = ClassMethod | ClassProperty | ObjectMember; +export type ObjectOrClassMember = ClassMethod | ClassProperty | ClassPrivateProperty | ObjectMember; export type ObjectMember = ObjectProperty | ObjectMethod; @@ -552,7 +559,7 @@ export type ClassMemberBase = NodeBase & HasDecorators & { export type Accessibility = "public" | "protected" | "private"; -export type ClassMember = ClassMethod | ClassProperty; +export type ClassMember = ClassMethod | ClassProperty | ClassPrivateProperty; export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod; @@ -584,6 +591,18 @@ export type ClassProperty = ClassMemberBase & { readonly?: true; }; +export type ClassPrivateProperty = ClassMemberBase & { + type: "ClassPrivateProperty"; + key: Identifier; + value: ?Expression; // TODO: Not in spec that this is nullable. + + typeAnnotation?: ?FlowTypeAnnotation; // TODO: Not in spec + variance?: ?FlowVariance; // TODO: Not in spec + + // TypeScript only: (TODO: Not in spec) + readonly?: true; +}; + export type OptClassDeclaration = ClassBase & DeclarationBase & HasDecorators & { type: "ClassDeclaration"; // TypeScript only diff --git a/test/fixtures/experimental/class-private-properties/asi-failure-computed/actual.js b/test/fixtures/experimental/class-private-properties/asi-failure-computed/actual.js new file mode 100644 index 0000000000..0fb2172e4d --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-failure-computed/actual.js @@ -0,0 +1,4 @@ +class Foo { + #p = x + [#m] () {} +} diff --git a/test/fixtures/experimental/class-private-properties/asi-failure-computed/options.json b/test/fixtures/experimental/class-private-properties/asi-failure-computed/options.json new file mode 100644 index 0000000000..31d89fa474 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-failure-computed/options.json @@ -0,0 +1,7 @@ +{ + "throws": "Unexpected token, expected ; (3:10)", + "plugins": [ + "classProperties", + "classPrivateProperties" + ] +} diff --git a/test/fixtures/experimental/class-private-properties/asi-failure-generator/actual.js b/test/fixtures/experimental/class-private-properties/asi-failure-generator/actual.js new file mode 100644 index 0000000000..5fa9418229 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-failure-generator/actual.js @@ -0,0 +1,4 @@ +class Foo { + #p = x + *#m () {} +} diff --git a/test/fixtures/experimental/class-private-properties/asi-failure-generator/options.json b/test/fixtures/experimental/class-private-properties/asi-failure-generator/options.json new file mode 100644 index 0000000000..0541546800 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-failure-generator/options.json @@ -0,0 +1,4 @@ +{ + "throws": "Unexpected token, expected ; (3:9)", + "plugins": ["classProperties", "classPrivateProperties"] +} diff --git a/test/fixtures/experimental/class-private-properties/asi-failure-inline/actual.js b/test/fixtures/experimental/class-private-properties/asi-failure-inline/actual.js new file mode 100644 index 0000000000..2b7855c1cb --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-failure-inline/actual.js @@ -0,0 +1,3 @@ +class Foo { + #x #y +} diff --git a/test/fixtures/experimental/class-private-properties/asi-failure-inline/options.json b/test/fixtures/experimental/class-private-properties/asi-failure-inline/options.json new file mode 100644 index 0000000000..123e8aa507 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-failure-inline/options.json @@ -0,0 +1,4 @@ +{ + "throws": "Unexpected token, expected ; (2:5)", + "plugins": ["classProperties", "classPrivateProperties"] +} diff --git a/test/fixtures/experimental/class-private-properties/asi-success/actual.js b/test/fixtures/experimental/class-private-properties/asi-success/actual.js new file mode 100644 index 0000000000..ac2b521a15 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-success/actual.js @@ -0,0 +1,4 @@ +class Foo { + #x + #y +} diff --git a/test/fixtures/experimental/class-private-properties/asi-success/expected.json b/test/fixtures/experimental/class-private-properties/asi-success/expected.json new file mode 100644 index 0000000000..8aa723ae03 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-success/expected.json @@ -0,0 +1,152 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "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": 23, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 14, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "computed": false, + "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": "ClassPrivateProperty", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 4 + }, + "identifierName": "y" + }, + "name": "y" + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} diff --git a/test/fixtures/experimental/class-private-properties/asi-success/options.json b/test/fixtures/experimental/class-private-properties/asi-success/options.json new file mode 100644 index 0000000000..19c38d2996 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/asi-success/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties", "classPrivateProperties"] +} diff --git a/test/fixtures/experimental/class-private-properties/inline/actual.js b/test/fixtures/experimental/class-private-properties/inline/actual.js new file mode 100644 index 0000000000..da82b9a9c5 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/inline/actual.js @@ -0,0 +1,3 @@ +class A { #x; #y; } + +class B { #x = 0; #y = 1; } diff --git a/test/fixtures/experimental/class-private-properties/inline/expected.json b/test/fixtures/experimental/class-private-properties/inline/expected.json new file mode 100644 index 0000000000..abb56c7d32 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/inline/expected.json @@ -0,0 +1,308 @@ +{ + "type": "File", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 27 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 27 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "y" + }, + "name": "y" + }, + "value": null + } + ] + } + }, + { + "type": "ClassDeclaration", + "start": 21, + "end": 48, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + }, + "identifierName": "B" + }, + "name": "B" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 29, + "end": 48, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 27 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 31, + "end": 38, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": { + "type": "NumericLiteral", + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "ClassPrivateProperty", + "start": 39, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "y" + }, + "name": "y" + }, + "value": { + "type": "NumericLiteral", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 23 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/test/fixtures/experimental/class-private-properties/inline/options.json b/test/fixtures/experimental/class-private-properties/inline/options.json new file mode 100644 index 0000000000..19c38d2996 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/inline/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties", "classPrivateProperties"] +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success.1/actual.js b/test/fixtures/experimental/class-private-properties/pbn-success.1/actual.js new file mode 100644 index 0000000000..457eaabf1a --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/pbn-success.1/actual.js @@ -0,0 +1,19 @@ +class Point { + #x; + #y; + + constructor(x = 0, y = 0) { + #x = +x; + #y = +y; + } + + get x() { return this.#x } + set x(value) { this.#x = +value } + + get y() { return this.#y } + set y(value) { this.#y = +value } + + equals(p) { return this.#x === p.#x && this.#y === p.#y } + + toString() { return `Point<${ this.#x },${ this.#y }>` } +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success.1/expected.json b/test/fixtures/experimental/class-private-properties/pbn-success.1/expected.json new file mode 100644 index 0000000000..b462f3ed7a --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/pbn-success.1/expected.json @@ -0,0 +1,1874 @@ +{ + "type": "File", + "start": 0, + "end": 369, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 369, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 369, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "Point" + }, + "name": "Point" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 12, + "end": 369, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + }, + "identifierName": "y" + }, + "name": "y" + }, + "value": null + }, + { + "type": "ClassMethod", + "start": 35, + "end": 102, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 8, + "column": 5 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 35, + "end": 46, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "AssignmentPattern", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "left": { + "type": "Identifier", + "start": 47, + "end": 48, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + }, + "right": { + "type": "NumericLiteral", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 5, + "column": 20 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "AssignmentPattern", + "start": 54, + "end": 59, + "loc": { + "start": { + "line": 5, + "column": 23 + }, + "end": { + "line": 5, + "column": 28 + } + }, + "left": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 5, + "column": 23 + }, + "end": { + "line": 5, + "column": 24 + }, + "identifierName": "y" + }, + "name": "y" + }, + "right": { + "type": "NumericLiteral", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 5, + "column": 27 + }, + "end": { + "line": 5, + "column": 28 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "body": { + "type": "BlockStatement", + "start": 61, + "end": 102, + "loc": { + "start": { + "line": 5, + "column": 30 + }, + "end": { + "line": 8, + "column": 5 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 71, + "end": 79, + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 16 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 71, + "end": 78, + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 15 + } + }, + "operator": "=", + "left": { + "type": "PrivateName", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 10 + } + }, + "name": { + "type": "Identifier", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "right": { + "type": "UnaryExpression", + "start": 76, + "end": 78, + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 15 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 6, + "column": 14 + }, + "end": { + "line": 6, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + }, + { + "type": "ExpressionStatement", + "start": 88, + "end": 96, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 16 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 88, + "end": 95, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 15 + } + }, + "operator": "=", + "left": { + "type": "PrivateName", + "start": 89, + "end": 90, + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 7, + "column": 10 + } + }, + "name": { + "type": "Identifier", + "start": 89, + "end": 90, + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 7, + "column": 10 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "right": { + "type": "UnaryExpression", + "start": 93, + "end": 95, + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 15 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 94, + "end": 95, + "loc": { + "start": { + "line": 7, + "column": 14 + }, + "end": { + "line": 7, + "column": 15 + }, + "identifierName": "y" + }, + "name": "y" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 108, + "end": 134, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 30 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 112, + "end": 113, + "loc": { + "start": { + "line": 10, + "column": 8 + }, + "end": { + "line": 10, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 116, + "end": 134, + "loc": { + "start": { + "line": 10, + "column": 12 + }, + "end": { + "line": 10, + "column": 30 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 118, + "end": 132, + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 28 + } + }, + "argument": { + "type": "MemberExpression", + "start": 125, + "end": 132, + "loc": { + "start": { + "line": 10, + "column": 21 + }, + "end": { + "line": 10, + "column": 28 + } + }, + "object": { + "type": "ThisExpression", + "start": 125, + "end": 129, + "loc": { + "start": { + "line": 10, + "column": 21 + }, + "end": { + "line": 10, + "column": 25 + } + } + }, + "property": { + "type": "PrivateName", + "start": 131, + "end": 132, + "loc": { + "start": { + "line": 10, + "column": 27 + }, + "end": { + "line": 10, + "column": 28 + } + }, + "name": { + "type": "Identifier", + "start": 131, + "end": 132, + "loc": { + "start": { + "line": 10, + "column": 27 + }, + "end": { + "line": 10, + "column": 28 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "computed": false + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 139, + "end": 172, + "loc": { + "start": { + "line": 11, + "column": 4 + }, + "end": { + "line": 11, + "column": 37 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 143, + "end": 144, + "loc": { + "start": { + "line": 11, + "column": 8 + }, + "end": { + "line": 11, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x" + }, + "kind": "set", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 145, + "end": 150, + "loc": { + "start": { + "line": 11, + "column": 10 + }, + "end": { + "line": 11, + "column": 15 + }, + "identifierName": "value" + }, + "name": "value" + } + ], + "body": { + "type": "BlockStatement", + "start": 152, + "end": 172, + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 37 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 154, + "end": 170, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 35 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 154, + "end": 170, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 35 + } + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 154, + "end": 161, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 26 + } + }, + "object": { + "type": "ThisExpression", + "start": 154, + "end": 158, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 23 + } + } + }, + "property": { + "type": "PrivateName", + "start": 160, + "end": 161, + "loc": { + "start": { + "line": 11, + "column": 25 + }, + "end": { + "line": 11, + "column": 26 + } + }, + "name": { + "type": "Identifier", + "start": 160, + "end": 161, + "loc": { + "start": { + "line": 11, + "column": 25 + }, + "end": { + "line": 11, + "column": 26 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "computed": false + }, + "right": { + "type": "UnaryExpression", + "start": 164, + "end": 170, + "loc": { + "start": { + "line": 11, + "column": 29 + }, + "end": { + "line": 11, + "column": 35 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 165, + "end": 170, + "loc": { + "start": { + "line": 11, + "column": 30 + }, + "end": { + "line": 11, + "column": 35 + }, + "identifierName": "value" + }, + "name": "value" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 178, + "end": 204, + "loc": { + "start": { + "line": 13, + "column": 4 + }, + "end": { + "line": 13, + "column": 30 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 182, + "end": 183, + "loc": { + "start": { + "line": 13, + "column": 8 + }, + "end": { + "line": 13, + "column": 9 + }, + "identifierName": "y" + }, + "name": "y" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 186, + "end": 204, + "loc": { + "start": { + "line": 13, + "column": 12 + }, + "end": { + "line": 13, + "column": 30 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 188, + "end": 202, + "loc": { + "start": { + "line": 13, + "column": 14 + }, + "end": { + "line": 13, + "column": 28 + } + }, + "argument": { + "type": "MemberExpression", + "start": 195, + "end": 202, + "loc": { + "start": { + "line": 13, + "column": 21 + }, + "end": { + "line": 13, + "column": 28 + } + }, + "object": { + "type": "ThisExpression", + "start": 195, + "end": 199, + "loc": { + "start": { + "line": 13, + "column": 21 + }, + "end": { + "line": 13, + "column": 25 + } + } + }, + "property": { + "type": "PrivateName", + "start": 201, + "end": 202, + "loc": { + "start": { + "line": 13, + "column": 27 + }, + "end": { + "line": 13, + "column": 28 + } + }, + "name": { + "type": "Identifier", + "start": 201, + "end": 202, + "loc": { + "start": { + "line": 13, + "column": 27 + }, + "end": { + "line": 13, + "column": 28 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "computed": false + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 209, + "end": 242, + "loc": { + "start": { + "line": 14, + "column": 4 + }, + "end": { + "line": 14, + "column": 37 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 213, + "end": 214, + "loc": { + "start": { + "line": 14, + "column": 8 + }, + "end": { + "line": 14, + "column": 9 + }, + "identifierName": "y" + }, + "name": "y" + }, + "kind": "set", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 215, + "end": 220, + "loc": { + "start": { + "line": 14, + "column": 10 + }, + "end": { + "line": 14, + "column": 15 + }, + "identifierName": "value" + }, + "name": "value" + } + ], + "body": { + "type": "BlockStatement", + "start": 222, + "end": 242, + "loc": { + "start": { + "line": 14, + "column": 17 + }, + "end": { + "line": 14, + "column": 37 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 224, + "end": 240, + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 35 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 224, + "end": 240, + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 35 + } + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 224, + "end": 231, + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 26 + } + }, + "object": { + "type": "ThisExpression", + "start": 224, + "end": 228, + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 23 + } + } + }, + "property": { + "type": "PrivateName", + "start": 230, + "end": 231, + "loc": { + "start": { + "line": 14, + "column": 25 + }, + "end": { + "line": 14, + "column": 26 + } + }, + "name": { + "type": "Identifier", + "start": 230, + "end": 231, + "loc": { + "start": { + "line": 14, + "column": 25 + }, + "end": { + "line": 14, + "column": 26 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "computed": false + }, + "right": { + "type": "UnaryExpression", + "start": 234, + "end": 240, + "loc": { + "start": { + "line": 14, + "column": 29 + }, + "end": { + "line": 14, + "column": 35 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 235, + "end": 240, + "loc": { + "start": { + "line": 14, + "column": 30 + }, + "end": { + "line": 14, + "column": 35 + }, + "identifierName": "value" + }, + "name": "value" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 248, + "end": 305, + "loc": { + "start": { + "line": 16, + "column": 4 + }, + "end": { + "line": 16, + "column": 61 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 248, + "end": 254, + "loc": { + "start": { + "line": 16, + "column": 4 + }, + "end": { + "line": 16, + "column": 10 + }, + "identifierName": "equals" + }, + "name": "equals" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 255, + "end": 256, + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 12 + }, + "identifierName": "p" + }, + "name": "p" + } + ], + "body": { + "type": "BlockStatement", + "start": 258, + "end": 305, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 61 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 260, + "end": 303, + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 59 + } + }, + "argument": { + "type": "LogicalExpression", + "start": 267, + "end": 303, + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 59 + } + }, + "left": { + "type": "BinaryExpression", + "start": 267, + "end": 283, + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 39 + } + }, + "left": { + "type": "MemberExpression", + "start": 267, + "end": 274, + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 30 + } + }, + "object": { + "type": "ThisExpression", + "start": 267, + "end": 271, + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 27 + } + } + }, + "property": { + "type": "PrivateName", + "start": 273, + "end": 274, + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 30 + } + }, + "name": { + "type": "Identifier", + "start": 273, + "end": 274, + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 30 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "computed": false + }, + "operator": "===", + "right": { + "type": "MemberExpression", + "start": 279, + "end": 283, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 39 + } + }, + "object": { + "type": "Identifier", + "start": 279, + "end": 280, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 36 + }, + "identifierName": "p" + }, + "name": "p" + }, + "property": { + "type": "PrivateName", + "start": 282, + "end": 283, + "loc": { + "start": { + "line": 16, + "column": 38 + }, + "end": { + "line": 16, + "column": 39 + } + }, + "name": { + "type": "Identifier", + "start": 282, + "end": 283, + "loc": { + "start": { + "line": 16, + "column": 38 + }, + "end": { + "line": 16, + "column": 39 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "computed": false + } + }, + "operator": "&&", + "right": { + "type": "BinaryExpression", + "start": 287, + "end": 303, + "loc": { + "start": { + "line": 16, + "column": 43 + }, + "end": { + "line": 16, + "column": 59 + } + }, + "left": { + "type": "MemberExpression", + "start": 287, + "end": 294, + "loc": { + "start": { + "line": 16, + "column": 43 + }, + "end": { + "line": 16, + "column": 50 + } + }, + "object": { + "type": "ThisExpression", + "start": 287, + "end": 291, + "loc": { + "start": { + "line": 16, + "column": 43 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + "property": { + "type": "PrivateName", + "start": 293, + "end": 294, + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 50 + } + }, + "name": { + "type": "Identifier", + "start": 293, + "end": 294, + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 50 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "computed": false + }, + "operator": "===", + "right": { + "type": "MemberExpression", + "start": 299, + "end": 303, + "loc": { + "start": { + "line": 16, + "column": 55 + }, + "end": { + "line": 16, + "column": 59 + } + }, + "object": { + "type": "Identifier", + "start": 299, + "end": 300, + "loc": { + "start": { + "line": 16, + "column": 55 + }, + "end": { + "line": 16, + "column": 56 + }, + "identifierName": "p" + }, + "name": "p" + }, + "property": { + "type": "PrivateName", + "start": 302, + "end": 303, + "loc": { + "start": { + "line": 16, + "column": 58 + }, + "end": { + "line": 16, + "column": 59 + } + }, + "name": { + "type": "Identifier", + "start": 302, + "end": 303, + "loc": { + "start": { + "line": 16, + "column": 58 + }, + "end": { + "line": 16, + "column": 59 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "computed": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 311, + "end": 367, + "loc": { + "start": { + "line": 18, + "column": 4 + }, + "end": { + "line": 18, + "column": 60 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 311, + "end": 319, + "loc": { + "start": { + "line": 18, + "column": 4 + }, + "end": { + "line": 18, + "column": 12 + }, + "identifierName": "toString" + }, + "name": "toString" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 322, + "end": 367, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 60 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 324, + "end": 365, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 58 + } + }, + "argument": { + "type": "TemplateLiteral", + "start": 331, + "end": 365, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 58 + } + }, + "expressions": [ + { + "type": "MemberExpression", + "start": 341, + "end": 348, + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 18, + "column": 41 + } + }, + "object": { + "type": "ThisExpression", + "start": 341, + "end": 345, + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "property": { + "type": "PrivateName", + "start": 347, + "end": 348, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 41 + } + }, + "name": { + "type": "Identifier", + "start": 347, + "end": 348, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 41 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "computed": false + }, + { + "type": "MemberExpression", + "start": 354, + "end": 361, + "loc": { + "start": { + "line": 18, + "column": 47 + }, + "end": { + "line": 18, + "column": 54 + } + }, + "object": { + "type": "ThisExpression", + "start": 354, + "end": 358, + "loc": { + "start": { + "line": 18, + "column": 47 + }, + "end": { + "line": 18, + "column": 51 + } + } + }, + "property": { + "type": "PrivateName", + "start": 360, + "end": 361, + "loc": { + "start": { + "line": 18, + "column": 53 + }, + "end": { + "line": 18, + "column": 54 + } + }, + "name": { + "type": "Identifier", + "start": 360, + "end": 361, + "loc": { + "start": { + "line": 18, + "column": 53 + }, + "end": { + "line": 18, + "column": 54 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "computed": false + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start": 332, + "end": 338, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 31 + } + }, + "value": { + "raw": "Point<", + "cooked": "Point<" + }, + "tail": false + }, + { + "type": "TemplateElement", + "start": 350, + "end": 351, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 44 + } + }, + "value": { + "raw": ",", + "cooked": "," + }, + "tail": false + }, + { + "type": "TemplateElement", + "start": 363, + "end": 364, + "loc": { + "start": { + "line": 18, + "column": 56 + }, + "end": { + "line": 18, + "column": 57 + } + }, + "value": { + "raw": ">", + "cooked": ">" + }, + "tail": true + } + ] + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success.1/options.json b/test/fixtures/experimental/class-private-properties/pbn-success.1/options.json new file mode 100644 index 0000000000..19c38d2996 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/pbn-success.1/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties", "classPrivateProperties"] +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success/actual.js b/test/fixtures/experimental/class-private-properties/pbn-success/actual.js new file mode 100644 index 0000000000..9c17fc00e8 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/pbn-success/actual.js @@ -0,0 +1,19 @@ +class Point { + #x; + #y; + + 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 }>` } +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success/expected.json b/test/fixtures/experimental/class-private-properties/pbn-success/expected.json new file mode 100644 index 0000000000..837f95fee8 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/pbn-success/expected.json @@ -0,0 +1,1626 @@ +{ + "type": "File", + "start": 0, + "end": 329, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 329, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 329, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "Point" + }, + "name": "Point" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 12, + "end": 329, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 19, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + }, + "identifierName": "y" + }, + "name": "y" + }, + "value": null + }, + { + "type": "ClassMethod", + "start": 35, + "end": 102, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 8, + "column": 5 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 35, + "end": 46, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "AssignmentPattern", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "left": { + "type": "Identifier", + "start": 47, + "end": 48, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + }, + "right": { + "type": "NumericLiteral", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 5, + "column": 20 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "AssignmentPattern", + "start": 54, + "end": 59, + "loc": { + "start": { + "line": 5, + "column": 23 + }, + "end": { + "line": 5, + "column": 28 + } + }, + "left": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 5, + "column": 23 + }, + "end": { + "line": 5, + "column": 24 + }, + "identifierName": "y" + }, + "name": "y" + }, + "right": { + "type": "NumericLiteral", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 5, + "column": 27 + }, + "end": { + "line": 5, + "column": 28 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "body": { + "type": "BlockStatement", + "start": 61, + "end": 102, + "loc": { + "start": { + "line": 5, + "column": 30 + }, + "end": { + "line": 8, + "column": 5 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 71, + "end": 79, + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 16 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 71, + "end": 78, + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 15 + } + }, + "operator": "=", + "left": { + "type": "PrivateName", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 10 + } + }, + "name": { + "type": "Identifier", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "right": { + "type": "UnaryExpression", + "start": 76, + "end": 78, + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 15 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 6, + "column": 14 + }, + "end": { + "line": 6, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + }, + { + "type": "ExpressionStatement", + "start": 88, + "end": 96, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 16 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 88, + "end": 95, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 15 + } + }, + "operator": "=", + "left": { + "type": "PrivateName", + "start": 89, + "end": 90, + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 7, + "column": 10 + } + }, + "name": { + "type": "Identifier", + "start": 89, + "end": 90, + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 7, + "column": 10 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "right": { + "type": "UnaryExpression", + "start": 93, + "end": 95, + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 15 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 94, + "end": 95, + "loc": { + "start": { + "line": 7, + "column": 14 + }, + "end": { + "line": 7, + "column": 15 + }, + "identifierName": "y" + }, + "name": "y" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 108, + "end": 129, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 112, + "end": 113, + "loc": { + "start": { + "line": 10, + "column": 8 + }, + "end": { + "line": 10, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 116, + "end": 129, + "loc": { + "start": { + "line": 10, + "column": 12 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 118, + "end": 127, + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 23 + } + }, + "argument": { + "type": "PrivateName", + "start": 126, + "end": 127, + "loc": { + "start": { + "line": 10, + "column": 22 + }, + "end": { + "line": 10, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 126, + "end": 127, + "loc": { + "start": { + "line": 10, + "column": 22 + }, + "end": { + "line": 10, + "column": 23 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 134, + "end": 162, + "loc": { + "start": { + "line": 11, + "column": 4 + }, + "end": { + "line": 11, + "column": 32 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 138, + "end": 139, + "loc": { + "start": { + "line": 11, + "column": 8 + }, + "end": { + "line": 11, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x" + }, + "kind": "set", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 140, + "end": 145, + "loc": { + "start": { + "line": 11, + "column": 10 + }, + "end": { + "line": 11, + "column": 15 + }, + "identifierName": "value" + }, + "name": "value" + } + ], + "body": { + "type": "BlockStatement", + "start": 147, + "end": 162, + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 32 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 149, + "end": 160, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 30 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 149, + "end": 160, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 30 + } + }, + "operator": "=", + "left": { + "type": "PrivateName", + "start": 150, + "end": 151, + "loc": { + "start": { + "line": 11, + "column": 20 + }, + "end": { + "line": 11, + "column": 21 + } + }, + "name": { + "type": "Identifier", + "start": 150, + "end": 151, + "loc": { + "start": { + "line": 11, + "column": 20 + }, + "end": { + "line": 11, + "column": 21 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "right": { + "type": "UnaryExpression", + "start": 154, + "end": 160, + "loc": { + "start": { + "line": 11, + "column": 24 + }, + "end": { + "line": 11, + "column": 30 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 155, + "end": 160, + "loc": { + "start": { + "line": 11, + "column": 25 + }, + "end": { + "line": 11, + "column": 30 + }, + "identifierName": "value" + }, + "name": "value" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 168, + "end": 189, + "loc": { + "start": { + "line": 13, + "column": 4 + }, + "end": { + "line": 13, + "column": 25 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 172, + "end": 173, + "loc": { + "start": { + "line": 13, + "column": 8 + }, + "end": { + "line": 13, + "column": 9 + }, + "identifierName": "y" + }, + "name": "y" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 176, + "end": 189, + "loc": { + "start": { + "line": 13, + "column": 12 + }, + "end": { + "line": 13, + "column": 25 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 178, + "end": 187, + "loc": { + "start": { + "line": 13, + "column": 14 + }, + "end": { + "line": 13, + "column": 23 + } + }, + "argument": { + "type": "PrivateName", + "start": 186, + "end": 187, + "loc": { + "start": { + "line": 13, + "column": 22 + }, + "end": { + "line": 13, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 186, + "end": 187, + "loc": { + "start": { + "line": 13, + "column": 22 + }, + "end": { + "line": 13, + "column": 23 + }, + "identifierName": "y" + }, + "name": "y" + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 194, + "end": 222, + "loc": { + "start": { + "line": 14, + "column": 4 + }, + "end": { + "line": 14, + "column": 32 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 198, + "end": 199, + "loc": { + "start": { + "line": 14, + "column": 8 + }, + "end": { + "line": 14, + "column": 9 + }, + "identifierName": "y" + }, + "name": "y" + }, + "kind": "set", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 200, + "end": 205, + "loc": { + "start": { + "line": 14, + "column": 10 + }, + "end": { + "line": 14, + "column": 15 + }, + "identifierName": "value" + }, + "name": "value" + } + ], + "body": { + "type": "BlockStatement", + "start": 207, + "end": 222, + "loc": { + "start": { + "line": 14, + "column": 17 + }, + "end": { + "line": 14, + "column": 32 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 209, + "end": 220, + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 30 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 209, + "end": 220, + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 30 + } + }, + "operator": "=", + "left": { + "type": "PrivateName", + "start": 210, + "end": 211, + "loc": { + "start": { + "line": 14, + "column": 20 + }, + "end": { + "line": 14, + "column": 21 + } + }, + "name": { + "type": "Identifier", + "start": 210, + "end": 211, + "loc": { + "start": { + "line": 14, + "column": 20 + }, + "end": { + "line": 14, + "column": 21 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "right": { + "type": "UnaryExpression", + "start": 214, + "end": 220, + "loc": { + "start": { + "line": 14, + "column": 24 + }, + "end": { + "line": 14, + "column": 30 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 215, + "end": 220, + "loc": { + "start": { + "line": 14, + "column": 25 + }, + "end": { + "line": 14, + "column": 30 + }, + "identifierName": "value" + }, + "name": "value" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 228, + "end": 275, + "loc": { + "start": { + "line": 16, + "column": 4 + }, + "end": { + "line": 16, + "column": 51 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 228, + "end": 234, + "loc": { + "start": { + "line": 16, + "column": 4 + }, + "end": { + "line": 16, + "column": 10 + }, + "identifierName": "equals" + }, + "name": "equals" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 235, + "end": 236, + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 12 + }, + "identifierName": "p" + }, + "name": "p" + } + ], + "body": { + "type": "BlockStatement", + "start": 238, + "end": 275, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 51 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 240, + "end": 273, + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 49 + } + }, + "argument": { + "type": "LogicalExpression", + "start": 247, + "end": 273, + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 49 + } + }, + "left": { + "type": "BinaryExpression", + "start": 247, + "end": 258, + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 34 + } + }, + "left": { + "type": "PrivateName", + "start": 248, + "end": 249, + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 25 + } + }, + "name": { + "type": "Identifier", + "start": 248, + "end": 249, + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 25 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "operator": "===", + "right": { + "type": "MemberExpression", + "start": 254, + "end": 258, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 34 + } + }, + "object": { + "type": "Identifier", + "start": 254, + "end": 255, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 31 + }, + "identifierName": "p" + }, + "name": "p" + }, + "property": { + "type": "PrivateName", + "start": 257, + "end": 258, + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 16, + "column": 34 + } + }, + "name": { + "type": "Identifier", + "start": 257, + "end": 258, + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 16, + "column": 34 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "computed": false + } + }, + "operator": "&&", + "right": { + "type": "BinaryExpression", + "start": 262, + "end": 273, + "loc": { + "start": { + "line": 16, + "column": 38 + }, + "end": { + "line": 16, + "column": 49 + } + }, + "left": { + "type": "PrivateName", + "start": 263, + "end": 264, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 40 + } + }, + "name": { + "type": "Identifier", + "start": 263, + "end": 264, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 40 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "operator": "===", + "right": { + "type": "MemberExpression", + "start": 269, + "end": 273, + "loc": { + "start": { + "line": 16, + "column": 45 + }, + "end": { + "line": 16, + "column": 49 + } + }, + "object": { + "type": "Identifier", + "start": 269, + "end": 270, + "loc": { + "start": { + "line": 16, + "column": 45 + }, + "end": { + "line": 16, + "column": 46 + }, + "identifierName": "p" + }, + "name": "p" + }, + "property": { + "type": "PrivateName", + "start": 272, + "end": 273, + "loc": { + "start": { + "line": 16, + "column": 48 + }, + "end": { + "line": 16, + "column": 49 + } + }, + "name": { + "type": "Identifier", + "start": 272, + "end": 273, + "loc": { + "start": { + "line": 16, + "column": 48 + }, + "end": { + "line": 16, + "column": 49 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "computed": false + } + } + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 281, + "end": 327, + "loc": { + "start": { + "line": 18, + "column": 4 + }, + "end": { + "line": 18, + "column": 50 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 281, + "end": 289, + "loc": { + "start": { + "line": 18, + "column": 4 + }, + "end": { + "line": 18, + "column": 12 + }, + "identifierName": "toString" + }, + "name": "toString" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 292, + "end": 327, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 50 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 294, + "end": 325, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 48 + } + }, + "argument": { + "type": "TemplateLiteral", + "start": 301, + "end": 325, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 48 + } + }, + "expressions": [ + { + "type": "PrivateName", + "start": 312, + "end": 313, + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 36 + } + }, + "name": { + "type": "Identifier", + "start": 312, + "end": 313, + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 36 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + { + "type": "PrivateName", + "start": 320, + "end": 321, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 44 + } + }, + "name": { + "type": "Identifier", + "start": 320, + "end": 321, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 44 + }, + "identifierName": "y" + }, + "name": "y" + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start": 302, + "end": 308, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 31 + } + }, + "value": { + "raw": "Point<", + "cooked": "Point<" + }, + "tail": false + }, + { + "type": "TemplateElement", + "start": 315, + "end": 316, + "loc": { + "start": { + "line": 18, + "column": 38 + }, + "end": { + "line": 18, + "column": 39 + } + }, + "value": { + "raw": ",", + "cooked": "," + }, + "tail": false + }, + { + "type": "TemplateElement", + "start": 323, + "end": 324, + "loc": { + "start": { + "line": 18, + "column": 46 + }, + "end": { + "line": 18, + "column": 47 + } + }, + "value": { + "raw": ">", + "cooked": ">" + }, + "tail": true + } + ] + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success/options.json b/test/fixtures/experimental/class-private-properties/pbn-success/options.json new file mode 100644 index 0000000000..19c38d2996 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/pbn-success/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties", "classPrivateProperties"] +}