diff --git a/changelog_unreleased/typescript/11426.md b/changelog_unreleased/typescript/11426.md new file mode 100644 index 000000000..ea8be9cde --- /dev/null +++ b/changelog_unreleased/typescript/11426.md @@ -0,0 +1,45 @@ +#### Support TypeScript 4.4 (#11426 by @sosukesuzuki) + +Support [TypeScript 4.4](https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/)! + +##### [`static` Blocks in Classes](https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#static-blocks) + +Class Static Blocks syntax is a Stage 3 ECMAScript proposal. See https://github.com/tc39/proposal-class-static-block for more details. + + +```tsx +// Input +class Foo { + static count = 0; + + // This is a static block: + static { + if (someCondition()) { + Foo.count++; + } + } +} + +// Prettier stable +SyntaxError: Declaration expected. (5:9) + 3 | + 4 | // This is a static block: +> 5 | static { + | ^ + 6 | if (someCondition()) { + 7 | Foo.count++; + 8 | } + +// Prettier main +class Foo { + static count = 0; + + // This is a static block: + static { + if (someCondition()) { + Foo.count++; + } + } +} + +``` diff --git a/package.json b/package.json index 4e3b88b3f..cf1045d17 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@babel/parser": "7.15.3", "@glimmer/syntax": "0.80.0", "@iarna/toml": "2.2.5", - "@typescript-eslint/typescript-estree": "4.29.3", + "@typescript-eslint/typescript-estree": "4.30.0", "angular-estree-parser": "2.5.0", "angular-html-parser": "1.8.0", "camelcase": "6.2.0", diff --git a/tests/format/typescript/static-blocks/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/static-blocks/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..4b93a1db6 --- /dev/null +++ b/tests/format/typescript/static-blocks/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,116 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`multiple.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "babel-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Foo { + static prop = 1 + static { + console.log(Foo.prop++); + } + static { + console.log(Foo.prop++); + } + static { + console.log(Foo.prop++); + } +} + +=====================================output===================================== +class Foo { + static prop = 1; + static { + console.log(Foo.prop++); + } + static { + console.log(Foo.prop++); + } + static { + console.log(Foo.prop++); + } +} + +================================================================================ +`; + +exports[`nested.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "babel-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +function foo () { + return class { + static foo = 1; + static { + const c = class { + static bar = 2; + static { + // do + } + } + } + } +} + +=====================================output===================================== +function foo() { + return class { + static foo = 1; + static { + const c = class { + static bar = 2; + static { + // do + } + }; + } + }; +} + +================================================================================ +`; + +exports[`static-blocks.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "babel-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Foo { + static #count = 0; + + get count() { + return Foo.#count; + } + + static { + try { + const lastInstances = loadLastInstances(); + Foo.#count += lastInstances.length; + } + catch {} + } +} + +=====================================output===================================== +class Foo { + static #count = 0; + + get count() { + return Foo.#count; + } + + static { + try { + const lastInstances = loadLastInstances(); + Foo.#count += lastInstances.length; + } catch {} + } +} + +================================================================================ +`; diff --git a/tests/format/typescript/static-blocks/jsfmt.spec.js b/tests/format/typescript/static-blocks/jsfmt.spec.js new file mode 100644 index 000000000..aff59cd2f --- /dev/null +++ b/tests/format/typescript/static-blocks/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript", "babel-ts"]); diff --git a/tests/format/typescript/static-blocks/multiple.ts b/tests/format/typescript/static-blocks/multiple.ts new file mode 100644 index 000000000..ebd64568e --- /dev/null +++ b/tests/format/typescript/static-blocks/multiple.ts @@ -0,0 +1,12 @@ +class Foo { + static prop = 1 + static { + console.log(Foo.prop++); + } + static { + console.log(Foo.prop++); + } + static { + console.log(Foo.prop++); + } +} diff --git a/tests/format/typescript/static-blocks/nested.ts b/tests/format/typescript/static-blocks/nested.ts new file mode 100644 index 000000000..0b6ee3c44 --- /dev/null +++ b/tests/format/typescript/static-blocks/nested.ts @@ -0,0 +1,13 @@ +function foo () { + return class { + static foo = 1; + static { + const c = class { + static bar = 2; + static { + // do + } + } + } + } +} diff --git a/tests/format/typescript/static-blocks/static-blocks.ts b/tests/format/typescript/static-blocks/static-blocks.ts new file mode 100644 index 000000000..066b4a09c --- /dev/null +++ b/tests/format/typescript/static-blocks/static-blocks.ts @@ -0,0 +1,15 @@ +class Foo { + static #count = 0; + + get count() { + return Foo.#count; + } + + static { + try { + const lastInstances = loadLastInstances(); + Foo.#count += lastInstances.length; + } + catch {} + } +} diff --git a/yarn.lock b/yarn.lock index 56f5d920a..9f88d4fe7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1762,10 +1762,10 @@ resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.26.1.tgz#9e7c523f73c34b04a765e4167ca5650436ef1d38" integrity sha512-STyMPxR3cS+LaNvS8yK15rb8Y0iL0tFXq0uyl6gY45glyI7w0CsyqyEXl/Fa0JlQy+pVANeK3sbwPneCbWE7yg== -"@typescript-eslint/types@4.29.3": - version "4.29.3" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.29.3.tgz#d7980c49aef643d0af8954c9f14f656b7fd16017" - integrity sha512-s1eV1lKNgoIYLAl1JUba8NhULmf+jOmmeFO1G5MN/RBCyyzg4TIOfIOICVNC06lor+Xmy4FypIIhFiJXOknhIg== +"@typescript-eslint/types@4.30.0": + version "4.30.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.30.0.tgz#fb9d9b0358426f18687fba82eb0b0f869780204f" + integrity sha512-YKldqbNU9K4WpTNwBqtAerQKLLW/X2A/j4yw92e3ZJYLx+BpKLeheyzoPfzIXHfM8BXfoleTdiYwpsvVPvHrDw== "@typescript-eslint/typescript-estree@4.26.1": version "4.26.1" @@ -1780,13 +1780,13 @@ semver "^7.3.5" tsutils "^3.21.0" -"@typescript-eslint/typescript-estree@4.29.3": - version "4.29.3" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.29.3.tgz#1bafad610015c4ded35c85a70b6222faad598b40" - integrity sha512-45oQJA0bxna4O5TMwz55/TpgjX1YrAPOI/rb6kPgmdnemRZx/dB0rsx+Ku8jpDvqTxcE1C/qEbVHbS3h0hflag== +"@typescript-eslint/typescript-estree@4.30.0": + version "4.30.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.30.0.tgz#ae57833da72a753f4846cd3053758c771670c2ac" + integrity sha512-6WN7UFYvykr/U0Qgy4kz48iGPWILvYL34xXJxvDQeiRE018B7POspNRVtAZscWntEPZpFCx4hcz/XBT+erenfg== dependencies: - "@typescript-eslint/types" "4.29.3" - "@typescript-eslint/visitor-keys" "4.29.3" + "@typescript-eslint/types" "4.30.0" + "@typescript-eslint/visitor-keys" "4.30.0" debug "^4.3.1" globby "^11.0.3" is-glob "^4.0.1" @@ -1801,12 +1801,12 @@ "@typescript-eslint/types" "4.26.1" eslint-visitor-keys "^2.0.0" -"@typescript-eslint/visitor-keys@4.29.3": - version "4.29.3" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.29.3.tgz#c691760a00bd86bf8320d2a90a93d86d322f1abf" - integrity sha512-MGGfJvXT4asUTeVs0Q2m+sY63UsfnA+C/FDgBKV3itLBmM9H0u+URcneePtkd0at1YELmZK6HSolCqM4Fzs6yA== +"@typescript-eslint/visitor-keys@4.30.0": + version "4.30.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.30.0.tgz#a47c6272fc71b0c627d1691f68eaecf4ad71445e" + integrity sha512-pNaaxDt/Ol/+JZwzP7MqWc8PJQTUhZwoee/PVlQ+iYoYhagccvoHnC9e4l+C/krQYYkENxznhVSDwClIbZVxRw== dependencies: - "@typescript-eslint/types" "4.29.3" + "@typescript-eslint/types" "4.30.0" eslint-visitor-keys "^2.0.0" "@webassemblyjs/ast@1.11.1":