Skip to content

Commit

Permalink
CHANGE: @W-16022979@: Rename RunOption fields to better reflect clien…
Browse files Browse the repository at this point in the history
…t names (#28)
  • Loading branch information
stephen-carter-at-sf authored Jun 17, 2024
1 parent 82d6f2b commit 7027975
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 107 deletions.
2 changes: 1 addition & 1 deletion 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.3.0"
"@salesforce/code-analyzer-engine-api": "0.4.0"
},
"devDependencies": {
"@eslint/js": "^8.57.0",
Expand Down
4 changes: 2 additions & 2 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.3.0",
"version": "0.4.0",
"author": "The Salesforce Code Analyzer Team",
"license": "BSD-3-Clause license",
"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.3.0",
"@salesforce/code-analyzer-engine-api": "0.4.0",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.0.0",
"csv-stringify": "^6.5.0",
Expand Down
52 changes: 26 additions & 26 deletions packages/code-analyzer-core/src/code-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import fs from "node:fs";
import path from "node:path";

export type RunOptions = {
filesToInclude: string[]
entryPoints?: string[]
workspaceFiles: string[]
pathStartPoints?: string[]
}

export class CodeAnalyzer {
Expand Down Expand Up @@ -238,17 +238,17 @@ function validateRuleDescriptions(ruleDescriptions: engApi.RuleDescription[], en
}

function extractEngineRunOptions(runOptions: RunOptions): engApi.RunOptions {
if(!runOptions.filesToInclude || runOptions.filesToInclude.length == 0) {
if(!runOptions.workspaceFiles || runOptions.workspaceFiles.length == 0) {
throw new Error(getMessage('AtLeastOneFileOrFolderMustBeIncluded'));
}
const engineRunOptions: engApi.RunOptions = {
filesToInclude: removeRedundantPaths(runOptions.filesToInclude.map(validateFileOrFolder))
workspaceFiles: removeRedundantPaths(runOptions.workspaceFiles.map(validateFileOrFolder))
};

if (runOptions.entryPoints && runOptions.entryPoints.length > 0) {
engineRunOptions.entryPoints = runOptions.entryPoints.flatMap(extractEngineEntryPoints)
if (runOptions.pathStartPoints && runOptions.pathStartPoints.length > 0) {
engineRunOptions.pathStartPoints = runOptions.pathStartPoints.flatMap(extractEnginePathStartPoints)
}
validateEntryPointsLiveUnderFilesToInclude(engineRunOptions);
validatePathStartPointsAreInsideWorkspace(engineRunOptions);
return engineRunOptions;
}

Expand Down Expand Up @@ -276,53 +276,53 @@ function validateFileOrFolder(fileOrFolder: string): string {
return absFileOrFolder;
}

function validateEntryPointFile(file: string, entryPointStr: string): string {
function validatePathStartPointFile(file: string, pathStartPointStr: string): string {
const absFile: string = toAbsolutePath(file);
if (!fs.existsSync(absFile)) {
throw new Error(getMessage('EntryPointFileDoesNotExist', entryPointStr, absFile));
throw new Error(getMessage('PathStartPointFileDoesNotExist', pathStartPointStr, absFile));
} else if (fs.statSync(absFile).isDirectory()) {
throw new Error(getMessage('EntryPointWithMethodMustNotBeFolder', entryPointStr, absFile));
throw new Error(getMessage('PathStartPointWithMethodMustNotBeFolder', pathStartPointStr, absFile));
}
return absFile;
}

function extractEngineEntryPoints(entryPointStr: string): engApi.EntryPoint[] {
const parts: string[] = entryPointStr.split('#');
function extractEnginePathStartPoints(pathStartPointStr: string): engApi.PathPoint[] {
const parts: string[] = pathStartPointStr.split('#');
if (parts.length == 1) {
return [{
file: validateFileOrFolder(entryPointStr)
file: validateFileOrFolder(pathStartPointStr)
}];
} else if (parts.length > 2) {
throw new Error(getMessage('InvalidEntryPoint', entryPointStr));
throw new Error(getMessage('InvalidPathStartPoint', pathStartPointStr));
}

const entryPointFile: string = validateEntryPointFile(parts[0], entryPointStr);
const pathStartPointFile: string = validatePathStartPointFile(parts[0], pathStartPointStr);
const VALID_METHOD_NAME_REGEX = /^[A-Za-z][A-Za-z0-9_]*$/;
const TRAILING_SPACES_AND_SEMICOLONS_REGEX = /\s+;*$/;
const methodNames: string = parts[1].replace(TRAILING_SPACES_AND_SEMICOLONS_REGEX, '');
return methodNames.split(";").map(methodName => {
if (! VALID_METHOD_NAME_REGEX.test(methodName) ) {
throw new Error(getMessage('InvalidEntryPoint', entryPointStr));
throw new Error(getMessage('InvalidPathStartPoint', pathStartPointStr));
}
return { file: entryPointFile, methodName: methodName };
return { file: pathStartPointFile, methodName: methodName };
});
}

function validateEntryPointsLiveUnderFilesToInclude(engineRunOptions: engApi.RunOptions) {
if (!engineRunOptions.entryPoints) {
function validatePathStartPointsAreInsideWorkspace(engineRunOptions: engApi.RunOptions) {
if (!engineRunOptions.pathStartPoints) {
return;
}
for (const engineEntryPoint of engineRunOptions.entryPoints) {
if (!fileIsUnderneath(engineEntryPoint.file, engineRunOptions.filesToInclude)) {
throw new Error(getMessage('EntryPointMustBeUnderFilesToInclude', engineEntryPoint.file,
JSON.stringify(engineRunOptions.filesToInclude)));
for (const enginePathStartPoint of engineRunOptions.pathStartPoints) {
if (!fileIsUnderneath(enginePathStartPoint.file, engineRunOptions.workspaceFiles)) {
throw new Error(getMessage('PathStartPointMustBeInsideWorkspace', enginePathStartPoint.file,
JSON.stringify(engineRunOptions.workspaceFiles)));
}
}
}

function fileIsUnderneath(entryPointFile: string, filesOrFolders: string[]): boolean {
return filesOrFolders.some(fileOrFolder => fileOrFolder == entryPointFile ||
(fs.statSync(fileOrFolder).isDirectory() && entryPointFile.startsWith(fileOrFolder)));
function fileIsUnderneath(file: string, filesOrFolders: string[]): boolean {
return filesOrFolders.some(fileOrFolder => fileOrFolder == file ||
(fs.statSync(fileOrFolder).isDirectory() && file.startsWith(fileOrFolder)));
}

function validateEngineRunResults(engineName: string, apiEngineRunResults: engApi.EngineRunResults, ruleSelection: RuleSelection): void {
Expand Down
18 changes: 9 additions & 9 deletions packages/code-analyzer-core/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ const messageCatalog : { [key: string]: string } = {
AtLeastOneFileOrFolderMustBeIncluded:
'At least one file or folder must be included.',

EntryPointFileDoesNotExist:
'The value "%s" is not a valid entry point since the file "%s" does not exist.',
PathStartPointFileDoesNotExist:
'The value "%s" is not a valid path starting point since the file "%s" does not exist.',

EntryPointWithMethodMustNotBeFolder:
'The value "%s" is not a valid entry point since "%s" is a folder instead of a file.',
PathStartPointWithMethodMustNotBeFolder:
'The value "%s" is not a valid path starting point since "%s" is a folder instead of a file.',

InvalidEntryPoint:
`The value "%s" is not a valid entry point. Expected value to be of the format "<fileOrFolder>", "<file>#<methodName>", or "<file>#<methodName1>;<methodName2>;...".`,
InvalidPathStartPoint:
`The value "%s" is not a valid path starting point. Expected value to be of the format "<fileOrFolder>", "<file>#<methodName>", or "<file>#<methodName1>;<methodName2>;...".`,

EntryPointMustBeUnderFilesToInclude:
'The specified entry point of "%s" does not that exists underneath any of the specified paths: %s',
PathStartPointMustBeInsideWorkspace:
'The specified path starting point of "%s" does not that exists underneath any of the specified paths: %s',

RunningWithRunOptions:
'Running with the following run options: %s',
Expand Down Expand Up @@ -121,7 +121,7 @@ const messageCatalog : { [key: string]: string } = {
}

/**
* getMessage - This is the main entry point to get a message out of the message catalog.
* getMessage - This is the main function to get a message out of the message catalog.
* @param msgId - The message identifier
* @param args - The arguments that will fill in the %s and %d markers.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/code-analyzer-core/test/output-format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ beforeAll(async () => {
(stubPlugin.getCreatedEngine('stubEngine2') as stubs.StubEngine2).resultsToReturn = {
violations: [stubs.getSampleViolationForStub2RuleC()]
};
runResults = await codeAnalyzer.run(codeAnalyzer.selectRules('all'), {filesToInclude: ['test']});
runResults = await codeAnalyzer.run(codeAnalyzer.selectRules('all'), {workspaceFiles: ['test']});
});

describe("Tests for the CSV output format", () => {
Expand Down Expand Up @@ -123,5 +123,5 @@ function getContentsOfExpectedOutputFile(expectedOutputFileName: string, escapeB
async function createResultsWithUnexpectedError(): Promise<RunResults> {
const codeAnalyzer: CodeAnalyzer = new CodeAnalyzer(CodeAnalyzerConfig.withDefaults());
await codeAnalyzer.addEnginePlugin(new stubs.ThrowingEnginePlugin());
return codeAnalyzer.run(codeAnalyzer.selectRules(), {filesToInclude: ['test']});
return codeAnalyzer.run(codeAnalyzer.selectRules(), {workspaceFiles: ['test']});
}
Loading

0 comments on commit 7027975

Please sign in to comment.