recursivePatternCapture=recursivePatternCapture;var_fs=require('fs');var_fs2=_interopRequireDefault(_fs);var_doctrine=require('doctrine');var_doctrine2=_interopRequireDefault(_doctrine);var_debug=require('debug');var_debug2=_interopRequireDefault(_debug);var_eslint=require('eslint');var_parse=require('eslint-module-utils/parse');var_parse2=_interopRequireDefault(_parse);var_resolve=require('eslint-module-utils/resolve');var_resolve2=_interopRequireDefault(_resolve);var_ignore=require('eslint-module-utils/ignore');var_ignore2=_interopRequireDefault(_ignore);var_hash=require('eslint-module-utils/hash');var_unambiguous=require('eslint-module-utils/unambiguous');varunambiguous=_interopRequireWildcard(_unambiguous);var_tsconfigLoader=require('tsconfig-paths/lib/tsconfig-loader');var_arrayIncludes=require('array-includes');var_arrayIncludes2=_interopRequireDefault(_arrayIncludes);function_interopRequireWildcard(obj){if(obj&&obj.__esModule){returnobj;}else{varnewObj={};if(obj!=null){for(varkeyinobj){if(Object.prototype.hasOwnProperty.call(obj,key))newObj[key]=obj[key];}}newObj.default=obj;returnnewObj;}}function_interopRequireDefault(obj){returnobj&&obj.__esModule?obj:{default:obj};}letparseConfigFileTextToJson;constlog=(0,_debug2.default)('eslint-plugin-import:ExportMap');constexportCache=newMap();classExportMap{constructor(path){this.path=path;this.namespace=newMap();// todo: restructure to key on path, value is resolver + map of names
if(imported==null)return{found:true,path:[this]// safeguard against cycles, only if name matches
};if(imported.path===this.path&&reexports.local===name){return{found:false,path:[this]};}constdeep=imported.hasDeep(reexports.local);deep.path.unshift(this);returndeep;}// default exports must be explicitly re-exported (#328)
if(name!=='default'){for(letdepofthis.dependencies){letinnerMap=dep();if(innerMap==null)return{found:true,path:[this]// todo: report as unresolved?
};if(!innerMap)continue;// safeguard against cycles
if(innerMap.path===this.path)continue;letinnerValue=innerMap.hasDeep(name);if(innerValue.found){innerValue.path.unshift(this);returninnerValue;}}}return{found:false,path:[this]};}get(name){if(this.namespace.has(name))returnthis.namespace.get(name);if(this.reexports.has(name)){constreexports=this.reexports.get(name),imported=reexports.getImport();// if import is ignored, return explicit 'null'
if(imported==null)returnnull;// safeguard against cycles, only if name matches
if(imported.path===this.path&&reexports.local===name)returnundefined;returnimported.get(reexports.local);}// default exports must be explicitly re-exported (#328)
if(name!=='default'){for(letdepofthis.dependencies){letinnerMap=dep();// todo: report as unresolved?
if(!innerMap)continue;// safeguard against cycles
if(innerMap.path===this.path)continue;letinnerValue=innerMap.get(name);if(innerValue!==undefined)returninnerValue;}}returnundefined;}forEach(callback,thisArg){this.namespace.forEach((v,n)=>callback.call(thisArg,v,n,this));this.reexports.forEach((reexports,name)=>{constreexported=reexports.getImport();// can't look up meta for ignored re-exports (#348)
if(comment.type!=='Block')return;try{doc=_doctrine2.default.parse(comment.value,{unwrap:true});}catch(err){/* don't care, for now? maybe add to `errors?` */}});returndoc;}/**
if(!(0,_ignore.hasValidExtension)(path,context)){exportCache.set(cacheKey,null);returnnull;}// check for and cache ignore
if((0,_ignore2.default)(path,context)){log('ignored path due to ignore settings:',path);exportCache.set(cacheKey,null);returnnull;}constcontent=_fs2.default.readFileSync(path,{encoding:'utf8'});// check for and cache unambiguous modules
if(!unambiguous.test(content)){log('ignored path due to unambiguous regex:',path);exportCache.set(cacheKey,null);returnnull;}log('cache miss',cacheKey,'for path',path);exportMap=ExportMap.parse(path,content,context);// ambiguous modules return null
}if(!unambiguous.isModule(ast))returnnull;constdocstyle=context.settings&&context.settings['import/docstyle']||['jsdoc'];constdocStyleParsers={};docstyle.forEach(style=>{docStyleParsers[style]=availableDocStyleParsers[style];});// attempt to collect module doc
if(ast.comments){ast.comments.some(c=>{if(c.type!=='Block')returnfalse;try{constdoc=_doctrine2.default.parse(c.value,{unwrap:true});if(doc.tags.some(t=>t.title==='module')){m.doc=doc;returntrue;}}catch(err){/* ignore */}returnfalse;});}constnamespaces=newMap();functionremotePath(value){return_resolve2.default.relative(value,path,context.settings);}functionresolveImport(value){constrp=remotePath(value);if(rp==null)returnnull;returnExportMap.for(childContext(rp,context));}functiongetNamespace(identifier){if(!namespaces.has(identifier.name))return;returnfunction(){returnresolveImport(namespaces.get(identifier.name));};}functionaddNamespace(object,identifier){constnsfn=getNamespace(identifier);if(nsfn){Object.defineProperty(object,'namespace',{get:nsfn});}returnobject;}functioncaptureDependency(declaration){if(declaration.source==null)returnnull;if(declaration.importKind==='type')returnnull;// skip Flow type imports
constimportedSpecifiers=newSet();constsupportedTypes=newSet(['ImportDefaultSpecifier','ImportNamespaceSpecifier']);lethasImportedType=false;if(declaration.specifiers){declaration.specifiers.forEach(specifier=>{constisType=specifier.importKind==='type';hasImportedType=hasImportedType||isType;if(supportedTypes.has(specifier.type)&&!isType){importedSpecifiers.add(specifier.type);}if(specifier.type==='ImportSpecifier'&&!isType){importedSpecifiers.add(specifier.imported.name);}});}// only Flow types were imported
if(hasImportedType&&importedSpecifiers.size===0)returnnull;constp=remotePath(declaration.source.value);if(p==null)returnnull;constexisting=m.imports.get(p);if(existing!=null)returnexisting.getter;constgetter=thunkFor(p,context);m.imports.set(p,{getter,source:{// capturing actual node reference holds full AST in memory!
value:declaration.source.value,loc:declaration.source.loc},importedSpecifiers});returngetter;}constsource=makeSourceCode(content,ast);functionisEsModuleInterop(){consttsConfigInfo=(0,_tsconfigLoader.tsConfigLoader)({cwd:context.parserOptions&&context.parserOptions.tsconfigRootDir||process.cwd(),getEnv:key=>process.env[key]});try{if(tsConfigInfo.tsConfigPath!==undefined){constjsonText=_fs2.default.readFileSync(tsConfigInfo.tsConfigPath).toString();if(!parseConfigFileTextToJson){var_require=require('typescript');// this is because projects not using TypeScript won't have typescript installed
parseConfigFileTextToJson=_require.parseConfigFileTextToJson;}consttsConfig=parseConfigFileTextToJson(tsConfigInfo.tsConfigPath,jsonText).config;returntsConfig.compilerOptions.esModuleInterop;}}catch(e){returnfalse;}}ast.body.forEach(function(n){if(n.type==='ExportDefaultDeclaration'){constexportMeta=captureDoc(source,docStyleParsers,n);if(n.declaration.type==='Identifier'){addNamespace(exportMeta,n.declaration);}m.namespace.set('default',exportMeta);return;}if(n.type==='ExportAllDeclaration'){constgetter=captureDependency(n);if(getter)m.dependencies.add(getter);return;}// capture namespaces in case of later export
if(n.declaration!=null){switch(n.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':m.namespace.set(n.declaration.id.name,captureDoc(source,docStyleParsers,n));break;case'VariableDeclaration':n.declaration.declarations.forEach(d=>recursivePatternCapture(d.id,id=>m.namespace.set(id.name,captureDoc(source,docStyleParsers,d,n))));break;}}constnsource=n.source&&n.source.value;n.specifiers.forEach(s=>{constexportMeta={};letlocal;switch(s.type){case'ExportDefaultSpecifier':if(!n.source)return;local='default';break;case'ExportNamespaceSpecifier':m.namespace.set(s.exported.name,Object.defineProperty(exportMeta,'namespace',{get(){returnresolveImport(nsource);}}));return;case'ExportSpecifier':if(!n.source){m.namespace.set(s.exported.name,addNamespace(exportMeta,s.local));return;}// else falls through
default:local=s.local.name;break;}// todo: JSDoc
m.reexports.set(s.exported.name,{local,getImport:()=>resolveImport(nsource)});});}constisEsModuleInteropTrue=isEsModuleInterop();constexports=['TSExportAssignment'];if(isEsModuleInteropTrue){exports.push('TSNamespaceExportDeclaration');}// This doesn't declare anything, but changes what's being exported.
if((0,_arrayIncludes2.default)(exports,n.type)){constexportedName=n.type==='TSNamespaceExportDeclaration'?n.id.name:n.expression&&n.expression.name||n.expression.id&&n.expression.id.name||null;constdeclTypes=['VariableDeclaration','ClassDeclaration','TSDeclareFunction','TSEnumDeclaration','TSTypeAliasDeclaration','TSInterfaceDeclaration','TSAbstractClassDeclaration','TSModuleDeclaration'];constexportedDecls=ast.body.filter((_ref)=>{lettype=_ref.type,id=_ref.id,declarations=_ref.declarations;return(0,_arrayIncludes2.default)(declTypes,type)&&(id&&id.name===exportedName||declarations&&declarations.find(d=>d.id.name===exportedName));});if(exportedDecls.length===0){// Export is not referencing any local declaration, must be re-exporting
m.namespace.set('default',captureDoc(source,docStyleParsers,n));return;}if(isEsModuleInteropTrue){m.namespace.set('default',{});}exportedDecls.forEach(decl=>{if(decl.type==='TSModuleDeclaration'){if(decl.body&&decl.body.type==='TSModuleDeclaration'){m.namespace.set(decl.body.id.name,captureDoc(source,docStyleParsers,decl.body));}elseif(decl.body&&decl.body.body){decl.body.body.forEach(moduleBlockNode=>{// Export-assignment exports all members in the namespace,
// explicitly exported or not.
constnamespaceDecl=moduleBlockNode.type==='ExportNamedDeclaration'?moduleBlockNode.declaration:moduleBlockNode;if(!namespaceDecl){// TypeScript can check this for us; we needn't
}elseif(namespaceDecl.type==='VariableDeclaration'){namespaceDecl.declarations.forEach(d=>recursivePatternCapture(d.id,id=>m.namespace.set(id.name,captureDoc(source,docStyleParsers,decl,namespaceDecl,moduleBlockNode))));}else{m.namespace.set(namespaceDecl.id.name,captureDoc(source,docStyleParsers,moduleBlockNode));}});}}else{// Export as default