|
| 1 | +// @flow strict |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +// @noflow |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const fs = require('fs'); |
| 9 | + |
| 10 | +const { HashMap } = require('./helper'); |
| 11 | + |
| 12 | +// Create a new Hashmap to store file names and declarations |
| 13 | +const mp = new HashMap(); |
| 14 | + |
| 15 | +// All the rules that are required by plugin |
| 16 | +const ExportNamedDeclaration = { |
| 17 | + enter(babelPath, state) { |
| 18 | + if (babelPath.node.declaration) { |
| 19 | + return handleDeclaration(babelPath, state); |
| 20 | + } |
| 21 | + |
| 22 | + if (babelPath.node.specifiers) { |
| 23 | + return handleNodeSpecifiers( |
| 24 | + babelPath.node.specifiers, |
| 25 | + state, |
| 26 | + babelPath.node.source ? babelPath.node.source.value : undefined, |
| 27 | + ); |
| 28 | + } |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +const ExportAllDeclaration = { |
| 33 | + enter(babelPath, state) { |
| 34 | + mp.add( |
| 35 | + '*', |
| 36 | + state, |
| 37 | + babelPath.node.source ? babelPath.node.source.value : undefined, |
| 38 | + ); |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +module.exports = function() { |
| 43 | + return { |
| 44 | + visitor: { |
| 45 | + ExportNamedDeclaration, |
| 46 | + ExportAllDeclaration, |
| 47 | + ExportDefaultDeclaration: ExportNamedDeclaration, |
| 48 | + Program: { |
| 49 | + exit() { |
| 50 | + return writeToJSON(); |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }; |
| 55 | +}; |
| 56 | + |
| 57 | +// Helper functions for the rules |
| 58 | +function handleDeclaration(babelPath, state) { |
| 59 | + switch (babelPath.node.declaration.type) { |
| 60 | + case 'VariableDeclaration': |
| 61 | + return handleVariableDeclarations( |
| 62 | + babelPath.node.declaration, |
| 63 | + state, |
| 64 | + babelPath.node.source ? babelPath.node.source.value : undefined, |
| 65 | + ); |
| 66 | + case 'FunctionDeclaration': |
| 67 | + case 'ClassDeclaration': |
| 68 | + return handleFunctionDeclarations( |
| 69 | + babelPath.node.declaration, |
| 70 | + state, |
| 71 | + babelPath.node.source ? babelPath.node.source.value : undefined, |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function handleNodeSpecifiers(specifiers, state, source) { |
| 77 | + return specifiers.forEach(specifier => { |
| 78 | + switch (specifier.type) { |
| 79 | + case 'ExportSpecifier': |
| 80 | + mp.add(specifier.local.name, state, source); |
| 81 | + break; |
| 82 | + case 'ExportNamespaceSpecifier': |
| 83 | + mp.add('*', state, source); |
| 84 | + break; |
| 85 | + } |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +function handleVariableDeclarations(variableDeclaration, state, source) { |
| 90 | + variableDeclaration.declarations.forEach(declaration => |
| 91 | + mp.add(declaration.id.name, state, source), |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +function handleFunctionDeclarations(declaration, state, source) { |
| 96 | + return mp.add(declaration.id.name, state, source); |
| 97 | +} |
| 98 | + |
| 99 | +// To write final result to JSON file |
| 100 | +function writeToJSON() { |
| 101 | + if (!fs.existsSync(path.join(__dirname, '/map.json'))) { |
| 102 | + fs.writeFileSync(path.join(__dirname, '/map.json'), JSON.stringify({})); |
| 103 | + } |
| 104 | + const exportedValues = require(path.join(__dirname, '/map.json')); |
| 105 | + for (const key of mp.keys()) { |
| 106 | + exportedValues[key] = exportedValues[key] || []; |
| 107 | + |
| 108 | + exportedValues[key] = exportedValues[key].concat(Array.from(mp.get(key))); |
| 109 | + |
| 110 | + exportedValues[key] = Array.from(new Set(exportedValues[key])); |
| 111 | + } |
| 112 | + |
| 113 | + fs.writeFileSync( |
| 114 | + path.join(__dirname, '/map.json'), |
| 115 | + JSON.stringify(exportedValues), |
| 116 | + ); |
| 117 | +} |
0 commit comments