Files

171 lines
28 KiB
JavaScript
Raw Permalink Normal View History

'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _arrayIncludes = require('array-includes');var _arrayIncludes2 = _interopRequireDefault(_arrayIncludes);
var _eslint = require('eslint');
var _doc = require('./doc');
var _namespace = require('./namespace');var _namespace2 = _interopRequireDefault(_namespace);
var _specifier = require('./specifier');var _specifier2 = _interopRequireDefault(_specifier);
var _captureDependency = require('./captureDependency');
var _patternCapture = require('./patternCapture');var _patternCapture2 = _interopRequireDefault(_patternCapture);
var _remotePath = require('./remotePath');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}
/**
* sometimes legacy support isn't _that_ hard... right?
*/
function makeSourceCode(text, ast) {
if (_eslint.SourceCode.length > 1) {
// ESLint 3
return new _eslint.SourceCode(text, ast);
} else {
// ESLint 4, 5
return new _eslint.SourceCode({ text: text, ast: ast });
}
}var
ImportExportVisitorBuilder = function () {
function ImportExportVisitorBuilder(
path,
context,
exportMap,
ExportMapBuilder,
content,
ast,
isEsModuleInteropTrue,
thunkFor)
{var _this = this;_classCallCheck(this, ImportExportVisitorBuilder);
this.context = context;
this.namespace = new _namespace2['default'](path, context, ExportMapBuilder);
this.remotePathResolver = new _remotePath.RemotePath(path, context);
this.source = makeSourceCode(content, ast);
this.exportMap = exportMap;
this.ast = ast;
this.isEsModuleInteropTrue = isEsModuleInteropTrue;
this.thunkFor = thunkFor;
var docstyle = this.context.settings && this.context.settings['import/docstyle'] || ['jsdoc'];
this.docStyleParsers = {};
docstyle.forEach(function (style) {
_this.docStyleParsers[style] = _doc.availableDocStyleParsers[style];
});
}_createClass(ImportExportVisitorBuilder, [{ key: 'build', value: function () {function build(
astNode) {var _this3 = this;
return Object.assign({
ExportDefaultDeclaration: function () {function ExportDefaultDeclaration() {
var exportMeta = (0, _doc.captureDoc)(this.source, this.docStyleParsers, astNode);
if (astNode.declaration.type === 'Identifier') {
this.namespace.add(exportMeta, astNode.declaration);
}
this.exportMap.namespace.set('default', exportMeta);
}return ExportDefaultDeclaration;}(),
ExportAllDeclaration: function () {function ExportAllDeclaration() {
var getter = (0, _captureDependency.captureDependency)(astNode, astNode.exportKind === 'type', this.remotePathResolver, this.exportMap, this.context, this.thunkFor);
if (getter) {this.exportMap.dependencies.add(getter);}
if (astNode.exported) {
(0, _specifier2['default'])(astNode, astNode.exported, this.exportMap, this.namespace);
}
}return ExportAllDeclaration;}(),
/** capture namespaces in case of later export */
ImportDeclaration: function () {function ImportDeclaration() {
(0, _captureDependency.captureDependencyWithSpecifiers)(astNode, this.remotePathResolver, this.exportMap, this.context, this.thunkFor);
var ns = astNode.specifiers.find(function (s) {return s.type === 'ImportNamespaceSpecifier';});
if (ns) {
this.namespace.rawSet(ns.local.name, astNode.source.value);
}
}return ImportDeclaration;}(),
ExportNamedDeclaration: function () {function ExportNamedDeclaration() {var _this2 = this;
(0, _captureDependency.captureDependencyWithSpecifiers)(astNode, this.remotePathResolver, this.exportMap, this.context, this.thunkFor);
// capture declaration
if (astNode.declaration != null) {
switch (astNode.declaration.type) {
case 'FunctionDeclaration':
case 'ClassDeclaration':
case 'TypeAlias': // flowtype with babel-eslint parser
case 'InterfaceDeclaration':
case 'DeclareFunction':
case 'TSDeclareFunction':
case 'TSEnumDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
case 'TSAbstractClassDeclaration':
case 'TSModuleDeclaration':
this.exportMap.namespace.set(astNode.declaration.id.name, (0, _doc.captureDoc)(this.source, this.docStyleParsers, astNode));
break;
case 'VariableDeclaration':
astNode.declaration.declarations.forEach(function (d) {
(0, _patternCapture2['default'])(
d.id,
function (id) {return _this2.exportMap.namespace.set(id.name, (0, _doc.captureDoc)(_this2.source, _this2.docStyleParsers, d, astNode));});
});
break;
default:}
}
astNode.specifiers.forEach(function (s) {return (0, _specifier2['default'])(s, astNode, _this2.exportMap, _this2.namespace);});
}return ExportNamedDeclaration;}(),
TSExportAssignment: function () {function TSExportAssignment() {return _this3.typeScriptExport(astNode);}return TSExportAssignment;}() },
this.isEsModuleInteropTrue && { TSNamespaceExportDeclaration: function () {function TSNamespaceExportDeclaration() {return _this3.typeScriptExport(astNode);}return TSNamespaceExportDeclaration;}() });
}return build;}()
// This doesn't declare anything, but changes what's being exported.
}, { key: 'typeScriptExport', value: function () {function typeScriptExport(astNode) {var _this4 = this;
var exportedName = astNode.type === 'TSNamespaceExportDeclaration' ?
(astNode.id || astNode.name).name :
astNode.expression && astNode.expression.name || astNode.expression.id && astNode.expression.id.name || null;
var declTypes = [
'VariableDeclaration',
'ClassDeclaration',
'TSDeclareFunction',
'TSEnumDeclaration',
'TSTypeAliasDeclaration',
'TSInterfaceDeclaration',
'TSAbstractClassDeclaration',
'TSModuleDeclaration'];
var exportedDecls = this.ast.body.filter(function (_ref) {var type = _ref.type,id = _ref.id,declarations = _ref.declarations;return (0, _arrayIncludes2['default'])(declTypes, type) && (
id && id.name === exportedName || declarations && declarations.find(function (d) {return d.id.name === exportedName;}));});
if (exportedDecls.length === 0) {
// Export is not referencing any local declaration, must be re-exporting
this.exportMap.namespace.set('default', (0, _doc.captureDoc)(this.source, this.docStyleParsers, astNode));
return;
}
if (
this.isEsModuleInteropTrue // esModuleInterop is on in tsconfig
&& !this.exportMap.namespace.has('default') // and default isn't added already
) {
this.exportMap.namespace.set('default', {}); // add default export
}
exportedDecls.forEach(function (decl) {
if (decl.type === 'TSModuleDeclaration') {
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
_this4.exportMap.namespace.set(decl.body.id.name, (0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, decl.body));
} else if (decl.body && decl.body.body) {
decl.body.body.forEach(function (moduleBlockNode) {
// Export-assignment exports all members in the namespace,
// explicitly exported or not.
var namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration' ?
moduleBlockNode.declaration :
moduleBlockNode;
if (!namespaceDecl) {
// TypeScript can check this for us; we needn't
} else if (namespaceDecl.type === 'VariableDeclaration') {
namespaceDecl.declarations.forEach(function (d) {return (0, _patternCapture2['default'])(d.id, function (id) {return _this4.exportMap.namespace.set(
id.name,
(0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, decl, namespaceDecl, moduleBlockNode));});});
} else {
_this4.exportMap.namespace.set(
namespaceDecl.id.name,
(0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, moduleBlockNode));
}
});
}
} else {
// Export as default
_this4.exportMap.namespace.set('default', (0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, decl));
}
});
}return typeScriptExport;}() }]);return ImportExportVisitorBuilder;}();exports['default'] = ImportExportVisitorBuilder;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9leHBvcnRNYXAvdmlzaXRvci5qcyJdLCJuYW1lcyI6WyJtYWtlU291cmNlQ29kZSIsInRleHQiLCJhc3QiLCJTb3VyY2VDb2RlIiwibGVuZ3RoIiwiSW1wb3J0RXhwb3J0VmlzaXRvckJ1aWxkZXIiLCJwYXRoIiwiY29udGV4dCIsImV4cG9ydE1hcCIsIkV4cG9ydE1hcEJ1aWxkZXIiLCJjb250ZW50IiwiaXNFc01vZHVsZUludGVyb3BUcnVlIiwidGh1bmtGb3IiLCJuYW1lc3BhY2UiLCJOYW1lc3BhY2UiLCJyZW1vdGVQYXRoUmVzb2x2ZXIiLCJSZW1vdGVQYXRoIiwic291cmNlIiwiZG9jc3R5bGUiLCJzZXR0aW5ncyIsImRv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