diff --git a/src/commands/to-arrow.test.ts b/src/commands/to-arrow.test.ts index 96de562..8e9ec86 100644 --- a/src/commands/to-arrow.test.ts +++ b/src/commands/to-arrow.test.ts @@ -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() { @@ -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() { @@ -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() { @@ -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() { diff --git a/src/commands/to-arrow.ts b/src/commands/to-arrow.ts index 74c2244..899905d 100644 --- a/src/commands/to-arrow.ts +++ b/src/commands/to-arrow.ts @@ -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, @@ -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) }, })