Skip to content

Commit

Permalink
Mark ThisExpression and Super as Purish (#12251)
Browse files Browse the repository at this point in the history
* Mark `ThisExpression` as `Purish`

The other purish types are functions and literals, so I
guess it means "it doesn't have side effects"

* Super & tests

* Fix tests
  • Loading branch information
nicolo-ribaudo committed Oct 27, 2020
1 parent 87a3052 commit df908fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/babel-traverse/test/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ describe("scope", () => {
expect(
getPath("String.raw`foo`").get("body")[0].get("expression").isPure(),
).toBeTruthy();
expect(getPath("this").get("body.0.expression").isPure()).toBeTruthy();
expect(getPath("this.foo").get("body.0.expression").isPure()).toBeFalsy();
expect(
getPath("({ m() { super.foo } })")
.get("body.0.expression.properties.0.body.body.0.expression")
.isPure(),
).toBeFalsy();
expect(
// This only tests "super", not "super.foo"
getPath("({ m() { super.foo } })")
.get("body.0.expression.properties.0.body.body.0.expression.object")
.isPure(),
).toBeTruthy();
});

test("label", function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-types/src/definitions/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ defineType("SwitchStatement", {
});

defineType("ThisExpression", {
aliases: ["Expression"],
aliases: ["Expression", "Pureish"],
});

defineType("ThrowStatement", {
Expand Down Expand Up @@ -1785,7 +1785,7 @@ defineType("SpreadElement", {
});

defineType("Super", {
aliases: ["Expression"],
aliases: ["Expression", "Pureish"],
});

defineType("TaggedTemplateExpression", {
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-types/src/validators/generated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3965,7 +3965,9 @@ export function isPureish(node: ?Object, opts?: Object): boolean {
"NullLiteral" === nodeType ||
"BooleanLiteral" === nodeType ||
"RegExpLiteral" === nodeType ||
"ThisExpression" === nodeType ||
"ArrowFunctionExpression" === nodeType ||
"Super" === nodeType ||
"BigIntLiteral" === nodeType ||
"DecimalLiteral" === nodeType ||
(nodeType === "Placeholder" && "StringLiteral" === node.expectedNode)
Expand Down

0 comments on commit df908fc

Please sign in to comment.