Skip to content

Commit

Permalink
fix(to-arrow): add missing modifiers/computed
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 3, 2024
1 parent 90bc041 commit b7e2f41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
18 changes: 14 additions & 4 deletions src/commands/to-arrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ run(
code: d`
const bar = {
/// to-arrow
async bar(a: number, b: number): number {
async [bar]?(a: number, b: number): number {
return a + b
},
foo() {
Expand All @@ -54,7 +54,7 @@ run(
}`,
output: d`
const bar = {
bar: async (a: number, b: number): number => {
[bar]: async (a: number, b: number): number => {
return a + b
},
foo() {
Expand All @@ -63,12 +63,22 @@ run(
}`,
errors: ['command-removal', 'command-fix'],
},
// Getter/setter
{
code: d`
const bar = {
/// to-arrow
get id() {}
}`,
output: null,
errors: 'command-error',
},
// Class method
{
code: d`
class Bar {
/// to-arrow
async bar(a: number, b: number): number {
private static override async [bar]?(a: number, b: number): number {
return a + b
}
foo() {
Expand All @@ -77,7 +87,7 @@ run(
}`,
output: d`
class Bar {
bar = async (a: number, b: number): number => {
private static override [bar] ? = async (a: number, b: number): number => {
return a + b
}
foo() {
Expand Down
32 changes: 19 additions & 13 deletions src/commands/to-arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export const toArrow: Command = {
let rangeStart = fn.range[0]
const rangeEnd = fn.range[1]

const parent = fn.parent

if (parent.type === 'Property' && parent.kind !== 'init')
return ctx.reportError(`Cannot convert ${parent.kind}ter property to arrow function`)

ctx.removeComment()
ctx.report({
node: fn,
Expand Down Expand Up @@ -42,24 +47,25 @@ export const toArrow: Command = {
}

// For object methods
else if (fn.parent.type === 'Property') {
rangeStart = fn.parent.range[0]
textName = ctx.getTextOf(fn.parent.key)
final = `${textName}: ${final}`
else if (parent.type === 'Property') {
rangeStart = parent.range[0]
textName = ctx.getTextOf(parent.key)
final = `${parent.computed ? `[${textName}]` : textName}: ${final}`
}

// For class methods
else if (fn.parent.type === 'MethodDefinition') {
rangeStart = fn.parent.range[0]
textName = ctx.getTextOf(fn.parent.key)
final = `${textName} = ${final}`
else if (parent.type === 'MethodDefinition') {
rangeStart = parent.range[0]
textName = ctx.getTextOf(parent.key)
final = `${[
parent.accessibility,
parent.static && 'static',
parent.override && 'override',
parent.computed ? `[${textName}]` : textName,
parent.optional && '?',
].filter(Boolean).join(' ')} = ${final}`
}

// console.log({
// final,
// original: code.slice(rangeStart, rangeEnd),
// p: fn.parent.type,
// })
return fixer.replaceTextRange([rangeStart, rangeEnd], final)
},
})
Expand Down

0 comments on commit b7e2f41

Please sign in to comment.