Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. const estraverse = require('estraverse');
  2.  
  3. function exportsExpression(node, name) {
  4.  
  5. let value = null;
  6.  
  7. estraverse.traverse(node, {
  8.  
  9. enter(node, parent) {
  10. if (node.type === 'ExportDefaultDeclaration' && node.declaration.name === name) {
  11. value = node.declaration.name;
  12. }
  13. }
  14. });
  15.  
  16. return value;
  17. }
  18.  
  19. function isRequire(data) {
  20. let value = false;
  21.  
  22. estraverse.traverse(data.node, {
  23. enter(node, parent) {
  24. if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require') {
  25. value = true;
  26. }
  27. }
  28. });
  29.  
  30. return value;
  31. }
  32.  
  33. function vueRequire(data) {
  34. let value = null;
  35.  
  36. estraverse.traverse(data.node, {
  37.  
  38. enter(node, parent) {
  39. if (
  40. node.type === 'ImportDeclaration' &&
  41. node.source &&
  42. node.source.type === 'Literal' &&
  43. node.source.value === 'vue'
  44. ) {
  45. node.specifiers.forEach((spec) => {
  46. if (spec.local && spec.local.name.toLowerCase() === 'vue') {
  47. value = spec.local.name;
  48. }
  49. })
  50. }
  51. }
  52. });
  53.  
  54. return value;
  55. }
  56.  
  57. function vueRequireComponent(data) {
  58. let value = null;
  59.  
  60. estraverse.traverse(data.node, {
  61. enter(node, parent) {
  62. if (
  63. node.type === 'ImportDeclaration' &&
  64. node.source &&
  65. node.source.type === 'Literal' &&
  66. node.source.value === 'vue-property-decorator'
  67. ) {
  68. node.specifiers.forEach((spec) => {
  69. if (spec.local && spec.local.name.toLowerCase() === 'component') {
  70. value = spec.local.name;
  71. }
  72. })
  73. }
  74. }
  75. });
  76.  
  77. return value;
  78. }
  79.  
  80. function vueClassDeclaration(data) {
  81. let value = null;
  82.  
  83. if (data.declare) {
  84. estraverse.traverse(data.node, {
  85. enter(node, parent) {
  86. if (node.type === 'ClassExpression') {
  87. if (node.superClass.name === data.declare) {
  88. value = {
  89. found: true,
  90. declaration: node,
  91. name: node.id.name
  92. };
  93. }
  94. }
  95. }
  96. });
  97. }
  98.  
  99. return value;
  100. }
  101.  
  102. function vueClassExportsExpression(data) {
  103.  
  104. let value = null;
  105.  
  106. if (data.node && data.declare && data.class) {
  107. estraverse.traverse(data.node, {
  108. enter(node, parent) {
  109. const match = exportsExpression(node, data.class.name);
  110. if (match === data.class.name) {
  111. value = match;
  112. }
  113. }
  114. });
  115. }
  116.  
  117. return value;
  118. }
  119.  
  120. function createVisitor(data) {
  121.  
  122. return {
  123. enter(node, parent) {
  124.  
  125. data.node = node;
  126. data.declare = data.declare || vueRequire(data);
  127. data.component = data.component || vueRequireComponent(data);
  128. data.class = data.class || vueClassDeclaration(data);
  129.  
  130. if (!data.hasComponent && vueClassExportsExpression(data)) {
  131.  
  132. data.hasComponent = true
  133.  
  134. const componentId = {
  135. type: 'Identifier',
  136. name: data.identifier
  137. }
  138.  
  139. data.class.declaration = componentId
  140.  
  141. return {
  142. type: 'VariableDeclaration',
  143. declarations: [{
  144. type: 'VariableDeclarator',
  145. id: componentId,
  146. init: {
  147. type: 'Identifier',
  148. name: data.class.name
  149. }
  150. }],
  151. kind: 'var'
  152. }
  153. } else {
  154. return node;
  155. }
  156. }
  157. }
  158. };
  159.  
  160. module.exports = (ast) => {
  161.  
  162. let data = {
  163. class: null,
  164. code: null,
  165. component: null,
  166. declare: null,
  167. hasComponent: false,
  168. identifier: '__component__',
  169. node: null
  170. };
  171.  
  172. let visit = (identifier) => {
  173.  
  174. data.identifier = identifier || '__component__';
  175.  
  176. const visitor = createVisitor(data);
  177.  
  178. data.code = estraverse.replace(ast, visitor);
  179.  
  180. delete data['node'];
  181.  
  182. return data;
  183. };
  184.  
  185. return { visit };
  186. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement