Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the transform of super.foo--/super[foo]-- (and prefix) #14162

Merged
merged 2 commits into from Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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