diff --git a/src/fast-path.js b/src/fast-path.js index 61b74c72..eaeb288a 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -622,6 +622,7 @@ function isStatement(node) { node.type === "ClassDeclaration" || node.type === "ClassMethod" || node.type === "ClassProperty" || + node.type === "ClassPrivateProperty" || node.type === "ContinueStatement" || node.type === "DebuggerStatement" || node.type === "DeclareClass" || diff --git a/src/parser-babylon.js b/src/parser-babylon.js index 1469cec3..400366c4 100644 --- a/src/parser-babylon.js +++ b/src/parser-babylon.js @@ -25,7 +25,8 @@ function parse(text, parsers, opts) { "numericSeparator", "importMeta", "optionalCatchBinding", - "optionalChaining" + "optionalChaining", + "classPrivateProperties" ] }; diff --git a/src/printer.js b/src/printer.js index 497d9f23..a8c930ec 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1809,7 +1809,8 @@ function genericPrintNoParens(path, options, print, args) { "}" ]); case "ClassProperty": - case "TSAbstractClassProperty": { + case "TSAbstractClassProperty": + case "ClassPrivateProperty": { if (n.accessibility) { parts.push(n.accessibility + " "); } @@ -2725,6 +2726,9 @@ function genericPrintNoParens(path, options, print, args) { return printStatementSequence(bodyPath, options, print); }, "body"); + case "PrivateName": + return concat(["#", path.call(print, "id")]); + default: /* istanbul ignore next */ throw new Error("unknown type: " + JSON.stringify(n.type)); diff --git a/tests/classes_private_fields/__snapshots__/jsfmt.spec.js.snap b/tests/classes_private_fields/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..d97244f5 --- /dev/null +++ b/tests/classes_private_fields/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,175 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`private_fields.js 1`] = ` +class A { #x; #y; } +class B { #x = 0; #y = 1; } + +class C { + static #x; + static #y = 1; +} + +class D { + #x + #y +} + +class Point { + #x = 1; + #y = 2; + + constructor(x = 0, y = 0) { + this.#x = +x; + this.#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 }>\` } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + #x; + #y; +} +class B { + #x = 0; + #y = 1; +} + +class C { + static #x; + static #y = 1; +} + +class D { + #x; + #y; +} + +class Point { + #x = 1; + #y = 2; + + constructor(x = 0, y = 0) { + this.#x = +x; + this.#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}>\`; + } +} + +`; + +exports[`private_fields.js 2`] = ` +class A { #x; #y; } +class B { #x = 0; #y = 1; } + +class C { + static #x; + static #y = 1; +} + +class D { + #x + #y +} + +class Point { + #x = 1; + #y = 2; + + constructor(x = 0, y = 0) { + this.#x = +x; + this.#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 }>\` } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class A { + #x + #y +} +class B { + #x = 0 + #y = 1 +} + +class C { + static #x + static #y = 1 +} + +class D { + #x + #y +} + +class Point { + #x = 1 + #y = 2 + + constructor(x = 0, y = 0) { + this.#x = +x + this.#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/tests/classes_private_fields/jsfmt.spec.js b/tests/classes_private_fields/jsfmt.spec.js new file mode 100644 index 00000000..5bd38cc6 --- /dev/null +++ b/tests/classes_private_fields/jsfmt.spec.js @@ -0,0 +1,2 @@ +run_spec(__dirname, { parser: "babylon" }); +run_spec(__dirname, { semi: false, parser: "babylon" }); diff --git a/tests/classes_private_fields/private_fields.js b/tests/classes_private_fields/private_fields.js new file mode 100644 index 00000000..68ac1610 --- /dev/null +++ b/tests/classes_private_fields/private_fields.js @@ -0,0 +1,32 @@ +class A { #x; #y; } +class B { #x = 0; #y = 1; } + +class C { + static #x; + static #y = 1; +} + +class D { + #x + #y +} + +class Point { + #x = 1; + #y = 2; + + constructor(x = 0, y = 0) { + this.#x = +x; + this.#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 }>` } +}