diff --git a/CHANGELOG.md b/CHANGELOG.md index e47aec59f..7efcd54e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber ## [Unreleased] ### Changed - Improve error handling in publish plugin ([#2526](https://github.com/cucumber/cucumber-js/pull/2526)) +- Improve type safety in features/support/formatter_output_helpers, removes explicit `any` ([#1648](https://github.com/cucumber/cucumber-js/pull/2106)) ## [11.2.0] - 2025-01-09 ### Added diff --git a/features/support/formatter_output_helpers.ts b/features/support/formatter_output_helpers.ts index 07f2dd0ed..b692cafa5 100644 --- a/features/support/formatter_output_helpers.ts +++ b/features/support/formatter_output_helpers.ts @@ -10,6 +10,10 @@ import { IJsonStep, } from '../../src/formatter/json_formatter' +function isObject(obj: unknown): obj is Record { + return typeof obj === 'object' && obj !== null +} + // Converting windows stack trace to posix and removing cwd // C:\\project\\path\\features\\support/code.js // becomes @@ -22,26 +26,34 @@ function normalizeExceptionAndUri(exception: string, cwd: string): string { .split('\n')[0] } -function normalizeMessage(obj: any, cwd: string): void { - if (doesHaveValue(obj.uri)) { - obj.uri = normalizeExceptionAndUri(obj.uri, cwd) - } - if (doesHaveValue(obj.sourceReference?.uri)) { - obj.sourceReference.uri = normalizeExceptionAndUri( - obj.sourceReference.uri, - cwd - ) - } - if (doesHaveValue(obj.testStepResult)) { - if (doesHaveValue(obj.testStepResult.duration)) { - obj.testStepResult.duration.nanos = 0 +function normalizeMessage( + obj: messages.Envelope[keyof messages.Envelope], + cwd: string +): void { + if (isObject(obj)) { + if (typeof obj.uri === 'string') { + obj.uri = normalizeExceptionAndUri(obj.uri, cwd) } - if (doesHaveValue(obj.testStepResult.message)) { - obj.testStepResult.message = normalizeExceptionAndUri( - obj.testStepResult.message, + if ( + isObject(obj.sourceReference) && + typeof obj.sourceReference.uri === 'string' + ) { + obj.sourceReference.uri = normalizeExceptionAndUri( + obj.sourceReference.uri, cwd ) } + if (isObject(obj.testStepResult)) { + if (isObject(obj.testStepResult.duration)) { + obj.testStepResult.duration.nanos = 0 + } + if (typeof obj.testStepResult.message === 'string') { + obj.testStepResult.message = normalizeExceptionAndUri( + obj.testStepResult.message, + cwd + ) + } + } } } @@ -49,10 +61,11 @@ export function normalizeMessageOutput( envelopeObjects: messages.Envelope[], cwd: string ): messages.Envelope[] { - envelopeObjects.forEach((e: any) => { - for (const key in e) { + envelopeObjects.forEach((e: messages.Envelope) => { + const keys = Object.keys(e) as (keyof messages.Envelope)[] + keys.forEach((key) => { normalizeMessage(e[key], cwd) - } + }) }) return envelopeObjects }