Skip to content

Commit

Permalink
chore: fixed code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
danielduarte committed Sep 9, 2023
1 parent 6e29758 commit 394c08b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [12.x, 14.x, 16.x, 17.x, 18.x]
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions src/engine/flow-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class FlowManager {
let error;
if (statusCode !== 200) {
error = new Error(`Request failed with status code: ${statusCode}`);
} else if (!(/^application\/json/.test(contentType) || /^text\/plain/.test(contentType))) {
error = new Error(`Invalid content-type. Expected application/json or text/plain but received ${contentType}`);
} else if (!(contentType.startsWith('application/json') || contentType.startsWith('text/plain'))) {
error = new Error(`Invalid content-type: Expected 'application/json' or 'text/plain' but received '${contentType}'`);
}

if (error) {
Expand Down
4 changes: 3 additions & 1 deletion src/engine/flow-state/flow-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ export abstract class FlowState implements IFlow {
this.runStatus.state.postProcessFinished(error, stopFlowExecutionOnError);
}

protected postProcessFinished(_error: Error | boolean, _stopFlowExecutionOnError: boolean): void {}
protected postProcessFinished(_error: Error | boolean, _stopFlowExecutionOnError: boolean): void {
// Default empty implementation to be overridden when applies.
}

protected createTransitionError(transition: string): Error {
return new Error(`Cannot execute transition ${transition} in current state ${this.getStateCode()}.`);
Expand Down
4 changes: 2 additions & 2 deletions src/engine/task-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class TaskProcess {

let resolverFn = this.taskResolverExecutor as TaskResolverFn;
let resolverThis: TaskResolverClass | undefined = undefined;
const isClassResolver = this.taskResolverExecutor.prototype && this.taskResolverExecutor.prototype.exec;
const isClassResolver = this.taskResolverExecutor.prototype?.exec;
if (isClassResolver) {
// @todo try to remove type casts in this code section
const resolverInstance = new (this.taskResolverExecutor as TaskResolverClass)();
Expand Down Expand Up @@ -68,7 +68,7 @@ export class TaskProcess {
// @sonar end-ignore

const resultIsObject = typeof resolverResult === 'object';
const resultIsPromise = resolverResult && resolverResult.constructor && resolverResult.constructor.name === 'Promise';
const resultIsPromise = resolverResult?.constructor?.name === 'Promise';

if (!resultIsObject) {
throw new Error(
Expand Down
29 changes: 11 additions & 18 deletions src/engine/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,19 @@ export class Task {
for (const [resolverParamName, paramSolvingInfo] of Object.entries(resolverParams)) {
// @todo Add test to check the case when a loop round does not set anything and make sure next value (`paramValue`) is undefined by default

// If it is string, it is a task param name
if (typeof paramSolvingInfo === 'string') {
const taskParamName = paramSolvingInfo;
paramValue = solvedReqs[taskParamName];
}

// If it is an object, expect the format { [value: <some value>], [transform: <some template>] }
else {
// Implicit case: if (typeof paramSolvingInfo === 'object' && paramSolvingInfo !== null)
// If it is string, it is a task param name
paramValue = solvedReqs[paramSolvingInfo];
} else if (Object.prototype.hasOwnProperty.call(paramSolvingInfo, 'value')) {
// If it is an object, expect the format { value: <some value> } or { transform: <some template> }
// Implicit condition: typeof paramSolvingInfo === 'object' && paramSolvingInfo !== null
// Direct value pre-processor
if (Object.prototype.hasOwnProperty.call(paramSolvingInfo, 'value')) {
paramValue = (paramSolvingInfo as ResolverParamInfoValue).value;
}

paramValue = (paramSolvingInfo as ResolverParamInfoValue).value;
} else {
// Template transform pre-processor
else {
// Implicit case: if (paramSolvingInfo.hasOwnProperty('transform'))
const template = (paramSolvingInfo as ResolverParamInfoTransform).transform;
paramValue = ST.select(solvedReqs).transformWith(template).root();
}
// Implicit condition: paramSolvingInfo.hasOwnProperty('transform')
const template = (paramSolvingInfo as ResolverParamInfoTransform).transform;
paramValue = ST.select(solvedReqs).transformWith(template).root();
}

params[resolverParamName] = paramValue;
Expand All @@ -120,7 +113,7 @@ export class Task {

const results: ValueMap = {};

let resolverResults = (this.spec.resolver ?? {}).results ?? {};
let resolverResults = this.spec.resolver?.results ?? {};

if (automap) {
const provides = this.spec.provides ?? [];
Expand Down
2 changes: 1 addition & 1 deletion test/load-from-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('can run a flow', function () {
throw new Error('An error should have been thrown');
} catch (err) {
expect((err as Error).message).to.be.eql(
'Invalid content-type. Expected application/json or text/plain but received some-unknown/content-format',
"Invalid content-type: Expected 'application/json' or 'text/plain' but received 'some-unknown/content-format'",
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion web/flowed.js

Large diffs are not rendered by default.

0 comments on commit 394c08b

Please sign in to comment.