From 4aa60a83a2884a3a67cfe0124b4ba7f80a365f5c Mon Sep 17 00:00:00 2001 From: Ian Schmitz Date: Mon, 12 Nov 2018 21:41:37 -0800 Subject: [PATCH 1/3] Fix TypeScript decorator support --- packages/babel-preset-react-app/create.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/babel-preset-react-app/create.js b/packages/babel-preset-react-app/create.js index 04b2b32a5c7..bbcc237ebd2 100644 --- a/packages/babel-preset-react-app/create.js +++ b/packages/babel-preset-react-app/create.js @@ -108,8 +108,13 @@ module.exports = function(api, opts, env) { // Strip flow types before any other transform, emulating the behavior // order as-if the browser supported all of the succeeding features // https://github.com/facebook/create-react-app/pull/5182 - isFlowEnabled && + // We will conditionally enable this plugin below in overrides as it clashes with + // @babel/plugin-proposal-decorators when using TypeScript. + // https://github.com/facebook/create-react-app/issues/5741 + isFlowEnabled && [ require('@babel/plugin-transform-flow-strip-types').default, + false, + ], // Experimental macros support. Will be documented after it's had some time // in the wild. require('babel-plugin-macros'), @@ -172,6 +177,10 @@ module.exports = function(api, opts, env) { require('babel-plugin-dynamic-import-node'), ].filter(Boolean), overrides: [ + isFlowEnabled && { + test: /\.(js|mjs|jsx)$/, + plugins: [require('@babel/plugin-transform-flow-strip-types').default], + }, isTypeScriptEnabled && { test: /\.tsx?$/, plugins: [ From fa85087ddf057a5aba0675f204d97712fa558a72 Mon Sep 17 00:00:00 2001 From: Ian Schmitz Date: Tue, 13 Nov 2018 20:48:43 -0800 Subject: [PATCH 2/3] Update babel flow override --- packages/babel-preset-react-app/create.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-preset-react-app/create.js b/packages/babel-preset-react-app/create.js index bbcc237ebd2..0c29c97d7ca 100644 --- a/packages/babel-preset-react-app/create.js +++ b/packages/babel-preset-react-app/create.js @@ -178,7 +178,7 @@ module.exports = function(api, opts, env) { ].filter(Boolean), overrides: [ isFlowEnabled && { - test: /\.(js|mjs|jsx)$/, + exclude: /\.tsx?$/, plugins: [require('@babel/plugin-transform-flow-strip-types').default], }, isTypeScriptEnabled && { From ae0da6bb2d75312bb779930031e2efbc7bfc9af9 Mon Sep 17 00:00:00 2001 From: Ian Schmitz Date: Tue, 20 Nov 2018 18:54:50 -0800 Subject: [PATCH 3/3] WIP --- test/fixtures/typescript/src/App.test.ts | 6 ++++++ test/fixtures/typescript/src/App.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/test/fixtures/typescript/src/App.test.ts b/test/fixtures/typescript/src/App.test.ts index cef45f281d3..fb0a9509592 100644 --- a/test/fixtures/typescript/src/App.test.ts +++ b/test/fixtures/typescript/src/App.test.ts @@ -6,3 +6,9 @@ it('reads a typescript file with no syntax error', () => { expect(App.foo.baz!.n).toBe(123); expect(app.n).toBe(123); }); + +it('supports decorators', () => { + const app = new App(); + expect((app as any).annotated).toBe(true); + expect(app.decorated).toBe(42); +}); diff --git a/test/fixtures/typescript/src/App.ts b/test/fixtures/typescript/src/App.ts index f3c31414c74..da5cc719a36 100644 --- a/test/fixtures/typescript/src/App.ts +++ b/test/fixtures/typescript/src/App.ts @@ -10,10 +10,23 @@ type MyObject = Pick; class App { static foo: MyObject = { bar: true, baz: { n: 123 } }; n = App.foo.baz!.n; + @propertyDecorator + decorated; } function annotation(target: any) { target.annotated = true; } +function propertyDecorator(target: any, key: string) { + if (delete target[key]) { + Object.defineProperty(target, key, { + get() { + return 42; + }, + enumerable: true, + }); + } +} + export default App;