Skip to content

Commit

Permalink
Fix the transform of super.foo--/super[foo]-- (and prefix) (#14162)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jan 16, 2022
1 parent dc5f419 commit 693f2d2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/babel-traverse/src/path/conversion.ts
Expand Up @@ -485,7 +485,12 @@ function standardizeSuperProperty(superProp) {
computedKey ? identifier(computedKey.name) : superProp.node.property,
superProp.node.computed,
),
binaryExpression("+", identifier(tmp.name), numericLiteral(1)),
binaryExpression(
// map `++` to `+`, and `--` to `-`
superProp.parentPath.node.operator[0] as "+" | "-",
identifier(tmp.name),
numericLiteral(1),
),
),
];

Expand Down
104 changes: 100 additions & 4 deletions packages/babel-traverse/test/arrow-transform.js
Expand Up @@ -518,7 +518,7 @@ describe("arrow function conversion", () => {
);
});

it("should convert super.prop prefix update", () => {
it("should convert `++super.prop` prefix update", () => {
assertConversion(
`
() => {
Expand All @@ -542,7 +542,31 @@ describe("arrow function conversion", () => {
);
});

it("should convert super[prop] prefix update", () => {
it("should convert `--super.prop` prefix update", () => {
assertConversion(
`
() => {
--super.foo;
};
--super.foo;
() => --super.foo;
`,
`
var _superprop_getFoo = () => super.foo,
_superprop_setFoo = _value => super.foo = _value;
(function () {
var _tmp;
_tmp = _superprop_getFoo(), _superprop_setFoo(_tmp - 1);
});
--super.foo;
() => --super.foo;
`,
);
});

it("should convert `++super[prop]` prefix update", () => {
assertConversion(
`
() => {
Expand All @@ -566,7 +590,31 @@ describe("arrow function conversion", () => {
);
});

it("should convert super.prop suffix update", () => {
it("should convert `--super[prop]` prefix update", () => {
assertConversion(
`
() => {
--super[foo];
};
--super[foo];
() => --super[foo];
`,
`
var _superprop_get = _prop2 => super[_prop2],
_superprop_set = (_prop3, _value) => super[_prop3] = _value;
(function () {
var _tmp, _prop;
_tmp = _superprop_get(_prop = foo), _superprop_set(_prop, _tmp - 1);
});
--super[foo];
() => --super[foo];
`,
);
});

it("should convert `super.prop++` suffix update", () => {
assertConversion(
`
() => {
Expand All @@ -590,7 +638,31 @@ describe("arrow function conversion", () => {
);
});

it("should convert super[prop] suffix update", () => {
it("should convert `super.prop--` suffix update", () => {
assertConversion(
`
() => {
super.foo--;
};
super.foo--;
() => super.foo--;
`,
`
var _superprop_getFoo = () => super.foo,
_superprop_setFoo = _value => super.foo = _value;
(function () {
var _tmp;
_tmp = _superprop_getFoo(), _superprop_setFoo(_tmp - 1), _tmp;
});
super.foo--;
() => super.foo--;
`,
);
});

it("should convert `super[prop]++` suffix update", () => {
assertConversion(
`
() => {
Expand All @@ -614,6 +686,30 @@ describe("arrow function conversion", () => {
);
});

it("should convert `super[prop]--` suffix update", () => {
assertConversion(
`
() => {
super[foo]--;
};
super[foo]--;
() => super[foo]--;
`,
`
var _superprop_get = _prop2 => super[_prop2],
_superprop_set = (_prop3, _value) => super[_prop3] = _value;
(function () {
var _tmp, _prop;
_tmp = _superprop_get(_prop = foo), _superprop_set(_prop, _tmp - 1), _tmp;
});
super[foo]--;
() => super[foo]--;
`,
);
});

it("should convert super.prop() calls without params", () => {
assertConversion(
`
Expand Down

0 comments on commit 693f2d2

Please sign in to comment.