function getModuleMetadata(programPath) {
    const localData = getLocalExportMetadata(programPath);

    const sourceData = new Map();
    const getData = sourceNode => {
        const source = sourceNode.value;

        let data = sourceData.get(source);
        if (!data) {
            data = {
                name: programPath.scope.generateUidIdentifier(
                    basename(source, extname(source))
                ).name,

                interop: 'none',

                loc: null,

                // Data about the requested sources and names.
                imports: new Map(),
                // importsNamespace: import * as util from './a/b/util';
                importsNamespace: new Set(),

                // Metadata about data that is passed directly from source to export.
                reexports: new Map(),
                reexportNamespace: new Set(),
                reexportAll: null,
            };
            sourceData.set(source, data);
        }
        return data;
    };

    programPath.get('body').forEach(child => {
        if (child.isImportDeclaration()) {
            const data = getData(child.node.source);
            if (!data.loc) {
                data.loc = child.node.loc;
            }

            child.get('specifiers').forEach(spec => {
                if (spec.isImportDefaultSpecifier()) {
                    const localName = spec.get('local').node.name;

                    data.imports.set(localName, 'default');

                    const reexport = localData.get(localName);
                    if (reexport) {
                        localData.delete(localName);

                        reexport.names.forEach(name => {
                            data.reexports.set(name, 'default');
                        });
                    }
                }
                else if (spec.isImportNamespaceSpecifier()) {
                    const localName = spec.get('local').node.name;

                    assert(
                        data.importsNamespace.size === 0,
                        `Duplicate import namespace: ${localName}`
                    );
                    data.importsNamespace.add(localName);

                    const reexport = localData.get(localName);
                    if (reexport) {
                        localData.delete(localName);

                        reexport.names.forEach(name => {
                            data.reexportNamespace.add(name);
                        });
                    }
                }
                else if (spec.isImportSpecifier()) {
                    const importName = spec.get('imported').node.name;
                    const localName = spec.get('local').node.name;

                    data.imports.set(localName, importName);

                    const reexport = localData.get(localName);
                    if (reexport) {
                        localData.delete(localName);

                        reexport.names.forEach(name => {
                            data.reexports.set(name, importName);
                        });
                    }
                }
            });
        }
        else if (child.isExportAllDeclaration()) {
            const data = getData(child.node.source);
            if (!data.loc) {
                data.loc = child.node.loc;
            }

            data.reexportAll = {
                loc: child.node.loc,
            };
        }
        else if (child.isExportNamedDeclaration() && child.node.source) {
            const data = getData(child.node.source);
            if (!data.loc) {
                data.loc = child.node.loc;
            }

            child.get('specifiers').forEach(spec => {
                if (!spec.isExportSpecifier()) {
                    throw spec.buildCodeFrameError('Unexpected export specifier type');
                }
                const importName = spec.get('local').node.name;
                const exportName = spec.get('exported').node.name;

                data.reexports.set(exportName, importName);

                if (exportName === '__esModule') {
                    throw exportName.buildCodeFrameError('Illegal export "__esModule".');
                }
            });
        }
    });

    for (const metadata of sourceData.values()) {
        if (metadata.importsNamespace.size > 0) {
            metadata.interop = 'namespace';
            continue;
        }
        let needsDefault = false;
        let needsNamed = false;
        for (const importName of metadata.imports.values()) {
            if (importName === 'default') {
                needsDefault = true;
            }
            else {
                needsNamed = true;
            }
        }
        for (const importName of metadata.reexports.values()) {
            if (importName === 'default') {
                needsDefault = true;
            }
            else {
                needsNamed = true;
            }
        }

        if (needsDefault && needsNamed) {
            // TODO(logan): Using the namespace interop here is unfortunate. Revisit.
            metadata.interop = 'namespace';
        }
        else if (needsDefault) {
            metadata.interop = 'default';
        }
    }

    return {
        local: localData,
        source: sourceData,
    };
}
function getModuleMetadata(programPath) {
    const localData = getLocalExportMetadata(programPath);

    const sourceData = new Map();
    const getData = sourceNode => {
        const source = sourceNode.value;

        let data = sourceData.get(source);
        if (!data) {
            data = {
                name: programPath.scope.generateUidIdentifier(
                    basename(source, extname(source))
                ).name,

                interop: 'none',

                loc: null,

                // Data about the requested sources and names.
                imports: new Map(),
                // importsNamespace: import * as util from './a/b/util';
                importsNamespace: new Set(),

                // Metadata about data that is passed directly from source to export.
                reexports: new Map(),
                reexportNamespace: new Set(),
                reexportAll: null,
            };
            sourceData.set(source, data);
        }
        return data;
    };

    programPath.get('body').forEach(child => {
        if (child.isImportDeclaration()) {
            const data = getData(child.node.source);
            if (!data.loc) {
                data.loc = child.node.loc;
            }

            child.get('specifiers').forEach(spec => {
                if (spec.isImportDefaultSpecifier()) {
                    const localName = spec.get('local').node.name;

                    data.imports.set(localName, 'default');

                    const reexport = localData.get(localName);
                    if (reexport) {
                        localData.delete(localName);

                        reexport.names.forEach(name => {
                            data.reexports.set(name, 'default');
                        });
                    }
                }
                else if (spec.isImportNamespaceSpecifier()) {
                    const localName = spec.get('local').node.name;

                    assert(
                        data.importsNamespace.size === 0,
                        `Duplicate import namespace: ${localName}`
                    );
                    data.importsNamespace.add(localName);

                    const reexport = localData.get(localName);
                    if (reexport) {
                        localData.delete(localName);

                        reexport.names.forEach(name => {
                            data.reexportNamespace.add(name);
                        });
                    }
                }
                else if (spec.isImportSpecifier()) {
                    const importName = spec.get('imported').node.name;
                    const localName = spec.get('local').node.name;

                    data.imports.set(localName, importName);

                    const reexport = localData.get(localName);
                    if (reexport) {
                        localData.delete(localName);

                        reexport.names.forEach(name => {
                            data.reexports.set(name, importName);
                        });
                    }
                }
            });
        }
        else if (child.isExportAllDeclaration()) {
            const data = getData(child.node.source);
            if (!data.loc) {
                data.loc = child.node.loc;
            }

            data.reexportAll = {
                loc: child.node.loc,
            };
        }
        else if (child.isExportNamedDeclaration() && child.node.source) {
            const data = getData(child.node.source);
            if (!data.loc) {
                data.loc = child.node.loc;
            }

            child.get('specifiers').forEach(spec => {
                if (!spec.isExportSpecifier()) {
                    throw spec.buildCodeFrameError('Unexpected export specifier type');
                }
                const importName = spec.get('local').node.name;
                const exportName = spec.get('exported').node.name;

                data.reexports.set(exportName, importName);

                if (exportName === '__esModule') {
                    throw exportName.buildCodeFrameError('Illegal export "__esModule".');
                }
            });
        }
    });

    for (const metadata of sourceData.values()) {
        if (metadata.importsNamespace.size > 0) {
            metadata.interop = 'namespace';
            continue;
        }
        let needsDefault = false;
        let needsNamed = false;
        for (const importName of metadata.imports.values()) {
            if (importName === 'default') {
                needsDefault = true;
            }
            else {
                needsNamed = true;
            }
        }
        for (const importName of metadata.reexports.values()) {
            if (importName === 'default') {
                needsDefault = true;
            }
            else {
                needsNamed = true;
            }
        }

        if (needsDefault && needsNamed) {
            // TODO(logan): Using the namespace interop here is unfortunate. Revisit.
            metadata.interop = 'namespace';
        }
        else if (needsDefault) {
            metadata.interop = 'default';
        }
    }

    return {
        local: localData,
        source: sourceData,
    };
}
function getModuleMetadata ( programPath ) { const localData = getLocalExportMetadata ( programPath ) ; const sourceData = new Map ( ) ; const getData = sourceNode => { const source = sourceNode . value ; let data = sourceData . get ( source ) ; if ( ! data ) { data = { name : programPath . scope . generateUidIdentifier ( basename ( source , extname ( source ) ) ) . name , interop : 'none' , loc : null , // Data about the requested sources and names. imports : new Map ( ) , // importsNamespace: import * as util from './a/b/util'; importsNamespace : new Set ( ) , // Metadata about data that is passed directly from source to export. reexports : new Map ( ) , reexportNamespace : new Set ( ) , reexportAll : null , } ; sourceData . set ( source , data ) ; } return data ; } ; programPath . get ( 'body' ) . forEach ( child => { if ( child . isImportDeclaration ( ) ) { const data = getData ( child . node . source ) ; if ( ! data . loc ) { data . loc = child . node . loc ; } child . get ( 'specifiers' ) . forEach ( spec => { if ( spec . isImportDefaultSpecifier ( ) ) { const localName = spec . get ( 'local' ) . node . name ; data . imports . set ( localName , 'default' ) ; const reexport = localData . get ( localName ) ; if ( reexport ) { localData . delete ( localName ) ; reexport . names . forEach ( name => { data . reexports . set ( name , 'default' ) ; } ) ; } } else if ( spec . isImportNamespaceSpecifier ( ) ) { const localName = spec . get ( 'local' ) . node . name ; assert ( data . importsNamespace . size === 0 , ` ${ localName } ` ) ; data . importsNamespace . add ( localName ) ; const reexport = localData . get ( localName ) ; if ( reexport ) { localData . delete ( localName ) ; reexport . names . forEach ( name => { data . reexportNamespace . add ( name ) ; } ) ; } } else if ( spec . isImportSpecifier ( ) ) { const importName = spec . get ( 'imported' ) . node . name ; const localName = spec . get ( 'local' ) . node . name ; data . imports . set ( localName , importName ) ; const reexport = localData . get ( localName ) ; if ( reexport ) { localData . delete ( localName ) ; reexport . names . forEach ( name => { data . reexports . set ( name , importName ) ; } ) ; } } } ) ; } else if ( child . isExportAllDeclaration ( ) ) { const data = getData ( child . node . source ) ; if ( ! data . loc ) { data . loc = child . node . loc ; } data . reexportAll = { loc : child . node . loc , } ; } else if ( child . isExportNamedDeclaration ( ) && child . node . source ) { const data = getData ( child . node . source ) ; if ( ! data . loc ) { data . loc = child . node . loc ; } child . get ( 'specifiers' ) . forEach ( spec => { if ( ! spec . isExportSpecifier ( ) ) { throw spec . buildCodeFrameError ( 'Unexpected export specifier type' ) ; } const importName = spec . get ( 'local' ) . node . name ; const exportName = spec . get ( 'exported' ) . node . name ; data . reexports . set ( exportName , importName ) ; if ( exportName === '__esModule' ) { throw exportName . buildCodeFrameError ( 'Illegal export "__esModule".' ) ; } } ) ; } } ) ; for ( const metadata of sourceData . values ( ) ) { if ( metadata . importsNamespace . size > 0 ) { metadata . interop = 'namespace' ; continue ; } let needsDefault = false ; let needsNamed = false ; for ( const importName of metadata . imports . values ( ) ) { if ( importName === 'default' ) { needsDefault = true ; } else { needsNamed = true ; } } for ( const importName of metadata . reexports . values ( ) ) { if ( importName === 'default' ) { needsDefault = true ; } else { needsNamed = true ; } } if ( needsDefault && needsNamed ) { // TODO(logan): Using the namespace interop here is unfortunate. Revisit. metadata . interop = 'namespace' ; } else if ( needsDefault ) { metadata . interop = 'default' ; } } return { local : localData , source : sourceData , } ; }
Get metadata about the imports and exports present in this module.
Get metadata about the imports and exports present in this module .
Get module metadata