Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- module.exports = function convertComponentOptions(babel) {
- const t = babel.types;
- const brandPrefix = t.identifier('brandPrefix');
- const options = t.identifier('options');
- const settings = t.identifier('settings');
- const brandPrefixVisitor = {
- Literal(path) {
- const tokens = /^(.*)bx(--.*)$/.exec(path.node.value);
- if (tokens) {
- path.replaceWith(
- t.templateLiteral(
- [t.templateElement({ raw: tokens[1] || '' }), t.templateElement({ raw: tokens[2] }, true)],
- [brandPrefix]
- )
- );
- this.found = true;
- }
- },
- };
- const topLevelVisitor = {
- ClassProperty(path) {
- if (path.node.static && !path.node.computed && t.isObjectExpression(path.node.value)) {
- const brandPrefixState = {};
- path.traverse(brandPrefixVisitor, brandPrefixState);
- if (brandPrefixState.found) {
- const brandPrefixId = t.objectPattern([t.objectProperty(brandPrefix, brandPrefix)]);
- const declarator = t.variableDeclarator(brandPrefixId, settings);
- path.replaceWith(
- t.classMethod(
- 'get',
- options,
- [],
- t.blockStatement([t.variableDeclaration('const', [declarator]), t.returnStatement(path.node.value)]),
- false,
- true
- )
- );
- path.skip();
- }
- }
- },
- };
- return {
- inherits: require('babel-plugin-syntax-class-properties'),
- visitor: topLevelVisitor,
- };
- };
Add Comment
Please, Sign In to add comment