From a736c779ebbfa6b7dbb06b9768784af6ee06a5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mahieu?= Date: Mon, 16 Dec 2019 19:24:50 +0100 Subject: [PATCH] Add optional chaining and nullish coalescing operators support (#370) * Add optional chaining and nullish coalescing operators support Closes #369 * Update `prettier@1.19` to support TypeScript 3.7 Release note: https://prettier.io/blog/2019/11/09/1.19.0.html --- package.json | 4 +- src/babelPluginTsdx.ts | 4 ++ test/fixtures/build-default/src/index.ts | 3 ++ .../src/syntax/nullish-coalescing.ts | 5 +++ .../src/syntax/optional-chaining.ts | 3 ++ yarn.lock | 38 +++++++++++++++++-- 6 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/build-default/src/syntax/nullish-coalescing.ts create mode 100644 test/fixtures/build-default/src/syntax/optional-chaining.ts diff --git a/package.json b/package.json index d3e35c98a..17e517567 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ "@babel/core": "^7.4.4", "@babel/helper-module-imports": "^7.0.0", "@babel/plugin-proposal-class-properties": "^7.4.4", + "@babel/plugin-proposal-nullish-coalescing-operator": "^7.7.4", + "@babel/plugin-proposal-optional-chaining": "^7.7.5", "@babel/plugin-syntax-dynamic-import": "^7.2.0", "@babel/plugin-transform-regenerator": "^7.4.5", "@babel/plugin-transform-runtime": "^7.6.0", @@ -78,7 +80,7 @@ "mkdirp": "^0.5.1", "ora": "^3.4.0", "pascal-case": "^2.0.1", - "prettier": "^1.18.2", + "prettier": "^1.19.1", "progress-estimator": "^0.2.2", "rimraf": "^3.0.0", "rollup": "^1.27.8", diff --git a/src/babelPluginTsdx.ts b/src/babelPluginTsdx.ts index 249f29e8a..66145ebb5 100644 --- a/src/babelPluginTsdx.ts +++ b/src/babelPluginTsdx.ts @@ -86,6 +86,10 @@ export const babelPluginTsdx = babelPlugin.custom((babelCore: any) => ({ name: '@babel/plugin-proposal-class-properties', loose: true, }, + // Adds syntax support for optional chaining (.?) + { name: '@babel/plugin-proposal-optional-chaining' }, + // Adds syntax support for default value using ?? operator + { name: '@babel/plugin-proposal-nullish-coalescing-operator' }, { name: '@babel/plugin-transform-regenerator', async: false, diff --git a/test/fixtures/build-default/src/index.ts b/test/fixtures/build-default/src/index.ts index b23b6213f..1716e60ed 100644 --- a/test/fixtures/build-default/src/index.ts +++ b/test/fixtures/build-default/src/index.ts @@ -1,3 +1,6 @@ +import './syntax/nullish-coalescing'; +import './syntax/optional-chaining'; + export { foo } from './foo'; export const sum = (a: number, b: number) => { diff --git a/test/fixtures/build-default/src/syntax/nullish-coalescing.ts b/test/fixtures/build-default/src/syntax/nullish-coalescing.ts new file mode 100644 index 000000000..8ceb5505b --- /dev/null +++ b/test/fixtures/build-default/src/syntax/nullish-coalescing.ts @@ -0,0 +1,5 @@ +// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing + +const bar = () => {}; +const foo = false; +export const x = foo ?? bar(); diff --git a/test/fixtures/build-default/src/syntax/optional-chaining.ts b/test/fixtures/build-default/src/syntax/optional-chaining.ts new file mode 100644 index 000000000..6978989b4 --- /dev/null +++ b/test/fixtures/build-default/src/syntax/optional-chaining.ts @@ -0,0 +1,3 @@ +// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining + +export const foo = (foo?: { bar: string }) => foo?.bar || 'bar'; diff --git a/yarn.lock b/yarn.lock index 8eee7a19b..726ef864d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -262,6 +262,14 @@ "@babel/helper-plugin-utils" "^7.0.0" "@babel/plugin-syntax-json-strings" "^7.2.0" +"@babel/plugin-proposal-nullish-coalescing-operator@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-nullish-coalescing-operator/-/plugin-proposal-nullish-coalescing-operator-7.7.4.tgz#7db302c83bc30caa89e38fee935635ef6bd11c28" + integrity sha512-TbYHmr1Gl1UC7Vo2HVuj/Naci5BEGNZ0AJhzqD2Vpr6QPFWpUmBRLrIDjedzx7/CShq0bRDS2gI4FIs77VHLVQ== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-nullish-coalescing-operator" "^7.7.4" + "@babel/plugin-proposal-object-rest-spread@^7.6.2": version "7.6.2" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-object-rest-spread/-/plugin-proposal-object-rest-spread-7.6.2.tgz#8ffccc8f3a6545e9f78988b6bf4fe881b88e8096" @@ -278,6 +286,14 @@ "@babel/helper-plugin-utils" "^7.0.0" "@babel/plugin-syntax-optional-catch-binding" "^7.2.0" +"@babel/plugin-proposal-optional-chaining@^7.7.5": + version "7.7.5" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.7.5.tgz#f0835f044cef85b31071a924010a2a390add11d4" + integrity sha512-sOwFqT8JSchtJeDD+CjmWCaiFoLxY4Ps7NjvwHC/U7l4e9i5pTRNt8nDMIFSOUL+ncFbYSwruHM8WknYItWdXw== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-optional-chaining" "^7.7.4" + "@babel/plugin-proposal-unicode-property-regex@^7.6.2": version "7.6.2" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.6.2.tgz#05413762894f41bfe42b9a5e80919bd575dcc802" @@ -308,6 +324,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-syntax-nullish-coalescing-operator@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-nullish-coalescing-operator/-/plugin-syntax-nullish-coalescing-operator-7.7.4.tgz#e53b751d0c3061b1ba3089242524b65a7a9da12b" + integrity sha512-XKh/yIRPiQTOeBg0QJjEus5qiSKucKAiApNtO1psqG7D17xmE+X2i5ZqBEuSvo0HRuyPaKaSN/Gy+Ha9KFQolw== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-object-rest-spread@^7.0.0", "@babel/plugin-syntax-object-rest-spread@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.2.0.tgz#3b7a3e733510c57e820b9142a6579ac8b0dfad2e" @@ -322,6 +345,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-syntax-optional-chaining@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-chaining/-/plugin-syntax-optional-chaining-7.7.4.tgz#c91fdde6de85d2eb8906daea7b21944c3610c901" + integrity sha512-2MqYD5WjZSbJdUagnJvIdSfkb/ucOC9/1fRJxm7GAxY6YQLWlUvkfxoNbUPcPLHJyetKUDQ4+yyuUyAoc0HriA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-transform-arrow-functions@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.2.0.tgz#9aeafbe4d6ffc6563bf8f8372091628f00779550" @@ -4732,10 +4762,10 @@ prettier-linter-helpers@^1.0.0: dependencies: fast-diff "^1.1.2" -prettier@^1.18.2: - version "1.18.2" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" - integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== +prettier@^1.19.1: + version "1.19.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb" + integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew== pretty-format@^24.9.0: version "24.9.0"