Skip to content

Commit

Permalink
fix(es/decorators): Support negative numbers (#10114)
Browse files Browse the repository at this point in the history
**Description:**

A negative number is not a numeric literal so we need to handle unary expressions with `op = "-"`.

**Related issue:**

 - Closes #10094
  • Loading branch information
kdy1 authored Feb 27, 2025
1 parent 3334932 commit 5044580
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/three-coins-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_transforms_proposal: patch
---

fix(es/decorators): Support negative numbers
23 changes: 23 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10094/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"tsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
},
"transform": {
"decoratorMetadata": true
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10094/input/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum NegativeStatus {
open = -1,
close = 3,
}

enum Status {
open = 1,
close = 2,
}

function prop() { }

class A {
@prop()
negativeStatus: NegativeStatus;

@prop()
status: Status;
}
25 changes: 25 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10094/output/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var _ts_decorate = require("@swc/helpers/_/_ts_decorate");
var _ts_metadata = require("@swc/helpers/_/_ts_metadata");
var NegativeStatus = /*#__PURE__*/ function(NegativeStatus) {
NegativeStatus[NegativeStatus["open"] = -1] = "open";
NegativeStatus[NegativeStatus["close"] = 3] = "close";
return NegativeStatus;
}(NegativeStatus || {});
var Status = /*#__PURE__*/ function(Status) {
Status[Status["open"] = 1] = "open";
Status[Status["close"] = 2] = "close";
return Status;
}(Status || {});
function prop() {}
class A {
negativeStatus;
status;
}
_ts_decorate._([
prop(),
_ts_metadata._("design:type", Number)
], A.prototype, "negativeStatus", void 0);
_ts_decorate._([
prop(),
_ts_metadata._("design:type", Number)
], A.prototype, "status", void 0);
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ impl Visit for TscDecorator {
.map(|member| member.init.as_ref())
.map(|init| match init {
Some(e) => match &**e {
Expr::Unary(UnaryExpr {
op: op!(unary, "-"),
..
}) => EnumKind::Num,
Expr::Lit(lit) => match lit {
Lit::Str(_) => EnumKind::Str,
Lit::Num(_) => EnumKind::Num,
Expand Down

0 comments on commit 5044580

Please sign in to comment.