Skip to content

Commit

Permalink
Merge pull request #63 from Lightning-Flow-Scanner/f2
Browse files Browse the repository at this point in the history
Improvement 	Unexisting specified rulesin configuration 
Incorrect severity configurations
  • Loading branch information
RubenHalman authored Feb 29, 2024
2 parents 5d12513 + 2f47c51 commit 797150a
Show file tree
Hide file tree
Showing 64 changed files with 747 additions and 680 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lightning-flow-scanner-core",
"version": "2.26.0",
"version": "2.27.0",
"main": "out/**",
"types": "out/index.d.ts",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- [Outdated API Version](#outdated-api-version)
- [Copy API Name](#copy-api-name)
- [DML Statement In A Loop'](#dml-statement-in-a-loop)
- [DML Statement In A Loop](#dml-statement-in-a-loop)
- [Duplicate DML Operation](#duplicate-dml-operation)
- [Missing Flow Description](#missing-flow-description)
- [Flow Naming Convention](#flow-naming-convention)
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { IRulesConfig } from './main/interfaces/IRulesConfig';
import { FixFlows } from './main/libs/FixFlows';
import { GetRuleDefinitions } from './main/libs/GetRuleDefinitions';
import { ScanFlows } from './main/libs/ScanFlows';
import { Flow } from './main/models/Flow';
import { ResultDetails } from './main/models/ResultDetails';
import { RuleResult } from './main/models/RuleResult';
import { ScanResult } from './main/models/ScanResult';
import Flow from './main/models/Flow';
import ResultDetails from './main/models/ResultDetails';
import RuleResult from './main/models/RuleResult';
import ScanResult from './main/models/ScanResult';

export function getRules(ruleNames?: string[]): IRuleDefinition[] {
if (ruleNames && ruleNames.length > 0) {
Expand Down Expand Up @@ -53,4 +53,9 @@ export function scan(flows: Flow[], ruleOptions?: IRulesConfig): ScanResult[] {

export function fix(flows: Flow[]): ScanResult[] {
return FixFlows(flows);
}
}

export { default as Flow } from './main/models/Flow';
export { default as ScanResult } from './main/models/ScanResult';
export { default as RuleResult } from './main/models/RuleResult';
export { default as ResultDetails } from './main/models/ResultDetails';
4 changes: 2 additions & 2 deletions src/main/interfaces/IRuleDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flow } from '../models/Flow';
import { RuleResult } from '../models/RuleResult';
import Flow from '../models/Flow';
import RuleResult from '../models/RuleResult';

export interface IRuleDefinition {
uri: string;
Expand Down
6 changes: 3 additions & 3 deletions src/main/libs/Compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flow } from '../models/Flow';
import Flow from '../models/Flow';
import { FlowNode } from '../models/FlowNode';

export class Compiler {
Expand Down Expand Up @@ -26,7 +26,7 @@ export class Compiler {

traverseFlow(flow: Flow, startElementName: string, visitCallback: (element: FlowNode) => void) {
// Iterative Deepening Depth-First Search (IDDFS)
let depth = 0;
// let depth = 0;
let elementsToVisit = [startElementName];

while (elementsToVisit.length > 0) {
Expand All @@ -44,8 +44,8 @@ export class Compiler {
}

elementsToVisit = nextElements;
depth++;
// add logic to control depth or terminate the traversal based on requirements.
// depth++;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/libs/DynamicRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class DynamicRule {

if (DefaultRuleStore[className] === undefined || DefaultRuleStore[className] === null) {

throw new Error(`Class type of \'${className}\' is not in the store`);
throw new Error(`Rule \'${className}\' does not exist in the store.`);
}
return new DefaultRuleStore[className]();
}
Expand Down
21 changes: 9 additions & 12 deletions src/main/libs/FixFlows.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { Flow } from '../models/Flow';
import { FlowNode } from '../models/FlowNode';
import { FlowVariable } from '../models/FlowVariable';
import { ResultDetails } from '../models/ResultDetails';
import { RuleResult } from '../models/RuleResult';
import { ScanResult } from '../models/ScanResult';
import { UnconnectedElement } from '../rules/UnconnectedElement';
import { UnusedVariable } from '../rules/UnusedVariable';
import { BuildFlow } from './BuildFlow';
import * as core from '../../index';

export function FixFlows(flows: Flow[]): ScanResult[] {
export function FixFlows(flows: core.Flow[]): core.ScanResult[] {

const flowResults: ScanResult[] = [];
const flowResults: core.ScanResult[] = [];
for (const flow of flows) {
const unconnectedElementsResult: RuleResult = new UnconnectedElement().execute(flow);
const unusedVariablesResult: RuleResult = new UnusedVariable().execute(flow);
const ruleResults: RuleResult[] = [unusedVariablesResult, unconnectedElementsResult];
const unusedVariableReferences = unusedVariablesResult.details ? (unusedVariablesResult.details as ResultDetails[]).map(unusedVariable => unusedVariable.name) : [];
const unconnectedElementsReferences = unconnectedElementsResult.details ? (unconnectedElementsResult.details as ResultDetails[]).map(unconnectedElement => unconnectedElement.name) : [];
const unconnectedElementsResult: core.RuleResult = new UnconnectedElement().execute(flow);
const unusedVariablesResult: core.RuleResult = new UnusedVariable().execute(flow);
const ruleResults: core.RuleResult[] = [unusedVariablesResult, unconnectedElementsResult];
const unusedVariableReferences = unusedVariablesResult.details ? (unusedVariablesResult.details as core.ResultDetails[]).map(unusedVariable => unusedVariable.name) : [];
const unconnectedElementsReferences = unconnectedElementsResult.details ? (unconnectedElementsResult.details as core.ResultDetails[]).map(unconnectedElement => unconnectedElement.name) : [];
const nodesToBuild = flow.elements.filter(node => {
switch (node.metaType) {
case 'variable':
Expand All @@ -38,7 +35,7 @@ export function FixFlows(flows: Flow[]): ScanResult[] {
);
flow.xmldata = BuildFlow(nodesToBuild);
flow.preProcessNodes();
flowResults.push(new ScanResult(flow, ruleResults));
flowResults.push(new core.ScanResult(flow, ruleResults));
}
return flowResults;
}
18 changes: 10 additions & 8 deletions src/main/libs/GetRuleDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ export function GetRuleDefinitions(ruleConfig?: Map<string, {}>): IRuleDefinitio

if (ruleConfig && ruleConfig instanceof Map) {
for (const ruleName of ruleConfig.keys()) {
const matchedRule = new DynamicRule(ruleName);
const configuredSeverity = ruleConfig.get(ruleName)['severity'];
if (configuredSeverity && (configuredSeverity === "error" || configuredSeverity === "warning" || configuredSeverity === "note")) {
severity = configuredSeverity;
} else {
throw new Error(`Invalid severity "${configuredSeverity}" provided for rule "${ruleName}".`);
try{
const matchedRule = new DynamicRule(ruleName);
const configuredSeverity = ruleConfig.get(ruleName)['severity'];
if (configuredSeverity && (configuredSeverity === "error" || configuredSeverity === "warning" || configuredSeverity === "note")) {
severity = configuredSeverity;
}
matchedRule['severity'] = severity;
matchedRules.push(matchedRule);
} catch (error) {
console.log(error.message)
}
matchedRule['severity'] = severity;
matchedRules.push(matchedRule);
}
} else {
// tslint:disable-next-line:forin
Expand Down
12 changes: 5 additions & 7 deletions src/main/libs/ScanFlows.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { IRuleDefinition } from '../interfaces/IRuleDefinition';
import { Flow } from '../models/Flow';
import { RuleResult } from '../models/RuleResult';
import { ScanResult } from '../models/ScanResult';
import { GetRuleDefinitions } from './GetRuleDefinitions';
import { keys } from './Keys';
import * as core from '../../index';

export function ScanFlows(flows: Flow[], rulesConfig?: Map<string, {}>): ScanResult[] {
export function ScanFlows(flows: core.Flow[], rulesConfig?: Map<string, {}>): core.ScanResult[] {

const flowResults: ScanResult[] = [];
const flowResults: core.ScanResult[] = [];
let selectedRules: IRuleDefinition[] = [];
if (rulesConfig) {
selectedRules = GetRuleDefinitions(rulesConfig);
Expand All @@ -18,7 +16,7 @@ export function ScanFlows(flows: Flow[], rulesConfig?: Map<string, {}>): ScanRes
for (const flow of flows) {

try{
const ruleResults: RuleResult[] = [];
const ruleResults: core.RuleResult[] = [];
for (const rule of selectedRules) {
if (rule.supportedTypes.includes(flow.type[0])) {
try {
Expand All @@ -39,7 +37,7 @@ export function ScanFlows(flows: Flow[], rulesConfig?: Map<string, {}>): ScanRes
}
}
}
flowResults.push(new ScanResult(flow, ruleResults));
flowResults.push(new core.ScanResult(flow, ruleResults));
}
catch (error) {
console.log(error.message)
Expand Down
2 changes: 1 addition & 1 deletion src/main/models/Flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FlowElement } from './FlowElement';
import { FlowVariable } from './FlowVariable';
import * as p from 'path';

export class Flow {
export default class Flow {

public label: string;
public xmldata;
Expand Down
2 changes: 1 addition & 1 deletion src/main/models/ResultDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FlowAttribute } from "./FlowAttribute";
import { FlowNode } from "./FlowNode";
import { FlowVariable } from "./FlowVariable";

export class ResultDetails {
export default class ResultDetails {

public violation: FlowNode | FlowVariable | FlowAttribute;
public name: string;
Expand Down
4 changes: 2 additions & 2 deletions src/main/models/RuleResult.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IRuleDefinition } from '../interfaces/IRuleDefinition';
import { ResultDetails } from './ResultDetails';
import ResultDetails from './ResultDetails';

export class RuleResult {
export default class RuleResult {

public occurs: boolean;
public ruleName: string;
Expand Down
6 changes: 3 additions & 3 deletions src/main/models/ScanResult.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Flow} from './Flow';
import {RuleResult} from './RuleResult';
import Flow from './Flow';
import RuleResult from './RuleResult';

export class ScanResult {
export default class ScanResult {

constructor(flow: Flow, ruleResults: RuleResult[]) {
this.flow = flow;
Expand Down
14 changes: 6 additions & 8 deletions src/main/rules/APIVersion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { IRuleDefinition } from '../interfaces/IRuleDefinition';
import { Flow } from '../models/Flow';
import * as core from '../../index';
import { FlowType } from '../models/FlowType';
import { RuleResult } from '../models/RuleResult';
import { RuleCommon } from '../models/RuleCommon';
import { ResultDetails } from '../models/ResultDetails';
import { FlowAttribute } from '../models/FlowAttribute';

export class APIVersion extends RuleCommon implements IRuleDefinition {
Expand All @@ -20,7 +18,7 @@ export class APIVersion extends RuleCommon implements IRuleDefinition {
});
}

public execute(flow: Flow, options?: { expression: string }): RuleResult {
public execute(flow: core.Flow, options?: { expression: string }): core.RuleResult {

let flowAPIVersionNumber: number;
if (flow.xmldata.apiVersion && flow.xmldata.apiVersion[0]) {
Expand All @@ -31,13 +29,13 @@ export class APIVersion extends RuleCommon implements IRuleDefinition {
if (options && options.expression) {
const expressionEvaluation = eval(flowAPIVersionNumber + options.expression);
return (!expressionEvaluation ?
new RuleResult(this, [new ResultDetails(new FlowAttribute(!expressionEvaluation ? ('' + flowAPIVersionNumber) : undefined, "apiVersion", options.expression))]) :
new RuleResult(this, []));
new core.RuleResult(this, [new core.ResultDetails(new FlowAttribute(!expressionEvaluation ? ('' + flowAPIVersionNumber) : undefined, "apiVersion", options.expression))]) :
new core.RuleResult(this, []));
} else {
return new RuleResult(this, []);
return new core.RuleResult(this, []);
}
} else {
return new RuleResult(this, [new ResultDetails(new FlowAttribute('API Version <49', "apiVersion", "<49"))]);
return new core.RuleResult(this, [new core.ResultDetails(new FlowAttribute('API Version <49', "apiVersion", "<49"))]);
}
}
}
10 changes: 4 additions & 6 deletions src/main/rules/CopyAPIName.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { IRuleDefinition } from '../interfaces/IRuleDefinition';
import { Flow } from '../models/Flow';
import * as core from '../../index';
import { FlowType } from '../models/FlowType';
import { RuleResult } from '../models/RuleResult';
import { RuleCommon } from '../models/RuleCommon';
import { FlowNode } from '../models/FlowNode';
import { ResultDetails } from '../models/ResultDetails';

export class CopyAPIName extends RuleCommon implements IRuleDefinition {

Expand All @@ -21,7 +19,7 @@ export class CopyAPIName extends RuleCommon implements IRuleDefinition {
);
}

public execute(flow: Flow): RuleResult {
public execute(flow: core.Flow): core.RuleResult {

const flowElements: FlowNode[] = flow.elements.filter(node => node instanceof FlowNode) as FlowNode[];
const copyOfElements = [];
Expand All @@ -33,8 +31,8 @@ export class CopyAPIName extends RuleCommon implements IRuleDefinition {
}
let results = [];
for (const det of copyOfElements) {
results.push(new ResultDetails(det));
results.push(new core.ResultDetails(det));
}
return new RuleResult(this, results);
return new core.RuleResult(this, results);
}
}
12 changes: 5 additions & 7 deletions src/main/rules/DMLStatementInLoop.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { IRuleDefinition } from '../interfaces/IRuleDefinition';
import { Flow } from '../models/Flow';
import { FlowNode } from '../models/FlowNode';
import { FlowType } from '../models/FlowType';
import { RuleResult } from '../models/RuleResult';
import { RuleCommon } from '../models/RuleCommon';
import { ResultDetails } from '../models/ResultDetails';
import { Compiler } from '../libs/Compiler';
import * as core from '../../index';

export class DMLStatementInLoop extends RuleCommon implements IRuleDefinition {

Expand All @@ -21,9 +19,9 @@ export class DMLStatementInLoop extends RuleCommon implements IRuleDefinition {
});
}

public execute(flow: Flow): RuleResult {
public execute(flow: core.Flow): core.RuleResult {
if (flow.type[0] === 'Survey') {
return new RuleResult(this, []);
return new core.RuleResult(this, []);
}

const dmlStatementTypes = ['recordDeletes', 'recordUpdates', 'recordCreates'];
Expand All @@ -44,9 +42,9 @@ export class DMLStatementInLoop extends RuleCommon implements IRuleDefinition {

let results = [];
for (const det of dmlStatementsInLoops) {
results.push(new ResultDetails(det));
results.push(new core.ResultDetails(det));
}

return new RuleResult(this, results);
return new core.RuleResult(this, results);
}
}
14 changes: 6 additions & 8 deletions src/main/rules/DuplicateDMLOperation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { IRuleDefinition } from '../interfaces/IRuleDefinition';
import { Flow } from '../models/Flow';
import { FlowNode } from '../models/FlowNode';
import { FlowType } from '../models/FlowType';
import { RuleResult } from '../models/RuleResult';
import { RuleCommon } from '../models/RuleCommon';
import { ResultDetails } from '../models/ResultDetails';
import * as core from '../../index';

export class DuplicateDMLOperation extends RuleCommon implements IRuleDefinition {

Expand All @@ -20,9 +18,9 @@ export class DuplicateDMLOperation extends RuleCommon implements IRuleDefinition
});
}

public execute(flow: Flow): RuleResult {
public execute(flow: core.Flow): core.RuleResult {
if (flow.type[0] === 'Survey') {
return new RuleResult(this, []);
return new core.RuleResult(this, []);
}
const flowElements: FlowNode[] = flow.elements.filter(node => node instanceof FlowNode) as FlowNode[];
const processedElementIndexes: number[] = [];
Expand Down Expand Up @@ -77,9 +75,9 @@ export class DuplicateDMLOperation extends RuleCommon implements IRuleDefinition

let results = [];
for (const det of DuplicateDMLOperations) {
results.push(new ResultDetails(det));
results.push(new core.ResultDetails(det));
}
return new RuleResult(this, results);
return new core.RuleResult(this, results);
}

private flagDML(element, dmlFlag) {
Expand All @@ -93,7 +91,7 @@ export class DuplicateDMLOperation extends RuleCommon implements IRuleDefinition
}
}

private findStart(flow: Flow) {
private findStart(flow: core.Flow) {
const flowElements: FlowNode[] = flow.elements.filter(node => node instanceof FlowNode) as FlowNode[];
let start;
if (flow.startElementReference) {
Expand Down
Loading

0 comments on commit 797150a

Please sign in to comment.