Repository: nicoespeon/abracadabra
Pull request #706 (2022-09-23 12:12:32)

Enhance the default name for new expression
When extracting a class instantiation, for instance `new ImageEditor()`, provide a more user-friendly default name (instead of `extracted`). The new default is the camel-cased class name, `imageEditor` in the example above.


Patch:
diff --git a/src/refactorings/extract/extract-variable/occurrence.ts b/src/refactorings/extract/extract-variable/occurrence.ts
index b082099d..a7460bb1 100644
--- a/src/refactorings/extract/extract-variable/occurrence.ts
+++ b/src/refactorings/extract/extract-variable/occurrence.ts
@@ -7,7 +7,8 @@ import {
   Variable,
   StringLiteralVariable,
   MemberExpressionVariable,
-  ShorthandVariable
+  ShorthandVariable,
+  NewExpressionVariable
 } from "./variable";
 import { Parts } from "./parts";
 import { DestructureStrategy } from "./destructure-strategy";
@@ -49,6 +50,10 @@ export function createOccurrence(
     );
   }
 
+  if (path.isNewExpression()) {
+    return new Occurrence(path, loc, new NewExpressionVariable(path));
+  }
+
   if (path.isStringLiteral()) {
     if (path.parentPath.isJSX()) {
       if (!selection.isEmpty() && selection.isStrictlyInsidePath(path)) {
diff --git a/src/refactorings/extract/extract-variable/variable.ts b/src/refactorings/extract/extract-variable/variable.ts
index 338d82ee..648c4bd2 100644
--- a/src/refactorings/extract/extract-variable/variable.ts
+++ b/src/refactorings/extract/extract-variable/variable.ts
@@ -87,6 +87,15 @@ export class StringLiteralVariable extends Variable<t.Node> {
   }
 }
 
+export class NewExpressionVariable extends Variable<t.NewExpression> {
+  constructor(path: t.NodePath<t.NewExpression>) {
+    super(path);
+    if (path.node.callee.type === "Identifier") {
+      this.tryToSetNameWith(camelCase(path.node.callee.name));
+    }
+  }
+}
+
 export class MemberExpressionVariable extends Variable<
   t.MemberExpression | t.OptionalMemberExpression
 > {


Test patch:
diff --git a/src/refactorings/extract/extract-variable/extract-variable.extractable.test.ts b/src/refactorings/extract/extract-variable/extract-variable.extractable.test.ts
index 381b70a5..9e9bf6a4 100644
--- a/src/refactorings/extract/extract-variable/extract-variable.extractable.test.ts
+++ b/src/refactorings/extract/extract-variable/extract-variable.extractable.test.ts
@@ -396,20 +396,20 @@ if (
       {
         description: "a class instantiation (cursor on new expression)",
         code: `console.log([cursor]new Card("jack"));`,
-        expected: `const extracted = new Card("jack");
-console.log(extracted);`
+        expected: `const card = new Card("jack");
+console.log(card);`
       },
       {
         description: "a class instantiation (cursor on class identifier)",
         code: `console.log(new [cursor]Card("jack"));`,
-        expected: `const extracted = new Card("jack");
-console.log(extracted);`
+        expected: `const card = new Card("jack");
+console.log(card);`
       },
       {
         description: "a thrown error",
         code: `throw new Er[cursor]ror("It failed");`,
-        expected: `const extracted = new Error("It failed");
-throw extracted;`
+        expected: `const error = new Error("It failed");
+throw error;`
       },
       {
         description: "a call expression parameter (multi-lines)",
@@ -462,9 +462,9 @@ doSomething([
         code: `doSomething([
   [cursor]new Author("Eliott")
 ]);`,
-        expected: `const extracted = new Author("Eliott");
+        expected: `const author = new Author("Eliott");
 doSomething([
-  extracted
+  author
 ]);`
       },
       {
diff --git a/src/refactorings/extract/extract-variable/extract-variable.multiple-occurrences.test.ts b/src/refactorings/extract/extract-variable/extract-variable.multiple-occurrences.test.ts
index 4230f0cb..bd2e0c57 100644
--- a/src/refactorings/extract/extract-variable/extract-variable.multiple-occurrences.test.ts
+++ b/src/refactorings/extract/extract-variable/extract-variable.multiple-occurrences.test.ts
@@ -407,9 +407,9 @@ sendMessage(extracted);`
         description: "new expression",
         code: `console.log([cursor]new Actor("John"));
 sendMessage(new Actor("John"));`,
-        expected: `const extracted = new Actor("John");
-console.log(extracted);
-sendMessage(extracted);`
+        expected: `const actor = new Actor("John");
+console.log(actor);
+sendMessage(actor);`
       },
       {
         description: "JSX Element",