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

Fix TypeScript decorator support #5783

Merged
merged 3 commits into from
Nov 22, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/babel-preset-react-app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -172,6 +177,10 @@ module.exports = function(api, opts, env) {
require('babel-plugin-dynamic-import-node'),
].filter(Boolean),
overrides: [
isFlowEnabled && {
exclude: /\.tsx?$/,
plugins: [require('@babel/plugin-transform-flow-strip-types').default],
},
isTypeScriptEnabled && {
test: /\.tsx?$/,
plugins: [
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/typescript/src/App.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
13 changes: 13 additions & 0 deletions test/fixtures/typescript/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ type MyObject = Pick<MyType, 'bar' | 'baz'>;
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;