Skip to content

Commit

Permalink
fix: preserve function params type if possible (#14694)
Browse files Browse the repository at this point in the history
* fix: preserve function params type if possible

* chore: add test for default value
  • Loading branch information
magic-akari committed Jun 27, 2022
1 parent a7c438c commit 767c7b4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-parameters/src/params.ts
Expand Up @@ -187,6 +187,7 @@ export default function convertFunctionParams(
body.push(defNode);
} else if (param.isObjectPattern() || param.isArrayPattern()) {
const uid = path.scope.generateUidIdentifier("ref");
uid.typeAnnotation = param.node.typeAnnotation;

const defNode = t.variableDeclaration("let", [
t.variableDeclarator(param.node, uid),
Expand Down
@@ -0,0 +1,13 @@
type Point = {
x: number;
y: number;
};

function value({ x, y }: Point) {
return x * y;
}

function valueWithDefault({ x, y }: Point = {}) {
return x * y;
}

@@ -0,0 +1,18 @@
type Point = {
x: number;
y: number;
};

function value(_ref: Point) {
var x = _ref.x,
y = _ref.y;
return x * y;
}

function valueWithDefault() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
x = _ref2.x,
y = _ref2.y;

return x * y;
}

0 comments on commit 767c7b4

Please sign in to comment.