feat(providence-analytics): supply prev results match-imports

This commit is contained in:
Thijs Louisse 2020-07-21 13:12:21 +02:00
parent 8396888699
commit e31d1f5150
3 changed files with 66 additions and 27 deletions

View file

@ -186,7 +186,6 @@ class Analyzer {
}
LogService.info(`starting ${LogService.pad(this.name, 16)} for ${this.identifier}`);
/**
* Get reference and search-target data
*/

View file

@ -201,11 +201,15 @@ class MatchImportsAnalyzer extends Analyzer {
* @property {GatherFilesConfig} [gatherFilesConfig]
* @property {array} [referenceProjectPath] reference paths
* @property {array} [targetProjectPath] search target paths
* @property {FindExportsAnalyzerResult} [exportsAnalyzerResult]
* @property {FindImportsAnalyzerResult} [importsAnalyzerResult]
*/
const cfg = {
gatherFilesConfig: {},
referenceProjectPath: null,
targetProjectPath: null,
exportsAnalyzerResult: null,
importsAnalyzerResult: null,
...customConfig,
};
@ -220,16 +224,23 @@ class MatchImportsAnalyzer extends Analyzer {
/**
* Traverse
*/
let { exportsAnalyzerResult } = cfg;
if (!exportsAnalyzerResult) {
const findExportsAnalyzer = new FindExportsAnalyzer();
const exportsAnalyzerResult = await findExportsAnalyzer.execute({
exportsAnalyzerResult = await findExportsAnalyzer.execute({
metaConfig: cfg.metaConfig,
targetProjectPath: cfg.referenceProjectPath,
});
}
let { importsAnalyzerResult } = cfg;
if (!importsAnalyzerResult) {
const findImportsAnalyzer = new FindImportsAnalyzer();
const importsAnalyzerResult = await findImportsAnalyzer.execute({
importsAnalyzerResult = await findImportsAnalyzer.execute({
metaConfig: cfg.metaConfig,
targetProjectPath: cfg.targetProjectPath,
});
}
const queryOutput = matchImportsPostprocess(exportsAnalyzerResult, importsAnalyzerResult, cfg);

View file

@ -2,6 +2,9 @@ const { expect } = require('chai');
const { providence } = require('../../../src/program/providence.js');
const { QueryService } = require('../../../src/program/services/QueryService.js');
const { InputDataService } = require('../../../src/program/services/InputDataService.js');
const FindExportsAnalyzer = require('../../../src/program/analyzers/find-exports.js');
const FindImportsAnalyzer = require('../../../src/program/analyzers/find-imports.js');
const {
mockTargetAndReferenceProject,
restoreMockedProjects,
@ -233,6 +236,22 @@ describe('Analyzer "match-imports"', () => {
restoreMockedProjects();
});
function testMatchedEntry(targetExportedId, queryResult, importedByFiles = []) {
const matchedEntry = queryResult.queryOutput.find(
r => r.exportSpecifier.id === targetExportedId,
);
const [name, filePath, project] = targetExportedId.split('::');
expect(matchedEntry.exportSpecifier).to.eql({
name,
filePath,
project,
id: targetExportedId,
});
expect(matchedEntry.matchesPerProject[0].project).to.equal('importing-target-project');
expect(matchedEntry.matchesPerProject[0].files).to.eql(importedByFiles);
}
describe('Extracting exports', () => {
it(`identifies all direct export specifiers consumed by "importing-target-project"`, async () => {
mockTargetAndReferenceProject(searchTargetProject, referenceProject);
@ -276,22 +295,6 @@ describe('Analyzer "match-imports"', () => {
describe('Matching', () => {
it(`produces a list of all matches, sorted by project`, async () => {
function testMatchedEntry(targetExportedId, queryResult, importedByFiles = []) {
const matchedEntry = queryResult.queryOutput.find(
r => r.exportSpecifier.id === targetExportedId,
);
const [name, filePath, project] = targetExportedId.split('::');
expect(matchedEntry.exportSpecifier).to.eql({
name,
filePath,
project,
id: targetExportedId,
});
expect(matchedEntry.matchesPerProject[0].project).to.equal('importing-target-project');
expect(matchedEntry.matchesPerProject[0].files).to.eql(importedByFiles);
}
mockTargetAndReferenceProject(searchTargetProject, referenceProject);
await providence(matchImportsQueryConfig, _providenceCfg);
const queryResult = queryResults[0];
@ -305,4 +308,30 @@ describe('Analyzer "match-imports"', () => {
});
});
});
describe('Configuration', () => {
it(`allows to provide results of FindExportsAnalyzer and FindImportsAnalyzer`, async () => {
mockTargetAndReferenceProject(searchTargetProject, referenceProject);
const importsAnalyzerResult = await new FindImportsAnalyzer().execute({
targetProjectPath: searchTargetProject.path,
});
const exportsAnalyzerResult = await new FindExportsAnalyzer().execute({
targetProjectPath: referenceProject.path,
});
await providence(matchImportsQueryConfig, {
..._providenceCfg,
importsAnalyzerResult,
exportsAnalyzerResult,
});
const queryResult = queryResults[0];
expectedExportIdsDirect.forEach(targetId => {
testMatchedEntry(targetId, queryResult, ['./target-src/direct-imports.js']);
});
expectedExportIdsIndirect.forEach(targetId => {
testMatchedEntry(targetId, queryResult, ['./target-src/indirect-imports.js']);
});
});
});
});