Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const estraverse = require('estraverse');
- function exportsExpression(node, name) {
- let value = null;
- estraverse.traverse(node, {
- enter(node, parent) {
- if (node.type === 'ExportDefaultDeclaration' && node.declaration.name === name) {
- value = node.declaration.name;
- }
- }
- });
- return value;
- }
- function isRequire(data) {
- let value = false;
- estraverse.traverse(data.node, {
- enter(node, parent) {
- if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require') {
- value = true;
- }
- }
- });
- return value;
- }
- function vueRequire(data) {
- let value = null;
- estraverse.traverse(data.node, {
- enter(node, parent) {
- if (
- node.type === 'ImportDeclaration' &&
- node.source &&
- node.source.type === 'Literal' &&
- node.source.value === 'vue'
- ) {
- node.specifiers.forEach((spec) => {
- if (spec.local && spec.local.name.toLowerCase() === 'vue') {
- value = spec.local.name;
- }
- })
- }
- }
- });
- return value;
- }
- function vueRequireComponent(data) {
- let value = null;
- estraverse.traverse(data.node, {
- enter(node, parent) {
- if (
- node.type === 'ImportDeclaration' &&
- node.source &&
- node.source.type === 'Literal' &&
- node.source.value === 'vue-property-decorator'
- ) {
- node.specifiers.forEach((spec) => {
- if (spec.local && spec.local.name.toLowerCase() === 'component') {
- value = spec.local.name;
- }
- })
- }
- }
- });
- return value;
- }
- function vueClassDeclaration(data) {
- let value = null;
- if (data.declare) {
- estraverse.traverse(data.node, {
- enter(node, parent) {
- if (node.type === 'ClassExpression') {
- if (node.superClass.name === data.declare) {
- value = {
- found: true,
- declaration: node,
- name: node.id.name
- };
- }
- }
- }
- });
- }
- return value;
- }
- function vueClassExportsExpression(data) {
- let value = null;
- if (data.node && data.declare && data.class) {
- estraverse.traverse(data.node, {
- enter(node, parent) {
- const match = exportsExpression(node, data.class.name);
- if (match === data.class.name) {
- value = match;
- }
- }
- });
- }
- return value;
- }
- function createVisitor(data) {
- return {
- enter(node, parent) {
- data.node = node;
- data.declare = data.declare || vueRequire(data);
- data.component = data.component || vueRequireComponent(data);
- data.class = data.class || vueClassDeclaration(data);
- if (!data.hasComponent && vueClassExportsExpression(data)) {
- data.hasComponent = true
- const componentId = {
- type: 'Identifier',
- name: data.identifier
- }
- data.class.declaration = componentId
- return {
- type: 'VariableDeclaration',
- declarations: [{
- type: 'VariableDeclarator',
- id: componentId,
- init: {
- type: 'Identifier',
- name: data.class.name
- }
- }],
- kind: 'var'
- }
- } else {
- return node;
- }
- }
- }
- };
- module.exports = (ast) => {
- let data = {
- class: null,
- code: null,
- component: null,
- declare: null,
- hasComponent: false,
- identifier: '__component__',
- node: null
- };
- let visit = (identifier) => {
- data.identifier = identifier || '__component__';
- const visitor = createVisitor(data);
- data.code = estraverse.replace(ast, visitor);
- delete data['node'];
- return data;
- };
- return { visit };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement