Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve keyword class property print #13997

Merged
merged 2 commits into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/language-js/print/statement.js
Expand Up @@ -176,8 +176,10 @@ const isClassProperty = ({ type }) =>
* @returns {boolean}
*/
function shouldPrintSemicolonAfterClassProperty(node, nextNode) {
const { name } = node.key;
const { type, name } = node.key;
if (
!node.computed &&
type === "Identifier" &&
(name === "static" ||
name === "get" ||
name === "set" ||
Expand Down
Expand Up @@ -61,6 +61,67 @@ class B {
================================================================================
`;

exports[`computed.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
class B {
[get];
foo() {}
}

class C {
[set];
foo(v) {}
}

=====================================output=====================================
class B {
[get]
foo() {}
}

class C {
[set]
foo(v) {}
}

================================================================================
`;

exports[`computed.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class B {
[get];
foo() {}
}

class C {
[set];
foo(v) {}
}

=====================================output=====================================
class B {
[get];
foo() {}
}

class C {
[set];
foo(v) {}
}

================================================================================
`;

exports[`get.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
Expand Down Expand Up @@ -122,6 +183,87 @@ class B {
================================================================================
`;

exports[`private.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
class A {
#static;
foo() {}
}

class B {
#get;
foo() {}
}

class C {
#set;
foo(v) {}
}

=====================================output=====================================
class A {
#static
foo() {}
}

class B {
#get
foo() {}
}

class C {
#set
foo(v) {}
}

================================================================================
`;

exports[`private.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class A {
#static;
foo() {}
}

class B {
#get;
foo() {}
}

class C {
#set;
foo(v) {}
}

=====================================output=====================================
class A {
#static;
foo() {}
}

class B {
#get;
foo() {}
}

class C {
#set;
foo(v) {}
}

================================================================================
`;

exports[`set.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
Expand Down
9 changes: 9 additions & 0 deletions tests/format/js/classes/keyword-property/computed.js
@@ -0,0 +1,9 @@
class B {
[get];
foo() {}
}

class C {
[set];
foo(v) {}
}
14 changes: 14 additions & 0 deletions tests/format/js/classes/keyword-property/private.js
@@ -0,0 +1,14 @@
class A {
#static;
foo() {}
}

class B {
#get;
foo() {}
}

class C {
#set;
foo(v) {}
}