Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
4c9929c
commit aa82ab6
Showing
7 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/babel-plugin-transform-typescript/test/fixtures/class/declare/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class A { | ||
declare x; | ||
} | ||
@foo declare y: string; | ||
} |
5 changes: 4 additions & 1 deletion
5
packages/babel-plugin-transform-typescript/test/fixtures/class/declare/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"plugins": [["transform-typescript", { "allowDeclareFields": true }]] | ||
"plugins": [ | ||
["transform-typescript", { "allowDeclareFields": true }], | ||
["syntax-decorators", { "legacy": true }] | ||
] | ||
} |
5 changes: 4 additions & 1 deletion
5
packages/babel-plugin-transform-typescript/test/fixtures/class/declare/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
class A {} | ||
class A { | ||
@foo | ||
y; | ||
} |
3 changes: 3 additions & 0 deletions
3
...bel-plugin-transform-typescript/test/fixtures/class/decorated-declare-properties/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Foo { | ||
@decorator declare bar: string; | ||
} |
7 changes: 7 additions & 0 deletions
7
...plugin-transform-typescript/test/fixtures/class/decorated-declare-properties/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"plugins": [ | ||
["transform-typescript", { "allowDeclareFields": true }], | ||
["proposal-decorators", { "legacy": true }], | ||
["proposal-class-properties"] | ||
] | ||
} |
21 changes: 21 additions & 0 deletions
21
...el-plugin-transform-typescript/test/fixtures/class/decorated-declare-properties/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |