* Add test case demonstrating invalid behavior * Add conditional check for child properties so RHS is not duplicated * Add recursive check to find any nested non single-property case * Add case for array destructuring * Add test case for a nested rest element * More safely recurse through array destructuring * Traverse assignment and nested rest operations * Refactor to be more clear which case statement we are executing against * Update missed snapshots * Update packages/babel-plugin-proposal-object-rest-spread/src/index.js Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com> * Filter null array elements, add early return, remove optional chaining * Use stronger type assertions * Update fall through to be false; add early return to RestElement case * Move hasMoreThanOneBinding to its own file with distinct tests * Rename testing helper to more appropriately match business logic * Yarn Dedup & rename hasMoreThanOneBinding to shouldStoreRHSInTemporaryVariable Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
47 lines
2.0 KiB
JavaScript
47 lines
2.0 KiB
JavaScript
import { parse } from "@babel/parser";
|
|
import shouldStoreRHSInTemporaryVariable from "../lib/shouldStoreRHSInTemporaryVariable";
|
|
|
|
function getFistObjectPattern(program) {
|
|
return parse(program, { sourceType: "module" }).program.body[0]
|
|
.declarations[0].id;
|
|
}
|
|
describe("shouldStoreRHSInTemporaryVariable", function () {
|
|
it.each([
|
|
["const { x: { ...y } } = z();", true],
|
|
["let { x4: { ...y4 } } = z();", true],
|
|
["let { x5: { w5, ...y5 } } = z();", true],
|
|
["let { x6: { w6: { a6, ...y6 } } } = z();", true],
|
|
["let { x7: { e7, r7 }, q7: { w7: { a7, ...y7 } } } = z();", true],
|
|
["let { x8, ...y8 } = z();", true],
|
|
["let { x9: { w9: { a9, ...y9 } }, x10: { a10, ...y10 }, } = z();", true],
|
|
["let { x11: [{ w11, ...z11 }] } = z();", true],
|
|
["let { x12: [{ a12, b12 }, { c12, ...d12 }] } = z();", true],
|
|
["let { x13: [, { c13, ...d13 }] } = z();", true],
|
|
["const { x14: [...{ q14, ...y14 }] } = z();", true],
|
|
["const { x15: [...{ ...y16 }] } = z();", true],
|
|
["const [...[ ...y17 ]] = z();", true],
|
|
["const [...{ ...y18 }] = z();", true],
|
|
["const [...{ a19, ...y19 }] = z();", true],
|
|
["const { x20: { ...y20 } = { } } = z();", true],
|
|
["const { x22: { q22, ...y22 } = {} } = z();", true],
|
|
["const [[ ...y23 ] = []] = z();", true],
|
|
["const [{ ...y24 } = []] = z();", true],
|
|
["const { x25: [ ...y25 ] = []} = z();", true],
|
|
["const { x26: [ q26, ...y26 ] = []} = z();", true],
|
|
["const {x28: [,,{...y28}]} = z();", true],
|
|
["const {x29: [,,{q29, ...y29}]} = z();", true],
|
|
["const [,,{y30, ...x30}] = z();", true],
|
|
["const [,,{...x31}] = z();", true],
|
|
["const { x32: { }, w32: { ...y32 } } = z();", true],
|
|
["const [,,{}, {...q32}] = z();", true],
|
|
["const { ...y33 } = z();", true],
|
|
["const { x16: [] } = z();", false],
|
|
["const {} = {};", false],
|
|
["const [,,x27] = z();", false],
|
|
])("%s", (code, expectedResult) => {
|
|
const ast = getFistObjectPattern(code);
|
|
const result = shouldStoreRHSInTemporaryVariable(ast);
|
|
expect(result).toEqual(expectedResult);
|
|
});
|
|
});
|