Mark ThisExpression and Super as Purish (#12251)

* 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
This commit is contained in:
Nicolò Ribaudo 2020-10-27 13:12:49 +01:00 committed by GitHub
parent 87a30524f6
commit df908fc63b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View File

@ -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 () {

View File

@ -980,7 +980,7 @@ defineType("SwitchStatement", {
});
defineType("ThisExpression", {
aliases: ["Expression"],
aliases: ["Expression", "Pureish"],
});
defineType("ThrowStatement", {
@ -1785,7 +1785,7 @@ defineType("SpreadElement", {
});
defineType("Super", {
aliases: ["Expression"],
aliases: ["Expression", "Pureish"],
});
defineType("TaggedTemplateExpression", {

View File

@ -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)