Skip to content

Commit

Permalink
feat(let-const): Support for array patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ticklepoke committed Jul 6, 2021
1 parent 888a499 commit 4fb7a82
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/__testfixtures__/convert-let-const.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ try {
let a = 1;
a++;
} catch (err) {}
let [d] = foo();
let [e, f] = bar();
2 changes: 2 additions & 0 deletions src/__testfixtures__/convert-let-const.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ try {
let a = 1;
a++;
} catch (err) {}
const [d] = foo();
const [e, f] = bar();
23 changes: 22 additions & 1 deletion src/convert-let-const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ExpressionStatement,
FileInfo,
FunctionDeclaration,
Property,
TryStatement,
VariableDeclaration,
} from 'jscodeshift';
Expand Down Expand Up @@ -72,6 +71,16 @@ function handleVariableDeclaration(node: VariableDeclaration, depth: number, sto
}
}
});
} else if (decl.id.type === 'ArrayPattern') {
decl.id.elements.forEach((el) => {
if (el?.type === 'Identifier') {
if (store.has(el.name)) {
store.get(el.name)?.push(depth);
} else {
store.set(el.name, [depth]);
}
}
});
}
if (decl.init?.type === 'FunctionExpression') {
decl.init.body = handleBlock(decl.init.body, depth + 1, store);
Expand Down Expand Up @@ -137,6 +146,18 @@ function convertLetConst(body: StatementKind[], depth: number, store: Store): St
if (inStore) {
stmt.kind = 'const';
}
} else if (decl.id.type === 'ArrayPattern') {
const inStore = decl.id.elements.every((el) => {
if (el?.type === 'Identifier') {
if (store.get(el.name)?.includes(depth)) {
return true;
}
return false;
}
});
if (inStore) {
stmt.kind = 'const';
}
}
}
});
Expand Down

0 comments on commit 4fb7a82

Please sign in to comment.