fix(providence): make sure all cached asts are retrieved

This commit is contained in:
Thijs Louisse 2024-12-05 16:55:26 +01:00 committed by Thijs Louisse
parent 73ff11990b
commit 344ffa9046
5 changed files with 48 additions and 26 deletions

View file

@ -0,0 +1,5 @@
---
'providence-analytics': patch
---
make sure all cached asts are retrieved

View file

@ -37,11 +37,10 @@
"test:node:unit": "mocha './{test-node,src}/**/*.test.js'"
},
"dependencies": {
"@babel/traverse": "^7.25.7",
"@rollup/plugin-node-resolve": "^15.3.0",
"commander": "^2.20.3",
"oxc-parser": "^0.34.0",
"parse5": "^7.2.0",
"oxc-parser": "^0.39.0",
"parse5": "^7.2.1",
"semver": "^7.6.3"
},
"peerDependencies": {
@ -50,16 +49,17 @@
"@swc/core": "^1.7.36"
},
"devDependencies": {
"@babel/parser": "^7.25.8",
"@babel/plugin-syntax-import-assertions": "^7.25.7",
"@babel/parser": "^7.26.3",
"@babel/plugin-syntax-import-assertions": "^7.26.0",
"@babel/traverse": "^7.26.3",
"@swc/core": "^1.7.36",
"@types/inquirer": "^9.0.7",
"@types/mocha": "^10.0.9",
"@types/mocha": "^10.0.10",
"@web/dev-server": "^0.4.6",
"@web/dev-server-core": "^0.7.3",
"@web/dev-server-core": "^0.7.4",
"globby": "^14.0.2",
"lit-element": "^4.1.1",
"mock-fs": "^5.4.0"
"mock-fs": "^5.4.1"
},
"keywords": [
"analysis",

View file

@ -1,7 +1,6 @@
/* eslint-disable no-param-reassign */
import path from 'path';
import semver from 'semver';
import path from 'path';
import { getFilePathRelativeFromRoot } from '../utils/get-file-path-relative-from-root.js';
import { InputDataService } from './InputDataService.js';

View file

@ -1,30 +1,30 @@
import path from 'path';
import { getCurrentDir } from '../utils/get-current-dir.js';
import { AstService } from './AstService.js';
import { LogService } from './LogService.js';
import { getCurrentDir } from '../utils/get-current-dir.js';
// import { memoize } from '../utils/memoize.js';
const memoize = fn => fn;
/**
* @typedef {import('./Analyzer.js').Analyzer} Analyzer
* @typedef {import('../../../types/index.js').PathRelativeFromProjectRoot} PathRelativeFromProjectRoot
* @typedef {import('../../../types/index.js').FindImportsAnalyzerResult} FindImportsAnalyzerResult
* @typedef {import('../../../types/index.js').FindImportsAnalyzerEntry} FindImportsAnalyzerEntry
* @typedef {import('../../../types/index.js').PathRelativeFromProjectRoot} PathRelativeFromProjectRoot
* @typedef {import('../../../types/index.js').QueryConfig} QueryConfig
* @typedef {import('../../../types/index.js').QueryResult} QueryResult
* @typedef {import('../../../types/index.js').FeatureQueryConfig} FeatureQueryConfig
* @typedef {import('../../../types/index.js').SearchQueryConfig} SearchQueryConfig
* @typedef {import('../../../types/index.js').AnalyzerQueryConfig} AnalyzerQueryConfig
* @typedef {import('../../../types/index.js').Feature} Feature
* @typedef {import('../../../types/index.js').AnalyzerQueryResult} AnalyzerQueryResult
* @typedef {import('../../../types/index.js').PathFromSystemRoot} PathFromSystemRoot
* @typedef {import('../../../types/index.js').FeatureQueryConfig} FeatureQueryConfig
* @typedef {import('../../../types/index.js').GatherFilesConfig} GatherFilesConfig
* @typedef {import('../../../types/index.js').SearchQueryConfig} SearchQueryConfig
* @typedef {import('../../../types/index.js').ProjectInputData} ProjectInputData
* @typedef {import('../../../types/index.js').AnalyzerConfig} AnalyzerConfig
* @typedef {import('../../../types/index.js').AnalyzerName} AnalyzerName
* @typedef {import('../../../types/index.js').AnalyzerAst} AnalyzerAst
* @typedef {import('../../../types/index.js').PathFromSystemRoot} PathFromSystemRoot
* @typedef {import('../../../types/index.js').GatherFilesConfig} GatherFilesConfig
* @typedef {import('../../../types/index.js').AnalyzerQueryResult} AnalyzerQueryResult
* @typedef {import('../../../types/index.js').QueryConfig} QueryConfig
* @typedef {import('../../../types/index.js').QueryResult} QueryResult
* @typedef {import('../../../types/index.js').Feature} Feature
* @typedef {import('./Analyzer.js').Analyzer} Analyzer
*/
const astProjectsDataCache = new Map();
@ -32,7 +32,7 @@ const astProjectsDataCache = new Map();
export class QueryService {
/**
* Retrieves the default export found in ./program/analyzers/find-import.js
* @param {typeof Analyzer} analyzerCtor
* @param {typeof Analyzer} analyzerObjectOrString
* @param {AnalyzerConfig} [analyzerConfig]
* @returns {Promise<AnalyzerQueryConfig>}
*/
@ -112,7 +112,8 @@ export class QueryService {
for (const projectData of projectsData) {
const cachedData = astProjectsDataCache.get(projectData.project.path);
if (cachedData) {
return cachedData;
resultWithAsts.push(cachedData);
continue; // eslint-disable-line no-continue
}
const resultEntries = [];

View file

@ -72,7 +72,17 @@ function getPackageRootFromNodeModulesPath(resolvedPath, dynamicImport) {
return resolvedPath.slice(0, lio + tailOfRootPath.length);
}
function incorporateDynamicImports(
/**
* @example
* ```js
* const importablePaths = resolveDynamicImportsForMockFs();
* mockFs({...Object.fromEntries(importablePaths.map(p => [p, mockFs.load(p)])), ...rest });
* ```
*
* @param {{name:string;siblings?:string[]}[]} dynamicImports
* @returns {string[]}
*/
export function resolveDynamicImportsForMockFs(
dynamicImports = [
{
name: 'oxc-parser',
@ -88,7 +98,14 @@ function incorporateDynamicImports(
const require = module.createRequire(import.meta.url);
const importablePaths = [];
for (const dynamicImport of dynamicImports) {
const resolvedPath = require.resolve(dynamicImport.name);
/** @type {string} */
let resolvedPath;
try {
resolvedPath = require.resolve(dynamicImport.name);
} catch {
console.warn(`[resolveDynamicImportsForMockFs] Did not find ${dynamicImport.name}`);
continue; // eslint-disable-line no-continue
}
const rootPath = getPackageRootFromNodeModulesPath(resolvedPath, dynamicImport.name);
importablePaths.push(rootPath);
for (const sibling of dynamicImport.siblings || []) {
@ -98,7 +115,7 @@ function incorporateDynamicImports(
}
return importablePaths;
}
const importablePaths = incorporateDynamicImports();
const importablePaths = resolveDynamicImportsForMockFs();
/**
* Makes sure that, whenever the main program (providence) calls