path.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { transform } from "../lib/index";
  2. import Plugin from "../lib/config/plugin";
  3. describe("traversal path", function() {
  4. it("replaceWithSourceString", function() {
  5. const expectCode = "function foo() {}";
  6. const actualCode = transform(expectCode, {
  7. cwd: __dirname,
  8. plugins: [
  9. new Plugin({
  10. visitor: {
  11. FunctionDeclaration: function(path) {
  12. path.replaceWithSourceString("console.whatever()");
  13. },
  14. },
  15. }),
  16. ],
  17. }).code;
  18. expect(actualCode).toBe("console.whatever();");
  19. });
  20. it("replaceWith (arrow expression body to block statement body)", function() {
  21. const expectCode = "var fn = () => true;";
  22. const actualCode = transform(expectCode, {
  23. cwd: __dirname,
  24. plugins: [
  25. new Plugin({
  26. visitor: {
  27. ArrowFunctionExpression: function(path) {
  28. path.get("body").replaceWith({
  29. type: "BlockStatement",
  30. body: [
  31. {
  32. type: "ReturnStatement",
  33. argument: {
  34. type: "BooleanLiteral",
  35. value: true,
  36. },
  37. },
  38. ],
  39. });
  40. },
  41. },
  42. }),
  43. ],
  44. }).code;
  45. expect(actualCode).toBe("var fn = () => {\n return true;\n};");
  46. });
  47. it("replaceWith (arrow block statement body to expression body)", function() {
  48. const expectCode = "var fn = () => { return true; }";
  49. const actualCode = transform(expectCode, {
  50. cwd: __dirname,
  51. plugins: [
  52. new Plugin({
  53. visitor: {
  54. ArrowFunctionExpression: function(path) {
  55. path.get("body").replaceWith({
  56. type: "BooleanLiteral",
  57. value: true,
  58. });
  59. },
  60. },
  61. }),
  62. ],
  63. }).code;
  64. expect(actualCode).toBe("var fn = () => true;");
  65. });
  66. it("replaceWith (for-in left expression to variable declaration)", function() {
  67. const expectCode = "for (KEY in right);";
  68. const actualCode = transform(expectCode, {
  69. cwd: __dirname,
  70. plugins: [
  71. new Plugin({
  72. visitor: {
  73. ForInStatement: function(path) {
  74. path.get("left").replaceWith({
  75. type: "VariableDeclaration",
  76. kind: "var",
  77. declarations: [
  78. {
  79. type: "VariableDeclarator",
  80. id: {
  81. type: "Identifier",
  82. name: "KEY",
  83. },
  84. },
  85. ],
  86. });
  87. },
  88. },
  89. }),
  90. ],
  91. }).code;
  92. expect(actualCode).toBe("for (var KEY in right);");
  93. });
  94. it("replaceWith (for-in left variable declaration to expression)", function() {
  95. const expectCode = "for (var KEY in right);";
  96. const actualCode = transform(expectCode, {
  97. cwd: __dirname,
  98. plugins: [
  99. new Plugin({
  100. visitor: {
  101. ForInStatement: function(path) {
  102. path.get("left").replaceWith({
  103. type: "Identifier",
  104. name: "KEY",
  105. });
  106. },
  107. },
  108. }),
  109. ],
  110. }).code;
  111. expect(actualCode).toBe("for (KEY in right);");
  112. });
  113. it("replaceWith (for-loop left expression to variable declaration)", function() {
  114. const expectCode = "for (KEY;;);";
  115. const actualCode = transform(expectCode, {
  116. cwd: __dirname,
  117. plugins: [
  118. new Plugin({
  119. visitor: {
  120. ForStatement: function(path) {
  121. path.get("init").replaceWith({
  122. type: "VariableDeclaration",
  123. kind: "var",
  124. declarations: [
  125. {
  126. type: "VariableDeclarator",
  127. id: {
  128. type: "Identifier",
  129. name: "KEY",
  130. },
  131. },
  132. ],
  133. });
  134. },
  135. },
  136. }),
  137. ],
  138. }).code;
  139. expect(actualCode).toBe("for (var KEY;;);");
  140. });
  141. it("replaceWith (for-loop left variable declaration to expression)", function() {
  142. const expectCode = "for (var KEY;;);";
  143. const actualCode = transform(expectCode, {
  144. cwd: __dirname,
  145. plugins: [
  146. new Plugin({
  147. visitor: {
  148. ForStatement: function(path) {
  149. path.get("init").replaceWith({
  150. type: "Identifier",
  151. name: "KEY",
  152. });
  153. },
  154. },
  155. }),
  156. ],
  157. }).code;
  158. expect(actualCode).toBe("for (KEY;;);");
  159. });
  160. });