ast-utils.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. const j = require("jscodeshift/dist/core");
  3. const utils = require("./ast-utils");
  4. describe("utils", () => {
  5. describe("createProperty", () => {
  6. it("should create properties for Boolean", () => {
  7. const res = utils.createProperty(j, "foo", true);
  8. expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
  9. });
  10. it("should create properties for Number", () => {
  11. const res = utils.createProperty(j, "foo", -1);
  12. expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
  13. });
  14. it("should create properties for String", () => {
  15. const res = utils.createProperty(j, "foo", "bar");
  16. expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
  17. });
  18. it("should create properties for complex keys", () => {
  19. const res = utils.createProperty(j, "foo-bar", "bar");
  20. expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
  21. });
  22. it("should create properties for non-literal keys", () => {
  23. const res = utils.createProperty(j, 1, "bar");
  24. expect(j(j.objectExpression([res])).toSource()).toMatchSnapshot();
  25. });
  26. });
  27. describe("findPluginsByName", () => {
  28. it("should find plugins in AST", () => {
  29. const ast = j(`
  30. { foo: new webpack.optimize.UglifyJsPlugin() }
  31. `);
  32. const res = utils.findPluginsByName(j, ast, [
  33. "webpack.optimize.UglifyJsPlugin"
  34. ]);
  35. expect(res.size()).toEqual(1);
  36. });
  37. it("should find all plugins in AST", () => {
  38. const ast = j(`
  39. [
  40. new UglifyJsPlugin(),
  41. new TestPlugin()
  42. ]
  43. `);
  44. const res = utils.findPluginsByName(j, ast, [
  45. "UglifyJsPlugin",
  46. "TestPlugin"
  47. ]);
  48. expect(res.size()).toEqual(2);
  49. });
  50. it("should not find false positives", () => {
  51. const ast = j(`
  52. { foo: new UglifyJsPlugin() }
  53. `);
  54. const res = utils.findPluginsByName(j, ast, [
  55. "webpack.optimize.UglifyJsPlugin"
  56. ]);
  57. expect(res.size()).toEqual(0);
  58. });
  59. });
  60. describe("findPluginsArrayAndRemoveIfEmpty", () => {
  61. it("should remove plugins property", () => {
  62. const ast = j(`
  63. const a = {
  64. plugins: []
  65. }
  66. `);
  67. utils.findPluginsArrayAndRemoveIfEmpty(j, ast);
  68. expect(ast.toSource()).toMatchSnapshot();
  69. });
  70. it("It should not remove plugins array, given an array with length greater than zero", () => {
  71. const ast = j(`
  72. const a = {
  73. plugins: [
  74. new MyCustomPlugin()
  75. ]
  76. }
  77. `);
  78. utils.findPluginsArrayAndRemoveIfEmpty(j, ast);
  79. expect(ast.toSource()).toMatchSnapshot();
  80. });
  81. });
  82. describe("findRootNodesByName", () => {
  83. it("should find plugins: [] nodes", () => {
  84. const ast = j(`
  85. const a = { plugins: [], foo: { plugins: [] } }
  86. `);
  87. const res = utils.findRootNodesByName(j, ast, "plugins");
  88. expect(res.size()).toEqual(2);
  89. });
  90. it("should not find plugins: [] nodes", () => {
  91. const ast = j(`
  92. const a = { plugs: [] }
  93. `);
  94. const res = utils.findRootNodesByName(j, ast, "plugins");
  95. expect(res.size()).toEqual(0);
  96. });
  97. });
  98. describe("createOrUpdatePluginByName", () => {
  99. it("should create a new plugin without arguments", () => {
  100. const ast = j("{ plugins: [] }");
  101. ast.find(j.ArrayExpression).forEach(node => {
  102. utils.createOrUpdatePluginByName(j, node, "Plugin");
  103. });
  104. expect(ast.toSource()).toMatchSnapshot();
  105. });
  106. it("should create a new plugin with arguments", () => {
  107. const ast = j("{ plugins: [] }");
  108. ast.find(j.ArrayExpression).forEach(node => {
  109. utils.createOrUpdatePluginByName(j, node, "Plugin", {
  110. foo: "bar"
  111. });
  112. });
  113. expect(ast.toSource()).toMatchSnapshot();
  114. });
  115. it("should add an object as an argument", () => {
  116. const ast = j("[new Plugin()]");
  117. ast.find(j.ArrayExpression).forEach(node => {
  118. utils.createOrUpdatePluginByName(j, node, "Plugin", {
  119. foo: true
  120. });
  121. });
  122. expect(ast.toSource()).toMatchSnapshot();
  123. });
  124. it("should merge options objects", () => {
  125. const ast = j("[new Plugin({ foo: true })]");
  126. ast.find(j.ArrayExpression).forEach(node => {
  127. utils.createOrUpdatePluginByName(j, node, "Plugin", {
  128. bar: "baz",
  129. foo: false
  130. });
  131. utils.createOrUpdatePluginByName(j, node, "Plugin", {
  132. "baz-long": true
  133. });
  134. });
  135. expect(ast.toSource()).toMatchSnapshot();
  136. });
  137. });
  138. describe("findVariableToPlugin", () => {
  139. it("should find the variable name of a plugin", () => {
  140. const ast = j(`
  141. const packageName = require('package-name');
  142. const someOtherconst = somethingElse;
  143. const otherPackage = require('other-package');
  144. `);
  145. const found = utils.findVariableToPlugin(j, ast, "other-package");
  146. expect(found).toEqual("otherPackage");
  147. });
  148. });
  149. describe("createLiteral", () => {
  150. it("should create basic literal", () => {
  151. const literal = utils.createLiteral(j, "stringLiteral");
  152. expect(j(literal).toSource()).toMatchSnapshot();
  153. });
  154. it("should create boolean", () => {
  155. const literal = utils.createLiteral(j, "true");
  156. expect(j(literal).toSource()).toMatchSnapshot();
  157. });
  158. });
  159. describe("createIdentifierOrLiteral", () => {
  160. it("should create basic literal", () => {
  161. const literal = utils.createIdentifierOrLiteral(j, "'stringLiteral'");
  162. expect(j(literal).toSource()).toMatchSnapshot();
  163. });
  164. it("should create boolean", () => {
  165. const literal = utils.createIdentifierOrLiteral(j, "true");
  166. expect(j(literal).toSource()).toMatchSnapshot();
  167. });
  168. });
  169. describe("findObjWithOneOfKeys", () => {
  170. it("should find keys", () => {
  171. const ast = j(`
  172. const ab = {
  173. a: 1,
  174. b: 2
  175. }
  176. `);
  177. expect(
  178. ast
  179. .find(j.ObjectExpression)
  180. .filter(p => utils.findObjWithOneOfKeys(p, ["a"]))
  181. .size()
  182. ).toEqual(1);
  183. });
  184. });
  185. describe("getRequire", () => {
  186. it("should create a require statement", () => {
  187. const require = utils.getRequire(j, "filesys", "fs");
  188. expect(j(require).toSource()).toMatchSnapshot();
  189. });
  190. });
  191. });