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
Update detection of pure nodes (Scope#isPure
)
#14424
Changes from all commits
df1bb80
ac872bb
a954708
723318f
5da9f64
d9b69e0
18c5ad7
02a010c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
var _ref2; | ||
var _ref; | ||
|
||
const result = (_ref2 = Math.pow(5, 2), [1, 2, 3].map(n => { | ||
var _ref; | ||
|
||
return _ref = (n + _ref2) * 2, `${_ref} apples`.toUpperCase(); | ||
}).join()); | ||
const result = (_ref = Math.pow(5, 2), [1, 2, 3].map(n => `${(n + _ref) * 2} apples`.toUpperCase()).join()); | ||
expect(result).toEqual('52 APPLES,54 APPLES,56 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
var _ref; | ||
|
||
const result = (_ref = Math.pow(5, 2) + 1, `${_ref} apples`.toUpperCase()); | ||
const result = (_ref = Math.pow(5, 2), `${_ref + 1} apples`.toUpperCase()); | ||
expect(result).toEqual('26 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
var _ref2; | ||
var _ref; | ||
|
||
const result = (_ref2 = Math.pow(5, 2), [1, 2, 3].map(n => { | ||
var _ref; | ||
|
||
return _ref = (n + _ref2) * 2, `${_ref} apples`.toUpperCase(); | ||
}).join()); | ||
const result = (_ref = Math.pow(5, 2), [1, 2, 3].map(n => `${(n + _ref) * 2} apples`.toUpperCase()).join()); | ||
expect(result).toEqual('52 APPLES,54 APPLES,56 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
var _ref; | ||
|
||
const result = (_ref = Math.pow(5, 2) + 1, `${_ref} apples`.toUpperCase()); | ||
const result = (_ref = Math.pow(5, 2), `${_ref + 1} apples`.toUpperCase()); | ||
expect(result).toEqual('26 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
var _ref2; | ||
var _ref; | ||
|
||
const result = (_ref2 = Math.pow(5, 2), [1, 2, 3].map(n => { | ||
var _ref; | ||
|
||
return _ref = (n + _ref2) * 2, `${_ref} apples`.toUpperCase(); | ||
}).join()); | ||
const result = (_ref = Math.pow(5, 2), [1, 2, 3].map(n => `${(n + _ref) * 2} apples`.toUpperCase()).join()); | ||
expect(result).toEqual('52 APPLES,54 APPLES,56 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
var _ref; | ||
|
||
const result = (_ref = Math.pow(5, 2) + 1, `${_ref} apples`.toUpperCase()); | ||
const result = (_ref = Math.pow(5, 2), `${_ref + 1} apples`.toUpperCase()); | ||
expect(result).toEqual('26 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
var _ref2; | ||
var _ref; | ||
|
||
const result = (_ref2 = Math.pow(5, 2), [1, 2, 3].map(n => { | ||
var _ref; | ||
|
||
return _ref = (n + _ref2) * 2, `${_ref} apples`.toUpperCase(); | ||
}).join()); | ||
const result = (_ref = Math.pow(5, 2), [1, 2, 3].map(n => `${(n + _ref) * 2} apples`.toUpperCase()).join()); | ||
expect(result).toEqual('52 APPLES,54 APPLES,56 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
var _ref; | ||
|
||
const result = (_ref = Math.pow(5, 2) + 1, `${_ref} apples`.toUpperCase()); | ||
const result = (_ref = Math.pow(5, 2), `${_ref + 1} apples`.toUpperCase()); | ||
expect(result).toEqual('26 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
var _ref2; | ||
var _ref; | ||
|
||
const result = (_ref2 = Math.pow(5, 2), [1, 2, 3].map(n => { | ||
var _ref; | ||
|
||
return _ref = (n + _ref2) * 2, `${_ref} apples`.toUpperCase(); | ||
}).join()); | ||
const result = (_ref = Math.pow(5, 2), [1, 2, 3].map(n => `${(n + _ref) * 2} apples`.toUpperCase()).join()); | ||
expect(result).toEqual('52 APPLES,54 APPLES,56 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
var _ref; | ||
|
||
const result = (_ref = Math.pow(5, 2) + 1, `${_ref} apples`.toUpperCase()); | ||
const result = (_ref = Math.pow(5, 2), `${_ref + 1} apples`.toUpperCase()); | ||
expect(result).toEqual('26 APPLES'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,12 @@ import { | |
unaryExpression, | ||
variableDeclaration, | ||
variableDeclarator, | ||
isRecordExpression, | ||
isTupleExpression, | ||
isObjectProperty, | ||
isTopicReference, | ||
isMetaProperty, | ||
isPrivateName, | ||
} from "@babel/types"; | ||
import type * as t from "@babel/types"; | ||
import { scope as scopeCache } from "../cache"; | ||
|
@@ -535,7 +541,7 @@ export default class Scope { | |
*/ | ||
|
||
isStatic(node: t.Node): boolean { | ||
if (isThisExpression(node) || isSuper(node)) { | ||
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A topic reference should be treated as a constant IdentifierReference, therefore it is static and pure. |
||
return true; | ||
} | ||
|
||
|
@@ -823,10 +829,20 @@ export default class Scope { | |
if (!binding) return false; | ||
if (constantsOnly) return binding.constant; | ||
return true; | ||
} else if ( | ||
isThisExpression(node) || | ||
isMetaProperty(node) || | ||
isTopicReference(node) || | ||
isPrivateName(node) | ||
) { | ||
return true; | ||
} else if (isClass(node)) { | ||
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) { | ||
return false; | ||
} | ||
if (node.decorators?.length > 0) { | ||
return false; | ||
} | ||
return this.isPure(node.body, constantsOnly); | ||
} else if (isClassBody(node)) { | ||
for (const method of node.body) { | ||
|
@@ -838,24 +854,34 @@ export default class Scope { | |
this.isPure(node.left, constantsOnly) && | ||
this.isPure(node.right, constantsOnly) | ||
); | ||
} else if (isArrayExpression(node)) { | ||
} else if (isArrayExpression(node) || isTupleExpression(node)) { | ||
for (const elem of node.elements) { | ||
if (!this.isPure(elem, constantsOnly)) return false; | ||
if (elem !== null && !this.isPure(elem, constantsOnly)) return false; | ||
} | ||
return true; | ||
} else if (isObjectExpression(node)) { | ||
} else if (isObjectExpression(node) || isRecordExpression(node)) { | ||
for (const prop of node.properties) { | ||
if (!this.isPure(prop, constantsOnly)) return false; | ||
} | ||
return true; | ||
} else if (isMethod(node)) { | ||
if (node.computed && !this.isPure(node.key, constantsOnly)) return false; | ||
if (node.kind === "get" || node.kind === "set") return false; | ||
if (node.decorators?.length > 0) { | ||
return false; | ||
} | ||
return true; | ||
} else if (isProperty(node)) { | ||
// @ts-expect-error todo(flow->ts): computed in not present on private properties | ||
if (node.computed && !this.isPure(node.key, constantsOnly)) return false; | ||
return this.isPure(node.value, constantsOnly); | ||
if (node.decorators?.length > 0) { | ||
return false; | ||
} | ||
if (isObjectProperty(node) || node.static) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Evaluating a non-static field definition should be pure as long as it has no computed keys / fancy decorators, since the field initializer is not executed yet. |
||
if (node.value !== null && !this.isPure(node.value, constantsOnly)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else if (isUnaryExpression(node)) { | ||
return this.isPure(node.argument, constantsOnly); | ||
} else if (isTaggedTemplateExpression(node)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { parse } from "@babel/parser"; | ||
|
||
import _traverse from "../../lib/index.js"; | ||
const traverse = _traverse.default; | ||
|
||
function getPath(code) { | ||
const ast = parse(code, { | ||
plugins: [ | ||
["decorators", { version: "2021-12", decoratorsBeforeExport: true }], | ||
["recordAndTuple", { syntaxType: "hash" }], | ||
"decoratorAutoAccessors", | ||
["pipelineOperator", { proposal: "hack", topicToken: "%" }], | ||
], | ||
}); | ||
let path; | ||
traverse(ast, { | ||
Program: function (_path) { | ||
path = _path; | ||
_path.stop(); | ||
}, | ||
}); | ||
return path; | ||
} | ||
|
||
describe("isPure() returns true", () => { | ||
it.each([ | ||
"class C { [0]() {} }", | ||
"class C extends class {} {}", | ||
"class C { static accessor x = 1; accessor y = f() }", | ||
"class C { #x = f(); static #y }", | ||
"class C { static target = new.target }", | ||
"class X { get foo() { return 1 } set foo(v) {} }", | ||
"class C { static #p = #p in C }", | ||
])(`NodePath(%p).get("body.0").isPure() should be true`, input => { | ||
const path = getPath(input).get("body.0"); | ||
expect(path.node).toBeTruthy(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion ensures that the pathRef passed in |
||
expect(path.isPure()).toBe(true); | ||
}); | ||
|
||
it.each([ | ||
"({ x: 1, foo() { return 1 } })", | ||
"String.raw`foo`", | ||
`"a" + "b"`, | ||
`[function () {}]`, | ||
`#{ 0: 0, 1n: 1, two: "two"}`, | ||
`#[0, 1n, "2", \`3\`]`, | ||
`[,]`, | ||
`-1 || void 0`, | ||
`null ?? (true && false)`, | ||
`this`, | ||
])(`NodePath(%p).get("body.0.expression").isPure() should be true`, input => { | ||
const path = getPath(input).get("body.0.expression"); | ||
expect(path.node).toBeTruthy(); | ||
expect(path.isPure()).toBe(true); | ||
}); | ||
|
||
it.each(["let a = 1; `${a}`", `let a = 1; a |> % + %`])( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh, the first one is definitely not pure: let a = { toString() { console.log("Hi :)") } };
`${a}`; however, it was already behaving like this before this PR so if we want to change it I'd prefer to do it in another one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, we can introduce the |
||
`NodePath(%p).get("body.1.expression").isPure() should be true`, | ||
input => { | ||
const path = getPath(input).get("body.1.expression"); | ||
expect(path.node).toBeTruthy(); | ||
expect(path.isPure()).toBe(true); | ||
}, | ||
); | ||
}); | ||
|
||
describe("isPure() returns false", () => { | ||
it.each([ | ||
"@dec() class X {}", | ||
"@dec class C {}; function dec () {}", | ||
"class C { @dec foo() {} }", | ||
"class C { @dec foo }", | ||
"class C { @dec accessor foo = 1 }", | ||
"class C { static {} }", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is technically pure, however the only reason to use a static block is to have a side effect so we can err on the side of "it's never pure" and avoid recursing in its contents. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, |
||
"class C extends class { [f()] } {}", | ||
])(`NodePath(%p).get("body.0").isPure() should be false`, input => { | ||
const path = getPath(input).get("body.0"); | ||
expect(path.node).toBeTruthy(); | ||
expect(path.isPure()).toBe(false); | ||
}); | ||
|
||
it.each(["`${a}`", "tagged`foo`"])( | ||
`NodePath(%p).get("body.0.expression").isPure() should be false`, | ||
input => { | ||
const path = getPath(input).get("body.0.expression"); | ||
expect(path.node).toBeTruthy(); | ||
expect(path.isPure()).toBe(false); | ||
}, | ||
); | ||
|
||
it.each(["let a = 1; `${a++}`"])( | ||
`NodePath(%p).get("body.1.expression").isPure() should be false`, | ||
input => { | ||
const path = getPath(input).get("body.1.expression"); | ||
expect(path.node).toBeTruthy(); | ||
expect(path.isPure()).toBe(false); | ||
}, | ||
); | ||
}); | ||
|
||
describe("isPure(constantsOnly: true) returns false", () => { | ||
it.each(["x", "1 + x", "({ [x]: 0 })", "(class { static x = x })"])( | ||
`NodePath(%p).get("body.0.expression").isPure(/* constantsOnly */true) should be false`, | ||
input => { | ||
const path = getPath(input).get("body.0.expression"); | ||
expect(path.node).toBeTruthy(); | ||
expect(path.isPure(true)).toBe(false); | ||
}, | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Babel is generating less.