Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update detection of pure nodes (
Scope#isPure
) (#14424)
- Loading branch information
Showing
16 changed files
with
163 additions
and
78 deletions.
There are no files selected for viewing
8 changes: 2 additions & 6 deletions
8
...operator/test/fixtures/hack-caret/pipe-body-with-arrow-function-and-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
2 changes: 1 addition & 1 deletion
2
...-proposal-pipeline-operator/test/fixtures/hack-caret/pipe-body-with-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
8 changes: 2 additions & 6 deletions
8
...ator/test/fixtures/hack-double-at/pipe-body-with-arrow-function-and-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
2 changes: 1 addition & 1 deletion
2
...posal-pipeline-operator/test/fixtures/hack-double-at/pipe-body-with-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
8 changes: 2 additions & 6 deletions
8
...r/test/fixtures/hack-double-caret/pipe-body-with-arrow-function-and-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
2 changes: 1 addition & 1 deletion
2
...al-pipeline-operator/test/fixtures/hack-double-caret/pipe-body-with-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
8 changes: 2 additions & 6 deletions
8
...-operator/test/fixtures/hack-hash/pipe-body-with-arrow-function-and-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
2 changes: 1 addition & 1 deletion
2
...n-proposal-pipeline-operator/test/fixtures/hack-hash/pipe-body-with-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
8 changes: 2 additions & 6 deletions
8
...erator/test/fixtures/hack-percent/pipe-body-with-arrow-function-and-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
2 changes: 1 addition & 1 deletion
2
...roposal-pipeline-operator/test/fixtures/hack-percent/pipe-body-with-nested-pipe/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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 |> % + %`])( | ||
`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 {} }", | ||
"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); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters