| 1 |
import { shouldTransform } from "../lib/util.js"; |
| 2 |
import { parseSync, traverse } from "@babel/core"; |
| 3 |
|
| 4 |
function getPath(input, parserOpts = {}) { |
| 5 |
let targetPath; |
| 6 |
traverse( |
| 7 |
parseSync(input, { |
| 8 |
parserOpts, |
| 9 |
filename: "example.js", |
| 10 |
configFile: false, |
| 11 |
}), |
| 12 |
{ |
| 13 |
FunctionExpression(path) { |
| 14 |
targetPath = path; |
| 15 |
path.stop(); |
| 16 |
}, |
| 17 |
}, |
| 18 |
); |
| 19 |
return targetPath; |
| 20 |
} |
| 21 |
|
| 22 |
describe("shouldTransform", () => { |
| 23 |
const positiveCases = [ |
| 24 |
"(function a([a]) {})", |
| 25 |
"({ b: function a([a]) {} })", |
| 26 |
"(function a({a}) {})", |
| 27 |
"(function a(...a) {})", |
| 28 |
"(function a({ ...a }) {})", |
| 29 |
"(function a([a = 1]) {})", |
| 30 |
"(function a(b, { a: [,...a] }) {})", |
| 31 |
]; |
| 32 |
|
| 33 |
const negativeCases = [ |
| 34 |
"(function () {})", |
| 35 |
"(function a() {})", |
| 36 |
"(function a(a) {})", |
| 37 |
"(function a() { var a })", |
| 38 |
"(function b([a]) { var a })", |
| 39 |
"(function b([a]) { function a() {} })", |
| 40 |
"(function a(x = a) {})", |
| 41 |
"(function a() { var { a } = {}; })", |
| 42 |
"(function b([a]) { var { a } = {}; })", |
| 43 |
"(function a({ [a]: b }) {})", |
| 44 |
]; |
| 45 |
|
| 46 |
describe("the following cases should be transformed", () => { |
| 47 |
test.each(positiveCases)("%p", input => { |
| 48 |
expect(shouldTransform(getPath(input))).toBe("a"); |
| 49 |
}); |
| 50 |
}); |
| 51 |
describe("the following cases should not be transformed", () => { |
| 52 |
test.each(negativeCases)("%p", input => { |
| 53 |
expect(shouldTransform(getPath(input))).toBe(false); |
| 54 |
}); |
| 55 |
}); |
| 56 |
}); |