UNPKG

2.49 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _babelTraverse = require('babel-traverse');
10
11var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
12
13var _babelTypes = require('babel-types');
14
15var _lodash = require('lodash');
16
17var _pascalCase = require('pascal-case');
18
19var _pascalCase2 = _interopRequireDefault(_pascalCase);
20
21var _getImports = require('./get-imports');
22
23var _getImports2 = _interopRequireDefault(_getImports);
24
25var _normalizeTagName = require('./normalize-tag-name');
26
27var _normalizeTagName2 = _interopRequireDefault(_normalizeTagName);
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31/**
32 * Get names of implicit dependencies for component ast
33 * @param {Object} ast - ast to search for unbound identifiers
34 * @param {Array<String>} candidates - normalized dependency names to import
35 * @return {Array<String>} local names to import implicitly
36 * @deprecate
37 */
38exports.default = function (ast) {
39 let candidates = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
40
41 const unboundIdentifiers = [];
42
43 (0, _babelTraverse2.default)(ast, {
44 ReferencedIdentifier: function ReferencedIdentifier(path) {
45 const binding = path.scope.getBinding(path.node.name);
46
47 if (binding) {
48 return;
49 }
50
51 const normalizedName = (0, _normalizeTagName2.default)(path.node.name);
52
53 // if is jsx or is PascalCase
54 // const firstChar = path.node.name.charAt(0);
55 if (!path.isJSXIdentifier() && !candidates.includes(normalizedName)) {
56 return;
57 }
58
59 if (_babelTypes.react.isCompatTag(normalizedName)) {
60 return;
61 }
62
63 unboundIdentifiers.push(normalizedName);
64 }
65 });
66
67 const explicitDependencies = (0, _getImports2.default)(ast).identifiers.map((_ref) => {
68 let value = _ref.value;
69 return (0, _pascalCase2.default)(value);
70 });
71
72 const implicitDependencies = (0, _lodash.uniq)((0, _lodash.difference)(unboundIdentifiers, explicitDependencies));
73
74 return implicitDependencies.reduce((registry, implicitDependencyName) => {
75 return _extends({}, registry, {
76 [implicitDependencyName]: (0, _lodash.kebabCase)(implicitDependencyName)
77 });
78 }, {});
79};
80
81module.exports = exports['default'];
\No newline at end of file