Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/ENGINE-TEMPLATE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"types": "dist/index.d.ts",
"dependencies": {
"@types/node": "^20.0.0",
"@salesforce/code-analyzer-engine-api": "0.27.0"
"@salesforce/code-analyzer-engine-api": "0.28.0-SNAPSHOT"
},
"devDependencies": {
"@eslint/js": "^9.32.0",
Expand Down Expand Up @@ -60,4 +60,4 @@
"!src/index.ts"
]
}
}
}
4 changes: 2 additions & 2 deletions packages/T-E-M-P-L-A-T-E/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"types": "dist/index.d.ts",
"dependencies": {
"@types/node": "^20.0.0",
"@salesforce/code-analyzer-engine-api": "0.27.0"
"@salesforce/code-analyzer-engine-api": "0.28.0-SNAPSHOT"
},
"devDependencies": {
"@eslint/js": "^9.32.0",
Expand Down Expand Up @@ -60,4 +60,4 @@
"!src/index.ts"
]
}
}
}
6 changes: 3 additions & 3 deletions packages/code-analyzer-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/code-analyzer-core",
"description": "Core Package for the Salesforce Code Analyzer",
"version": "0.33.0",
"version": "0.34.0-SNAPSHOT",
"author": "The Salesforce Code Analyzer Team",
"license": "BSD-3-Clause",
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",
Expand All @@ -13,7 +13,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"@salesforce/code-analyzer-engine-api": "0.27.0",
"@salesforce/code-analyzer-engine-api": "0.28.0-SNAPSHOT",
"@types/node": "^20.0.0",
"csv-stringify": "^6.6.0",
"js-yaml": "^4.1.0",
Expand Down Expand Up @@ -69,4 +69,4 @@
"!src/index.ts"
]
}
}
}
41 changes: 27 additions & 14 deletions packages/code-analyzer-core/src/code-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ import * as engApi from "@salesforce/code-analyzer-engine-api"
import {Clock, RealClock} from '@salesforce/code-analyzer-engine-api/utils';
import {EventEmitter} from "node:events";
import {CodeAnalyzerConfig, ConfigDescription, EngineOverrides, FIELDS, RuleOverride} from "./config";
import {EngineProgressAggregator, RuntimeUniqueIdGenerator, toAbsolutePath, UniqueIdGenerator} from "./utils";
import {
EngineProgressAggregator, RuntimeTempFolder,
RuntimeUniqueIdGenerator,
TempFolder,
toAbsolutePath,
UniqueIdGenerator
} from "./utils";
import fs from "node:fs";
import path from 'node:path';

Expand Down Expand Up @@ -94,6 +100,7 @@ const MINIMUM_SUPPORTED_NODE = 20;
export class CodeAnalyzer {
private readonly config: CodeAnalyzerConfig;
private clock: Clock = new RealClock();
private tempFolder: TempFolder = new RuntimeTempFolder();
private uniqueIdGenerator: UniqueIdGenerator = new RuntimeUniqueIdGenerator();
private readonly eventEmitter: EventEmitter = new EventEmitter();
private readonly engines: Map<string, engApi.Engine> = new Map();
Expand All @@ -118,12 +125,15 @@ export class CodeAnalyzer {
}

// For testing purposes only
_setClock(clock: Clock) {
_setClock(clock: Clock): void {
this.clock = clock;
}
_setUniqueIdGenerator(uniqueIdGenerator: UniqueIdGenerator) {
_setUniqueIdGenerator(uniqueIdGenerator: UniqueIdGenerator): void {
this.uniqueIdGenerator = uniqueIdGenerator;
}
_setTempFolder(tempFolder: TempFolder): void {
this.tempFolder = tempFolder;
}

/**
* Convenience method to return the same CodeAnalyzerConfig instance that was provided to the constructor
Expand Down Expand Up @@ -296,15 +306,19 @@ export class CodeAnalyzer {
// called a second time before the first call to run hasn't finished. This can occur if someone builds
// up a bunch of RunResults promises and then does a Promise.all on them. Otherwise, the progress events may
// override each other.
const runWorkingFolderName: string = `code-analyzer-run-${this.clock.formatToDateTimeString()}`;

const engineRunOptions: engApi.RunOptions = extractEngineRunOptions(runOptions, this.config.getLogFolder());
this.emitLogEvent(LogLevel.Debug, getMessage('RunningWithWorkspace', JSON.stringify({
filesAndFolders: runOptions.workspace.getRawFilesAndFolders(),
targets: runOptions.workspace.getRawTargets()
})));

const runPromises: Promise<EngineRunResults>[] = ruleSelection.getEngineNames().map(
engineName => this.runEngineAndValidateResults(engineName, ruleSelection, engineRunOptions));
async (engineName) => this.runEngineAndValidateResults(engineName, ruleSelection, {
logFolder: this.config.getLogFolder(),
workingFolder: await this.tempFolder.createSubfolder(runWorkingFolderName, engineName),
workspace: toEngApiWorkspace(runOptions.workspace)
}));
const engineRunResultsList: EngineRunResults[] = await Promise.all(runPromises);

const runResults: RunResultsImpl = new RunResultsImpl(this.clock);
Expand All @@ -330,11 +344,16 @@ export class CodeAnalyzer {

private async getAllRules(workspace?: Workspace): Promise<RuleImpl[]> {
const cacheKey: string = workspace ? workspace.getWorkspaceId() : process.cwd();
const describeWorkingFolderName: string = `code-analyzer-describe-${this.clock.formatToDateTimeString()}`;
if (!this.rulesCache.has(cacheKey)) {
this.engineRuleDiscoveryProgressAggregator.reset(this.getEngineNames());
const engApiWorkspace: engApi.Workspace | undefined = workspace ? toEngApiWorkspace(workspace) : undefined;
const rulePromises: Promise<RuleImpl[]>[] = this.getEngineNames().map(engineName =>
this.getAllRulesFor(engineName, {workspace: engApiWorkspace, logFolder: this.config.getLogFolder()}));
const rulePromises: Promise<RuleImpl[]>[] = this.getEngineNames().map(async (engineName) =>
this.getAllRulesFor(engineName, {
workspace: engApiWorkspace,
workingFolder: await this.tempFolder.createSubfolder(describeWorkingFolderName, engineName),
logFolder: this.config.getLogFolder()
}));
this.rulesCache.set(cacheKey, (await Promise.all(rulePromises)).flat());
}
return this.rulesCache.get(cacheKey)!;
Expand Down Expand Up @@ -602,13 +621,6 @@ function validateRuleDescriptions(ruleDescriptions: engApi.RuleDescription[], en
}
}

function extractEngineRunOptions(runOptions: RunOptions, logFolder: string): engApi.RunOptions {
return {
logFolder: logFolder,
workspace: toEngApiWorkspace(runOptions.workspace),
};
}

async function validateFileOrFolder(fileOrFolder: string): Promise<string> {
const absFileOrFolder: string = toAbsolutePath(fileOrFolder);
try {
Expand Down Expand Up @@ -692,6 +704,7 @@ function validateViolationCodeLocations(violation: engApi.Violation, engineName:
engineName, violation.ruleName, codeLocation.endLine, codeLocation.startLine));
}

// istanbul ignore else
if (codeLocation.endColumn !== undefined) {
if (!isValidLineOrColumn(codeLocation.endColumn)) {
throw new Error(getMessage('EngineReturnedViolationWithCodeLocationWithInvalidLineOrColumn',
Expand Down
28 changes: 26 additions & 2 deletions packages/code-analyzer-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as path from "node:path";
import * as crypto from "node:crypto";
import path from "node:path";
import crypto from "node:crypto";
import fs from "node:fs";
import {createTempDir} from "@salesforce/code-analyzer-engine-api/utils";

// THIS FILE CONTAINS UTILITIES WHICH ARE USED INTERNALLY ONLY.
// None of the following exported interfaces and functions should be exported from the index file.
Expand Down Expand Up @@ -27,6 +29,28 @@ export class RuntimeUniqueIdGenerator implements UniqueIdGenerator {
}
}

export interface TempFolder {
getPath(): Promise<string>;
createSubfolder(...subfolderPathSegs: string[]): Promise<string>;
}

export class RuntimeTempFolder implements TempFolder {
private rootFolder?: string;

async getPath(): Promise<string> {
if (!this.rootFolder) {
this.rootFolder = await createTempDir();
}
return this.rootFolder;
}

async createSubfolder(...subFolderPathSegs: string[]): Promise<string> {
const absPathToSubFolder: string = path.join(await this.getPath(), ...subFolderPathSegs);
await fs.promises.mkdir(absPathToSubFolder, {recursive: true});
return absPathToSubFolder;
}
}

export class EngineProgressAggregator {
private readonly percentagesMap: Map<string, number> = new Map();

Expand Down
Loading
Loading