Skip to content

Commit

Permalink
Typescript: always strip declare from class fields (#11747)
Browse files Browse the repository at this point in the history
* test: add failing test for decorated declare

* fix: always strip declare from class fields

* test: add decorated-declare-properties test
  • Loading branch information
jamescdavis committed Jul 30, 2020
1 parent 4c9929c commit aa82ab6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-typescript/src/index.js
Expand Up @@ -93,6 +93,7 @@ export default declare(
if (node.optional) node.optional = null;
if (node.typeAnnotation) node.typeAnnotation = null;
if (node.definite) node.definite = null;
if (node.declare) node.declare = null;
},
method({ node }) {
if (node.accessibility) node.accessibility = null;
Expand Down
@@ -1,3 +1,4 @@
class A {
declare x;
}
@foo declare y: string;
}
@@ -1,3 +1,6 @@
{
"plugins": [["transform-typescript", { "allowDeclareFields": true }]]
"plugins": [
["transform-typescript", { "allowDeclareFields": true }],
["syntax-decorators", { "legacy": true }]
]
}
@@ -1 +1,4 @@
class A {}
class A {
@foo
y;
}
@@ -0,0 +1,3 @@
class Foo {
@decorator declare bar: string;
}
@@ -0,0 +1,7 @@
{
"plugins": [
["transform-typescript", { "allowDeclareFields": true }],
["proposal-decorators", { "legacy": true }],
["proposal-class-properties"]
]
}
@@ -0,0 +1,21 @@
var _class, _descriptor, _temp;

function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); }

let Foo = (_class = (_temp = class Foo {
constructor() {
_initializerDefineProperty(this, "bar", _descriptor, this);
}

}, _temp), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "bar", [decorator], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
})), _class);

0 comments on commit aa82ab6

Please sign in to comment.