Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(to-destructuring): new command #12

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { toArrow } from './to-arrow'
import { keepSorted } from './keep-sorted'
import { toForEach } from './to-for-each'
import { toForOf } from './to-for-of'
import { toDestructuringAssignment } from './to-destructuring-assignment'
import { toDynamicImport } from './to-dynamic-import'
import { toStringLiteral } from './to-string-literal'
import { toTemplateLiteral } from './to-template-literal'
Expand All @@ -20,6 +21,7 @@ export {
noShorthand,
noType,
toArrow,
toDestructuringAssignment,
toDynamicImport,
toForEach,
toForOf,
Expand All @@ -37,6 +39,7 @@ export const builtinCommands = [
noShorthand,
noType,
toArrow,
toDestructuringAssignment,
toDynamicImport,
toForEach,
toForOf,
Expand Down
56 changes: 56 additions & 0 deletions src/commands/to-destructuring-assignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# `to-destructuring-assignment`
9romise marked this conversation as resolved.
Show resolved Hide resolved

Convert import statement to dynamic import.

## Triggers

- `/// to-destructuring-assignment`
- `/// to-destructuring`
- `/// 2destructuring-assignment`
- `/// 2destructuring`

## Examples

```js
/// to-destructuring-assignment
const foo = bar.foo
```

Will be converted to:

```js
const { foo } = bar
```

```js
/// to-destructuring-assignment
const baz = bar.foo
```

Will be converted to:

```js
const { foo: baz } = bar
```

```js
/// to-destructuring-assignment
const foo = bar[0]
```

Will be converted to:

```js
const [foo] = bar
```

```js
/// to-destructuring-assignment
const foo = bar[1]
```

Will be converted to:

```js
const [,foo] = bar
```
102 changes: 102 additions & 0 deletions src/commands/to-destructuring-assignment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { toDestructuringAssignment as command } from './to-destructuring-assignment'
import { $, run } from './_test-utils'

run(
command,
{
code: $`
/// to-destructuring-assignment
const foo = bar.foo`,
output: $`
const { foo } = bar`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
const baz = bar.foo`,
output: $`
const { foo: baz } = bar`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
const foo = bar?.foo`,
output: $`
const { foo } = bar ?? {}`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
const foo = bar[1]`,
output: $`
const [,foo] = bar`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
const foo = bar?.[0]`,
output: $`
const [foo] = bar ?? []`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
const foo = bar().foo`,
output: $`
const { foo } = bar()`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
const foo = bar()?.foo`,
output: $`
const { foo } = bar() ?? {}`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
foo = bar.foo`,
output: $`
;({ foo } = bar)`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
baz = bar.foo`,
output: $`
;({ foo: baz } = bar)`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
foo = bar[0]`,
output: $`
;([foo] = bar)`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
foo = bar().foo`,
output: $`
;({ foo } = bar())`,
errors: ['command-removal', 'command-fix'],
},
{
code: $`
/// to-destructuring-assignment
baz = bar().foo`,
output: $`
;({ foo: baz } = bar())`,
errors: ['command-removal', 'command-fix'],
},
)
44 changes: 44 additions & 0 deletions src/commands/to-destructuring-assignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Command } from '../types'

export const toDestructuringAssignment: Command = {
name: 'to-destructuring-assignment',
match: /^[\/:@]\s*(?:to-|2)?(?:destructuring|d)(?:-?assignment|a)?$/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 assignment')

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.removeComment()
ctx.report({
node,
message: 'Convert to destructuring assignment',
fix: fixer => fixer.replaceTextRange(node.range, str),
})
},
}