-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: js-scope refactor - various renames and structure changes * fix: make jsxScope static property read-only * fix: minor formatting changes
- Loading branch information
1 parent
6c2d122
commit dc4da39
Showing
72 changed files
with
869 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright IBM Corp. 2024, 2024 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import getPropertyByPath from 'lodash/get.js' | ||
import { ObjectPath } from 'object-scan' | ||
import path from 'path' | ||
|
||
import { getTrackedSourceFiles } from '../../core/get-tracked-source-files.js' | ||
import { Logger } from '../../core/log/logger.js' | ||
import { NoInstallationFoundError } from '../../exceptions/no-installation-found-error.js' | ||
import { getDependencyTree } from '../npm/get-dependency-tree.js' | ||
import { getDirectoryPrefix } from '../npm/get-directory-prefix.js' | ||
import { getInstalledVersionPaths } from '../npm/get-installed-version-paths.js' | ||
import { getPackageData } from '../npm/get-package-data.js' | ||
import { getPackageTrees } from '../npm/get-package-trees.js' | ||
import { getTreePredecessor } from '../npm/get-tree-predecessor.js' | ||
import { DependencyTree, PackageData } from '../npm/interfaces.js' | ||
|
||
/** | ||
* Finds tracked source files and then filters them based on ones that appear in a project which | ||
* depends on the in-context instrumented package/version. | ||
* | ||
* @param instrumentedPackage - Data about the instrumented package to use during filtering. | ||
* @param cwd - Current working directory. This must be inside of the root directory. This is an | ||
* absolute path. | ||
* @param root - Root-most directory. This is an absolute path. | ||
* @param fileExtensions - List of file extensions to capture metrics for. | ||
* @param logger - Logger instance. | ||
* @returns A (possibly empty) array of source files. | ||
*/ | ||
export async function findRelevantSourceFiles( | ||
instrumentedPackage: PackageData, | ||
cwd: string, | ||
root: string, | ||
fileExtensions: string[], | ||
logger: Logger | ||
) { | ||
logger.traceEnter('', 'findRelevantSourceFiles', [instrumentedPackage, cwd, root, fileExtensions]) | ||
const sourceFiles = await getTrackedSourceFiles(root, logger, fileExtensions) | ||
|
||
const dependencyTree = await getDependencyTree(cwd, root, logger) | ||
|
||
const filterPromises = sourceFiles.map(async (f) => { | ||
const prefix = await getDirectoryPrefix(path.dirname(f.fileName), logger) | ||
const prefixPackageData = await getPackageData(prefix, root, logger) | ||
|
||
let packageTrees = getPackageTrees(dependencyTree, prefixPackageData) | ||
|
||
let instrumentedInstallVersions: string[] | undefined = undefined | ||
let shortestPathLength: number | undefined = undefined | ||
do { | ||
for (const tree of packageTrees) { | ||
const instrumentedInstallPaths = getInstalledVersionPaths(tree, instrumentedPackage.name) | ||
if (instrumentedInstallPaths.length > 0) { | ||
const pathsLength = instrumentedInstallPaths[0]?.length ?? 0 | ||
if (shortestPathLength === undefined || pathsLength < shortestPathLength) { | ||
instrumentedInstallVersions = instrumentedInstallPaths.map( | ||
(path) => getPropertyByPath(tree, path)['version'] | ||
) | ||
shortestPathLength = pathsLength | ||
} | ||
} | ||
} | ||
// did not find, go up one level for all packages | ||
packageTrees = packageTrees | ||
.map((tree) => getTreePredecessor(dependencyTree, tree['path'] as ObjectPath)) | ||
.filter((tree) => tree !== undefined) as DependencyTree[] | ||
} while (shortestPathLength === undefined && packageTrees.length > 0) | ||
|
||
if (instrumentedInstallVersions === undefined) { | ||
throw new NoInstallationFoundError(instrumentedPackage.name) | ||
} | ||
|
||
return instrumentedInstallVersions.some((version) => version === instrumentedPackage.version) | ||
}) | ||
const filterData = await Promise.all(filterPromises) | ||
|
||
const results = sourceFiles.filter((_, index) => { | ||
return filterData[index] | ||
}) | ||
|
||
logger.traceEnter('', 'findRelevantSourceFiles', results) | ||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright IBM Corp. 2024, 2024 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import type * as ts from 'typescript' | ||
|
||
import { Logger } from '../../core/log/logger.js' | ||
import { ComplexValue } from './complex-value.js' | ||
import { JsNodeHandler } from './node-handlers/js-node-handler.js' | ||
import { NodeValueHandler } from './node-handlers/value-handlers/node-value-handler.js' | ||
|
||
export interface JsImport { | ||
name: string | ||
path: string | ||
isDefault: boolean | ||
isAll: boolean | ||
rename?: string | ||
} | ||
|
||
type JsNodeHandlerClass<DataType> = new ( | ||
node: ts.SourceFile, | ||
logger: Logger | ||
) => JsNodeHandler<DataType> | ||
|
||
export type JsNodeHandlerMap = Partial<Record<ts.SyntaxKind, JsNodeHandlerClass<unknown>>> | ||
|
||
export type NodeValue = string | number | boolean | ComplexValue | null | undefined | ||
|
||
type NodeValueHandlerProducer = new (node: ts.SourceFile, logger: Logger) => NodeValueHandler | ||
|
||
export type NodeValueHandlerMap = Partial<Record<ts.SyntaxKind, NodeValueHandlerProducer>> | ||
|
||
export interface JsImportMatcher<Element> { | ||
findMatch: (element: Element, imports: JsImport[]) => JsImport | undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2024 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { JsImport } from '../js/interfaces.js' | ||
|
||
/** | ||
* Base class for all JS Accumulators. | ||
* Responsible for maintaining an aggregated state of imports and other elements. | ||
*/ | ||
export abstract class JsAccumulator { | ||
public readonly imports: JsImport[] | ||
|
||
constructor() { | ||
this.imports = [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.