From 344ffa9046282e93197c47fbf6d417ad4cf4a5c1 Mon Sep 17 00:00:00 2001 From: Thijs Louisse Date: Thu, 5 Dec 2024 16:55:26 +0100 Subject: [PATCH] fix(providence): make sure all cached asts are retrieved --- .changeset/healthy-starfishes-impress.md | 5 ++++ .../providence-analytics/package.json | 16 +++++------ .../src/program/core/Analyzer.js | 3 +-- .../src/program/core/QueryService.js | 27 ++++++++++--------- .../test-helpers/mock-project-helpers.js | 23 +++++++++++++--- 5 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 .changeset/healthy-starfishes-impress.md diff --git a/.changeset/healthy-starfishes-impress.md b/.changeset/healthy-starfishes-impress.md new file mode 100644 index 000000000..003080c42 --- /dev/null +++ b/.changeset/healthy-starfishes-impress.md @@ -0,0 +1,5 @@ +--- +'providence-analytics': patch +--- + +make sure all cached asts are retrieved diff --git a/packages-node/providence-analytics/package.json b/packages-node/providence-analytics/package.json index 907a27d22..9a2482aac 100644 --- a/packages-node/providence-analytics/package.json +++ b/packages-node/providence-analytics/package.json @@ -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", diff --git a/packages-node/providence-analytics/src/program/core/Analyzer.js b/packages-node/providence-analytics/src/program/core/Analyzer.js index 38c43e0d1..181b37d76 100644 --- a/packages-node/providence-analytics/src/program/core/Analyzer.js +++ b/packages-node/providence-analytics/src/program/core/Analyzer.js @@ -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'; diff --git a/packages-node/providence-analytics/src/program/core/QueryService.js b/packages-node/providence-analytics/src/program/core/QueryService.js index b9f92adaa..008a1f741 100644 --- a/packages-node/providence-analytics/src/program/core/QueryService.js +++ b/packages-node/providence-analytics/src/program/core/QueryService.js @@ -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} */ @@ -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 = []; diff --git a/packages-node/providence-analytics/test-helpers/mock-project-helpers.js b/packages-node/providence-analytics/test-helpers/mock-project-helpers.js index 89316796f..001bdc706 100644 --- a/packages-node/providence-analytics/test-helpers/mock-project-helpers.js +++ b/packages-node/providence-analytics/test-helpers/mock-project-helpers.js @@ -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