Repository: skovhus/jest-codemods
Pull request #129 (2018-09-12 06:30:23)

Pull template literals out to comments


Patch:
diff --git a/src/transformers/chai-should.js b/src/transformers/chai-should.js
index 27ded67..9c82679 100644
--- a/src/transformers/chai-should.js
+++ b/src/transformers/chai-should.js
@@ -210,6 +210,27 @@ module.exports = function transformer(fileInfo, api, options) {
         }
     }
 
+    const createLeadingComments = rest => comment => {
+        if (comment.type === 'Literal') {
+            addLeadingComment(rest, j.commentLine(` ${comment.value}`));
+            return;
+        }
+
+        if (comment.type === 'TemplateLiteral') {
+            addLeadingComment(
+                rest,
+                j.commentLine(
+                    ` ${j(comment)
+                        .toSource()
+                        .replace(/`/g, '')}`
+                )
+            );
+            return;
+        }
+
+        addLeadingComment(rest, j.commentLine(` ${j(comment).toSource()}`));
+    };
+
     function getRestWithLengthHandled(p, rest) {
         const containsLength = chainContains('length', p.value.callee, isPrefix);
         const newRest = containsLength
@@ -218,9 +239,7 @@ module.exports = function transformer(fileInfo, api, options) {
         if (newRest.arguments) {
             // Add expect's second argument as a comment (if one exists)
             const comments = newRest.arguments.slice(1, 2);
-            comments.forEach(comment => {
-                addLeadingComment(newRest, j.commentLine(` ${comment.value}`));
-            });
+            comments.forEach(createLeadingComments(newRest));
 
             // Jest's expect only allows one argument
             newRest.arguments = newRest.arguments.slice(0, 1);
@@ -313,9 +332,7 @@ module.exports = function transformer(fileInfo, api, options) {
                 if (rest.arguments !== undefined) {
                     // Add expect's second argument as a comment (if one exists)
                     const comments = rest.arguments.slice(1, 2);
-                    comments.forEach(comment => {
-                        addLeadingComment(rest, j.commentLine(` ${comment.value}`));
-                    });
+                    comments.forEach(createLeadingComments(rest));
 
                     // Jest's expect only allows one argument
                     rest.arguments = rest.arguments.slice(0, 1);


Test patch:
diff --git a/src/transformers/chai-should.test.js b/src/transformers/chai-should.test.js
index 15add51..b6bf869 100644
--- a/src/transformers/chai-should.test.js
+++ b/src/transformers/chai-should.test.js
@@ -636,12 +636,18 @@ testChanged(
     `
         expect(foo, 'Expected foo to be defined').to.exist;
         expect(foo, 'Expected foo to be defined').to.equal(true);
+        expect(foo, \`Expected foo to be defined for \${id}\`).to.be.defined;
+        expect(foo, 'Expected ' + foo + ' to be defined').to.equal(true);
     `,
     `
         // Expected foo to be defined
         expect(foo).toBeDefined();
         // Expected foo to be defined
         expect(foo).toBe(true);
+        // Expected foo to be defined for \${id}
+        expect(foo).toBeDefined();
+        // 'Expected ' + foo + ' to be defined'
+        expect(foo).toBe(true);
     `
 );