Typescript: always strip declare from class fields (#11747)

* test: add failing test for decorated declare

* fix: always strip declare from class fields

* test: add decorated-declare-properties test
This commit is contained in:
James C. Davis 2020-07-30 14:06:45 -04:00 committed by GitHub
parent 4c9929ca28
commit aa82ab6358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 3 deletions

View File

@ -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;

View File

@ -1,3 +1,4 @@
class A {
declare x;
@foo declare y: string;
}

View File

@ -1,3 +1,6 @@
{
"plugins": [["transform-typescript", { "allowDeclareFields": true }]]
"plugins": [
["transform-typescript", { "allowDeclareFields": true }],
["syntax-decorators", { "legacy": true }]
]
}

View File

@ -1 +1,4 @@
class A {}
class A {
@foo
y;
}

View File

@ -0,0 +1,3 @@
class Foo {
@decorator declare bar: string;
}

View File

@ -0,0 +1,7 @@
{
"plugins": [
["transform-typescript", { "allowDeclareFields": true }],
["proposal-decorators", { "legacy": true }],
["proposal-class-properties"]
]
}

View File

@ -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);