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

@@ -250,7 +250,7 @@ function parse(docs, cached_units, typemap) {
function parseUnit(tokens, unit, typemap) { function parseUnit(tokens, unit, typemap) {
let package_name = ''; let package_name = '';
// init resolved imports with java.lang.* // init resolved imports with java.lang.*
let resolved_imports = resolveImports(typemap, [], [], null).resolved.slice(); let resolved_imports = resolveImports(typemap, null).slice();
// retrieve the implicit imports // retrieve the implicit imports
while (tokens.current) { while (tokens.current) {
let modifiers = [], annotations = []; let modifiers = [], annotations = [];
@@ -284,9 +284,9 @@ function parseUnit(tokens, unit, typemap) {
if (!package_name) { if (!package_name) {
unit.package_ = pkg; unit.package_ = pkg;
package_name = pkg.name; package_name = pkg.name;
const imprt = resolveImports(typemap, [], [], pkg.name, []); const imprts = resolveImports(typemap, pkg.name, []);
if (imprt.resolved.length) { if (imprts.length) {
resolved_imports.unshift(...imprt.resolved); resolved_imports.unshift(...imprts);
} }
} }
continue; continue;

View File

@@ -1,5 +1,4 @@
const { ImportBlock } = require('./parser9');
const ResolvedImport = require('./parsetypes/resolved-import'); const ResolvedImport = require('./parsetypes/resolved-import');
/** /**
@@ -22,14 +21,6 @@ function fetchImportedTypes(typenames, dotted_import, demandload) {
return matching_names; 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 * 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 import declarations (in order of declaration),
* - followed by implicit packages * - followed by implicit packages
* *
* @param {Map<string, import('java-mti').CEIType>} androidLibrary * @param {Map<string, import('java-mti').CEIType>} typemap
* @param {import('./source-types').SourceType[]} sourceTypes
* @param {ImportBlock[]} imports list of declared imports in the module
* @param {string} package_name package name of the module * @param {string} package_name package name of the module
* @param {string[]} [implicitPackages] list of implicit demand-load packages * @param {string[]} [implicitPackages] list of implicit demand-load packages
*/ */
function resolveImports(androidLibrary, sourceTypes, imports, package_name, implicitPackages = ['java.lang']) { function resolveImports(typemap, 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);
})
// construct the list of typenames // construct the list of typenames
const typenames = [...typemap.keys()].join('\n'); const typenames = [...typemap.keys()].join('\n');
/**
* The list of explicit import declarations we are unable to resolve
* @type {ImportBlock[]}
*/
const unresolved = [];
/** @type {ResolvedImport[]} */ /** @type {ResolvedImport[]} */
const resolved = []; const resolved = [];
@@ -113,20 +89,6 @@ function resolveImports(androidLibrary, sourceTypes, imports, package_name, impl
resolved.push(new ResolvedImport(null, matches, null, typemap, 'owner-package')); 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 // import types from the implicit packages
implicitPackages.forEach(package_name => { implicitPackages.forEach(package_name => {
const matches = fetchImportedTypes(typenames, package_name, true); 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. * return the resolved imports.
* The typemap is also included to support fully qualified type names that, by virtue of
* being fully-qualified, don't require importing.
*/ */
return { return resolved;
resolved,
unresolved,
typemap,
}
} }
module.exports = { module.exports = {

View File

@@ -9,7 +9,7 @@ const { parseBody } = require('./body-parser');
*/ */
function parseMethodBodies(unit, typemap) { function parseMethodBodies(unit, typemap) {
const resolved_types = [ const resolved_types = [
...resolveImports(typemap, [], [], unit.packageName).resolved, ...resolveImports(typemap, unit.packageName),
...unit.imports.filter(i => i.resolved).map(i => i.resolved), ...unit.imports.filter(i => i.resolved).map(i => i.resolved),
] ]
unit.types.forEach(t => { unit.types.forEach(t => {