Guest User

Untitled

a guest
Jan 17th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. 'use strict';
  2.  
  3. module.exports = function convertComponentOptions(babel) {
  4. const t = babel.types;
  5. const brandPrefix = t.identifier('brandPrefix');
  6. const options = t.identifier('options');
  7. const settings = t.identifier('settings');
  8.  
  9. const brandPrefixVisitor = {
  10. Literal(path) {
  11. const tokens = /^(.*)bx(--.*)$/.exec(path.node.value);
  12. if (tokens) {
  13. path.replaceWith(
  14. t.templateLiteral(
  15. [t.templateElement({ raw: tokens[1] || '' }), t.templateElement({ raw: tokens[2] }, true)],
  16. [brandPrefix]
  17. )
  18. );
  19. this.found = true;
  20. }
  21. },
  22. };
  23.  
  24. const topLevelVisitor = {
  25. ClassProperty(path) {
  26. if (path.node.static && !path.node.computed && t.isObjectExpression(path.node.value)) {
  27. const brandPrefixState = {};
  28. path.traverse(brandPrefixVisitor, brandPrefixState);
  29. if (brandPrefixState.found) {
  30. const brandPrefixId = t.objectPattern([t.objectProperty(brandPrefix, brandPrefix)]);
  31. const declarator = t.variableDeclarator(brandPrefixId, settings);
  32. path.replaceWith(
  33. t.classMethod(
  34. 'get',
  35. options,
  36. [],
  37. t.blockStatement([t.variableDeclaration('const', [declarator]), t.returnStatement(path.node.value)]),
  38. false,
  39. true
  40. )
  41. );
  42. path.skip();
  43. }
  44. }
  45. },
  46. };
  47.  
  48. return {
  49. inherits: require('babel-plugin-syntax-class-properties'),
  50. visitor: topLevelVisitor,
  51. };
  52. };
Add Comment
Please, Sign In to add comment