-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(to-destructuring): new command (#12)
Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# `to-destructuring` | ||
|
||
Convert an assignment expression to destructuring assignment. | ||
|
||
## Triggers | ||
|
||
- `/// to-destructuring` | ||
- `/// to-dest` | ||
- `/// 2destructuring` | ||
- `/// 2dest` | ||
|
||
## Examples | ||
|
||
```js | ||
/// to-destructuring | ||
const foo = bar.foo | ||
|
||
/// to-dest | ||
const baz = bar?.foo | ||
|
||
/// 2destructuring | ||
const foo = bar[0] | ||
|
||
/// 2dest | ||
const foo = bar?.[1] | ||
|
||
let foo | ||
/// to-destructuring | ||
foo = bar().foo | ||
``` | ||
Will be converted to: | ||
```js | ||
const { foo } = bar | ||
|
||
const { foo: baz } = bar ?? {} | ||
|
||
const [foo] = bar | ||
|
||
const [,foo] = bar ?? [] | ||
|
||
let foo | ||
;({ foo } = bar()) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { toDestructuring as command } from './to-destructuring' | ||
import { $, run } from './_test-utils' | ||
|
||
run( | ||
command, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const foo = bar.foo`, | ||
output: $` | ||
const { foo } = bar`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const baz = bar.foo`, | ||
output: $` | ||
const { foo: baz } = bar`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const foo = bar?.foo`, | ||
output: $` | ||
const { foo } = bar ?? {}`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const foo = bar[1]`, | ||
output: $` | ||
const [,foo] = bar`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const foo = bar?.[0]`, | ||
output: $` | ||
const [foo] = bar ?? []`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const foo = bar().foo`, | ||
output: $` | ||
const { foo } = bar()`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
const foo = bar()?.foo`, | ||
output: $` | ||
const { foo } = bar() ?? {}`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
foo = bar.foo`, | ||
output: $` | ||
;({ foo } = bar)`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
baz = bar.foo`, | ||
output: $` | ||
;({ foo: baz } = bar)`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
foo = bar[0]`, | ||
output: $` | ||
;([foo] = bar)`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
foo = bar().foo`, | ||
output: $` | ||
;({ foo } = bar())`, | ||
errors: ['command-fix'], | ||
}, | ||
{ | ||
code: $` | ||
/// to-destructuring | ||
baz = bar().foo`, | ||
output: $` | ||
;({ foo: baz } = bar())`, | ||
errors: ['command-fix'], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Command } from '../types' | ||
|
||
export const toDestructuring: Command = { | ||
name: 'to-destructuring', | ||
match: /^[\/:@]\s*(?:to-|2)?(?:destructuring|dest)?$/i, | ||
action(ctx) { | ||
const node = ctx.findNodeBelow( | ||
'VariableDeclaration', | ||
'AssignmentExpression', | ||
) | ||
if (!node) | ||
return ctx.reportError('Unable to find object/array to convert') | ||
|
||
const isDeclaration = node.type === 'VariableDeclaration' | ||
|
||
const rightExpression = isDeclaration ? node.declarations[0].init : node.right | ||
const member = rightExpression?.type === 'ChainExpression' ? rightExpression.expression : rightExpression | ||
|
||
if (member?.type !== 'MemberExpression') | ||
return ctx.reportError('Unable to convert to destructuring') | ||
|
||
const id = isDeclaration ? ctx.getTextOf(node.declarations[0].id) : ctx.getTextOf(node.left) | ||
const property = ctx.getTextOf(member.property) | ||
|
||
// TODO maybe there is no good way to know if this is an array or object | ||
const isArray = !Number.isNaN(Number(property)) | ||
|
||
const left = isArray ? `${','.repeat(Number(property))}${id}` : `${id === property ? id : `${property}: ${id}`}` | ||
|
||
let right = `${ctx.getTextOf(member.object)}` | ||
if (member.optional) | ||
right += ` ?? ${isArray ? '[]' : '{}'}` | ||
|
||
let str = isArray ? `[${left}] = ${right}` : `{ ${left} } = ${right}` | ||
str = isDeclaration ? `${node.kind} ${str}` : `;(${str})` | ||
|
||
ctx.report({ | ||
node, | ||
message: 'Convert to destructuring', | ||
fix: fixer => fixer.replaceTextRange(node.range, str), | ||
}) | ||
}, | ||
} |