clean up import resolving code

This commit is contained in:
Dave Holoway
2020-06-29 15:45:46 +01:00
parent bdb5986c4b
commit 61397dab95
3 changed files with 9 additions and 53 deletions

View File

@@ -1,5 +1,4 @@
const { ImportBlock } = require('./parser9');
const ResolvedImport = require('./parsetypes/resolved-import');
/**
@@ -22,14 +21,6 @@ function fetchImportedTypes(typenames, dotted_import, demandload) {
return matching_names;
}
/**
* @param {string} typenames newline-separated list of fully qualified type names
* @param {ImportBlock} import_decl import declaration
*/
function resolveImportTypes(typenames, import_decl) {
return fetchImportedTypes(typenames, import_decl.name, import_decl.isDemandLoad);
}
/**
* Resolve a single parsed import
*
@@ -79,30 +70,15 @@ function resolveSingleImport(typemap, dotted_name, is_static, on_demand, import_
* - followed by import declarations (in order of declaration),
* - followed by implicit packages
*
* @param {Map<string, import('java-mti').CEIType>} androidLibrary
* @param {import('./source-types').SourceType[]} sourceTypes
* @param {ImportBlock[]} imports list of declared imports in the module
* @param {Map<string, import('java-mti').CEIType>} typemap
* @param {string} package_name package name of the module
* @param {string[]} [implicitPackages] list of implicit demand-load packages
*/
function resolveImports(androidLibrary, sourceTypes, imports, package_name, implicitPackages = ['java.lang']) {
const typemap = new Map(androidLibrary);
sourceTypes.forEach(t => {
// todo - should we overwrite entries when source types match types in the library?
typemap.set(t.shortSignature, t);
})
function resolveImports(typemap, package_name, implicitPackages = ['java.lang']) {
// construct the list of typenames
const typenames = [...typemap.keys()].join('\n');
/**
* The list of explicit import declarations we are unable to resolve
* @type {ImportBlock[]}
*/
const unresolved = [];
/** @type {ResolvedImport[]} */
const resolved = [];
@@ -113,20 +89,6 @@ function resolveImports(androidLibrary, sourceTypes, imports, package_name, impl
resolved.push(new ResolvedImport(null, matches, null, typemap, 'owner-package'));
}
// import types from each import declaration
imports.forEach(import_decl => {
const matches = resolveImportTypes(typenames, import_decl);
if (matches) {
resolved.push(new ResolvedImport(import_decl, matches, null, typemap, 'import'));
} else {
// if we cannot match the import to any types, add it to the unresolved list so
// we can flag it as a warning later.
// Note that empty packages (packages with no types) will appear here - they
// are technically valid, but represent useless imports
unresolved.push(import_decl);
}
});
// import types from the implicit packages
implicitPackages.forEach(package_name => {
const matches = fetchImportedTypes(typenames, package_name, true);
@@ -135,15 +97,9 @@ function resolveImports(androidLibrary, sourceTypes, imports, package_name, impl
})
/**
* return the resolved and unresolved imports.
* The typemap is also included to support fully qualified type names that, by virtue of
* being fully-qualified, don't require importing.
* return the resolved imports.
*/
return {
resolved,
unresolved,
typemap,
}
return resolved;
}
module.exports = {