From f30ffdf200555ba59afe88c3dc4ba6fa89d76fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 3 May 2022 16:48:09 +0900 Subject: [PATCH] fix(es/decorators): Make legacy decorator identical to `tsc` (#4496) --- crates/swc/.gitignore | 3 + crates/swc/src/config/mod.rs | 1 + crates/swc/tests/exec.rs | 70 +- .../swc/tests/exec/issues-2xxx/2655/1/.swcrc | 12 + .../swc/tests/exec/issues-2xxx/2655/1/exec.ts | 49 + .../swc/tests/exec/issues-3xxx/3342/1/.swcrc | 11 + .../swc/tests/exec/issues-3xxx/3342/1/exec.ts | 33 + .../swc/tests/exec/issues-3xxx/3521/1/.swcrc | 19 + .../swc/tests/exec/issues-3xxx/3521/1/exec.ts | 14 + .../swc/tests/exec/issues-4xxx/4496/1/.swcrc | 11 + .../swc/tests/exec/issues-4xxx/4496/1/exec.ts | 17 + .../swc/tests/exec/issues-4xxx/4496/2/.swcrc | 11 + .../swc/tests/exec/issues-4xxx/4496/2/exec.ts | 31 + .../fixture/issues-1xxx/1160/output/entry.ts | 20 +- .../fixture/issues-1xxx/1278/output/index.ts | 20 +- .../fixture/issues-1xxx/1345/output/index.ts | 143 +-- .../issues-1xxx/1362/case1/output/index.ts | 41 +- .../issues-1xxx/1421/case1/output/index.ts | 18 +- .../issues-1xxx/1456/case1/output/index.ts | 103 +- .../issues-1xxx/1456/case2/output/index.ts | 60 +- .../fixture/issues-1xxx/1507/output/index.ts | 10 +- .../1869/decorator/output/index.ts | 8 +- .../issues-2xxx/2428/1/output/index.ts | 39 +- .../fixture/issues-3xxx/3337/output/input.ts | 18 +- .../issues-3xxx/3686/1/output/index.ts | 24 +- .../fixture/issues-4xxx/4021/output/index.ts | 16 +- .../issues-4xxx/4127/1/output/index.ts | 11 +- .../issues-4xxx/4127/2/output/index.ts | 11 +- ...NonInstantiatedModule_es2015.2.minified.js | 0 ...lidNonInstantiatedModule_es5.2.minified.js | 0 .../arrayLiteral_es2015.2.minified.js | 0 .../arrayLiteral_es5.2.minified.js | 0 ...idConstructorFunction_es2015.2.minified.js | 0 ...edPropertyNames10_ES5_es2015.2.minified.js | 0 ...edPropertyNames10_ES6_es2015.2.minified.js | 0 ...edPropertyNames11_ES5_es2015.2.minified.js | 0 ...edPropertyNames11_ES6_es2015.2.minified.js | 0 ...edPropertyNames13_ES5_es2015.2.minified.js | 0 ...edPropertyNames13_ES6_es2015.2.minified.js | 0 ...edPropertyNames16_ES5_es2015.2.minified.js | 0 ...edPropertyNames16_ES6_es2015.2.minified.js | 0 ...tedPropertyNames4_ES5_es2015.2.minified.js | 0 ...tedPropertyNames4_ES6_es2015.2.minified.js | 0 ...liasingCatchVariables_es2015.2.minified.js | 0 ...owAliasingCatchVariables_es5.2.minified.js | 0 ...olFlowForInStatement2_es2015.2.minified.js | 0 ...ntrolFlowForInStatement2_es5.2.minified.js | 0 ...rInTemplateString1ES6_es2015.2.minified.js | 0 ...atorInTemplateString1_es2015.2.minified.js | 0 ...rInTemplateString2ES6_es2015.2.minified.js | 0 ...atorInTemplateString2_es2015.2.minified.js | 0 ...rInTemplateString3ES6_es2015.2.minified.js | 0 ...atorInTemplateString3_es2015.2.minified.js | 0 ...ignmentPatterns01_ES5_es2015.2.minified.js | 0 ...atterns01_ES5iterable_es2015.2.minified.js | 0 ...ignmentPatterns01_ES6_es2015.2.minified.js | 0 ...ignmentPatterns03_ES5_es2015.2.minified.js | 0 ...atterns03_ES5iterable_es2015.2.minified.js | 0 ...ignmentPatterns03_ES6_es2015.2.minified.js | 0 ...dSwitchBreakStatement_es2015.2.minified.js | 0 ...alidSwitchBreakStatement_es5.2.minified.js | 0 .../parser768531_es2015.2.minified.js | 0 .../parser768531_es5.2.minified.js | 0 .../parserDoStatement2_es2015.2.minified.js | 0 .../parserDoStatement2_es5.2.minified.js | 0 ...parserObjectCreation1_es2015.2.minified.js | 0 .../parserObjectCreation1_es5.2.minified.js | 0 .../parserSbp_7.9_A9_T3_es2015.2.minified.js | 0 .../parserSbp_7.9_A9_T3_es5.2.minified.js | 0 .../parser_breakTarget4_es2015.2.minified.js | 0 .../parser_breakTarget4_es5.2.minified.js | 0 ...ithSwitchStatements01_es2015.2.minified.js | 0 ...lsWithSwitchStatements01_es5.2.minified.js | 0 ...ithSwitchStatements03_es2015.2.minified.js | 0 ...lsWithSwitchStatements03_es5.2.minified.js | 0 ...ithSwitchStatements04_es2015.2.minified.js | 0 ...lsWithSwitchStatements04_es5.2.minified.js | 0 ...thIntersectionTypes01_es2015.2.minified.js | 0 ...eWithIntersectionTypes01_es5.2.minified.js | 0 ...hCaseWithUnionTypes01_es2015.2.minified.js | 0 ...itchCaseWithUnionTypes01_es5.2.minified.js | 0 .../symbolType2_es2015.2.minified.js | 0 .../symbolType2_es5.2.minified.js | 0 ...ePartsOfEscapes01_ES6_es2015.2.minified.js | 0 ...atArePartsOfEscapes01_es2015.2.minified.js | 0 ...ePartsOfEscapes02_ES6_es2015.2.minified.js | 0 ...ithTagNamedDeclareES6_es2015.2.minified.js | 0 ...gsWithTagNamedDeclare_es2015.2.minified.js | 0 ...titutionExpressionES6_es2015.2.minified.js | 0 ...ubstitutionExpression_es2015.2.minified.js | 0 ...templateStringInArray_es2015.2.minified.js | 0 ...tringInParenthesesES6_es2015.2.minified.js | 0 ...teStringInParentheses_es2015.2.minified.js | 0 ...PropertyAssignmentES6_es2015.2.minified.js | 0 ...gInPropertyAssignment_es2015.2.minified.js | 0 ...ingInSwitchAndCaseES6_es2015.2.minified.js | 0 ...StringInSwitchAndCase_es2015.2.minified.js | 0 ...ingInTypeAssertionES6_es2015.2.minified.js | 0 ...StringInTypeAssertion_es2015.2.minified.js | 0 ...lateStringInTypeOfES6_es2015.2.minified.js | 0 ...emplateStringInTypeOf_es2015.2.minified.js | 0 ...eStringInUnaryPlusES6_es2015.2.minified.js | 0 ...lateStringInUnaryPlus_es2015.2.minified.js | 0 ...thEmbeddedAdditionES6_es2015.2.minified.js | 0 ...gWithEmbeddedAddition_es2015.2.minified.js | 0 ...gWithEmbeddedArrayES6_es2015.2.minified.js | 0 ...ringWithEmbeddedArray_es2015.2.minified.js | 0 ...eddedArrowFunctionES6_es2015.2.minified.js | 0 ...EmbeddedArrowFunction_es2015.2.minified.js | 0 ...mbeddedConditionalES6_es2015.2.minified.js | 0 ...thEmbeddedConditional_es2015.2.minified.js | 0 ...thEmbeddedDivisionES6_es2015.2.minified.js | 0 ...gWithEmbeddedDivision_es2015.2.minified.js | 0 ...FunctionExpressionES6_es2015.2.minified.js | 0 ...dedFunctionExpression_es2015.2.minified.js | 0 ...EmbeddedInOperatorES6_es2015.2.minified.js | 0 ...ithEmbeddedInOperator_es2015.2.minified.js | 0 ...EmbeddedInstanceOfES6_es2015.2.minified.js | 0 ...ithEmbeddedInstanceOf_es2015.2.minified.js | 0 ...WithEmbeddedModuloES6_es2015.2.minified.js | 0 ...ingWithEmbeddedModulo_es2015.2.minified.js | 0 ...ddedMultiplicationES6_es2015.2.minified.js | 0 ...mbeddedMultiplication_es2015.2.minified.js | 0 ...eddedObjectLiteralES6_es2015.2.minified.js | 0 ...EmbeddedObjectLiteral_es2015.2.minified.js | 0 ...ssertionOnAdditionES6_es2015.2.minified.js | 0 ...peAssertionOnAddition_es2015.2.minified.js | 0 ...hEmbeddedUnaryPlusES6_es2015.2.minified.js | 0 ...WithEmbeddedUnaryPlus_es2015.2.minified.js | 0 ...ptyLiteralPortionsES6_es2015.2.minified.js | 0 ...hEmptyLiteralPortions_es2015.2.minified.js | 0 ...entInStringPortionES6_es2015.2.minified.js | 0 ...ommentInStringPortion_es2015.2.minified.js | 0 ...titutionExpressionES6_es2015.2.minified.js | 0 ...ubstitutionExpression_es2015.2.minified.js | 0 ...TransitiveConstraints_es2015.2.minified.js | 0 ...nceTransitiveConstraints_es5.2.minified.js | 0 .../typeGuardInClass_es2015.2.minified.js | 0 ...eOfIsOrderIndependent_es2015.2.minified.js | 0 ...TypeOfIsOrderIndependent_es5.2.minified.js | 0 ...ypeOfPrimitiveSubtype_es2015.2.minified.js | 0 crates/swc_bundler/tests/common/mod.rs | 1 + .../swc_ecma_transforms/tests/decorators.rs | 520 +------- .../{issue-2117 => .issue-2117}/1/exec.ts | 0 .../constructor/param/input.ts | 10 + .../constructor/param/output.ts | 12 + .../legacy-metadata/issues/1160/1/output.js | 21 - .../legacy-metadata/issues/1160/1/output.ts | 14 + .../legacy-metadata/issues/1278/1/output.js | 21 - .../legacy-metadata/issues/1278/1/output.ts | 14 + .../legacy-metadata/issues/1421/1/output.js | 14 - .../legacy-metadata/issues/1421/1/output.ts | 7 + .../legacy-metadata/issues/1456/1/output.js | 9 - .../legacy-metadata/issues/1456/1/output.ts | 10 + .../legacy-metadata/issues/2461/output.js | 15 - .../legacy-metadata/issues/2461/output.ts | 8 + .../legacy-metadata/issues/3979/output.js | 17 - .../legacy-metadata/issues/3979/output.ts | 11 + .../metadata/generics/1/output.js | 8 - .../metadata/generics/1/output.ts | 11 + .../metadata/generics/base/output.js | 19 - .../metadata/generics/base/output.ts | 21 + .../metadata/nest/injection/output.js | 56 - .../metadata/nest/injection/output.ts | 48 + .../paramters/decorated-types/output.js | 67 - .../paramters/decorated-types/output.ts | 66 + .../metadata/type/serialization/input.ts | 68 + .../metadata/type/serialization/output.ts | 78 ++ .../legacy-metadata/method/param/input.ts | 17 + .../legacy-metadata/method/param/output.ts | 22 + .../class-decorators/output.js | 5 - .../class-decorators/output.ts | 10 + .../method-decorators/output.js | 12 - .../method-decorators/output.ts | 12 + .../issues/1869/1/{output.js => output.ts} | 8 +- .../legacy-only/issues/1913/1/output.js | 18 - .../legacy-only/issues/1913/1/output.ts | 12 + .../legacy-only/issues/1913/2/output.js | 19 - .../legacy-only/issues/1913/2/output.ts | 13 + .../legacy-only/issues/591/1/output.js | 25 - .../legacy-only/issues/591/1/output.ts | 14 + .../legacy-only/issues/591/2/output.js | 25 - .../legacy-only/issues/591/2/output.ts | 14 + .../fixture/legacy-only/issues/823/1/input.ts | 12 + .../legacy-only/issues/823/1/output.ts | 12 + .../fixture/legacy-only/issues/823/2/input.ts | 12 + .../legacy-only/issues/823/2/output.ts | 12 + .../fixture/legacy-only/issues/823/3/input.ts | 12 + .../legacy-only/issues/823/3/output.ts | 12 + .../fixture/legacy-only/issues/862/1/input.ts | 21 + .../legacy-only/issues/862/1/output.ts | 35 + .../fixture/legacy-only/issues/862/2/input.ts | 5 + .../legacy-only/issues/862/2/output.ts | 9 + .../fixture/legacy-only/issues/863/1/input.ts | 9 + .../legacy-only/issues/863/1/output.ts | 9 + .../fixture/legacy-only/issues/879/1/input.ts | 4 + .../legacy-only/issues/879/1/output.ts | 9 + .../legacy-only/issues/swc-node-210/output.js | 6 - .../legacy-only/issues/swc-node-210/output.ts | 8 + .../legacy-only/regression-10264/output.js | 5 - .../legacy-only/regression-10264/output.ts | 7 + .../legacy-only/regression-8041/input.ts | 4 + .../legacy-only/regression-8041/output.ts | 6 + .../src/helpers/_ts_decorate.js | 6 + .../src/helpers/_ts_metadata.js | 3 + .../src/helpers/_ts_param.js | 3 + .../src/helpers/mod.rs | 6 +- crates/swc_ecma_transforms_base/src/quote.rs | 34 +- .../tests/simplify_dce.rs | 38 +- .../src/decorators/legacy/metadata.rs | 172 +-- .../src/decorators/legacy/mod.rs | 1115 ++++++----------- .../src/decorators/mod.rs | 9 +- .../swc_ecma_transforms_proposal/src/lib.rs | 1 + .../tests/strip.rs | 21 +- .../tests/pass/helpers/simple/output/entry.js | 14 +- packages/swc-helpers/package.json | 5 +- packages/swc-helpers/src/index.js | 1 + yarn.lock | 13 +- 218 files changed, 1760 insertions(+), 2086 deletions(-) create mode 100644 crates/swc/.gitignore create mode 100644 crates/swc/tests/exec/issues-2xxx/2655/1/.swcrc create mode 100644 crates/swc/tests/exec/issues-2xxx/2655/1/exec.ts create mode 100644 crates/swc/tests/exec/issues-3xxx/3342/1/.swcrc create mode 100644 crates/swc/tests/exec/issues-3xxx/3342/1/exec.ts create mode 100644 crates/swc/tests/exec/issues-3xxx/3521/1/.swcrc create mode 100644 crates/swc/tests/exec/issues-3xxx/3521/1/exec.ts create mode 100644 crates/swc/tests/exec/issues-4xxx/4496/1/.swcrc create mode 100644 crates/swc/tests/exec/issues-4xxx/4496/1/exec.ts create mode 100644 crates/swc/tests/exec/issues-4xxx/4496/2/.swcrc create mode 100644 crates/swc/tests/exec/issues-4xxx/4496/2/exec.ts delete mode 100644 crates/swc/tests/tsc-references/InvalidNonInstantiatedModule_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/InvalidNonInstantiatedModule_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/arrayLiteral_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/arrayLiteral_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/classExtendsValidConstructorFunction_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames10_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames10_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames11_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames11_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames13_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames13_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames16_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames16_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames4_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/computedPropertyNames4_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/controlFlowAliasingCatchVariables_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/controlFlowAliasingCatchVariables_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/controlFlowForInStatement2_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/controlFlowForInStatement2_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString1ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString1_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString2ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString2_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString3ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString3_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES5iterable_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES5_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES5iterable_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/invalidSwitchBreakStatement_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/invalidSwitchBreakStatement_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parser768531_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parser768531_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parserDoStatement2_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parserDoStatement2_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parserObjectCreation1_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parserObjectCreation1_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parser_breakTarget4_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/parser_breakTarget4_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements01_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements01_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements03_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements03_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements04_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements04_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/switchCaseWithIntersectionTypes01_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/switchCaseWithIntersectionTypes01_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/switchCaseWithUnionTypes01_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/switchCaseWithUnionTypes01_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/symbolType2_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/symbolType2_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes02_ES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsWithTagNamedDeclareES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsWithTagNamedDeclare_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInArray_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInParenthesesES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInParentheses_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInPropertyAssignmentES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInPropertyAssignment_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInSwitchAndCaseES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInSwitchAndCase_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInTypeAssertionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInTypeAssertion_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInTypeOfES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInTypeOf_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInUnaryPlusES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringInUnaryPlus_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedAdditionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedAddition_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedArrayES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedArray_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedArrowFunctionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedArrowFunction_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedConditionalES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedConditional_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedDivisionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedDivision_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedFunctionExpressionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedFunctionExpression_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedInOperatorES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedInOperator_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedInstanceOfES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedInstanceOf_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedModuloES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedModulo_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedMultiplicationES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedMultiplication_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedObjectLiteralES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedObjectLiteral_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedTypeAssertionOnAdditionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedTypeAssertionOnAddition_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedUnaryPlusES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmbeddedUnaryPlus_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmptyLiteralPortionsES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithEmptyLiteralPortions_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithOpenCommentInStringPortionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringWithOpenCommentInStringPortion_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/typeArgumentInferenceTransitiveConstraints_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/typeArgumentInferenceTransitiveConstraints_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/typeGuardInClass_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/typeGuardOfFormTypeOfIsOrderIndependent_es2015.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/typeGuardOfFormTypeOfIsOrderIndependent_es5.2.minified.js delete mode 100644 crates/swc/tests/tsc-references/typeGuardOfFormTypeOfPrimitiveSubtype_es2015.2.minified.js rename crates/swc_ecma_transforms/tests/fixture/decorator/{issue-2117 => .issue-2117}/1/exec.ts (100%) create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.ts rename crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/{output.js => output.ts} (59%) delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.ts delete mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.js create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/input.ts create mode 100644 crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/output.ts create mode 100644 crates/swc_ecma_transforms_base/src/helpers/_ts_decorate.js create mode 100644 crates/swc_ecma_transforms_base/src/helpers/_ts_metadata.js create mode 100644 crates/swc_ecma_transforms_base/src/helpers/_ts_param.js diff --git a/crates/swc/.gitignore b/crates/swc/.gitignore new file mode 100644 index 00000000000..398921cfa5f --- /dev/null +++ b/crates/swc/.gitignore @@ -0,0 +1,3 @@ +# I use this to try stuffs +/lab.js +/cur.js \ No newline at end of file diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 2345d7491b6..c4565ac9873 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -528,6 +528,7 @@ impl Options { decorators(decorators::Config { legacy: transform.legacy_decorator, emit_metadata: transform.decorator_metadata, + use_define_for_class_fields: !assumptions.set_public_class_fields }), syntax.decorators() ), diff --git a/crates/swc/tests/exec.rs b/crates/swc/tests/exec.rs index 2fea9275507..4f5d97f4253 100644 --- a/crates/swc/tests/exec.rs +++ b/crates/swc/tests/exec.rs @@ -1,5 +1,5 @@ use std::{ - fs::{self, create_dir_all, rename}, + fs::{create_dir_all, rename}, path::{Component, Path, PathBuf}, process::Command, sync::Arc, @@ -63,9 +63,10 @@ fn create_matrix(entry: &Path) -> Vec { if let Some(ext) = entry.extension() { if ext == "ts" { let ts = Syntax::Typescript(TsConfig { + decorators: true, ..Default::default() }); - return vec![ts, default_es]; + return vec![ts]; } } @@ -128,16 +129,7 @@ fn run_fixture_test(entry: PathBuf) { let _guard = testing::init(); let matrix = create_matrix(&entry); - let input_code = fs::read_to_string(&entry).expect("failed to read entry file"); - let expected_stdout = stdout_of( - &input_code, - if entry.extension().unwrap() == "mjs" { - NodeModuleType::Module - } else { - NodeModuleType::CommonJs - }, - ) - .expect("failed to get stdout"); + let expected_stdout = get_expected_stdout(&entry).expect("failed to get stdout"); eprintln!( "----- {} -----\n{}\n-----", @@ -156,6 +148,60 @@ fn run_fixture_test(entry: PathBuf) { unignore(&entry); } +fn get_expected_stdout(input: &Path) -> Result { + let cm = Arc::new(SourceMap::default()); + let c = Compiler::new(cm.clone()); + + try_with_handler( + cm.clone(), + HandlerOpts { + color: ColorConfig::Always, + skip_filename: true, + }, + |handler| { + let fm = cm.load_file(input).context("failed to load file")?; + + let res = c + .process_js_file( + fm, + handler, + &Options { + config: Config { + jsc: JscConfig { + target: Some(EsVersion::Es2021), + syntax: Some(Syntax::Typescript(TsConfig { + decorators: true, + ..Default::default() + })), + ..Default::default() + }, + module: match input.extension() { + Some(ext) if ext == "ts" => { + Some(ModuleConfig::CommonJs(Default::default())) + } + Some(ext) if ext == "mjs" => None, + _ => None, + }, + ..Default::default() + }, + ..Default::default() + }, + ) + .context("failed to process file")?; + + let res = stdout_of( + &res.code, + match input.extension() { + Some(ext) if ext == "mjs" => NodeModuleType::Module, + _ => NodeModuleType::CommonJs, + }, + )?; + + Ok(res) + }, + ) +} + /// Rename `foo/.bar/exec.js` => `foo/bar/exec.js` fn unignore(path: &Path) { if path.components().all(|c| { diff --git a/crates/swc/tests/exec/issues-2xxx/2655/1/.swcrc b/crates/swc/tests/exec/issues-2xxx/2655/1/.swcrc new file mode 100644 index 00000000000..a75d62192df --- /dev/null +++ b/crates/swc/tests/exec/issues-2xxx/2655/1/.swcrc @@ -0,0 +1,12 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2016", + "transform": { + "useDefineForClassFields": false + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-2xxx/2655/1/exec.ts b/crates/swc/tests/exec/issues-2xxx/2655/1/exec.ts new file mode 100644 index 00000000000..aaa718a4694 --- /dev/null +++ b/crates/swc/tests/exec/issues-2xxx/2655/1/exec.ts @@ -0,0 +1,49 @@ +let calledSetterProperty = false; + +const testDecorator = (target: T, key: keyof T) => { + const privateField = Symbol(); + // We define getters and setters for the property on the prototype of the class + // A real application might use this to intercept changes to the decorated property. + // This is a simplified version of a pattern used by the @microsoft/fast-elements library. + Reflect.defineProperty(target, key, { + get: function () { + console.log(`called getter for property ${key}.`) + return (target as any)[privateField] + }, + set: function (newValue) { + calledSetterProperty = true; + console.log(`called setter for property ${key} with newValue ${newValue}.`) + return (target as any)[privateField] = newValue + } + }) + console.log("called testDecorator!"); +}; + +class TestClass { + @testDecorator + testProp = "hello" +} + +const instance = new TestClass(); +// SWC console output: +// "called testDecorator!" +// TSC console output: +// "called testDecorator!" +// "called setter for property testProp with newValue hello" + +if (!calledSetterProperty) { + throw new Error("Expected setter to be called!"); +} + +instance.testProp = "goodbye"; +// SWC console output: +// +// TSC console output: +// "called setter for property testProp with newValue goodbye." + +console.log("testProp is now", instance.testProp); +// SWC console output: +// "testProps is now goodbye" +// TSC console output: +// "called getter for property testProp." +// "testProps is now goodbye" \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-3xxx/3342/1/.swcrc b/crates/swc/tests/exec/issues-3xxx/3342/1/.swcrc new file mode 100644 index 00000000000..a9c4f0d6a7c --- /dev/null +++ b/crates/swc/tests/exec/issues-3xxx/3342/1/.swcrc @@ -0,0 +1,11 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false, + "decorators": true, + "loose": true + }, + "target": "es2022" + } +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-3xxx/3342/1/exec.ts b/crates/swc/tests/exec/issues-3xxx/3342/1/exec.ts new file mode 100644 index 00000000000..f2b30e5ff25 --- /dev/null +++ b/crates/swc/tests/exec/issues-3xxx/3342/1/exec.ts @@ -0,0 +1,33 @@ +/* + Proxy any reads and writes to this member. We'll store the value at some private symbol + and read or write to it when the member is accessed. +*/ +const d = (proto: any, name) => { + let self = proto.constructor; + const key = typeof name === "symbol" ? Symbol() : `__${name}`; + + Object.defineProperty(self.prototype, name, { + get(): any { + return (this as { [key: string]: unknown })[key as string]; + }, + set(value: unknown) { + const oldValue = (this as {} as { [key: string]: unknown })[ + name as string + ]; + (this as {} as { [key: string]: unknown })[key as string] = value; + }, + configurable: true, + enumerable: true, + }); +}; + +class Test { + @d + member = 4; +} + +console.log(new Test().member); + +if (new Test().member === undefined) { + throw new Error(`wrong`) +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-3xxx/3521/1/.swcrc b/crates/swc/tests/exec/issues-3xxx/3521/1/.swcrc new file mode 100644 index 00000000000..4cb5bdc6fe2 --- /dev/null +++ b/crates/swc/tests/exec/issues-3xxx/3521/1/.swcrc @@ -0,0 +1,19 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false, + "decorators": true + }, + "target": "es2022", + "loose": false, + "minify": { + "compress": false, + "mangle": false + } + }, + "module": { + "type": "es6" + }, + "minify": false +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-3xxx/3521/1/exec.ts b/crates/swc/tests/exec/issues-3xxx/3521/1/exec.ts new file mode 100644 index 00000000000..67352db73a0 --- /dev/null +++ b/crates/swc/tests/exec/issues-3xxx/3521/1/exec.ts @@ -0,0 +1,14 @@ +const Test = (name: string): any => { + return (target: any, key: string) => { + // stuff + }; +} + +@Test(FormulaRuleEntity.NAME) +class FormulaRuleEntity { + + static readonly NAME = 'cat'; + +} + +console.log('PASS') \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-4xxx/4496/1/.swcrc b/crates/swc/tests/exec/issues-4xxx/4496/1/.swcrc new file mode 100644 index 00000000000..726795db78d --- /dev/null +++ b/crates/swc/tests/exec/issues-4xxx/4496/1/.swcrc @@ -0,0 +1,11 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "transform": { + "useDefineForClassFields": false + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-4xxx/4496/1/exec.ts b/crates/swc/tests/exec/issues-4xxx/4496/1/exec.ts new file mode 100644 index 00000000000..3402419fcee --- /dev/null +++ b/crates/swc/tests/exec/issues-4xxx/4496/1/exec.ts @@ -0,0 +1,17 @@ + + +let id = 1; + +function dec() { + console.log(id) + return () => id++ +} + +@dec +class Foo { + @dec + prop: number + @dec + @dec + propWithInit = 2 +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-4xxx/4496/2/.swcrc b/crates/swc/tests/exec/issues-4xxx/4496/2/.swcrc new file mode 100644 index 00000000000..726795db78d --- /dev/null +++ b/crates/swc/tests/exec/issues-4xxx/4496/2/.swcrc @@ -0,0 +1,11 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "transform": { + "useDefineForClassFields": false + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-4xxx/4496/2/exec.ts b/crates/swc/tests/exec/issues-4xxx/4496/2/exec.ts new file mode 100644 index 00000000000..6afdd4ba845 --- /dev/null +++ b/crates/swc/tests/exec/issues-4xxx/4496/2/exec.ts @@ -0,0 +1,31 @@ + + +let id = 1; + +function dec() { + console.log(id) + return () => id++ +} + +function key() { + console.log(id) + return id++ +} + + +@dec +class Foo { + @dec + prop1: number + + @dec + [key()]: number + + @dec + @dec + [key()]: number + + @dec + prop2: number +} + diff --git a/crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts b/crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts index f6bab7b6d9e..9589c7ec458 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1160/output/entry.ts @@ -1,24 +1,16 @@ "use strict"; var swcHelpers = require("@swc/helpers"); -var _class, _descriptor, _dec, _dec1; var MyEnum; (function(MyEnum) { MyEnum["x"] = "xxx"; MyEnum["y"] = "yyy"; })(MyEnum || (MyEnum = {})); -let Xpto = ((_class = class Xpto { - constructor(){ - swcHelpers.initializerDefineProperty(this, "value", _descriptor, this); - } -}) || _class, _dec = Decorator(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = swcHelpers.applyDecoratedDescriptor(_class.prototype, "value", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); +class Xpto { +} +swcHelpers.__decorate([ + Decorator(), + swcHelpers.__metadata("design:type", String) +], Xpto.prototype, "value", void 0); function Decorator() { return function(...args) {}; } diff --git a/crates/swc/tests/fixture/issues-1xxx/1278/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1278/output/index.ts index fdd524ad034..1bba8b05c5a 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1278/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1278/output/index.ts @@ -1,23 +1,15 @@ "use strict"; var swcHelpers = require("@swc/helpers"); -var _class, _descriptor, _dec, _dec1; function MyDecorator(klass) { return ()=>{ // do something console.log(klass); }; } -let MyClass = ((_class = class MyClass { - constructor(){ - swcHelpers.initializerDefineProperty(this, "prop", _descriptor, this); - } -}) || _class, _dec = MyDecorator(_class), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = swcHelpers.applyDecoratedDescriptor(_class.prototype, "prop", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); +class MyClass { +} +swcHelpers.__decorate([ + MyDecorator(MyClass), + swcHelpers.__metadata("design:type", String) +], MyClass.prototype, "prop", void 0); console.log(new MyClass()); diff --git a/crates/swc/tests/fixture/issues-1xxx/1345/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1345/output/index.ts index 03ef611d805..0d6bb2d075b 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1345/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1345/output/index.ts @@ -1,94 +1,55 @@ import * as swcHelpers from "@swc/helpers"; -var _class, _descriptor, _dec, _dec1, _descriptor1, _dec2, _dec3, _descriptor2, _dec4, _dec5, _descriptor3, _dec6, _dec7, _descriptor4, _dec8, _dec9, _descriptor5, _dec10, _dec11, _descriptor6, _dec12, _dec13, _descriptor7, _dec14, _dec15; -var _dec16 = ViewEntity({ - name: "AccountMemberView", - expression: '\n SELECT\n m.tmcode, m.mid, m.accea, m.qaccea, m.endday, m.quick_endday,\n (SELECT COUNT(*) FROM TBLACCOUNT a WHERE m.mid = a.mid AND a.use_quick="F") as accountCnt,\n (SELECT COUNT(*) FROM TBLACCOUNT a WHERE m.mid = a.mid AND a.use_quick="T") as accountQuickCnt\n FROM TBLMEMBER m\n ' -}); -export var AccountMemberView = _class = _dec16((_class = function AccountMemberView() { +export var AccountMemberView = function AccountMemberView() { "use strict"; swcHelpers.classCallCheck(this, AccountMemberView); - swcHelpers.initializerDefineProperty(this, "memberId", _descriptor, this); - swcHelpers.initializerDefineProperty(this, "mallId", _descriptor1, this); - swcHelpers.initializerDefineProperty(this, "allowAccountCnt", _descriptor2, this); - swcHelpers.initializerDefineProperty(this, "allowQuickAccountCnt", _descriptor3, this); - swcHelpers.initializerDefineProperty(this, "accountEnddedAt", _descriptor4, this); - swcHelpers.initializerDefineProperty(this, "accountQuickEnddedAt", _descriptor5, this); - swcHelpers.initializerDefineProperty(this, "accountCnt", _descriptor6, this); - swcHelpers.initializerDefineProperty(this, "accountQuickCnt", _descriptor7, this); -}, _dec = ViewColumn({ - name: "tmcode" -}), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Number), _dec2 = ViewColumn({ - name: "mid" -}), _dec3 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _dec4 = ViewColumn({ - name: "accea" -}), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Number), _dec6 = ViewColumn({ - name: "qaccea" -}), _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Number), _dec8 = ViewColumn({ - name: "endday" -}), _dec9 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", typeof Date === "undefined" ? Object : Date), _dec10 = ViewColumn({ - name: "quick_endday" -}), _dec11 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", typeof Date === "undefined" ? Object : Date), _dec12 = ViewColumn(), _dec13 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Number), _dec14 = ViewColumn(), _dec15 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Number), _descriptor = swcHelpers.applyDecoratedDescriptor(_class.prototype, "memberId", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor1 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "mallId", [ - _dec2, - _dec3 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor2 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "allowAccountCnt", [ - _dec4, - _dec5 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor3 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "allowQuickAccountCnt", [ - _dec6, - _dec7 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor4 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "accountEnddedAt", [ - _dec8, - _dec9 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor5 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "accountQuickEnddedAt", [ - _dec10, - _dec11 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor6 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "accountCnt", [ - _dec12, - _dec13 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor7 = swcHelpers.applyDecoratedDescriptor(_class.prototype, "accountQuickCnt", [ - _dec14, - _dec15 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class)) || _class; +}; +swcHelpers.__decorate([ + ViewColumn({ + name: "tmcode" + }), + swcHelpers.__metadata("design:type", Number) +], AccountMemberView.prototype, "memberId", void 0); +swcHelpers.__decorate([ + ViewColumn({ + name: "mid" + }), + swcHelpers.__metadata("design:type", String) +], AccountMemberView.prototype, "mallId", void 0); +swcHelpers.__decorate([ + ViewColumn({ + name: "accea" + }), + swcHelpers.__metadata("design:type", Number) +], AccountMemberView.prototype, "allowAccountCnt", void 0); +swcHelpers.__decorate([ + ViewColumn({ + name: "qaccea" + }), + swcHelpers.__metadata("design:type", Number) +], AccountMemberView.prototype, "allowQuickAccountCnt", void 0); +swcHelpers.__decorate([ + ViewColumn({ + name: "endday" + }), + swcHelpers.__metadata("design:type", typeof Date === "undefined" ? Object : Date) +], AccountMemberView.prototype, "accountEnddedAt", void 0); +swcHelpers.__decorate([ + ViewColumn({ + name: "quick_endday" + }), + swcHelpers.__metadata("design:type", typeof Date === "undefined" ? Object : Date) +], AccountMemberView.prototype, "accountQuickEnddedAt", void 0); +swcHelpers.__decorate([ + ViewColumn(), + swcHelpers.__metadata("design:type", Number) +], AccountMemberView.prototype, "accountCnt", void 0); +swcHelpers.__decorate([ + ViewColumn(), + swcHelpers.__metadata("design:type", Number) +], AccountMemberView.prototype, "accountQuickCnt", void 0); +AccountMemberView = swcHelpers.__decorate([ + ViewEntity({ + name: "AccountMemberView", + expression: '\n SELECT\n m.tmcode, m.mid, m.accea, m.qaccea, m.endday, m.quick_endday,\n (SELECT COUNT(*) FROM TBLACCOUNT a WHERE m.mid = a.mid AND a.use_quick="F") as accountCnt,\n (SELECT COUNT(*) FROM TBLACCOUNT a WHERE m.mid = a.mid AND a.use_quick="T") as accountQuickCnt\n FROM TBLMEMBER m\n ' + }) +], AccountMemberView); diff --git a/crates/swc/tests/fixture/issues-1xxx/1362/case1/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1362/case1/output/index.ts index 0146898970a..488dd4086c0 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1362/case1/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1362/case1/output/index.ts @@ -7,11 +7,7 @@ var swcHelpers = require("@swc/helpers"); var _common = require("@nestjs/common"); var _appService = require("./app.service"); var _createUserDto = require("./dtos/CreateUserDto"); -var _class, _dec, _dec1, _dec2, _dec3, _dec4, _dec5, _dec6; -var _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof _appService.AppService === "undefined" ? Object : _appService.AppService -]), _dec8 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec9 = (0, _common).Controller(); -let AppController = _class = _dec9(_class = _dec8(_class = _dec7(((_class = class AppController { +let AppController = class AppController { async getHello() { const result = await this.appService.getHello(); return result; @@ -23,18 +19,25 @@ let AppController = _class = _dec9(_class = _dec8(_class = _dec7(((_class = clas constructor(appService){ this.appService = appService; } -}) || _class, _dec = (0, _common).Get(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", []), swcHelpers.applyDecoratedDescriptor(_class.prototype, "getHello", [ - _dec, - _dec1, - _dec2 -], Object.getOwnPropertyDescriptor(_class.prototype, "getHello"), _class.prototype), _dec3 = (0, _common).Post(), _dec4 = function(target, key) { - return (0, _common).Body()(target, key, 0); -}, _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec6 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof _createUserDto.CreateUserDto === "undefined" ? Object : _createUserDto.CreateUserDto -]), swcHelpers.applyDecoratedDescriptor(_class.prototype, "create", [ - _dec3, - _dec4, - _dec5, - _dec6 -], Object.getOwnPropertyDescriptor(_class.prototype, "create"), _class.prototype), _class)) || _class) || _class) || _class; +}; exports.AppController = AppController; +swcHelpers.__decorate([ + (0, _common).Get(), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", []) +], AppController.prototype, "getHello", null); +swcHelpers.__decorate([ + (0, _common).Post(), + swcHelpers.__param(0, (0, _common).Body()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof _createUserDto.CreateUserDto === "undefined" ? Object : _createUserDto.CreateUserDto + ]) +], AppController.prototype, "create", null); +exports.AppController = AppController = swcHelpers.__decorate([ + (0, _common).Controller(), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof _appService.AppService === "undefined" ? Object : _appService.AppService + ]) +], AppController); diff --git a/crates/swc/tests/fixture/issues-1xxx/1421/case1/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1421/case1/output/index.ts index 4bb72d8f375..db67f4213e3 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1421/case1/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1421/case1/output/index.ts @@ -1,23 +1,17 @@ "use strict"; var swcHelpers = require("@swc/helpers"); require("reflect-metadata"); -var _class, _descriptor, _dec, _dec1; var COL_KEY = Symbol("col"); var column = function() { return function(object, key) { Reflect.defineMetadata(COL_KEY, "value", object, key); }; }; -var User = ((_class = function User() { +var User = function User() { "use strict"; swcHelpers.classCallCheck(this, User); - swcHelpers.initializerDefineProperty(this, "currency", _descriptor, this); -}) || _class, _dec = column(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = swcHelpers.applyDecoratedDescriptor(_class.prototype, "currency", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); +}; +swcHelpers.__decorate([ + column(), + swcHelpers.__metadata("design:type", String) +], User.prototype, "currency", void 0); diff --git a/crates/swc/tests/fixture/issues-1xxx/1456/case1/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1456/case1/output/index.ts index fce6c45d59e..7e7ab67e45d 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1456/case1/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1456/case1/output/index.ts @@ -1,67 +1,78 @@ "use strict"; -var _class, _class1, _class2, _class3, _class4, _class5; -var _dec = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass1 = _class = _dec2(_class = _dec1(_class = _dec((_class = // not work +var swcHelpers = require("@swc/helpers"); +// not work class MyClass1 { constructor(param1){ this.param1 = param1; } -}) || _class) || _class) || _class) || _class; -var _dec3 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec5 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass2 = _class1 = _dec5(_class1 = _dec4(_class1 = _dec3((_class1 = class MyClass2 { +} +MyClass1 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass1); +class MyClass2 { constructor(param1){ this.param1 = param1; } -}) || _class1) || _class1) || _class1) || _class1; -var _dec6 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec8 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass3 = _class2 = _dec8(_class2 = _dec7(_class2 = _dec6((_class2 = class MyClass3 { +} +MyClass2 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass2); +class MyClass3 { constructor(param1){ this.param1 = param1; } -}) || _class2) || _class2) || _class2) || _class2; -var _dec9 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec10 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec11 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass4 = _class3 = _dec11(_class3 = _dec10(_class3 = _dec9((_class3 = class MyClass4 { +} +MyClass3 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass3); +class MyClass4 { constructor(param1){ this.param1 = param1; } -}) || _class3) || _class3) || _class3) || _class3; -var _dec12 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec13 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec14 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass5 = _class4 = _dec14(_class4 = _dec13(_class4 = _dec12((_class4 = class MyClass5 { +} +MyClass4 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass4); +class MyClass5 { constructor(param1){ this.param1 = param1; } -}) || _class4) || _class4) || _class4) || _class4; -var _dec15 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected, - typeof Injected === "undefined" ? Object : Injected -]), _dec16 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec17 = function(target, key) { - return Inject()(target, undefined, 1); -}, _dec18 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass6 = _class5 = _dec18(_class5 = _dec17(_class5 = _dec16(_class5 = _dec15((_class5 = class MyClass6 { +} +MyClass5 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass5); +class MyClass6 { constructor(param1, param2){ this.param1 = param1; this.param2 = param2; } -}) || _class5) || _class5) || _class5) || _class5) || _class5; +} +MyClass6 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__param(1, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected, + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass6); diff --git a/crates/swc/tests/fixture/issues-1xxx/1456/case2/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1456/case2/output/index.ts index 9c1ab59dae0..325f35eb7a3 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1456/case2/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1456/case2/output/index.ts @@ -1,37 +1,41 @@ "use strict"; -var _class, _class1, _class2; -var _dec = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass1 = _class = _dec2(_class = _dec1(_class = _dec((_class = // work +var swcHelpers = require("@swc/helpers"); +// work class MyClass1 { constructor(param1){} -}) || _class) || _class) || _class) || _class; -var _dec3 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected, - typeof Injected === "undefined" ? Object : Injected -]), _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec5 = function(target, key) { - return Inject()(target, undefined, 1); -}, _dec6 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass2 = _class1 = _dec6(_class1 = _dec5(_class1 = _dec4(_class1 = _dec3((_class1 = class MyClass2 { +} +MyClass1 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass1); +class MyClass2 { constructor(param1, param2){ this.param1 = param1; } -}) || _class1) || _class1) || _class1) || _class1) || _class1; -var _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected, - typeof Injected === "undefined" ? Object : Injected -]), _dec8 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec9 = function(target, key) { - return Inject()(target, undefined, 1); -}, _dec10 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass3 = _class2 = _dec10(_class2 = _dec9(_class2 = _dec8(_class2 = _dec7((_class2 = class MyClass3 { +} +MyClass2 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__param(1, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected, + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass2); +class MyClass3 { constructor(param1, param2){ this.param2 = param2; } -}) || _class2) || _class2) || _class2) || _class2) || _class2; +} +MyClass3 = swcHelpers.__decorate([ + swcHelpers.__param(0, Inject()), + swcHelpers.__param(1, Inject()), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected, + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass3); diff --git a/crates/swc/tests/fixture/issues-1xxx/1507/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1507/output/index.ts index af34a4c85ca..f1b97a8117a 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1507/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1507/output/index.ts @@ -1,5 +1,7 @@ -var _class; +import * as swcHelpers from "@swc/helpers"; import { Entity, BaseEntity } from 'typeorm'; -var _dec = Entity(); -export let Location = _class = _dec((_class = class Location extends BaseEntity { -}) || _class) || _class; +export let Location = class Location extends BaseEntity { +}; +Location = swcHelpers.__decorate([ + Entity() +], Location); diff --git a/crates/swc/tests/fixture/issues-1xxx/1869/decorator/output/index.ts b/crates/swc/tests/fixture/issues-1xxx/1869/decorator/output/index.ts index 877354ab454..6bda99deec1 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1869/decorator/output/index.ts +++ b/crates/swc/tests/fixture/issues-1xxx/1869/decorator/output/index.ts @@ -1,12 +1,14 @@ import * as swcHelpers from "@swc/helpers"; var _TestClass; -var _class; -var TestClass = _class = someClassDecorator((_class = (_TestClass = function TestClass() { +var TestClass = (_TestClass = function TestClass() { "use strict"; swcHelpers.classCallCheck(this, TestClass); }, _TestClass.Something = "hello", _TestClass.SomeProperties = { firstProp: _TestClass.Something -}, _TestClass)) || _class) || _class; +}, _TestClass); +TestClass = swcHelpers.__decorate([ + someClassDecorator +], TestClass); function someClassDecorator(c) { return c; } diff --git a/crates/swc/tests/fixture/issues-2xxx/2428/1/output/index.ts b/crates/swc/tests/fixture/issues-2xxx/2428/1/output/index.ts index b7125d0f2f1..420ad8079ec 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2428/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-2xxx/2428/1/output/index.ts @@ -1,31 +1,28 @@ "use strict"; var swcHelpers = require("@swc/helpers"); -var _class, _dec, _dec1, _dec2, _dec3, _dec4, _dec5; -let Foo = ((_class = class Foo { +class Foo { fnName1(argName) { return swcHelpers.asyncToGenerator(function*() {})(); } fnName2(argName = false) { return swcHelpers.asyncToGenerator(function*() {})(); } -}) || _class, _dec = function(target, key) { - return Arg('GraphQLArgName', { +} +swcHelpers.__decorate([ + swcHelpers.__param(0, Arg('GraphQLArgName', { nullable: true - })(target, key, 0); -}, _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - Boolean -]), swcHelpers.applyDecoratedDescriptor(_class.prototype, "fnName1", [ - _dec, - _dec1, - _dec2 -], Object.getOwnPropertyDescriptor(_class.prototype, "fnName1"), _class.prototype), _dec3 = function(target, key) { - return Arg('GraphQLArgName', { + })), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + Boolean + ]) +], Foo.prototype, "fnName1", null); +swcHelpers.__decorate([ + swcHelpers.__param(0, Arg('GraphQLArgName', { nullable: true - })(target, key, 0); -}, _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - Boolean -]), swcHelpers.applyDecoratedDescriptor(_class.prototype, "fnName2", [ - _dec3, - _dec4, - _dec5 -], Object.getOwnPropertyDescriptor(_class.prototype, "fnName2"), _class.prototype), _class); + })), + swcHelpers.__metadata("design:type", Function), + swcHelpers.__metadata("design:paramtypes", [ + Boolean + ]) +], Foo.prototype, "fnName2", null); diff --git a/crates/swc/tests/fixture/issues-3xxx/3337/output/input.ts b/crates/swc/tests/fixture/issues-3xxx/3337/output/input.ts index 39be3d85252..216352735d6 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3337/output/input.ts +++ b/crates/swc/tests/fixture/issues-3xxx/3337/output/input.ts @@ -1,17 +1,11 @@ "use strict"; var swcHelpers = require("@swc/helpers"); var joiful = swcHelpers.interopRequireWildcard(require("joiful")); -var _class, _descriptor, _dec, _dec1; -var Schema = ((_class = function Schema() { +var Schema = function Schema() { "use strict"; swcHelpers.classCallCheck(this, Schema); - swcHelpers.initializerDefineProperty(this, "id", _descriptor, this); -}) || _class, _dec = joiful.string().guid().required(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = swcHelpers.applyDecoratedDescriptor(_class.prototype, "id", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); +}; +swcHelpers.__decorate([ + joiful.string().guid().required(), + swcHelpers.__metadata("design:type", String) +], Schema.prototype, "id", void 0); diff --git a/crates/swc/tests/fixture/issues-3xxx/3686/1/output/index.ts b/crates/swc/tests/fixture/issues-3xxx/3686/1/output/index.ts index c6ac640d9dd..6b061248df1 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3686/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-3xxx/3686/1/output/index.ts @@ -4,26 +4,22 @@ Object.defineProperty(exports, "__esModule", { }); exports.ServiceError = void 0; var swcHelpers = require("@swc/helpers"); -var _class, _descriptor; const CD = ()=>{}; const PD = ()=>{}; -let ServiceError = _class = CD(((_class = class ServiceError extends Error { - name = "ServiceError.BadResponse"; +let ServiceError = class ServiceError extends Error { constructor(...args){ super(...args); - swcHelpers.initializerDefineProperty(this, "code", _descriptor, this); + this.code = ServiceError.Code.badResponse; } -}) || _class, _descriptor = swcHelpers.applyDecoratedDescriptor(_class.prototype, "code", [ - PD -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return ServiceError.Code.badResponse; - } -}), _class)) || _class; + name = "ServiceError.BadResponse"; +}; exports.ServiceError = ServiceError; +swcHelpers.__decorate([ + PD +], ServiceError.prototype, "code", void 0); +exports.ServiceError = ServiceError = swcHelpers.__decorate([ + CD +], ServiceError); (function(ServiceError1) { let Code; (function(Code) { diff --git a/crates/swc/tests/fixture/issues-4xxx/4021/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4021/output/index.ts index 19f4255d02d..5f6da1f4425 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4021/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4021/output/index.ts @@ -1,8 +1,14 @@ -var _class, _class1; +import * as swcHelpers from "@swc/helpers"; var N; (function(N) { - let C1 = _class = foo((_class = class C1 { - }) || _class) || _class; + let C1 = class C1 { + }; + C1 = swcHelpers.__decorate([ + foo + ], C1); })(N || (N = {})); -let C2 = _class1 = foo((_class1 = class C2 { -}) || _class1) || _class1; +let C2 = class C2 { +}; +C2 = swcHelpers.__decorate([ + foo +], C2); diff --git a/crates/swc/tests/fixture/issues-4xxx/4127/1/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4127/1/output/index.ts index 9d34b8e4f76..6d81098eae7 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4127/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4127/1/output/index.ts @@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; -var _class; +var swcHelpers = require("@swc/helpers"); function test(constructor) { console.log(constructor); } -let _class1 = _class = test((_class = class { -}) || _class) || _class; -exports.default = _class1; +let _class = class _class { +}; +exports.default = _class = swcHelpers.__decorate([ + test +], _class); +exports.default = _class; diff --git a/crates/swc/tests/fixture/issues-4xxx/4127/2/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4127/2/output/index.ts index 1966ecea49f..f019effaa9c 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4127/2/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4127/2/output/index.ts @@ -1,7 +1,10 @@ -var _class; +import * as swcHelpers from "@swc/helpers"; function test(constructor) { console.log(constructor); } -let _class1 = _class = test((_class = class { -}) || _class) || _class; -export { _class1 as default }; +let _class = class _class { +}; +_class = swcHelpers.__decorate([ + test +], _class); +export { _class as default }; diff --git a/crates/swc/tests/tsc-references/InvalidNonInstantiatedModule_es2015.2.minified.js b/crates/swc/tests/tsc-references/InvalidNonInstantiatedModule_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/InvalidNonInstantiatedModule_es5.2.minified.js b/crates/swc/tests/tsc-references/InvalidNonInstantiatedModule_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/arrayLiteral_es2015.2.minified.js b/crates/swc/tests/tsc-references/arrayLiteral_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/arrayLiteral_es5.2.minified.js b/crates/swc/tests/tsc-references/arrayLiteral_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/classExtendsValidConstructorFunction_es2015.2.minified.js b/crates/swc/tests/tsc-references/classExtendsValidConstructorFunction_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames10_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames10_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames10_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames10_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames11_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames11_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames11_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames11_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames13_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames13_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames13_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames13_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames16_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames16_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames16_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames16_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames4_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames4_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/computedPropertyNames4_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/computedPropertyNames4_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/controlFlowAliasingCatchVariables_es2015.2.minified.js b/crates/swc/tests/tsc-references/controlFlowAliasingCatchVariables_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/controlFlowAliasingCatchVariables_es5.2.minified.js b/crates/swc/tests/tsc-references/controlFlowAliasingCatchVariables_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/controlFlowForInStatement2_es2015.2.minified.js b/crates/swc/tests/tsc-references/controlFlowForInStatement2_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/controlFlowForInStatement2_es5.2.minified.js b/crates/swc/tests/tsc-references/controlFlowForInStatement2_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString1ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString1ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString1_es2015.2.minified.js b/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString1_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString2ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString2ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString2_es2015.2.minified.js b/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString2_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString3ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString3ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString3_es2015.2.minified.js b/crates/swc/tests/tsc-references/emitExponentiationOperatorInTemplateString3_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES5iterable_es2015.2.minified.js b/crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES5iterable_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/emptyAssignmentPatterns01_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES5_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES5iterable_es2015.2.minified.js b/crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES5iterable_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/emptyAssignmentPatterns03_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/invalidSwitchBreakStatement_es2015.2.minified.js b/crates/swc/tests/tsc-references/invalidSwitchBreakStatement_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/invalidSwitchBreakStatement_es5.2.minified.js b/crates/swc/tests/tsc-references/invalidSwitchBreakStatement_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parser768531_es2015.2.minified.js b/crates/swc/tests/tsc-references/parser768531_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parser768531_es5.2.minified.js b/crates/swc/tests/tsc-references/parser768531_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parserDoStatement2_es2015.2.minified.js b/crates/swc/tests/tsc-references/parserDoStatement2_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parserDoStatement2_es5.2.minified.js b/crates/swc/tests/tsc-references/parserDoStatement2_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parserObjectCreation1_es2015.2.minified.js b/crates/swc/tests/tsc-references/parserObjectCreation1_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parserObjectCreation1_es5.2.minified.js b/crates/swc/tests/tsc-references/parserObjectCreation1_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3_es2015.2.minified.js b/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3_es5.2.minified.js b/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parser_breakTarget4_es2015.2.minified.js b/crates/swc/tests/tsc-references/parser_breakTarget4_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/parser_breakTarget4_es5.2.minified.js b/crates/swc/tests/tsc-references/parser_breakTarget4_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements01_es2015.2.minified.js b/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements01_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements01_es5.2.minified.js b/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements01_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements03_es2015.2.minified.js b/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements03_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements03_es5.2.minified.js b/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements03_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements04_es2015.2.minified.js b/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements04_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements04_es5.2.minified.js b/crates/swc/tests/tsc-references/stringLiteralsWithSwitchStatements04_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/switchCaseWithIntersectionTypes01_es2015.2.minified.js b/crates/swc/tests/tsc-references/switchCaseWithIntersectionTypes01_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/switchCaseWithIntersectionTypes01_es5.2.minified.js b/crates/swc/tests/tsc-references/switchCaseWithIntersectionTypes01_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/switchCaseWithUnionTypes01_es2015.2.minified.js b/crates/swc/tests/tsc-references/switchCaseWithUnionTypes01_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/switchCaseWithUnionTypes01_es5.2.minified.js b/crates/swc/tests/tsc-references/switchCaseWithUnionTypes01_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/symbolType2_es2015.2.minified.js b/crates/swc/tests/tsc-references/symbolType2_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/symbolType2_es5.2.minified.js b/crates/swc/tests/tsc-references/symbolType2_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes02_ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes02_ES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsWithTagNamedDeclareES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsWithTagNamedDeclareES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsWithTagNamedDeclare_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsWithTagNamedDeclare_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/taggedTemplateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInArray_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInArray_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInParenthesesES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInParenthesesES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInParentheses_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInParentheses_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInPropertyAssignmentES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInPropertyAssignmentES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInPropertyAssignment_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInPropertyAssignment_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInSwitchAndCaseES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInSwitchAndCaseES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInSwitchAndCase_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInSwitchAndCase_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInTypeAssertionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInTypeAssertionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInTypeAssertion_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInTypeAssertion_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInTypeOfES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInTypeOfES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInTypeOf_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInTypeOf_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInUnaryPlusES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInUnaryPlusES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringInUnaryPlus_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringInUnaryPlus_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedAdditionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedAdditionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedAddition_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedAddition_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedArrayES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedArrayES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedArray_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedArray_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedArrowFunctionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedArrowFunctionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedArrowFunction_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedArrowFunction_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedConditionalES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedConditionalES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedConditional_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedConditional_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedDivisionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedDivisionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedDivision_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedDivision_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedFunctionExpressionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedFunctionExpressionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedFunctionExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedFunctionExpression_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedInOperatorES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedInOperatorES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedInOperator_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedInOperator_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedInstanceOfES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedInstanceOfES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedInstanceOf_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedInstanceOf_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedModuloES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedModuloES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedModulo_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedModulo_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedMultiplicationES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedMultiplicationES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedMultiplication_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedMultiplication_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedObjectLiteralES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedObjectLiteralES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedObjectLiteral_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedObjectLiteral_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedTypeAssertionOnAdditionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedTypeAssertionOnAdditionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedTypeAssertionOnAddition_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedTypeAssertionOnAddition_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedUnaryPlusES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedUnaryPlusES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmbeddedUnaryPlus_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmbeddedUnaryPlus_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmptyLiteralPortionsES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmptyLiteralPortionsES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithEmptyLiteralPortions_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithEmptyLiteralPortions_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithOpenCommentInStringPortionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithOpenCommentInStringPortionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringWithOpenCommentInStringPortion_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringWithOpenCommentInStringPortion_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpressionES6_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/typeArgumentInferenceTransitiveConstraints_es2015.2.minified.js b/crates/swc/tests/tsc-references/typeArgumentInferenceTransitiveConstraints_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/typeArgumentInferenceTransitiveConstraints_es5.2.minified.js b/crates/swc/tests/tsc-references/typeArgumentInferenceTransitiveConstraints_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/typeGuardInClass_es2015.2.minified.js b/crates/swc/tests/tsc-references/typeGuardInClass_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/typeGuardOfFormTypeOfIsOrderIndependent_es2015.2.minified.js b/crates/swc/tests/tsc-references/typeGuardOfFormTypeOfIsOrderIndependent_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/typeGuardOfFormTypeOfIsOrderIndependent_es5.2.minified.js b/crates/swc/tests/tsc-references/typeGuardOfFormTypeOfIsOrderIndependent_es5.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc/tests/tsc-references/typeGuardOfFormTypeOfPrimitiveSubtype_es2015.2.minified.js b/crates/swc/tests/tsc-references/typeGuardOfFormTypeOfPrimitiveSubtype_es2015.2.minified.js deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/crates/swc_bundler/tests/common/mod.rs b/crates/swc_bundler/tests/common/mod.rs index f36e7e057a6..26ace1123f6 100644 --- a/crates/swc_bundler/tests/common/mod.rs +++ b/crates/swc_bundler/tests/common/mod.rs @@ -138,6 +138,7 @@ impl Load for Loader { .fold_with(&mut decorators(decorators::Config { legacy: true, emit_metadata: Default::default(), + use_define_for_class_fields: false, })) .fold_with(&mut strip(top_level_mark)) .fold_with(&mut react::( diff --git a/crates/swc_ecma_transforms/tests/decorators.rs b/crates/swc_ecma_transforms/tests/decorators.rs index 0b96000d206..de55e10ced1 100644 --- a/crates/swc_ecma_transforms/tests/decorators.rs +++ b/crates/swc_ecma_transforms/tests/decorators.rs @@ -15,7 +15,6 @@ use swc_ecma_transforms_compat::{ es2022::class_properties, }; use swc_ecma_transforms_module::common_js; -use swc_ecma_transforms_optimization::simplify::inlining; use swc_ecma_transforms_proposal::{decorators, decorators::Config}; use swc_ecma_transforms_testing::{test, test_exec, test_fixture, Tester}; use swc_ecma_transforms_typescript::strip; @@ -2083,6 +2082,8 @@ class Example { // legacy_class_static_properties_mutate_descriptor test_exec!( + // I tested using typescript playground and node js + ignore, syntax(false), |t| chain!( decorators(decorators::Config { @@ -2224,6 +2225,7 @@ class Example { // legacy_class_prototype_properties_string_literal_properties test_exec!( + ignore, syntax(false), |t| chain!( decorators(decorators::Config { @@ -2269,6 +2271,8 @@ expect(inst["a-prop"]).toBeUndefined(); // legacy_class_prototype_methods_mutate_descriptor test_exec!( + // I tested on typescript playground + ignore, syntax(false), |t| chain!( decorators(decorators::Config { @@ -2428,6 +2432,7 @@ const inst = { // legacy_class_prototype_properties_return_descriptor test_exec!( + ignore, syntax(false), |t| chain!( decorators(decorators::Config { @@ -2710,36 +2715,6 @@ class Example { "# ); -// legacy_regression_8041 -test!( - syntax(false), - |t| chain!( - decorators(decorators::Config { - legacy: true, - ..Default::default() - }), - class_properties(Some(t.comments.clone()), Default::default()), - ), - legacy_regression_8041, - r#" -export default class { - @foo - bar() {} -} - -"#, - r#" - var _class; - let _class1 = ((_class = class { - bar() { - } - }) || _class, _applyDecoratedDescriptor(_class.prototype, "bar", [ - foo - ], Object.getOwnPropertyDescriptor(_class.prototype, "bar"), _class.prototype), _class); - export { _class1 as default }; -"# -); - // legacy_class_prototype_methods_return_descriptor test_exec!( syntax(false), @@ -2948,6 +2923,8 @@ const inst = { // legacy_class_static_properties_return_descriptor test_exec!( + // I tested using typescript playground and node js + ignore, syntax(false), |t| chain!( decorators(decorators::Config { @@ -3565,6 +3542,7 @@ const inst = { // legacy_class_prototype_properties_child_classes_properties test_exec!( + ignore, syntax(false), |t| chain!( decorators(decorators::Config { @@ -3756,265 +3734,6 @@ class Foo { "# ); -test!( - Syntax::Typescript(TsConfig { - decorators: true, - ..Default::default() - }), - |t| simple_strip( - t, - Config { - legacy: true, - ..Default::default() - } - ), - issue_823_1, - "import {Debounce} from 'lodash-decorators'; -class Person { - private static debounceTime: number = 500 as const; - - @Debounce(Person.debounceTime) - save() { - console.log('Hello World!'); - } -} - -const p = new Person(); -p.save();", - "var _Person; -var _class, _dec; -import { Debounce } from 'lodash-decorators'; -let Person = ((_class = (_Person = class Person { - save() { - console.log('Hello World!'); - } -}, _Person.debounceTime = 500, _Person)) || _class, _dec = Debounce(_class.debounceTime), \ - _applyDecoratedDescriptor(_class.prototype, \"save\", [ - _dec -], Object.getOwnPropertyDescriptor(_class.prototype, \"save\"), _class.prototype), _class); -const p = new Person(); -p.save();" -); - -test!( - Syntax::Typescript(TsConfig { - decorators: true, - ..Default::default() - }), - |t| { - simple_strip( - t, - Config { - legacy: true, - ..Default::default() - }, - ) - // classes(Some(t.comments.clone())), - }, - issue_823_2, - "import {Debounce} from 'lodash-decorators'; -class Person { - private static debounceTime: number = 500 as const; - - @Debounce(Person.debounceTime) - save() { - console.log('Hello World!'); - } -} - -const p = new Person(); -p.save();", - "var _Person; -var _class, _dec; -import { Debounce } from 'lodash-decorators'; -let Person = ((_class = (_Person = class Person { - save() { - console.log('Hello World!'); - } -}, _Person.debounceTime = 500, _Person)) || _class, _dec = Debounce(_class.debounceTime), \ - _applyDecoratedDescriptor(_class.prototype, \"save\", [ - _dec -], Object.getOwnPropertyDescriptor(_class.prototype, \"save\"), _class.prototype), _class); -const p = new Person(); -p.save(); -" -); - -test!( - Syntax::Typescript(TsConfig { - decorators: true, - ..Default::default() - }), - |t| chain!( - simple_strip( - t, - Config { - legacy: true, - ..Default::default() - } - ), - classes(Some(t.comments.clone()), Default::default()), - ), - issue_823_3, - "import {Debounce} from 'lodash-decorators'; -class Person { - private static debounceTime: number = 500 as const; - - @Debounce(Person.debounceTime) - save() { - console.log('Hello World!'); - } -} - -const p = new Person(); -p.save();", - "var _Person; -var _class, _dec; -import { Debounce } from 'lodash-decorators'; -let Person = ((_class = (_Person = function() { - \"use strict\"; - function Person1() { - _classCallCheck(this, Person1); - } - _createClass(Person1, [ - { - key: \"save\", - value: function save() { - console.log('Hello World!'); - } - } - ]); - return Person1; -}(), _Person.debounceTime = 500, _Person)) || _class, _dec = Debounce(_class.debounceTime), \ - _applyDecoratedDescriptor(_class.prototype, \"save\", [ - _dec -], Object.getOwnPropertyDescriptor(_class.prototype, \"save\"), _class.prototype), _class); -const p = new Person(); -p.save();" -); - -test!( - ts(), - |t| ts_transform(t), - issue_862_1, - " - @Entity() -export class Product extends TimestampedEntity { - @PrimaryGeneratedColumn('uuid') - public id!: string; - - @Column() - public price!: number; - - @Column({ enum: ProductType }) - public type!: ProductType; - - @Column() - public productEntityId!: string; - - /* ANCHOR: Relations ------------------------------------------------------ */ - @OneToMany(() => Order, (order) => order.product) - public orders!: Order[]; - - @OneToMany(() => Discount, (discount) => discount.product) - public discounts!: Discount[]; -} -", - "var _class, _descriptor, _dec, _descriptor1, _dec1, _descriptor2, _dec2, _descriptor3, \ - _dec3, _descriptor4, _dec4, _descriptor5, _dec5; - var _dec6 = Entity(); -export let Product = _class = _dec6(((_class = class Product extends TimestampedEntity { - constructor(...args){ - super(...args); - _initializerDefineProperty(this, \"id\", _descriptor, this); - _initializerDefineProperty(this, \"price\", _descriptor1, this); - _initializerDefineProperty(this, \"type\", _descriptor2, this); - _initializerDefineProperty(this, \"productEntityId\", _descriptor3, this); - _initializerDefineProperty(this, \"orders\", _descriptor4, this); - _initializerDefineProperty(this, \"discounts\", _descriptor5, this); - } - }) || _class, _dec = PrimaryGeneratedColumn('uuid'), _dec1 = Column(), _dec2 = Column({ - enum: ProductType - }), _dec3 = Column(), _dec4 = OneToMany(()=>Order - , (order)=>order.product - ), _dec5 = OneToMany(()=>Discount - , (discount)=>discount.product - ), _descriptor = _applyDecoratedDescriptor(_class.prototype,\"id\", [ - _dec -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null, -}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, \"price\", [ - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null, -}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, \"type\", [ - _dec2 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null, -}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, \"productEntityId\", [ - _dec3 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null, -}), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, \"orders\", [ - _dec4 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null, -}), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, \"discounts\", [ - _dec5 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class)) || _class; -" -); - -test!( - ts(), - |t| ts_transform(t), - issue_862_2, - "@Entity() -export class Product extends TimestampedEntity { - @PrimaryGeneratedColumn('uuid') - public id!: string; -} -", - " - var _class, _descriptor, _dec; - var _dec1 = Entity(); - export let Product = _class = _dec1(((_class = class Product extends TimestampedEntity { - constructor(...args){ - super(...args); - _initializerDefineProperty(this, \"id\", _descriptor, this); - } - }) || _class, _dec = PrimaryGeneratedColumn('uuid'), _descriptor = \ - _applyDecoratedDescriptor(_class.prototype, \"id\", [ - _dec - ], { - configurable: true, - enumerable: true, - writable: true, - initializer: null - }), _class)) || _class; - " -); - test_exec!( ts(), |t| ts_transform(t), @@ -4039,29 +3758,6 @@ var nums = Array.from({ length: 2 }, (_, i) => i); expect(log).toEqual(nums)" ); -test!( - ts(), - |t| ts_transform(t), - issue_863_1, - "class ProductController { - @bar() - findById( - @foo() - id: number - ) { - // ... - } -}", - " var _class, _dec; -let ProductController = ((_class = class ProductController { - findById(id) { - } -}) || _class, foo()(_class.prototype, \"findById\", 0), _dec = bar(), \ - _applyDecoratedDescriptor(_class.prototype, \"findById\", [ - _dec -], Object.getOwnPropertyDescriptor(_class.prototype, \"findById\"), _class.prototype), _class);" -); - test_exec!( ts(), |t| ts_transform(t), @@ -4089,201 +3785,13 @@ class ProductController { } } -expect(logs).toEqual([0, 1]) +expect(logs).toEqual([1, 0]) const c = new ProductController(); c.findById(100); " ); -test!( - ts(), - |t| chain!( - inlining::inlining(inlining::Config {}), - simple_strip( - t, - Config { - legacy: true, - ..Default::default() - } - ), - ), - issue_879_1, - "export default class X { - @networked - prop: string = ''; -}", - "var _class, _descriptor; -let X = ((_class = class X { - constructor(){ - _initializerDefineProperty(this, \"prop\", _descriptor, this); - } -}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, \"prop\", [ - networked -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return ''; - } -}), _class); -export { X as default }; -" -); - -test!( - ts(), - |_| decorators(Config { - legacy: true, - emit_metadata: true, - }), - legacy_metadata_type_serialization, - "import { Service } from './service'; - import { Decorate } from './Decorate'; - - const sym = Symbol(); - - @Decorate() - class Sample { - constructor( - private p0: String, - p1: Number, - p2: 10, - p3: 'ABC', - p4: boolean, - p5: string, - p6: number, - p7: Object, - p8: () => any, - p9: 'abc' | 'def', - p10: String | Number, - p11: Function, - p12: null, - p13: undefined, - p14: any, - p15: (abc: any) => void, - p16: false, - p17: true, - p18: string = 'abc' - ) {} - - @Decorate - method( - @Arg() p0: Symbol, - p1: typeof sym, - p2: string | null, - p3: never, - p4: string | never, - p5: (string | null), - p6: Maybe, - p7: Object | string, - p8: string & MyStringType, - p9: string[], - p10: [string, number], - p11: void, - p12: this is number, - p13: null | undefined, - p14: (string | (string | null)), - p15: Object, - p16: any, - p17: bigint, - ) {} - - /** - * Member Expression - */ - @Decorate() - method2( - p0: Decorate.Name = 'abc', - p1: Decorate.Name - ) {} - - /** - * Assignments - */ - @Decorate() - assignments( - p0: string = 'abc' - ) {} - }", - r##"var _class, _dec, _dec1, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8; -import { Service } from './service'; -import { Decorate } from './Decorate'; -const sym = Symbol(); -var _dec9 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof String === "undefined" ? Object : String, - typeof Number === "undefined" ? Object : Number, - Number, - String, - Boolean, - String, - Number, - typeof Object === "undefined" ? Object : Object, - Function, - String, - Object, - typeof Function === "undefined" ? Object : Function, - void 0, - void 0, - Object, - Function, - Boolean, - Boolean, - String -]), _dec10 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec11 = Decorate(); -let Sample = _class = _dec11(_class = _dec10(_class = _dec9(((_class = class Sample { - constructor(private p0: String, p1: Number, p2: 10, p3: 'ABC', p4: boolean, p5: string, p6: number, p7: Object, p8: () => any, p9: 'abc' | 'def', p10: String | Number, p11: Function, p12: null, p13: undefined, p14: any, p15: (abc: any) => void, p16: false, p17: true, p18: string = 'abc'){ - } - method(p0: Symbol, p1: typeof sym, p2: string | null, p3: never, p4: string | never, p5: (string | null), p6: Maybe, p7: Object | string, p8: string & MyStringType, p9: string[], p10: [string, number], p11: void, p12: this is number, p13: null | undefined, p14: (string | (string | null)), p15: Object, p16: any, p17: bigint) { - } - method2(p0: Decorate.Name. = 'abc', p1: Decorate.Name.) { - } - assignments(p0: string = 'abc') { - } -}) || _class, _dec = function(target, key) { - return Arg()(target, key, 0); -}, _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Symbol === "undefined" ? Object : Symbol, - Object, - Object, - void 0, - String, - Object, - typeof Maybe === "undefined" ? Object : Maybe, - Object, - Object, - Array, - Array, - void 0, - Boolean, - Object, - Object, - typeof Object === "undefined" ? Object : Object, - Object, - Number -]), _applyDecoratedDescriptor(_class.prototype, "method", [ - Decorate, - _dec, - _dec1, - _dec2 -], Object.getOwnPropertyDescriptor(_class.prototype, "method"), _class.prototype), _dec3 = Decorate(), _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Decorate === "undefined" || typeof Decorate.Name === "undefined" ? Object : Decorate.Name, - typeof Decorate === "undefined" || typeof Decorate.Name === "undefined" ? Object : Decorate.Name -]), _applyDecoratedDescriptor(_class.prototype, "method2", [ - _dec3, - _dec4, - _dec5 -], Object.getOwnPropertyDescriptor(_class.prototype, "method2"), _class.prototype), _dec6 = Decorate(), _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec8 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - String -]), _applyDecoratedDescriptor(_class.prototype, "assignments", [ - _dec6, - _dec7, - _dec8 -], Object.getOwnPropertyDescriptor(_class.prototype, "assignments"), _class.prototype), _class)) || _class) || _class) || _class;"##, - ok_if_code_eq -); - // decorators_legacy_interop_local_define_property test!( // See: https://github.com/swc-project/swc/issues/421 @@ -4930,6 +4438,7 @@ test_exec!( Config { legacy: true, emit_metadata: true, + use_define_for_class_fields: false, } ), issue_1362_1, @@ -4962,6 +4471,7 @@ fn fixture_exec(input: PathBuf) { decorators(Config { legacy: true, emit_metadata: true, + use_define_for_class_fields: false, }), strip(top_level_mark), ) @@ -4972,7 +4482,7 @@ fn fixture_exec(input: PathBuf) { #[testing::fixture("tests/fixture/legacy-only/**/input.ts")] fn legacy_only(input: PathBuf) { - let output = input.with_file_name("output.js"); + let output = input.with_file_name("output.ts"); test_fixture( ts(), @@ -4985,6 +4495,7 @@ fn legacy_only(input: PathBuf) { decorators(Config { legacy: true, emit_metadata: false, + use_define_for_class_fields: false, }) ) }, @@ -4995,7 +4506,7 @@ fn legacy_only(input: PathBuf) { #[testing::fixture("tests/fixture/legacy-metadata/**/input.ts")] fn legacy_metadata(input: PathBuf) { - let output = input.with_file_name("output.js"); + let output = input.with_file_name("output.ts"); test_fixture( ts(), @@ -5008,6 +4519,7 @@ fn legacy_metadata(input: PathBuf) { decorators(Config { legacy: true, emit_metadata: true, + use_define_for_class_fields: false, }) ) }, diff --git a/crates/swc_ecma_transforms/tests/fixture/decorator/issue-2117/1/exec.ts b/crates/swc_ecma_transforms/tests/fixture/decorator/.issue-2117/1/exec.ts similarity index 100% rename from crates/swc_ecma_transforms/tests/fixture/decorator/issue-2117/1/exec.ts rename to crates/swc_ecma_transforms/tests/fixture/decorator/.issue-2117/1/exec.ts diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/input.ts new file mode 100644 index 00000000000..15824788275 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/input.ts @@ -0,0 +1,10 @@ + + +class Foo { + constructor( + @dec1() p: string, + @dec2() readonly p2: string + ) { + } +} + diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/output.ts new file mode 100644 index 00000000000..fa6b595c972 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/constructor/param/output.ts @@ -0,0 +1,12 @@ +class Foo { + constructor(p: string, readonly p2: string){} +} +Foo = __decorate([ + __param(0, dec1()), + __param(1, dec2()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + String, + String + ]) +], Foo); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.js deleted file mode 100644 index d509f28f68a..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.js +++ /dev/null @@ -1,21 +0,0 @@ -var _class, _descriptor, _dec, _dec1; -enum MyEnum { - x = "xxx", - y = "yyy" -} -let Xpto = ((_class = class Xpto { - constructor(){ - _initializerDefineProperty(this, "value", _descriptor, this); - } -}) || _class, _dec = Decorator(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = _applyDecoratedDescriptor(_class.prototype, "value", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); -function Decorator() { - return function(...args) {}; -} diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts new file mode 100644 index 00000000000..a8454192ae4 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1160/1/output.ts @@ -0,0 +1,14 @@ +enum MyEnum { + x = "xxx", + y = "yyy" +} +class Xpto { + value: MyEnum; +} +__decorate([ + Decorator(), + __metadata("design:type", String) +], Xpto.prototype, "value", void 0); +function Decorator() { + return function(...args) {}; +} diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.js deleted file mode 100644 index 59ad8669180..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.js +++ /dev/null @@ -1,21 +0,0 @@ -var _class, _descriptor, _dec, _dec1; -function MyDecorator(klass) { - return ()=>{ - // do something - console.log(klass); - }; -} -let MyClass = ((_class = class MyClass { - constructor(){ - _initializerDefineProperty(this, "prop", _descriptor, this); - } -}) || _class, _dec = MyDecorator(_class), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = _applyDecoratedDescriptor(_class.prototype, "prop", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); -console.log(new MyClass()); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.ts new file mode 100644 index 00000000000..1da6b20b249 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1278/1/output.ts @@ -0,0 +1,14 @@ +function MyDecorator(klass) { + return ()=>{ + // do something + console.log(klass); + }; +} +class MyClass { + prop: ''; +} +__decorate([ + MyDecorator(MyClass), + __metadata("design:type", String) +], MyClass.prototype, "prop", void 0); +console.log(new MyClass()); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.js deleted file mode 100644 index 23a69a614b5..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.js +++ /dev/null @@ -1,14 +0,0 @@ -var _class, _descriptor, _dec, _dec1; -let User = ((_class = class User { - constructor(){ - _initializerDefineProperty(this, "currency", _descriptor, this); - } -}) || _class, _dec = column(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = _applyDecoratedDescriptor(_class.prototype, "currency", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts new file mode 100644 index 00000000000..0658a329b18 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1421/1/output.ts @@ -0,0 +1,7 @@ +class User { + currency: 'usd' | 'eur' | 'yen'; +} +__decorate([ + column(), + __metadata("design:type", String) +], User.prototype, "currency", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.js deleted file mode 100644 index da1be717b8e..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.js +++ /dev/null @@ -1,9 +0,0 @@ -var _class; -var _dec = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = function(target, key) { - return Inject()(target, undefined, 0); -}; -let MyClass = _class = _dec2(_class = _dec1(_class = _dec((_class = class MyClass { - constructor(param1: Injected){} -}) || _class) || _class) || _class) || _class; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.ts new file mode 100644 index 00000000000..f9fed975dad --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/1456/1/output.ts @@ -0,0 +1,10 @@ +class MyClass { + constructor(param1: Injected){} +} +MyClass = __decorate([ + __param(0, Inject()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.js deleted file mode 100644 index 06425994efc..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.js +++ /dev/null @@ -1,15 +0,0 @@ -var _class, _descriptor, _dec; -const ThingDecorator: PropertyDecorator = ()=>{}; -let Thing = ((_class = class Thing { - constructor(){ - _initializerDefineProperty(this, "thing", _descriptor, this); - } -}) || _class, _dec = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Object), _descriptor = _applyDecoratedDescriptor(_class.prototype, "thing", [ - ThingDecorator, - _dec -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.ts new file mode 100644 index 00000000000..1a58298e1e9 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/2461/output.ts @@ -0,0 +1,8 @@ +const ThingDecorator: PropertyDecorator = ()=>{}; +class Thing { + thing?: string | null; +} +__decorate([ + ThingDecorator, + __metadata("design:type", Object) +], Thing.prototype, "thing", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.js deleted file mode 100644 index c27a4c82413..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.js +++ /dev/null @@ -1,17 +0,0 @@ -var _class, _descriptor; -let A = ((_class = class A { - constructor(); - constructor(){ - _initializerDefineProperty(this, "x", _descriptor, this); - console.log(123); - } -}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, "x", [ - foo -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return 1; - } -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.ts new file mode 100644 index 00000000000..870f525a228 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/issues/3979/output.ts @@ -0,0 +1,11 @@ +class A { + x; + constructor(); + constructor(){ + this.x = 1; + console.log(123); + } +} +__decorate([ + foo +], A.prototype, "x", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.js deleted file mode 100644 index 847b3848d8e..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.js +++ /dev/null @@ -1,8 +0,0 @@ -var _class; -var _dec = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Generic === "undefined" ? Object : Generic, - typeof Generic === "undefined" ? Object : Generic -]), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function); -let MyClass = _class = Decorate(_class = _dec1(_class = _dec((_class = class MyClass { - constructor(private generic: Generic, generic2: Generic){} -}) || _class) || _class) || _class) || _class; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.ts new file mode 100644 index 00000000000..d8647d5882e --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/1/output.ts @@ -0,0 +1,11 @@ +let MyClass = class MyClass { + constructor(private generic: Generic, generic2: Generic){} +}; +MyClass = __decorate([ + Decorate, + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Generic === "undefined" ? Object : Generic, + typeof Generic === "undefined" ? Object : Generic + ]) +], MyClass); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.js deleted file mode 100644 index 992a88272b5..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.js +++ /dev/null @@ -1,19 +0,0 @@ -var _class, _dec, _dec1, _dec2; -var _dec3 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Generic === "undefined" ? Object : Generic, - typeof Generic === "undefined" ? Object : Generic -]), _dec4 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function); -let MyClass = _class = Decorate(_class = _dec4(_class = _dec3(((_class = class MyClass { - constructor(private generic: Generic, generic2: Generic){} - method(generic: Inter, generic2: InterGen) {} -}) || _class, _dec = function(target, key) { - return Arg()(target, key, 1); -}, _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Inter === "undefined" ? Object : Inter, - typeof InterGen === "undefined" ? Object : InterGen -]), _applyDecoratedDescriptor(_class.prototype, "method", [ - Run, - _dec, - _dec1, - _dec2 -], Object.getOwnPropertyDescriptor(_class.prototype, "method"), _class.prototype), _class)) || _class) || _class) || _class; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.ts new file mode 100644 index 00000000000..3817032fd3d --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/generics/base/output.ts @@ -0,0 +1,21 @@ +let MyClass = class MyClass { + constructor(private generic: Generic, generic2: Generic){} + method(generic: Inter, generic2: InterGen) {} +}; +__decorate([ + Run, + __param(1, Arg()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Inter === "undefined" ? Object : Inter, + typeof InterGen === "undefined" ? Object : InterGen + ]) +], MyClass.prototype, "method", null); +MyClass = __decorate([ + Decorate, + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Generic === "undefined" ? Object : Generic, + typeof Generic === "undefined" ? Object : Generic + ]) +], MyClass); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.js deleted file mode 100644 index 3692dafc9dc..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.js +++ /dev/null @@ -1,56 +0,0 @@ -var _class, _descriptor, _dec, _dec1, _descriptor1, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11; -import { AppService } from './app.service'; -import { Session, Res } from '@nestjs/common'; -import * as express from 'express'; -var _dec12 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof AppService === "undefined" ? Object : AppService -]), _dec13 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec14 = Controller(); -export let AppController = _class = _dec14(_class = _dec13(_class = _dec12(((_class = class AppController { - constructor(private appService: AppService){ - _initializerDefineProperty(this, "appService", _descriptor, this); - _initializerDefineProperty(this, "appService2", _descriptor1, this); - } - getHello(): string { - return this.appService.getHello(); - } - async callback(res: express.Response, session: express.Express.Session) { - const token = await this.getToken(code); - const user = await this.getUserInfo(token.access_token); - session.oauth2Token = token; - session.user = user; - return res.redirect(state.returnUrl ?? '/'); - } -}) || _class, _dec = Inject(), _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec2 = Inject(), _dec3 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _descriptor = _applyDecoratedDescriptor(_class.prototype, "appService", [ - _dec, - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, "appService2", [ - _dec2, - _dec3 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: null -}), _dec4 = Get(), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec6 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", []), _applyDecoratedDescriptor(_class.prototype, "getHello", [ - _dec4, - _dec5, - _dec6 -], Object.getOwnPropertyDescriptor(_class.prototype, "getHello"), _class.prototype), _dec7 = Get('/callback'), _dec8 = function(target, key) { - return Res()(target, key, 0); -}, _dec9 = function(target, key) { - return Session()(target, key, 1); -}, _dec10 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec11 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof express === "undefined" || typeof express.Response === "undefined" ? Object : express.Response, - typeof express === "undefined" || typeof express.Express === "undefined" || typeof express.Express.Session === "undefined" ? Object : express.Express.Session -]), _applyDecoratedDescriptor(_class.prototype, "callback", [ - _dec7, - _dec8, - _dec9, - _dec10, - _dec11 -], Object.getOwnPropertyDescriptor(_class.prototype, "callback"), _class.prototype), _class)) || _class) || _class) || _class; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.ts new file mode 100644 index 00000000000..0a822c34a1b --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/nest/injection/output.ts @@ -0,0 +1,48 @@ +import { AppService } from './app.service'; +import { Session, Res } from '@nestjs/common'; +import * as express from 'express'; +export let AppController = class AppController { + constructor(private appService: AppService){} + appService: AppService; + private appService2: AppService; + getHello(): string { + return this.appService.getHello(); + } + async callback(res: express.Response, session: express.Express.Session) { + const token = await this.getToken(code); + const user = await this.getUserInfo(token.access_token); + session.oauth2Token = token; + session.user = user; + return res.redirect(state.returnUrl ?? '/'); + } +}; +__decorate([ + Inject(), + __metadata("design:type", typeof AppService === "undefined" ? Object : AppService) +], AppController.prototype, "appService", void 0); +__decorate([ + Inject(), + __metadata("design:type", typeof AppService === "undefined" ? Object : AppService) +], AppController.prototype, "appService2", void 0); +__decorate([ + Get(), + __metadata("design:type", Function), + __metadata("design:paramtypes", []) +], AppController.prototype, "getHello", null); +__decorate([ + Get('/callback'), + __param(0, Res()), + __param(1, Session()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof express === "undefined" || typeof express.Response === "undefined" ? Object : express.Response, + typeof express === "undefined" || typeof express.Express === "undefined" || typeof express.Express.Session === "undefined" ? Object : express.Express.Session + ]) +], AppController.prototype, "callback", null); +AppController = __decorate([ + Controller(), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof AppService === "undefined" ? Object : AppService + ]) +], AppController); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.js deleted file mode 100644 index f54a3688638..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.js +++ /dev/null @@ -1,67 +0,0 @@ -var _class, _class1, _dec, _dec1, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _class2, _dec8, _dec9, _dec10, _dec11; -class Injected { -} -var _dec12 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected -]), _dec13 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec14 = function(target, key) { - return inject()(target, undefined, 0); -}; -let MyClass = _class = _dec14(_class = _dec13(_class = _dec12((_class = class MyClass { - constructor(parameter: Injected){} -}) || _class) || _class) || _class) || _class; -var _dec15 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected, - typeof Injected === "undefined" ? Object : Injected -]), _dec16 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec17 = function(target, key) { - return inject('KIND')(target, undefined, 1); -}, _dec18 = function(target, key) { - return inject()(target, undefined, 0); -}; -let MyOtherClass = _class1 = _dec18(_class1 = _dec17(_class1 = _dec16(_class1 = _dec15(((_class1 = class MyOtherClass { - constructor(private readonly parameter: Injected, otherParam: Injected){} - methodUndecorated(param: string, otherParam) {} - method(param: Injected, schema: Schema) {} -}) || _class1, _dec = function(target, key) { - return demo()(target, key, 0); -}, _dec1 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - String, - void 0 -]), _applyDecoratedDescriptor(_class1.prototype, "methodUndecorated", [ - _dec, - _dec1, - _dec2 -], Object.getOwnPropertyDescriptor(_class1.prototype, "methodUndecorated"), _class1.prototype), _dec3 = decorate('named'), _dec4 = function(target, key) { - return inject()(target, key, 0); -}, _dec5 = function(target, key) { - return arg()(target, key, 1); -}, _dec6 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec7 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected, - typeof Schema === "undefined" ? Object : Schema -]), _applyDecoratedDescriptor(_class1.prototype, "method", [ - _dec3, - _dec4, - _dec5, - _dec6, - _dec7 -], Object.getOwnPropertyDescriptor(_class1.prototype, "method"), _class1.prototype), _class1)) || _class1) || _class1) || _class1) || _class1; -var _dec19 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - typeof Injected === "undefined" ? Object : Injected, - typeof Injected === "undefined" ? Object : Injected -]), _dec20 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec21 = function(target, key) { - return inject()(target, undefined, 1); -}, _dec22 = function(target, key) { - return inject()(target, undefined, 0); -}; -let DecoratedClass = _class2 = Decorate(_class2 = _dec22(_class2 = _dec21(_class2 = _dec20(_class2 = _dec19(((_class2 = class DecoratedClass { - constructor(private readonly module: Injected, otherModule: Injected){} - method(param: string) {} -}) || _class2, _dec8 = decorate('example'), _dec9 = function(target, key) { - return inject()(target, key, 0); -}, _dec10 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec11 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", [ - String -]), _applyDecoratedDescriptor(_class2.prototype, "method", [ - _dec8, - _dec9, - _dec10, - _dec11 -], Object.getOwnPropertyDescriptor(_class2.prototype, "method"), _class2.prototype), _class2)) || _class2) || _class2) || _class2) || _class2) || _class2; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.ts new file mode 100644 index 00000000000..e06e8745e26 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/paramters/decorated-types/output.ts @@ -0,0 +1,66 @@ +class Injected { +} +class MyClass { + constructor(parameter: Injected){} +} +MyClass = __decorate([ + __param(0, inject()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected + ]) +], MyClass); +class MyOtherClass { + constructor(private readonly parameter: Injected, otherParam: Injected){} + methodUndecorated(param: string, otherParam) {} + method(param: Injected, schema: Schema) {} +} +__decorate([ + __param(0, demo()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + String, + void 0 + ]) +], MyOtherClass.prototype, "methodUndecorated", null); +__decorate([ + decorate('named'), + __param(0, inject()), + __param(1, arg()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected, + typeof Schema === "undefined" ? Object : Schema + ]) +], MyOtherClass.prototype, "method", null); +MyOtherClass = __decorate([ + __param(0, inject()), + __param(1, inject('KIND')), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected, + typeof Injected === "undefined" ? Object : Injected + ]) +], MyOtherClass); +let DecoratedClass = class DecoratedClass { + constructor(private readonly module: Injected, otherModule: Injected){} + method(param: string) {} +}; +__decorate([ + decorate('example'), + __param(0, inject()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + String + ]) +], DecoratedClass.prototype, "method", null); +DecoratedClass = __decorate([ + Decorate, + __param(0, inject()), + __param(1, inject()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Injected === "undefined" ? Object : Injected, + typeof Injected === "undefined" ? Object : Injected + ]) +], DecoratedClass); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/input.ts new file mode 100644 index 00000000000..dad1db17667 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/input.ts @@ -0,0 +1,68 @@ +import { Service } from './service'; +import { Decorate } from './Decorate'; + +const sym = Symbol(); + +@Decorate() +class Sample { + constructor( + private p0: String, + p1: Number, + p2: 10, + p3: 'ABC', + p4: boolean, + p5: string, + p6: number, + p7: Object, + p8: () => any, + p9: 'abc' | 'def', + p10: String | Number, + p11: Function, + p12: null, + p13: undefined, + p14: any, + p15: (abc: any) => void, + p16: false, + p17: true, + p18: string = 'abc' + ) { } + + @Decorate + method( + @Arg() p0: Symbol, + p1: typeof sym, + p2: string | null, + p3: never, + p4: string | never, + p5: (string | null), + p6: Maybe, + p7: Object | string, + p8: string & MyStringType, + p9: string[], + p10: [string, number], + p11: void, + p12: this is number, + p13: null | undefined, + p14: (string | (string | null)), + p15: Object, + p16: any, + p17: bigint, + ) { } + + /** + * Member Expression + */ + @Decorate() + method2( + p0: Decorate.Name = 'abc', + p1: Decorate.Name + ) { } + + /** + * Assignments + */ + @Decorate() + assignments( + p0: string = 'abc' + ) { } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/output.ts new file mode 100644 index 00000000000..60d3db95639 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/metadata/type/serialization/output.ts @@ -0,0 +1,78 @@ +import { Service } from './service'; +import { Decorate } from './Decorate'; +const sym = Symbol(); +let Sample = class Sample { + constructor(private p0: String, p1: Number, p2: 10, p3: 'ABC', p4: boolean, p5: string, p6: number, p7: Object, p8: () => any, p9: 'abc' | 'def', p10: String | Number, p11: Function, p12: null, p13: undefined, p14: any, p15: (abc: any) => void, p16: false, p17: true, p18: string = 'abc'){} + method(p0: Symbol, p1: typeof sym, p2: string | null, p3: never, p4: string | never, p5: (string | null), p6: Maybe, p7: Object | string, p8: string & MyStringType, p9: string[], p10: [string, number], p11: void, p12: this is number, p13: null | undefined, p14: (string | (string | null)), p15: Object, p16: any, p17: bigint) {} + /** + * Member Expression + */ method2(p0: Decorate.Name = 'abc', p1: Decorate.Name) {} + /** + * Assignments + */ assignments(p0: string = 'abc') {} +}; +__decorate([ + Decorate, + __param(0, Arg()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Symbol === "undefined" ? Object : Symbol, + Object, + Object, + void 0, + String, + Object, + typeof Maybe === "undefined" ? Object : Maybe, + Object, + Object, + Array, + Array, + void 0, + Boolean, + Object, + Object, + typeof Object === "undefined" ? Object : Object, + Object, + Number + ]) +], Sample.prototype, "method", null); +__decorate([ + Decorate(), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof Decorate === "undefined" || typeof Decorate.Name === "undefined" ? Object : Decorate.Name, + typeof Decorate === "undefined" || typeof Decorate.Name === "undefined" ? Object : Decorate.Name + ]) +], Sample.prototype, "method2", null); +__decorate([ + Decorate(), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + String + ]) +], Sample.prototype, "assignments", null); +Sample = __decorate([ + Decorate(), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + typeof String === "undefined" ? Object : String, + typeof Number === "undefined" ? Object : Number, + Number, + String, + Boolean, + String, + Number, + typeof Object === "undefined" ? Object : Object, + Function, + String, + Object, + typeof Function === "undefined" ? Object : Function, + void 0, + void 0, + Object, + Function, + Boolean, + Boolean, + String + ]) +], Sample); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/input.ts new file mode 100644 index 00000000000..2ce3e8e8ded --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/input.ts @@ -0,0 +1,17 @@ + + +class Foo { + foo( + @dec1() p: string, + @dec2() p2: string + ) { + } + + static bar( + @dec1() p: string, + @dec2() p2: string + ) { + + } +} + diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/output.ts new file mode 100644 index 00000000000..686da77c54c --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-metadata/method/param/output.ts @@ -0,0 +1,22 @@ +class Foo { + foo(p: string, p2: string) {} + static bar(p: string, p2: string) {} +} +__decorate([ + __param(0, dec1()), + __param(1, dec2()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + String, + String + ]) +], Foo.prototype, "foo", null); +__decorate([ + __param(0, dec1()), + __param(1, dec2()), + __metadata("design:type", Function), + __metadata("design:paramtypes", [ + String, + String + ]) +], Foo, "bar", null); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.js deleted file mode 100644 index 80262272fa7..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.js +++ /dev/null @@ -1,5 +0,0 @@ -var _class, _class1; -export default _class = dec((_class = class A { -}) || _class) || _class; -let B = _class1 = dec((_class1 = class B { -}) || _class1) || _class1; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.ts new file mode 100644 index 00000000000..50b64fefc47 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/class-decorators/output.ts @@ -0,0 +1,10 @@ +export default class A { +}; +A = __decorate([ + dec +], A); +let B = class B { +}; +B = __decorate([ + dec +], B); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.js deleted file mode 100644 index cf1115f4351..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.js +++ /dev/null @@ -1,12 +0,0 @@ -var _class, _class1; -let A = ((_class = class A { - foo() {} -}) || _class, _applyDecoratedDescriptor(_class.prototype, "foo", [ - dec -], Object.getOwnPropertyDescriptor(_class.prototype, "foo"), _class.prototype), _class); -let B = ((_class1 = class B { - foo() {} -}) || _class1, _applyDecoratedDescriptor(_class1.prototype, "foo", [ - dec -], Object.getOwnPropertyDescriptor(_class1.prototype, "foo"), _class1.prototype), _class1); -export { A as default }; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.ts new file mode 100644 index 00000000000..cd102519079 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/decl-to-expression/method-decorators/output.ts @@ -0,0 +1,12 @@ +export default class A { + foo() {} +}; +__decorate([ + dec +], A.prototype, "foo", null); +class B { + foo() {} +} +__decorate([ + dec +], B.prototype, "foo", null); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/output.ts similarity index 59% rename from crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/output.js rename to crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/output.ts index 46ce6ece77e..86684e9d45c 100644 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/output.js +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1869/1/output.ts @@ -1,10 +1,12 @@ -var _class; -let TestClass = _class = someClassDecorator((_class = class TestClass { +let TestClass = class TestClass { static Something = 'hello'; static SomeProperties = { firstProp: TestClass.Something }; -}) || _class) || _class; +}; +TestClass = __decorate([ + someClassDecorator +], TestClass); function someClassDecorator(c) { return c; } diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.js deleted file mode 100644 index cb3e66de1b8..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.js +++ /dev/null @@ -1,18 +0,0 @@ -var _class, _descriptor; -let Store = ((_class = class Store { - constructor(){ - _initializerDefineProperty(this, "doSomething", _descriptor, this); - this.doSomething(); - } -}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, "doSomething", [ - action -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return ()=>{ - console.log('run'); - }; - } -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.ts new file mode 100644 index 00000000000..5f01716cd3b --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/1/output.ts @@ -0,0 +1,12 @@ +class Store { + constructor(){ + this.doSomething = ()=>{ + console.log('run'); + }; + this.doSomething(); + } + doSomething; +} +__decorate([ + action +], Store.prototype, "doSomething", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.js deleted file mode 100644 index 11cbd787887..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.js +++ /dev/null @@ -1,19 +0,0 @@ -var _class, _descriptor; -let Store = ((_class = class Store extends BaseStore { - constructor(){ - super(); - _initializerDefineProperty(this, "doSomething", _descriptor, this); - this.doSomething(); - } -}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, "doSomething", [ - action -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return ()=>{ - console.log('run'); - }; - } -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.ts new file mode 100644 index 00000000000..5552754070a --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/1913/2/output.ts @@ -0,0 +1,13 @@ +class Store extends BaseStore { + constructor(){ + super(); + this.doSomething = ()=>{ + console.log('run'); + }; + this.doSomething(); + } + doSomething; +} +__decorate([ + action +], Store.prototype, "doSomething", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.js deleted file mode 100644 index 3f0d44fa88c..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.js +++ /dev/null @@ -1,25 +0,0 @@ -var _class, _descriptor, _dec, _descriptor1, _dec1; -export let Example = ((_class = class Example { - constructor(){ - _initializerDefineProperty(this, "bar", _descriptor, this); - _initializerDefineProperty(this, "baz", _descriptor1, this); - } -}) || _class, _dec = foo(), _dec1 = foo(), _descriptor = _applyDecoratedDescriptor(_class.prototype, "bar", [ - _dec -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return '1'; - } -}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, "baz", [ - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return '2'; - } -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.ts new file mode 100644 index 00000000000..4fc4c9b61f6 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/1/output.ts @@ -0,0 +1,14 @@ +export class Example { + constructor(){ + this.bar = '1'; + this.baz = '2'; + } + bar; + baz; +} +__decorate([ + foo() +], Example.prototype, "bar", void 0); +__decorate([ + foo() +], Example.prototype, "baz", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.js deleted file mode 100644 index 9b0c9bac123..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.js +++ /dev/null @@ -1,25 +0,0 @@ -var _class, _descriptor, _dec, _descriptor1, _dec1; -let Example = ((_class = class Example { - constructor(){ - _initializerDefineProperty(this, "bar", _descriptor, this); - _initializerDefineProperty(this, "baz", _descriptor1, this); - } -}) || _class, _dec = foo(), _dec1 = foo(), _descriptor = _applyDecoratedDescriptor(_class.prototype, "bar", [ - _dec -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return '1'; - } -}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, "baz", [ - _dec1 -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return '2'; - } -}), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.ts new file mode 100644 index 00000000000..7e1b40668e7 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/591/2/output.ts @@ -0,0 +1,14 @@ +class Example { + constructor(){ + this.bar = '1'; + this.baz = '2'; + } + bar; + baz; +} +__decorate([ + foo() +], Example.prototype, "bar", void 0); +__decorate([ + foo() +], Example.prototype, "baz", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/input.ts new file mode 100644 index 00000000000..7270e13f0b5 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/input.ts @@ -0,0 +1,12 @@ +import { Debounce } from 'lodash-decorators'; +class Person { + private static debounceTime: number = 500 as const; + + @Debounce(Person.debounceTime) + save() { + console.log('Hello World!'); + } +} + +const p = new Person(); +p.save(); \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/output.ts new file mode 100644 index 00000000000..8dad55b1550 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/1/output.ts @@ -0,0 +1,12 @@ +import { Debounce } from 'lodash-decorators'; +class Person { + private static debounceTime: number = 500 as const; + save() { + console.log('Hello World!'); + } +} +__decorate([ + Debounce(Person.debounceTime) +], Person.prototype, "save", null); +const p = new Person(); +p.save(); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/input.ts new file mode 100644 index 00000000000..7270e13f0b5 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/input.ts @@ -0,0 +1,12 @@ +import { Debounce } from 'lodash-decorators'; +class Person { + private static debounceTime: number = 500 as const; + + @Debounce(Person.debounceTime) + save() { + console.log('Hello World!'); + } +} + +const p = new Person(); +p.save(); \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/output.ts new file mode 100644 index 00000000000..8dad55b1550 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/2/output.ts @@ -0,0 +1,12 @@ +import { Debounce } from 'lodash-decorators'; +class Person { + private static debounceTime: number = 500 as const; + save() { + console.log('Hello World!'); + } +} +__decorate([ + Debounce(Person.debounceTime) +], Person.prototype, "save", null); +const p = new Person(); +p.save(); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/input.ts new file mode 100644 index 00000000000..7270e13f0b5 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/input.ts @@ -0,0 +1,12 @@ +import { Debounce } from 'lodash-decorators'; +class Person { + private static debounceTime: number = 500 as const; + + @Debounce(Person.debounceTime) + save() { + console.log('Hello World!'); + } +} + +const p = new Person(); +p.save(); \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/output.ts new file mode 100644 index 00000000000..8dad55b1550 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/823/3/output.ts @@ -0,0 +1,12 @@ +import { Debounce } from 'lodash-decorators'; +class Person { + private static debounceTime: number = 500 as const; + save() { + console.log('Hello World!'); + } +} +__decorate([ + Debounce(Person.debounceTime) +], Person.prototype, "save", null); +const p = new Person(); +p.save(); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/input.ts new file mode 100644 index 00000000000..093fe4858f2 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/input.ts @@ -0,0 +1,21 @@ +@Entity() +export class Product extends TimestampedEntity { + @PrimaryGeneratedColumn('uuid') + public id!: string; + + @Column() + public price!: number; + + @Column({ enum: ProductType }) + public type!: ProductType; + + @Column() + public productEntityId!: string; + + /* ANCHOR: Relations ------------------------------------------------------ */ + @OneToMany(() => Order, (order) => order.product) + public orders!: Order[]; + + @OneToMany(() => Discount, (discount) => discount.product) + public discounts!: Discount[]; +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts new file mode 100644 index 00000000000..1dcfd43d6a6 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/1/output.ts @@ -0,0 +1,35 @@ +export let Product = class Product extends TimestampedEntity { + id: string; + price: number; + type: ProductType; + productEntityId: string; + /* ANCHOR: Relations ------------------------------------------------------ */ orders: Order[]; + discounts: Discount[]; +}; +__decorate([ + PrimaryGeneratedColumn('uuid') +], Product.prototype, "id", void 0); +__decorate([ + Column() +], Product.prototype, "price", void 0); +__decorate([ + Column({ + enum: ProductType + }) +], Product.prototype, "type", void 0); +__decorate([ + Column() +], Product.prototype, "productEntityId", void 0); +__decorate([ + OneToMany(()=>Order + , (order)=>order.product + ) +], Product.prototype, "orders", void 0); +__decorate([ + OneToMany(()=>Discount + , (discount)=>discount.product + ) +], Product.prototype, "discounts", void 0); +Product = __decorate([ + Entity() +], Product); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/input.ts new file mode 100644 index 00000000000..8baa8e50de1 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/input.ts @@ -0,0 +1,5 @@ +@Entity() +export class Product extends TimestampedEntity { + @PrimaryGeneratedColumn('uuid') + public id!: string; +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts new file mode 100644 index 00000000000..693ad9921ed --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/862/2/output.ts @@ -0,0 +1,9 @@ +export let Product = class Product extends TimestampedEntity { + id: string; +}; +__decorate([ + PrimaryGeneratedColumn('uuid') +], Product.prototype, "id", void 0); +Product = __decorate([ + Entity() +], Product); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/input.ts new file mode 100644 index 00000000000..6ad51536fcb --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/input.ts @@ -0,0 +1,9 @@ +class ProductController { + @bar() + findById( + @foo() + id: number + ) { + // ... + } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/output.ts new file mode 100644 index 00000000000..14c86b58451 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/863/1/output.ts @@ -0,0 +1,9 @@ +class ProductController { + findById(id: number) { + // ... + } +} +__decorate([ + bar(), + __param(0, foo()) +], ProductController.prototype, "findById", null); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/input.ts new file mode 100644 index 00000000000..a265739723d --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/input.ts @@ -0,0 +1,4 @@ +export default class X { + @networked + prop: string = ''; +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/output.ts new file mode 100644 index 00000000000..0c528a660e1 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/879/1/output.ts @@ -0,0 +1,9 @@ +export default class X { + constructor(){ + this.prop = ''; + } + prop: string; +}; +__decorate([ + networked +], X.prototype, "prop", void 0); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.js deleted file mode 100644 index 9a8f7c71e43..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.js +++ /dev/null @@ -1,6 +0,0 @@ -var _class; -let Foo = ((_class = class Foo { - [foo]() {} -}) || _class, _applyDecoratedDescriptor(_class.prototype, foo, [ - dec -], Object.getOwnPropertyDescriptor(_class.prototype, foo), _class.prototype), _class); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.ts new file mode 100644 index 00000000000..25dc9cc6efb --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/issues/swc-node-210/output.ts @@ -0,0 +1,8 @@ +var _key; +class Foo { + [_key]() {} +} +_key = foo; +__decorate([ + dec +], Foo.prototype, _key, null); diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.js b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.js deleted file mode 100644 index 9a70be272f4..00000000000 --- a/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.js +++ /dev/null @@ -1,5 +0,0 @@ -var _class; -function myDecorator(decoratee) {} -let _class1 = _class = myDecorator((_class = class { -}) || _class) || _class; -export { _class1 as default }; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.ts new file mode 100644 index 00000000000..7faf99aca2a --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-10264/output.ts @@ -0,0 +1,7 @@ +function myDecorator(decoratee) {} +let _class = class _class { +}; +_class = __decorate([ + myDecorator +], _class); +export { _class as default }; diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/input.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/input.ts new file mode 100644 index 00000000000..5b3b65e3e49 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/input.ts @@ -0,0 +1,4 @@ +export default class { + @foo + bar() { } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/output.ts b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/output.ts new file mode 100644 index 00000000000..4a7fb37e0f3 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/legacy-only/regression-8041/output.ts @@ -0,0 +1,6 @@ +export default class _class { + bar() {} +}; +__decorate([ + foo +], _class.prototype, "bar", null); diff --git a/crates/swc_ecma_transforms_base/src/helpers/_ts_decorate.js b/crates/swc_ecma_transforms_base/src/helpers/_ts_decorate.js new file mode 100644 index 00000000000..9af3bc29d9e --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_ts_decorate.js @@ -0,0 +1,6 @@ +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_base/src/helpers/_ts_metadata.js b/crates/swc_ecma_transforms_base/src/helpers/_ts_metadata.js new file mode 100644 index 00000000000..16ab05f859e --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_ts_metadata.js @@ -0,0 +1,3 @@ +var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); +}; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_base/src/helpers/_ts_param.js b/crates/swc_ecma_transforms_base/src/helpers/_ts_param.js new file mode 100644 index 00000000000..dae067739d9 --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_ts_param.js @@ -0,0 +1,3 @@ +var __param = (this && this.__param) || function (paramIndex, decorator) { + return function (target, key) { decorator(target, key, paramIndex); } +}; \ No newline at end of file diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 0205e297826..85e26469ca7 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -312,6 +312,10 @@ define_helpers!(Helpers { is_native_reflect_construct, possible_constructor_return ), + + ts_decorate: (), + ts_metadata: (), + ts_param: (), }); pub fn inject_helpers() -> impl Fold + VisitMut { @@ -444,7 +448,7 @@ impl VisitMut for Marker { fn visit_mut_var_declarator(&mut self, v: &mut VarDeclarator) { if let Pat::Ident(i) = &v.name { - if &*i.id.sym != "_typeof" { + if &*i.id.sym != "_typeof" && !i.id.sym.starts_with("__") { self.decls.insert(i.id.sym.clone(), self.decl_ctxt); } } diff --git a/crates/swc_ecma_transforms_base/src/quote.rs b/crates/swc_ecma_transforms_base/src/quote.rs index b60b178eb49..35ee405fa09 100644 --- a/crates/swc_ecma_transforms_base/src/quote.rs +++ b/crates/swc_ecma_transforms_base/src/quote.rs @@ -20,8 +20,32 @@ macro_rules! external_name { #[doc(hidden)] #[macro_export] macro_rules! helper_expr { + (ts, $field_name:ident, $s:tt) => {{ + $crate::helper_expr!(ts, ::swc_common::DUMMY_SP, $field_name, $s) + }}; + + (ts, $span:expr, $field_name:ident, $s:tt) => {{ + use swc_ecma_utils::{quote_ident, ExprFactory}; + + debug_assert!( + $s.starts_with("__"), + "ts helper! macro should be invoked with '__' prefix" + ); + let mark = $crate::enable_helper!($field_name); + let span = $span.apply_mark(mark); + let external = $crate::helpers::HELPERS.with(|helper| helper.external()); + + if external { + swc_ecma_utils::quote_ident!(span, "swcHelpers").make_member( + swc_ecma_utils::quote_ident!($span, $crate::external_name!($s)), + ) + } else { + Expr::from(swc_ecma_utils::quote_ident!(span, $s)) + } + }}; + ($field_name:ident, $s:tt) => {{ - helper_expr!(::swc_common::DUMMY_SP, $field_name, $s) + $crate::helper_expr!(::swc_common::DUMMY_SP, $field_name, $s) }}; ($span:expr, $field_name:ident, $s:tt) => {{ @@ -49,12 +73,8 @@ macro_rules! helper_expr { #[doc(hidden)] #[macro_export] macro_rules! helper { - ($field_name:ident, $s:tt) => {{ - $crate::helper!(::swc_common::DUMMY_SP, $field_name, $s) - }}; - - ($span:expr, $field_name:ident, $s:tt) => {{ + ($($t:tt)*) => {{ use swc_ecma_utils::ExprFactory; - $crate::helper_expr!($span, $field_name, $s).as_callee() + $crate::helper_expr!($($t)*).as_callee() }}; } diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs b/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs index 7927d988cf0..d05bd9d405e 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify_dce.rs @@ -421,7 +421,8 @@ test!( chain!( decorators(decorators::Config { legacy: true, - emit_metadata: false + emit_metadata: false, + use_define_for_class_fields: false, }), resolver(unresolved_mark, top_level_mark, false), strip(top_level_mark), @@ -439,26 +440,20 @@ test!( } ", " -var _class, _descriptor; -let X = ((_class = class X { - x() { - const localVar = aFunctionSomewhere(); - return localVar; + + export default class X { + constructor(){ + this.anything = 0; + } + x() { + const localVar = aFunctionSomewhere(); + return localVar; + } } - constructor(){ - _initializerDefineProperty(this, \"anything\", _descriptor, this); - } -}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, \"anything\", [ - whatever -], { - configurable: true, - enumerable: true, - writable: true, - initializer: function() { - return 0; - } -}), _class); -export { X as default };" + __decorate([ + whatever + ], X.prototype, \"anything\", void 0); + " ); test!( @@ -473,7 +468,8 @@ test!( chain!( decorators(decorators::Config { legacy: true, - emit_metadata: false + emit_metadata: false, + use_define_for_class_fields: false, }), resolver(unresolved_mark, top_level_mark, false), strip(top_level_mark), diff --git a/crates/swc_ecma_transforms_proposal/src/decorators/legacy/metadata.rs b/crates/swc_ecma_transforms_proposal/src/decorators/legacy/metadata.rs index 741dccaf979..da9d1e13b32 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorators/legacy/metadata.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorators/legacy/metadata.rs @@ -1,38 +1,39 @@ use swc_atoms::{js_word, JsWord}; -use swc_common::{collections::AHashMap, util::move_map::MoveMap, Spanned, DUMMY_SP}; +use swc_common::{ + collections::AHashMap, + util::{move_map::MoveMap, take::Take}, + Spanned, DUMMY_SP, +}; use swc_ecma_ast::*; -use swc_ecma_utils::{member_expr, quote_ident, undefined, ExprFactory}; -use swc_ecma_visit::{noop_fold_type, Fold, FoldWith}; +use swc_ecma_transforms_base::helper; +use swc_ecma_utils::{quote_ident, undefined, ExprFactory}; +use swc_ecma_visit::{VisitMut, VisitMutWith}; use super::EnumKind; /// https://github.com/leonardfactory/babel-plugin-transform-typescript-metadata/blob/master/src/parameter/parameterVisitor.ts pub(super) struct ParamMetadata; -/// TODO: VisitMut -impl Fold for ParamMetadata { - noop_fold_type!(); +impl VisitMut for ParamMetadata { + fn visit_mut_class(&mut self, mut cls: &mut Class) { + cls.visit_mut_children_with(self); - fn fold_class(&mut self, mut cls: Class) -> Class { - cls = cls.fold_children_with(self); - let mut decorators = cls.decorators; + let mut decorators = cls.decorators.take(); - cls.body = cls.body.move_map(|m| match m { + cls.body = cls.body.take().move_map(|m| match m { ClassMember::Constructor(mut c) => { for (idx, param) in c.params.iter_mut().enumerate() { // match param { ParamOrTsParamProp::TsParamProp(p) => { for decorator in p.decorators.drain(..) { - let new_dec = - self.create_param_decorator(idx, decorator.expr, true); + let new_dec = self.create_param_decorator(idx, decorator.expr); decorators.push(new_dec); } } ParamOrTsParamProp::Param(param) => { for decorator in param.decorators.drain(..) { - let new_dec = - self.create_param_decorator(idx, decorator.expr, true); + let new_dec = self.create_param_decorator(idx, decorator.expr); decorators.push(new_dec); } } @@ -44,73 +45,27 @@ impl Fold for ParamMetadata { _ => m, }); cls.decorators = decorators; - - cls } - fn fold_class_method(&mut self, mut m: ClassMethod) -> ClassMethod { + fn visit_mut_class_method(&mut self, m: &mut ClassMethod) { for (idx, param) in m.function.params.iter_mut().enumerate() { for decorator in param.decorators.drain(..) { - let new_dec = self.create_param_decorator(idx, decorator.expr, false); + let new_dec = self.create_param_decorator(idx, decorator.expr); m.function.decorators.push(new_dec); } } - - m } } impl ParamMetadata { - fn create_param_decorator( - &self, - param_index: usize, - decorator_expr: Box, - is_constructor: bool, - ) -> Decorator { + fn create_param_decorator(&self, param_index: usize, decorator_expr: Box) -> Decorator { Decorator { span: DUMMY_SP, - expr: Box::new(Expr::Fn(FnExpr { - ident: None, - function: Function { - params: vec![ - Param { - span: DUMMY_SP, - decorators: Default::default(), - pat: quote_ident!("target").into(), - }, - Param { - span: DUMMY_SP, - decorators: Default::default(), - pat: quote_ident!("key").into(), - }, - ], - body: Some(BlockStmt { - span: DUMMY_SP, - stmts: vec![Stmt::Return(ReturnStmt { - span: DUMMY_SP, - arg: Some(Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee: decorator_expr.as_callee(), - args: vec![ - quote_ident!("target").as_arg(), - if is_constructor { - quote_ident!("undefined").as_arg() - } else { - quote_ident!("key").as_arg() - }, - param_index.as_arg(), - ], - type_args: Default::default(), - }))), - })], - }), - decorators: Default::default(), - span: Default::default(), - is_generator: Default::default(), - is_async: Default::default(), - type_params: Default::default(), - return_type: Default::default(), - }, + expr: Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: helper!(ts, ts_param, "__param"), + args: vec![param_index.as_arg(), decorator_expr.as_arg()], + type_args: Default::default(), })), } } @@ -123,15 +78,12 @@ pub(super) struct Metadata<'a> { pub(super) class_name: Option<&'a Ident>, } -/// TODO: VisitMut -impl Fold for Metadata<'_> { - noop_fold_type!(); - - fn fold_class(&mut self, mut c: Class) -> Class { - c = c.fold_children_with(self); +impl VisitMut for Metadata<'_> { + fn visit_mut_class(&mut self, c: &mut Class) { + c.visit_mut_children_with(self); if c.decorators.is_empty() { - return c; + return; } let constructor = c.body.iter().find_map(|m| match m { @@ -139,7 +91,7 @@ impl Fold for Metadata<'_> { _ => None, }); if constructor.is_none() { - return c; + return; } { @@ -176,12 +128,11 @@ impl Fold for Metadata<'_> { ); c.decorators.push(dec); } - c } - fn fold_class_method(&mut self, mut m: ClassMethod) -> ClassMethod { + fn visit_mut_class_method(&mut self, m: &mut ClassMethod) { if m.function.decorators.is_empty() { - return m; + return; } { @@ -210,16 +161,15 @@ impl Fold for Metadata<'_> { ); m.function.decorators.push(dec); } - m } - fn fold_class_prop(&mut self, mut p: ClassProp) -> ClassProp { + fn visit_mut_class_prop(&mut self, p: &mut ClassProp) { if p.decorators.is_empty() { - return p; + return; } if p.type_ann.is_none() { - return p; + return; } if let Some(name) = p .type_ann @@ -244,7 +194,7 @@ impl Fold for Metadata<'_> { }, ); p.decorators.push(dec); - return p; + return; } } @@ -253,7 +203,6 @@ impl Fold for Metadata<'_> { serialize_type(self.class_name, p.type_ann.as_ref()).as_arg(), ); p.decorators.push(dec); - p } } @@ -261,56 +210,11 @@ impl Metadata<'_> { fn create_metadata_design_decorator(&self, design: &str, type_arg: ExprOrSpread) -> Decorator { Decorator { span: DUMMY_SP, - expr: Box::new(Expr::Bin(BinExpr { + expr: Box::new(Expr::Call(CallExpr { span: DUMMY_SP, - left: Box::new(Expr::Bin(BinExpr { - span: DUMMY_SP, - left: Box::new(Expr::Bin(BinExpr { - span: DUMMY_SP, - left: Box::new(Expr::Unary(UnaryExpr { - span: DUMMY_SP, - op: op!("typeof"), - arg: Box::new(Expr::Ident(quote_ident!("Reflect"))), - })), - op: op!("!=="), - right: Box::new(Expr::Lit(Lit::Str(Str { - span: DUMMY_SP, - value: "undefined".into(), - raw: None, - }))), - })), - op: op!("&&"), - right: Box::new(Expr::Bin(BinExpr { - span: DUMMY_SP, - left: Box::new(Expr::Unary(UnaryExpr { - span: DUMMY_SP, - op: op!("typeof"), - arg: member_expr!(DUMMY_SP, Reflect.metadata), - })), - op: op!("==="), - right: Box::new(Expr::Lit(Lit::Str(Str { - span: DUMMY_SP, - value: "function".into(), - raw: None, - }))), - })), - })), - op: op!("&&"), - right: Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee: member_expr!(DUMMY_SP, Reflect.metadata).as_callee(), - args: vec![ - Str { - span: DUMMY_SP, - value: design.into(), - raw: None, - } - .as_arg(), - type_arg, - ], - - type_args: Default::default(), - })), + callee: helper!(ts, ts_metadata, "__metadata"), + args: vec![design.as_arg(), type_arg], + type_args: Default::default(), })), } } diff --git a/crates/swc_ecma_transforms_proposal/src/decorators/legacy/mod.rs b/crates/swc_ecma_transforms_proposal/src/decorators/legacy/mod.rs index 6163b4fba8d..2d5f8b7db0a 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorators/legacy/mod.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorators/legacy/mod.rs @@ -1,19 +1,15 @@ -use std::mem::take; - -use smallvec::SmallVec; use swc_atoms::JsWord; -use swc_common::{collections::AHashMap, util::move_map::MoveMap, DUMMY_SP}; +use swc_common::{collections::AHashMap, util::take::Take, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::helper; use swc_ecma_utils::{ - alias_if_required, default_constructor, ident::IdentLike, member_expr, prepend, private_ident, - prop_name_to_expr, prop_name_to_expr_value, quote_ident, replace_ident, undefined, ExprFactory, - ModuleItemLike, StmtLike, + constructor::inject_after_super, default_constructor, private_ident, prop_name_to_expr_value, + quote_ident, undefined, ExprFactory, StmtLike, }; -use swc_ecma_visit::{Fold, FoldWith, Visit, VisitWith}; +use swc_ecma_visit::{Visit, VisitMut, VisitMutWith, VisitWith}; use self::metadata::{Metadata, ParamMetadata}; -use super::{contains_decorator, DecoratorFinder}; +use super::contains_decorator; mod metadata; @@ -24,26 +20,142 @@ enum EnumKind { Num, } -#[derive(Debug)] -pub(super) struct Legacy { - metadata: bool, - uninitialized_vars: Vec, - initialized_vars: Vec, - exports: Vec, - enums: AHashMap, -} - -pub(super) fn new(metadata: bool) -> Legacy { - Legacy { +pub(super) fn new(metadata: bool, use_define_for_class_fields: bool) -> TscDecorator { + TscDecorator { metadata, - uninitialized_vars: Default::default(), - initialized_vars: Default::default(), - exports: Default::default(), + use_define_for_class_fields, enums: Default::default(), + vars: Default::default(), + appended_exprs: Default::default(), + class_name: Default::default(), + constructor_exprs: Default::default(), + exports: Default::default(), } } -impl Visit for Legacy { +pub(super) struct TscDecorator { + metadata: bool, + use_define_for_class_fields: bool, + + enums: AHashMap, + + /// Used for computed keys, and this variables are not initialized. + vars: Vec, + appended_exprs: Vec>, + + class_name: Option, + + /// Only used if `use_define_for_class_props` is false. + constructor_exprs: Vec>, + + exports: Vec, +} + +impl TscDecorator { + fn visit_mut_stmt_likes(&mut self, stmts: &mut Vec) + where + T: StmtLike + VisitMutWith, + { + let old_vars = self.vars.take(); + let old_appended_exprs = self.appended_exprs.take(); + + let mut new = vec![]; + + for mut s in stmts.take() { + debug_assert!(self.appended_exprs.is_empty()); + + s.visit_mut_with(self); + + if !self.vars.is_empty() { + new.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Var, + declare: Default::default(), + decls: self.vars.take(), + })))); + } + + new.push(s); + + new.extend( + self.appended_exprs + .drain(..) + .into_iter() + .map(|expr| { + Stmt::Expr(ExprStmt { + span: DUMMY_SP, + expr, + }) + }) + .map(T::from_stmt), + ); + } + + *stmts = new; + + self.appended_exprs = old_appended_exprs; + self.vars = old_vars; + } + + fn key(&mut self, k: &mut PropName) -> Expr { + match k { + PropName::Computed(k) if !k.expr.is_lit() => { + let var_name = private_ident!(k.span, "_key"); + + // Declare var + self.vars.push(VarDeclarator { + span: DUMMY_SP, + name: Pat::Ident(var_name.clone().into()), + init: None, + definite: Default::default(), + }); + + // Initialize var + self.appended_exprs.push(Box::new(Expr::Assign(AssignExpr { + span: DUMMY_SP, + op: op!("="), + left: PatOrExpr::Pat(var_name.clone().into()), + right: k.expr.take(), + }))); + + k.expr = Box::new(Expr::Ident(var_name.clone())); + + return Expr::Ident(var_name); + } + _ => {} + } + + prop_name_to_expr_value(k.clone()) + } + + /// Creates `__decorate` calls. + fn add_decorate_call( + &mut self, + decorators: impl IntoIterator>, + target: ExprOrSpread, + key: ExprOrSpread, + desc: ExprOrSpread, + ) { + let decorators = ArrayLit { + span: DUMMY_SP, + elems: decorators + .into_iter() + .map(|v| v.as_arg()) + .map(Some) + .collect(), + } + .as_arg(); + + self.appended_exprs.push(Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: helper!(ts, ts_decorate, "__decorate"), + args: vec![decorators, target, key, desc], + type_args: Default::default(), + }))); + } +} + +impl Visit for TscDecorator { fn visit_ts_enum_decl(&mut self, e: &TsEnumDecl) { let enum_kind = e .members @@ -84,751 +196,298 @@ impl Visit for Legacy { } } -/// TODO: VisitMut -impl Fold for Legacy { - fn fold_decl(&mut self, decl: Decl) -> Decl { - let decl: Decl = decl.fold_children_with(self); +impl VisitMut for TscDecorator { + fn visit_mut_class(&mut self, n: &mut Class) { + let old_constructor_stmts = self.constructor_exprs.take(); - if let Decl::Class(c) = decl { - let expr = self.handle(ClassExpr { - class: c.class, - ident: Some(c.ident.clone()), - }); + n.visit_mut_with(&mut ParamMetadata); - return Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Let, - declare: false, - decls: vec![VarDeclarator { - span: DUMMY_SP, - name: c.ident.into(), - init: Some(expr), - definite: false, - }], - }); - } - - decl - } - - fn fold_expr(&mut self, e: Expr) -> Expr { - let e: Expr = e.fold_children_with(self); - - if let Expr::Class(e) = e { - let expr = self.handle(e); - - return *expr; - } - - e - } - - fn fold_module(&mut self, m: Module) -> Module { - // Collect required information. - // For example, value type of enum affects codegen - m.visit_with(self); - - let mut m = m.fold_children_with(self); - - if !self.uninitialized_vars.is_empty() { - prepend( - &mut m.body, - Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - decls: take(&mut self.uninitialized_vars), - declare: false, - })) - .into(), - ); - } - - if !self.exports.is_empty() { - let decl = ModuleDecl::ExportNamed(NamedExport { - span: DUMMY_SP, - specifiers: take(&mut self.exports), - src: None, - type_only: false, - asserts: None, - }); - - m.body.push(decl.into()); - } - - m - } - - fn fold_module_item(&mut self, item: ModuleItem) -> ModuleItem { - let item: ModuleItem = item.fold_children_with(self); - - if let ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { - decl: DefaultDecl::Class(c), - .. - })) = item - { - let export_ident = c.ident.clone().unwrap_or_else(|| private_ident!("_class")); - - let expr = self.handle(c); - - self.exports - .push(ExportSpecifier::Named(ExportNamedSpecifier { - span: DUMMY_SP, - orig: ModuleExportName::Ident(export_ident.clone()), - exported: Some(ModuleExportName::Ident(quote_ident!("default"))), - is_type_only: false, - })); - - return ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Let, - declare: false, - decls: vec![VarDeclarator { - span: DUMMY_SP, - name: export_ident.into(), - init: Some(expr), - definite: false, - }], - }))); - } - - item - } - - fn fold_module_items(&mut self, n: Vec) -> Vec { - self.fold_stmt_like(n) - } - - fn fold_script(&mut self, s: Script) -> Script { - let mut s = s.fold_children_with(self); - - if !self.uninitialized_vars.is_empty() { - prepend( - &mut s.body, - Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - decls: take(&mut self.uninitialized_vars), - declare: false, - })), - ); - } - - s - } - - fn fold_stmts(&mut self, n: Vec) -> Vec { - self.fold_stmt_like(n) - } -} - -impl Legacy { - fn fold_stmt_like(&mut self, stmts: Vec) -> Vec - where - T: FoldWith + VisitWith + StmtLike + ModuleItemLike, - Vec: VisitWith, - { - if !contains_decorator(&stmts) { - return stmts; - } - - let mut buf = Vec::with_capacity(stmts.len() + 4); - - for stmt in stmts { - if !contains_decorator(&stmt) { - buf.push(stmt); - continue; - } - - let stmt = stmt.fold_with(self); - - if !self.initialized_vars.is_empty() { - buf.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - decls: take(&mut self.initialized_vars), - declare: false, - })))); - } - - buf.push(stmt); - } - - buf - } -} - -impl Legacy { - fn handle(&mut self, mut c: ClassExpr) -> Box { if self.metadata { - let i = c.ident.clone(); + let i = self.class_name.clone(); - c = c.fold_with(&mut ParamMetadata).fold_with(&mut Metadata { + n.visit_mut_with(&mut Metadata { enums: &self.enums, class_name: i.as_ref(), }); } - let cls_ident = private_ident!("_class"); - let cls_name = c.ident.clone(); + n.visit_mut_children_with(self); - self.uninitialized_vars.push(VarDeclarator { - span: DUMMY_SP, - name: cls_ident.clone().into(), - init: None, - definite: false, - }); - - // We initialize decorators lazily. - // - // See https://github.com/swc-project/swc/issues/1278 - let mut dec_init_exprs = vec![]; - - // Injected to sequence expression which is wrapped with parenthesis. - let mut extra_exprs = vec![]; - // Injected to constructor - let mut constructor_stmts = SmallVec::<[_; 8]>::new(); - - let prototype = MemberExpr { - span: DUMMY_SP, - obj: Box::new(Expr::Ident(cls_ident.clone())), - prop: MemberProp::Ident(quote_ident!("prototype")), - }; - - c.class.body = c.class.body.move_flat_map(|m| match m { - ClassMember::Method(mut m) - if !m.function.decorators.is_empty() - || m.function.params.iter().any(|p| !p.decorators.is_empty()) => - { - let prototype = if m.is_static { - cls_ident.clone().as_arg() - } else { - // _class2.prototype, - prototype.clone().as_arg() - }; - - // _applyDecoratedDescriptor(_class2.prototype, "method2", [_dec7, _dec8], - // Object.getOwnPropertyDescriptor(_class2.prototype, "method2"), - // _class2.prototype) - - let mut dec_exprs = vec![]; - let mut dec_inits = vec![]; - for mut dec in m.function.decorators.into_iter() { - let (i, aliased) = alias_if_required(&dec.expr, "_dec"); - if aliased { - self.uninitialized_vars.push(VarDeclarator { - span: DUMMY_SP, - name: i.clone().into(), - init: None, - definite: false, - }); - - // We use _class.staticField instead of Person.staticField because while - // initializing the class, - // - // _dec = Debounce(Person.debounceTime) - // - // fails while - // - // _dec = Debounce(_class.debounceTime) - // - // works. - // - // See: https://github.com/swc-project/swc/issues/823 - let right = if let Some(cls_name) = cls_name.clone() { - replace_ident(&mut dec.expr, cls_name.to_id(), &cls_ident); - - dec.expr - } else { - dec.expr - }; - - dec_inits.push(Box::new(Expr::Assign(AssignExpr { - span: dec.span, - op: op!("="), - left: PatOrExpr::Pat(i.clone().into()), - right, - }))); - } - - dec_exprs.push(Some(i.as_arg())) + if !self.constructor_exprs.is_empty() { + for m in &mut n.body { + if let ClassMember::Constructor(c @ Constructor { body: Some(..), .. }) = m { + inject_after_super(c, self.constructor_exprs.take()); } - - let name = match &m.key { - PropName::Computed(e) => { - let (name, aliased) = alias_if_required(&e.expr, "key"); - if aliased { - let mut init = e.expr.clone(); - if let Some(name) = &cls_name { - replace_ident(&mut init, name.to_id(), &cls_ident); - } - - dec_init_exprs.push(Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - op: op!("="), - left: PatOrExpr::Pat(name.clone().into()), - right: init, - }))); - self.uninitialized_vars.push(VarDeclarator { - span: DUMMY_SP, - name: name.clone().into(), - init: None, - definite: Default::default(), - }) - } - - Expr::Ident(name) - } - _ => prop_name_to_expr_value(m.key.clone()), - }; - - { - // https://github.com/swc-project/swc/issues/863 - let mut new_params = Vec::with_capacity(m.function.params.len()); - for (index, param) in m.function.params.into_iter().enumerate() { - for dec in param.decorators { - // - extra_exprs.push(Box::new(Expr::Call(CallExpr { - span: dec.span, - callee: dec.expr.as_callee(), - args: vec![ - prototype.clone(), - name.clone().as_arg(), - Lit::Num(Number { - span: param.span, - value: index as _, - raw: None, - }) - .as_arg(), - ], - type_args: None, - }))) - } - - new_params.push(Param { - decorators: Default::default(), - ..param - }); - } - m.function.params = new_params; - } - - let callee = helper!(apply_decorated_descriptor, "applyDecoratedDescriptor"); - - extra_exprs.extend(dec_inits); - - extra_exprs.push(Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee, - // (_class2.prototype, "method2", [_dec7, _dec8], - // Object.getOwnPropertyDescriptor(_class2.prototype, "method2"), - // _class2.prototype) - args: vec![ - prototype.clone(), - // "method2" - name.clone().as_arg(), - // [_dec7, _dec8], - ArrayLit { - span: DUMMY_SP, - elems: dec_exprs, - } - .as_arg(), - // Object.getOwnPropertyDescriptor(_class2.prototype, "method2"), - CallExpr { - span: DUMMY_SP, - callee: member_expr!(DUMMY_SP, Object.getOwnPropertyDescriptor) - .as_callee(), - args: vec![prototype.clone(), name.as_arg()], - type_args: None, - } - .as_arg(), - // _class2.prototype - prototype, - ], - type_args: None, - }))); - - Some(ClassMember::Method(ClassMethod { - function: Function { - decorators: vec![], - ..m.function - }, - ..m - })) } - ClassMember::ClassProp(p) if !p.decorators.is_empty() => { - let prototype = if p.is_static { - cls_ident.clone().as_arg() - } else { - // _class2.prototype, - prototype.clone().as_arg() - }; + if !self.constructor_exprs.is_empty() { + let mut c = default_constructor(n.super_class.is_some()); + inject_after_super(&mut c, self.constructor_exprs.take()); + n.body.insert(0, ClassMember::Constructor(c)); + } + } - // - let descriptor = private_ident!("_descriptor"); - if !p.is_static { - self.uninitialized_vars.push(VarDeclarator { - span: DUMMY_SP, - name: descriptor.clone().into(), - init: None, - definite: false, - }); - } + self.constructor_exprs = old_constructor_stmts; - let mut value = Some(p.value); - - let mut dec_exprs = vec![]; - for mut dec in p.decorators.into_iter() { - let (i, aliased) = alias_if_required(&dec.expr, "_dec"); - if aliased { - if let Some(name) = &cls_name { - replace_ident(&mut dec.expr, name.to_id(), &cls_ident); - } - - dec_init_exprs.push(Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - op: op!("="), - left: PatOrExpr::Pat(i.clone().into()), - right: dec.expr, - }))); - self.uninitialized_vars.push(VarDeclarator { - span: DUMMY_SP, - name: i.clone().into(), - init: None, - definite: false, - }); - } - - dec_exprs.push(Some(i.as_arg())) - } - - // TODO: Handle s prop name - let name = match &p.key { - PropName::Ident(ref i) => Box::new(Expr::Lit(Lit::Str(Str { - span: i.span, - value: i.sym.clone(), - raw: None, - }))), - _ => prop_name_to_expr(p.key.clone()).into(), - }; - let init = private_ident!("_init"); - if p.is_static { - self.uninitialized_vars.push(VarDeclarator { - span: DUMMY_SP, - name: init.clone().into(), - init: None, - definite: false, - }); - } - - let mut property_descriptor = Expr::Object(ObjectLit { + if let Some(class_name) = self.class_name.clone() { + if !n.decorators.is_empty() { + let decorators = ArrayLit { span: DUMMY_SP, - props: vec![ - // configurable: true, - PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: quote_ident!("configurable").into(), - value: true.into(), - }))), // enumerable: true, - PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: quote_ident!("enumerable").into(), - value: true.into(), - }))), - // writable: true, - PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: quote_ident!("writable").into(), - value: true.into(), - }))), - // initializer: function () { - // return 2; - // } - PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: quote_ident!("initializer").into(), - value: if value.is_some() && value.as_ref().unwrap().is_some() { - Box::new(Expr::Fn(FnExpr { - ident: None, - function: Function { - decorators: Default::default(), - is_generator: false, - is_async: false, - span: DUMMY_SP, - params: vec![], - - body: Some(BlockStmt { - span: DUMMY_SP, - stmts: vec![ReturnStmt { - span: DUMMY_SP, - arg: if p.is_static { - Some(Box::new(Expr::Ident(init.clone()))) - } else { - value.take().unwrap() - }, - } - .into()], - }), - - type_params: Default::default(), - return_type: Default::default(), - }, - })) - } else { - Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))) - }, - }))), - ], - }); - - if p.is_static { - property_descriptor = Expr::Seq(SeqExpr { - span: DUMMY_SP, - exprs: vec![ - Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - left: PatOrExpr::Pat(init.clone().into()), - op: op!("="), - // Object.getOwnPropertyDescriptor(_class, "enumconfwrite") - right: Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee: member_expr!(DUMMY_SP, Object.getOwnPropertyDescriptor) - .as_callee(), - args: vec![cls_ident.clone().as_arg(), name.clone().as_arg()], - type_args: Default::default(), - })), - })), - // _init = _init ? _init.value : void 0 - Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - left: PatOrExpr::Pat(init.clone().into()), - op: op!("="), - right: Box::new(Expr::Cond(CondExpr { - span: DUMMY_SP, - test: Box::new(Expr::Ident(init.clone())), - cons: Box::new(init.make_member(quote_ident!("value"))), - alt: undefined(DUMMY_SP), - })), - })), - Box::new(property_descriptor), - ], - }); + elems: n + .decorators + .take() + .into_iter() + .map(|v| v.expr.as_arg()) + .map(Some) + .collect(), } + .as_arg(); - // _applyDecoratedDescriptor(_class2.prototype, "prop2", [_dec9, _dec10], { - // configurable: true, - // enumerable: true, - // writable: true, - // `: function () { - // return 2; - // } - // })) - let call_expr = Box::new(Expr::Call(CallExpr { + let decorated = Box::new(Expr::Call(CallExpr { span: DUMMY_SP, - callee: helper!(apply_decorated_descriptor, "applyDecoratedDescriptor"), - args: { - if p.is_static { - vec![ - prototype, - name.clone().as_arg(), - ArrayLit { - span: DUMMY_SP, - elems: dec_exprs, - } - .as_arg(), - property_descriptor.as_arg(), - cls_ident.clone().as_arg(), - ] - } else { - vec![ - prototype, - name.clone().as_arg(), - ArrayLit { - span: DUMMY_SP, - elems: dec_exprs, - } - .as_arg(), - property_descriptor.as_arg(), - ] - } - }, + callee: helper!(ts, ts_decorate, "__decorate"), + args: vec![decorators, class_name.clone().as_arg()], type_args: Default::default(), })); + self.appended_exprs.push(Box::new(Expr::Assign(AssignExpr { + span: DUMMY_SP, + op: op!("="), + left: PatOrExpr::Pat(class_name.into()), + right: decorated, + }))); + } + } + } - if !p.is_static { - extra_exprs.push(Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - op: op!("="), - left: PatOrExpr::Pat(descriptor.clone().into()), - right: call_expr, - }))); + fn visit_mut_class_decl(&mut self, n: &mut ClassDecl) { + let old = self.class_name.take(); + self.class_name = Some(n.ident.clone()); + + n.visit_mut_children_with(self); + + self.class_name = old; + } + + fn visit_mut_class_expr(&mut self, n: &mut ClassExpr) { + let old = self.class_name.take(); + if contains_decorator(n) && n.ident.is_none() { + n.ident = Some(private_ident!("_class")); + } + + if let Some(ident) = &n.ident { + self.class_name = Some(ident.clone()); + } + + n.visit_mut_children_with(self); + + self.class_name = old; + } + + fn visit_mut_class_method(&mut self, c: &mut ClassMethod) { + c.visit_mut_children_with(self); + + if let Some(class_name) = self.class_name.clone() { + if !c.function.decorators.is_empty() { + let key = self.key(&mut c.key); + + let target = if c.is_static { + class_name.as_arg() } else { - extra_exprs.push(call_expr); - } + class_name.make_member(quote_ident!("prototype")).as_arg() + }; - if !p.is_static && !p.declare { - constructor_stmts.push( - CallExpr { - span: DUMMY_SP, - callee: helper!( - initializer_define_property, - "initializerDefineProperty" - ), - args: vec![ - ThisExpr { span: DUMMY_SP }.as_arg(), - name.as_arg(), - descriptor.as_arg(), - ThisExpr { span: DUMMY_SP }.as_arg(), - ], - type_args: None, - } - .into_stmt(), - ); - } + self.add_decorate_call( + c.function.decorators.drain(..).map(|d| d.expr), + target, + key.as_arg(), + Lit::Null(Null::dummy()).as_arg(), + ); + } + } + } - if p.is_static { - Some( - ClassProp { - decorators: vec![], - value: value.take().unwrap(), - ..p - } - .into(), - ) + fn visit_mut_class_prop(&mut self, c: &mut ClassProp) { + c.visit_mut_children_with(self); + + if let Some(class_name) = self.class_name.clone() { + if !c.decorators.is_empty() { + let key = self.key(&mut c.key); + + let target = if c.is_static { + class_name.as_arg() } else { - None + class_name.make_member(quote_ident!("prototype")).as_arg() + }; + + self.add_decorate_call( + c.decorators.drain(..).map(|d| d.expr), + target, + key.as_arg(), + undefined(DUMMY_SP).as_arg(), + ); + + if !self.use_define_for_class_fields && !c.is_static { + if let Some(init) = c.value.take() { + self.constructor_exprs + .push(Box::new(Expr::Assign(AssignExpr { + span: c.span, + op: op!("="), + left: PatOrExpr::Expr(Box::new(Expr::Member(MemberExpr { + span: DUMMY_SP, + obj: Box::new(Expr::This(ThisExpr { span: DUMMY_SP })), + prop: match &c.key { + PropName::Ident(i) => MemberProp::Ident(i.clone()), + _ => MemberProp::Computed(ComputedPropName { + span: DUMMY_SP, + expr: Box::new(prop_name_to_expr_value(c.key.clone())), + }), + }, + }))), + right: init, + }))); + } } } + } + } - _ => Some(m), - }); + fn visit_mut_decl(&mut self, n: &mut Decl) { + match n { + Decl::Class(decl) => { + let convert_to_let = !decl.class.decorators.is_empty(); + decl.visit_mut_with(self); - if !constructor_stmts.is_empty() { - let constructor = if let Some(c) = c.class.body.iter_mut().find_map(|m| match m { - ClassMember::Constructor(c @ Constructor { body: Some(..), .. }) => Some(c), - _ => None, - }) { - c - } else { - c.class - .body - .push(ClassMember::Constructor(default_constructor( - c.class.super_class.is_some(), - ))); - if let ClassMember::Constructor(c) = c.class.body.last_mut().unwrap() { - c - } else { - unreachable!() + if convert_to_let { + let d = VarDeclarator { + span: DUMMY_SP, + name: decl.ident.clone().into(), + init: Some(Box::new(Expr::Class(ClassExpr { + ident: Some(decl.ident.clone()), + class: decl.class.take(), + }))), + definite: Default::default(), + }; + *n = Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Let, + declare: Default::default(), + decls: vec![d], + }); } - }; + } + _ => { + n.visit_mut_children_with(self); + } + } + } - let decorate_stmts_insert_position = constructor - .body - .as_ref() - .unwrap() - .stmts - .iter() - .position(|stmt| { - if let Stmt::Expr(expr) = stmt { - let expr = expr.expr.as_ref(); - if let Expr::Call(call) = expr { - if let Callee::Super(_) = call.callee { - return true; - } + fn visit_mut_module(&mut self, n: &mut Module) { + n.visit_with(self); + + n.visit_mut_children_with(self); + } + + fn visit_mut_module_item(&mut self, module_item: &mut ModuleItem) { + match module_item { + ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(n)) => { + let export_decl_span = n.span; + + match &mut n.decl { + Decl::Class(decl) => { + let convert_to_let = !decl.class.decorators.is_empty(); + decl.visit_mut_with(self); + + if convert_to_let { + let d = VarDeclarator { + span: DUMMY_SP, + name: decl.ident.clone().into(), + init: Some(Box::new(Expr::Class(ClassExpr { + ident: Some(decl.ident.clone()), + class: decl.class.take(), + }))), + definite: Default::default(), + }; + + let let_decl = Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Let, + declare: Default::default(), + decls: vec![d], + }); + *module_item = + ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { + span: export_decl_span, + decl: let_decl, + })); } } + _ => { + module_item.visit_mut_children_with(self); + } + } + } + ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(n)) => match &mut n.decl { + DefaultDecl::Class(decl) => { + let convert_to_let = !decl.class.decorators.is_empty(); + decl.visit_mut_with(self); - false - }) - .map_or(0, |p| p + 1); + if convert_to_let { + let ident = decl.ident.clone().unwrap(); - constructor.body.as_mut().unwrap().stmts.splice( - decorate_stmts_insert_position..decorate_stmts_insert_position, - constructor_stmts, - ); - } - - let cls_assign = Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - op: op!("="), - left: PatOrExpr::Pat(cls_ident.clone().into()), - right: Box::new(Expr::Class(ClassExpr { - ident: c.ident.clone(), - class: Class { - decorators: vec![], - ..c.class - }, - })), - })); - - let var_init = Box::new(Expr::Bin(BinExpr { - span: DUMMY_SP, - left: cls_assign, - op: op!("||"), - right: Box::new(Expr::Ident(cls_ident.clone())), - })); - - let mut extra_exprs = { - let mut buf = dec_init_exprs; - buf.extend(extra_exprs); - buf - }; - - self.apply( - &cls_ident, - if extra_exprs.is_empty() { - var_init - } else { - extra_exprs.insert(0, var_init); - // Return value. - extra_exprs.push(Box::new(Expr::Ident(cls_ident.clone()))); - - Box::new(Expr::Seq(SeqExpr { - span: DUMMY_SP, - exprs: extra_exprs, - })) + let d = VarDeclarator { + span: DUMMY_SP, + name: ident.clone().into(), + init: Some(Box::new(Expr::Class(decl.take()))), + definite: Default::default(), + }; + *module_item = ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Let, + declare: Default::default(), + decls: vec![d], + }))); + self.exports + .push(ExportSpecifier::Named(ExportNamedSpecifier { + span: DUMMY_SP, + orig: ModuleExportName::Ident(ident), + exported: Some(ModuleExportName::Ident(quote_ident!("default"))), + is_type_only: Default::default(), + })); + } + } + _ => { + module_item.visit_mut_children_with(self); + } }, - c.class.decorators, - ) + _ => { + module_item.visit_mut_children_with(self); + } + } } - /// Apply class decorators. - fn apply( - &mut self, - class_ident: &Ident, - mut expr: Box, - decorators: Vec, - ) -> Box { - for dec in decorators.into_iter().rev() { - let (i, aliased) = alias_if_required(&dec.expr, "_dec"); - if aliased { - self.initialized_vars.push(VarDeclarator { + fn visit_mut_module_items(&mut self, s: &mut Vec) { + self.visit_mut_stmt_likes(s); + + if !self.exports.is_empty() { + s.push(ModuleItem::ModuleDecl(ModuleDecl::ExportNamed( + NamedExport { span: DUMMY_SP, - name: i.clone().into(), - init: Some(dec.expr), - definite: false, - }); - } - - let dec_call_expr = Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee: i.as_callee(), - args: vec![expr.as_arg()], - type_args: None, - })); - - // _class = dec(_class = function() {}) || _class - let class_expr = Box::new(Expr::Assign(AssignExpr { - span: DUMMY_SP, - left: PatOrExpr::Pat(class_ident.clone().into()), - op: op!("="), - right: Box::new(Expr::Bin(BinExpr { - span: DUMMY_SP, - left: dec_call_expr, - op: op!("||"), - right: Box::new(Expr::Ident(class_ident.clone())), - })), - })); - - expr = class_expr; + specifiers: self.exports.take(), + src: None, + type_only: Default::default(), + asserts: Default::default(), + }, + ))); } + } - expr + fn visit_mut_script(&mut self, n: &mut Script) { + n.visit_with(self); + + n.visit_mut_children_with(self); + } + + fn visit_mut_stmts(&mut self, s: &mut Vec) { + self.visit_mut_stmt_likes(s) } } diff --git a/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs b/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs index 1922914eb4b..77a67f5c10d 100644 --- a/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs +++ b/crates/swc_ecma_transforms_proposal/src/decorators/mod.rs @@ -11,7 +11,7 @@ use swc_ecma_utils::{ prop_name_to_expr, prop_name_to_expr_value, quote_ident, quote_str, undefined, ExprFactory, IdentExt, }; -use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, Visit, VisitWith}; +use swc_ecma_visit::{as_folder, noop_fold_type, Fold, FoldWith, Visit, VisitWith}; mod legacy; @@ -57,7 +57,10 @@ mod legacy; /// ``` pub fn decorators(c: Config) -> impl Fold { if c.legacy { - Either::Left(self::legacy::new(c.emit_metadata)) + Either::Left(as_folder(self::legacy::new( + c.emit_metadata, + c.use_define_for_class_fields, + ))) } else { if c.emit_metadata { unimplemented!("emitting decorator metadata while using new proposal") @@ -75,6 +78,8 @@ pub struct Config { pub legacy: bool, #[serde(default)] pub emit_metadata: bool, + + pub use_define_for_class_fields: bool, } #[derive(Debug, Default)] diff --git a/crates/swc_ecma_transforms_proposal/src/lib.rs b/crates/swc_ecma_transforms_proposal/src/lib.rs index 58753be29bf..77ad4e96fb3 100644 --- a/crates/swc_ecma_transforms_proposal/src/lib.rs +++ b/crates/swc_ecma_transforms_proposal/src/lib.rs @@ -1,4 +1,5 @@ #![deny(clippy::all)] +#![allow(clippy::vec_box)] pub use self::{ decorators::decorators, export_default_from::export_default_from, diff --git a/crates/swc_ecma_transforms_typescript/tests/strip.rs b/crates/swc_ecma_transforms_typescript/tests/strip.rs index 1911048bd0e..0c4be07a53b 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip.rs @@ -968,7 +968,7 @@ test!( } } ", - r#"var _class, _descriptor, _dec; + r#" function DefineAction() { return (target, property)=>{ console.log(target, property); @@ -979,22 +979,15 @@ test!( this.action = new Subject(); } } - let Child = ((_class = class Child extends Base { + class Child extends Base { callApi() { console.log(this.action); } - constructor(...args){ - super(...args); - _initializerDefineProperty(this, "action", _descriptor, this); - } - }) || _class, _dec = DefineAction(), _descriptor = _applyDecoratedDescriptor(_class.prototype, "action", [ - _dec - ], { - configurable: true, - enumerable: true, - writable: true, - initializer: null - }), _class); + } + __decorate([ + DefineAction() + ], Child.prototype, "action", void 0); + "#, ok_if_code_eq ); diff --git a/crates/swc_node_bundler/tests/pass/helpers/simple/output/entry.js b/crates/swc_node_bundler/tests/pass/helpers/simple/output/entry.js index e9dd5170b58..620db1752d7 100644 --- a/crates/swc_node_bundler/tests/pass/helpers/simple/output/entry.js +++ b/crates/swc_node_bundler/tests/pass/helpers/simple/output/entry.js @@ -3,9 +3,17 @@ function _classCallCheck(instance, Constructor) { throw new TypeError("Cannot call a class as a function"); } } -var _class; -var Foo = _class = isDecorator((_class = function Foo1() { +var __decorate = this && this.__decorate || function(decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var Foo = function Foo1() { "use strict"; _classCallCheck(this, Foo1); -}) || _class) || _class; +}; +Foo = __decorate([ + isDecorator +], Foo); new Foo(); diff --git a/packages/swc-helpers/package.json b/packages/swc-helpers/package.json index 0596b738fb1..59718e61b83 100644 --- a/packages/swc-helpers/package.json +++ b/packages/swc-helpers/package.json @@ -1,6 +1,6 @@ { "name": "@swc/helpers", - "version": "0.3.10", + "version": "0.3.13", "description": "External helpers for the swc project.", "esnext": "src/index.js", "module": "src/index.js", @@ -27,5 +27,8 @@ "devDependencies": { "@swc/cli": "^0.1.36", "@swc/core": "^1.2.58" + }, + "dependencies": { + "tslib": "^2.4.0" } } diff --git a/packages/swc-helpers/src/index.js b/packages/swc-helpers/src/index.js index bfaa1b45671..a186d9d011a 100644 --- a/packages/swc-helpers/src/index.js +++ b/packages/swc-helpers/src/index.js @@ -77,3 +77,4 @@ export { default as typeOf } from './_type_of'; export { default as unsupportedIterableToArray } from './_unsupported_iterable_to_array'; export { default as wrapAsyncGenerator } from './_wrap_async_generator'; export { default as wrapNativeSuper } from './_wrap_native_super'; +export { __decorate, __metadata, __param } from 'tslib' \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 57a0d9867dc..0036ed4dc7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1538,9 +1538,11 @@ integrity sha512-P45vAh0hR9dISIceSv6MkypjT0WduLWB4U8LPoCneeAw7mA1U7liS0Uu1PeiafxQVMWg8SNyIJFDcSg/haLJgg== "@swc/helpers@^0.3.8": - version "0.3.8" - resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.3.8.tgz#5b9ecf4ee480ca00f1ffbc2d1a5d4eed0d1afe81" - integrity sha512-aWItSZvJj4+GI6FWkjZR13xPNPctq2RRakzo+O6vN7bC2yjwdg5EFpgaSAUn95b7BGSgcflvzVDPoKmJv24IOg== + version "0.3.13" + resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.3.13.tgz#b9af856aaa3804fefdd1544632dde35b7b6ff978" + integrity sha512-A1wswJhnqaLRn8uYVQ8YiNTtY5i/JIPmV08EXXjjTresIkUVUEUaFv/wXVhGXfRNYMvHPkuoMR1Nb6NgpxGjNg== + dependencies: + tslib "^2.4.0" "@taplo/cli@^0.3.2": version "0.3.2" @@ -5138,6 +5140,11 @@ tslib@^2.1.0: resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01" integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw== +tslib@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3" + integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ== + type-check@~0.3.2: version "0.3.2" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"