From e4240aeaf03807fdc95284ab1a2900915b53136f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 1 Dec 2022 17:03:37 +0800 Subject: [PATCH] Add tests for keyword class property (#13927) --- src/language-js/parse/postprocess/index.js | 20 + src/language-js/print/statement.js | 2 - .../__snapshots__/jsfmt.spec.js.snap | 509 ++++++++++++++++++ .../js/classes/keyword-property/async.js | 9 + .../format/js/classes/keyword-property/get.js | 9 + .../js/classes/keyword-property/jsfmt.spec.js | 6 + .../format/js/classes/keyword-property/set.js | 9 + .../classes/keyword-property/static-async.js | 9 + .../js/classes/keyword-property/static-get.js | 9 + .../js/classes/keyword-property/static-set.js | 9 + .../classes/keyword-property/static-static.js | 9 + .../js/classes/keyword-property/static.js | 9 + 12 files changed, 607 insertions(+), 2 deletions(-) create mode 100644 tests/format/js/classes/keyword-property/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/js/classes/keyword-property/async.js create mode 100644 tests/format/js/classes/keyword-property/get.js create mode 100644 tests/format/js/classes/keyword-property/jsfmt.spec.js create mode 100644 tests/format/js/classes/keyword-property/set.js create mode 100644 tests/format/js/classes/keyword-property/static-async.js create mode 100644 tests/format/js/classes/keyword-property/static-get.js create mode 100644 tests/format/js/classes/keyword-property/static-set.js create mode 100644 tests/format/js/classes/keyword-property/static-static.js create mode 100644 tests/format/js/classes/keyword-property/static.js diff --git a/src/language-js/parse/postprocess/index.js b/src/language-js/parse/postprocess/index.js index 62959af5b16e..d2005c4f1f34 100644 --- a/src/language-js/parse/postprocess/index.js +++ b/src/language-js/parse/postprocess/index.js @@ -153,6 +153,26 @@ function postprocess(ast, options) { } break; } + // TODO: Remove this when https://github.com/meriyah/meriyah/issues/231 get fixed + case "PropertyDefinition": + if ( + options.parser === "meriyah" && + node.static && + !node.computed && + !node.key + ) { + const name = "static"; + const start = locStart(node); + Object.assign(node, { + static: false, + key: { + type: "Identifier", + name, + range: [start, start + name.length], + }, + }); + } + break; } }); diff --git a/src/language-js/print/statement.js b/src/language-js/print/statement.js index da2ff299e387..69675d3c219e 100644 --- a/src/language-js/print/statement.js +++ b/src/language-js/print/statement.js @@ -174,8 +174,6 @@ const isClassProperty = ({ type }) => */ function shouldPrintSemicolonAfterClassProperty(node, nextNode) { const name = node.key && node.key.name; - // this isn't actually possible yet with most parsers available today - // so isn't properly tested yet. if ( (name === "static" || name === "get" || name === "set") && !node.value && diff --git a/tests/format/js/classes/keyword-property/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/classes/keyword-property/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..0c96b7a25cec --- /dev/null +++ b/tests/format/js/classes/keyword-property/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,509 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`async.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + async; + foo() {}; +} + +class B { + async = 1; + foo() {} +} + +=====================================output===================================== +class A { + async + foo() {} +} + +class B { + async = 1 + foo() {} +} + +================================================================================ +`; + +exports[`async.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + async; + foo() {}; +} + +class B { + async = 1; + foo() {} +} + +=====================================output===================================== +class A { + async; + foo() {} +} + +class B { + async = 1; + foo() {} +} + +================================================================================ +`; + +exports[`get.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + get; + foo() {} +} + +class B { + get = 1; + foo() {} +} + +=====================================output===================================== +class A { + get; + foo() {} +} + +class B { + get = 1 + foo() {} +} + +================================================================================ +`; + +exports[`get.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + get; + foo() {} +} + +class B { + get = 1; + foo() {} +} + +=====================================output===================================== +class A { + get; + foo() {} +} + +class B { + get = 1; + foo() {} +} + +================================================================================ +`; + +exports[`set.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + set; + foo(v) {} +} + +class B { + set = 1; + foo(v) {} +} + +=====================================output===================================== +class A { + set; + foo(v) {} +} + +class B { + set = 1 + foo(v) {} +} + +================================================================================ +`; + +exports[`set.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + set; + foo(v) {} +} + +class B { + set = 1; + foo(v) {} +} + +=====================================output===================================== +class A { + set; + foo(v) {} +} + +class B { + set = 1; + foo(v) {} +} + +================================================================================ +`; + +exports[`static.js [flow] format 1`] = ` +"Unexpected token \`;\`, expected an identifier (2:9) + 1 | class A { +> 2 | static; + | ^ + 3 | foo() {}; + 4 | } + 5 |" +`; + +exports[`static.js - {"semi":false} [flow] format 1`] = ` +"Unexpected token \`;\`, expected an identifier (2:9) + 1 | class A { +> 2 | static; + | ^ + 3 | foo() {}; + 4 | } + 5 |" +`; + +exports[`static.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + static; + foo() {}; +} + +class B { + static = 1; + foo() {}; +} + +=====================================output===================================== +class A { + static; + foo() {} +} + +class B { + static = 1 + foo() {} +} + +================================================================================ +`; + +exports[`static.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + static; + foo() {}; +} + +class B { + static = 1; + foo() {}; +} + +=====================================output===================================== +class A { + static; + foo() {} +} + +class B { + static = 1; + foo() {} +} + +================================================================================ +`; + +exports[`static-async.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + static async; + foo() {} +} + +class B { + static async = 1; + foo() {} +} + +=====================================output===================================== +class A { + static async + foo() {} +} + +class B { + static async = 1 + foo() {} +} + +================================================================================ +`; + +exports[`static-async.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + static async; + foo() {} +} + +class B { + static async = 1; + foo() {} +} + +=====================================output===================================== +class A { + static async; + foo() {} +} + +class B { + static async = 1; + foo() {} +} + +================================================================================ +`; + +exports[`static-get.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + static get; + foo() {} +} + +class B { + static get = 1; + foo() {} +} + +=====================================output===================================== +class A { + static get; + foo() {} +} + +class B { + static get = 1 + foo() {} +} + +================================================================================ +`; + +exports[`static-get.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + static get; + foo() {} +} + +class B { + static get = 1; + foo() {} +} + +=====================================output===================================== +class A { + static get; + foo() {} +} + +class B { + static get = 1; + foo() {} +} + +================================================================================ +`; + +exports[`static-set.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + static set; + foo(v) {} +} + +class B { + static set = 1; + foo(v) {} +} + +=====================================output===================================== +class A { + static set; + foo(v) {} +} + +class B { + static set = 1 + foo(v) {} +} + +================================================================================ +`; + +exports[`static-set.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + static set; + foo(v) {} +} + +class B { + static set = 1; + foo(v) {} +} + +=====================================output===================================== +class A { + static set; + foo(v) {} +} + +class B { + static set = 1; + foo(v) {} +} + +================================================================================ +`; + +exports[`static-static.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +class A { + static static; + foo() {} +} + +class B { + static static = 1; + foo() {} +} + +=====================================output===================================== +class A { + static static; + foo() {} +} + +class B { + static static = 1 + foo() {} +} + +================================================================================ +`; + +exports[`static-static.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + static static; + foo() {} +} + +class B { + static static = 1; + foo() {} +} + +=====================================output===================================== +class A { + static static; + foo() {} +} + +class B { + static static = 1; + foo() {} +} + +================================================================================ +`; diff --git a/tests/format/js/classes/keyword-property/async.js b/tests/format/js/classes/keyword-property/async.js new file mode 100644 index 000000000000..db7dbccccbc0 --- /dev/null +++ b/tests/format/js/classes/keyword-property/async.js @@ -0,0 +1,9 @@ +class A { + async; + foo() {}; +} + +class B { + async = 1; + foo() {} +} diff --git a/tests/format/js/classes/keyword-property/get.js b/tests/format/js/classes/keyword-property/get.js new file mode 100644 index 000000000000..63bf058f9b83 --- /dev/null +++ b/tests/format/js/classes/keyword-property/get.js @@ -0,0 +1,9 @@ +class A { + get; + foo() {} +} + +class B { + get = 1; + foo() {} +} diff --git a/tests/format/js/classes/keyword-property/jsfmt.spec.js b/tests/format/js/classes/keyword-property/jsfmt.spec.js new file mode 100644 index 000000000000..a0d2e24cda04 --- /dev/null +++ b/tests/format/js/classes/keyword-property/jsfmt.spec.js @@ -0,0 +1,6 @@ +const errors = { + flow: ["static.js"], +}; + +run_spec(__dirname, ["babel", "flow", "typescript"], { errors }); +run_spec(__dirname, ["babel", "flow", "typescript"], { errors, semi: false }); diff --git a/tests/format/js/classes/keyword-property/set.js b/tests/format/js/classes/keyword-property/set.js new file mode 100644 index 000000000000..45d6dbd75c84 --- /dev/null +++ b/tests/format/js/classes/keyword-property/set.js @@ -0,0 +1,9 @@ +class A { + set; + foo(v) {} +} + +class B { + set = 1; + foo(v) {} +} diff --git a/tests/format/js/classes/keyword-property/static-async.js b/tests/format/js/classes/keyword-property/static-async.js new file mode 100644 index 000000000000..ea3a7c2d88bb --- /dev/null +++ b/tests/format/js/classes/keyword-property/static-async.js @@ -0,0 +1,9 @@ +class A { + static async; + foo() {} +} + +class B { + static async = 1; + foo() {} +} diff --git a/tests/format/js/classes/keyword-property/static-get.js b/tests/format/js/classes/keyword-property/static-get.js new file mode 100644 index 000000000000..910e47daf610 --- /dev/null +++ b/tests/format/js/classes/keyword-property/static-get.js @@ -0,0 +1,9 @@ +class A { + static get; + foo() {} +} + +class B { + static get = 1; + foo() {} +} diff --git a/tests/format/js/classes/keyword-property/static-set.js b/tests/format/js/classes/keyword-property/static-set.js new file mode 100644 index 000000000000..3300eae1a2d5 --- /dev/null +++ b/tests/format/js/classes/keyword-property/static-set.js @@ -0,0 +1,9 @@ +class A { + static set; + foo(v) {} +} + +class B { + static set = 1; + foo(v) {} +} diff --git a/tests/format/js/classes/keyword-property/static-static.js b/tests/format/js/classes/keyword-property/static-static.js new file mode 100644 index 000000000000..406943ae7230 --- /dev/null +++ b/tests/format/js/classes/keyword-property/static-static.js @@ -0,0 +1,9 @@ +class A { + static static; + foo() {} +} + +class B { + static static = 1; + foo() {} +} diff --git a/tests/format/js/classes/keyword-property/static.js b/tests/format/js/classes/keyword-property/static.js new file mode 100644 index 000000000000..e2aca638eaab --- /dev/null +++ b/tests/format/js/classes/keyword-property/static.js @@ -0,0 +1,9 @@ +class A { + static; + foo() {}; +} + +class B { + static = 1; + foo() {}; +}