| 1 |
import { parse } from "@babel/parser"; |
| 2 |
|
| 3 |
import _shouldStoreRHSInTemporaryVariable from "../lib/shouldStoreRHSInTemporaryVariable.js"; |
| 4 |
const shouldStoreRHSInTemporaryVariable = |
| 5 |
_shouldStoreRHSInTemporaryVariable.default || |
| 6 |
_shouldStoreRHSInTemporaryVariable; |
| 7 |
|
| 8 |
function getFistObjectPattern(program) { |
| 9 |
return parse(program, { sourceType: "module" }).program.body[0] |
| 10 |
.declarations[0].id; |
| 11 |
} |
| 12 |
describe("shouldStoreRHSInTemporaryVariable", function () { |
| 13 |
it.each([ |
| 14 |
["const { x: { ...y } } = z();", true], |
| 15 |
["let { x4: { ...y4 } } = z();", true], |
| 16 |
["let { x5: { w5, ...y5 } } = z();", true], |
| 17 |
["let { x6: { w6: { a6, ...y6 } } } = z();", true], |
| 18 |
["let { x7: { e7, r7 }, q7: { w7: { a7, ...y7 } } } = z();", true], |
| 19 |
["let { x8, ...y8 } = z();", true], |
| 20 |
["let { x9: { w9: { a9, ...y9 } }, x10: { a10, ...y10 }, } = z();", true], |
| 21 |
["let { x11: [{ w11, ...z11 }] } = z();", true], |
| 22 |
["let { x12: [{ a12, b12 }, { c12, ...d12 }] } = z();", true], |
| 23 |
["let { x13: [, { c13, ...d13 }] } = z();", true], |
| 24 |
["const { x14: [...{ q14, ...y14 }] } = z();", true], |
| 25 |
["const { x15: [...{ ...y16 }] } = z();", true], |
| 26 |
["const [...[ ...y17 ]] = z();", true], |
| 27 |
["const [...{ ...y18 }] = z();", true], |
| 28 |
["const [...{ a19, ...y19 }] = z();", true], |
| 29 |
["const { x20: { ...y20 } = { } } = z();", true], |
| 30 |
["const { x22: { q22, ...y22 } = {} } = z();", true], |
| 31 |
["const [[ ...y23 ] = []] = z();", true], |
| 32 |
["const [{ ...y24 } = []] = z();", true], |
| 33 |
["const { x25: [ ...y25 ] = []} = z();", true], |
| 34 |
["const { x26: [ q26, ...y26 ] = []} = z();", true], |
| 35 |
["const {x28: [,,{...y28}]} = z();", true], |
| 36 |
["const {x29: [,,{q29, ...y29}]} = z();", true], |
| 37 |
["const [,,{y30, ...x30}] = z();", true], |
| 38 |
["const [,,{...x31}] = z();", true], |
| 39 |
["const { x32: { }, w32: { ...y32 } } = z();", true], |
| 40 |
["const [,,{}, {...q32}] = z();", true], |
| 41 |
["const { ...y33 } = z();", true], |
| 42 |
["const { x16: [] } = z();", false], |
| 43 |
["const {} = {};", false], |
| 44 |
["const [,,x27] = z();", false], |
| 45 |
])("%s", (code, expectedResult) => { |
| 46 |
const ast = getFistObjectPattern(code); |
| 47 |
const result = shouldStoreRHSInTemporaryVariable(ast); |
| 48 |
expect(result).toEqual(expectedResult); |
| 49 |
}); |
| 50 |
}); |