Skip to content

Commit

Permalink
fix(type-construct): should not transform 0 length array
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Sep 29, 2023
1 parent 556b349 commit 376fd18
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function foo(numStr, result) {
return \`\${result} = \${arr}\`;
}
const emptyArr = [];
const oneArr = [,];
`,
`
var a = 6 + Number(x);
Expand All @@ -47,5 +49,8 @@ function foo(numStr, result) {
var arr = Array(3).fill(String(num)).join(' + ');
return \`\${result} = \${arr}\`;
}
const emptyArr = [];
const oneArr = Array(1);
`,
)
13 changes: 10 additions & 3 deletions packages/unminify/src/transformations/un-type-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,16 @@ export const transformAST: ASTTransformation = (context) => {
/**
* [,,,] -> Array(3)
*/
root.find(j.ArrayExpression, { elements: elements => elements.every(element => element === null) }).replaceWith(({ node }) => {
return j.callExpression(j.identifier('Array'), [j.literal(node.elements.length)])
})
root
.find(j.ArrayExpression, {
elements: (elements) => {
return elements.length > 0
&& elements.every(element => element === null)
},
})
.replaceWith(({ node }) => {
return j.callExpression(j.identifier('Array'), [j.literal(node.elements.length)])
})
}

export default wrap(transformAST)

0 comments on commit 376fd18

Please sign in to comment.