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

[plugin-transform-typescript] Fix transpiling of TS abstract classes with decorators #9693

Conversation

@agoldis
Copy link
Contributor

agoldis commented Mar 15, 2019

Q                       A
Fixed Issues? #8172
Patch: Bug Fix? yes
Major: Breaking Change? no
Minor: New Feature? no
Tests Added + Pass? Added, not passing 😞
Documentation PR Link
Any Dependency Changes? No
License MIT

When ["@babel/plugin-proposal-decorators", { "legacy": true }] and @babel/preset-typescript plugins are in use, using decorators with or within Typescript abstract classes causes generation of wrong syntax and preserves non-JS compliant abstract specification for class.

E.g. of code:

export default abstract class SyncableEntity {
  @observable id: string;
}

becomes:

let SyncableEntity = (_class = (_temp = abstract class SyncableEntity {
    _initializerDefineProperty(this, "id", _descriptor, this);
    // ...
}

This PR resolves it this issue by removing abstract property from Class visitor of @babel/plugin-transform-typescript.

I am having a trouble with tests, though - used in test syntax is not accepted by parser:

var foo = abstract class Foo {}

I get BABEL_PARSE_ERROR. Please help!

@agoldis agoldis changed the title Fix abstract classed assignment to variables [plugin-transform-typescript] Fix abstract classed assignment to variables Mar 15, 2019
@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

nicolo-ribaudo commented Mar 16, 2019

I am having a trouble with tests, though - used in test syntax is not accepted by parser:

var foo = abstract class Foo {}

I get BABEL_PARSE_ERROR. Please help!

That throws because it isn't valid typescript 😛 You'll need to generate that code using a custom plugin in your test, similarly to what has been done in #9692


You can remove if (node.abstract) node.abstract = null; from the ClassDeclaration visitor, since it is handled by Class.

@agoldis

This comment has been minimized.

Copy link
Contributor Author

agoldis commented Mar 16, 2019

😀 so silly! Thanks @nicolo-ribaudo!

@agoldis agoldis force-pushed the agoldis:babel-plugin-transform-typescript-decorators-assignment branch from ad1b672 to 8eb0f6a Mar 17, 2019
@agoldis agoldis changed the title [plugin-transform-typescript] Fix abstract classed assignment to variables [plugin-transform-typescript] Fix transpiling of TS abstract classes with decorators Mar 17, 2019
Usage of "@babel/plugin-proposal-decorators" with legacy
support causes "abstract" keyword to be preserved.
This fixes it by moving "abstract" node property removal
from "ClassDeclaration" visitor to "Class". Resolves #8172
@agoldis agoldis force-pushed the agoldis:babel-plugin-transform-typescript-decorators-assignment branch from 8eb0f6a to d9d016e Mar 17, 2019
@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Mar 17, 2019

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10504/

@agoldis

This comment has been minimized.

Copy link
Contributor Author

agoldis commented Mar 17, 2019

@nicolo-ribaudo I've decided to not introduce a test with invalid TS syntax - instead I have added few tests with actual ["@babel/plugin-proposal-decorators", { "legacy": true }] usage, as already appears at https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-typescript/test/fixtures/imports/parameter-decorators

Copy link
Member

nicolo-ribaudo left a comment

Thanks!

@danez
danez approved these changes Mar 17, 2019
@nicolo-ribaudo nicolo-ribaudo merged commit efd2ca9 into babel:master Mar 17, 2019
5 checks passed
5 checks passed
babel/repl REPL preview is available
Details
buildsize Significant change of babel.js down by 1.14 KB (0.01%)
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 87.15% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@lock lock bot added the outdated label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.