Skip to content

feat(core): Deprecate flatten #14454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2024
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
5 changes: 4 additions & 1 deletion packages/core/src/utils-hoist/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type NestedArray<T> = Array<NestedArray<T> | T>;

/** Flattens a multi-dimensional array */
/** Flattens a multi-dimensional array
*
* @deprecated This function is deprecated and will be removed in the next major version.
*/
export function flatten<T>(input: NestedArray<T>): T[] {
const result: T[] = [];

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils-hoist/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { applyAggregateErrorsToEvent } from './aggregate-errors';
// eslint-disable-next-line deprecation/deprecation
export { flatten } from './array';
export { getBreadcrumbLogLevelFromHttpStatusCode } from './breadcrumb-log-level';
export { getComponentName, getDomElement, getLocationHref, htmlTreeAsString } from './browser';
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/utils-hoist/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type { NestedArray } from '../../src/utils-hoist/array';
// eslint-disable-next-line deprecation/deprecation
import { flatten } from '../../src/utils-hoist/array';

describe('flatten', () => {
it('should return the same array when input is a flat array', () => {
const input = [1, 2, 3, 4];
const expected = [1, 2, 3, 4];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

it('should flatten a nested array of numbers', () => {
const input = [[1, 2, [3]], 4];
const expected = [1, 2, 3, 4];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

Expand All @@ -20,6 +23,7 @@ describe('flatten', () => {
['How', 'Are', 'You'],
];
const expected = ['Hello', 'World', 'How', 'Are', 'You'];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

Expand All @@ -29,30 +33,35 @@ describe('flatten', () => {
[{ a: 3 }, { b: 4 }],
];
const expected = [{ a: 1 }, { b: 2 }, { a: 3 }, { b: 4 }];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

it('should flatten a mixed type array', () => {
const input: NestedArray<string | { b: number }> = [['a', { b: 2 }, 'c'], 'd'];
const expected = ['a', { b: 2 }, 'c', 'd'];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

it('should flatten a deeply nested array', () => {
const input = [1, [2, [3, [4, [5]]]]];
const expected = [1, 2, 3, 4, 5];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

it('should return an empty array when input is empty', () => {
const input: any[] = [];
const expected: any[] = [];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});

it('should return the same array when input is a flat array', () => {
const input = [1, 'a', { b: 2 }, 'c', 3];
const expected = [1, 'a', { b: 2 }, 'c', 3];
// eslint-disable-next-line deprecation/deprecation
expect(flatten(input)).toEqual(expected);
});
});
21 changes: 20 additions & 1 deletion packages/node/src/utils/redisCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CommandArgs as IORedisCommandArgs } from '@opentelemetry/instrumentation-ioredis';
import { flatten } from '@sentry/core';

const SINGLE_ARG_COMMANDS = ['get', 'set', 'setex'];

Expand Down Expand Up @@ -95,3 +94,23 @@ export function calculateCacheItemSize(response: unknown): number | undefined {
}, 0)
: getSize(response);
}

// TODO(v9): This is inlined from core so we can deprecate `flatten`.
// It's usage can be replaced with `Array.flat` in v9.
type NestedArray<T> = Array<NestedArray<T> | T>;
function flatten<T>(input: NestedArray<T>): T[] {
const result: T[] = [];

const flattenHelper = (input: NestedArray<T>): void => {
input.forEach((el: T | NestedArray<T>) => {
if (Array.isArray(el)) {
flattenHelper(el as NestedArray<T>);
} else {
result.push(el as T);
}
});
};

flattenHelper(input);
return result;
}
21 changes: 12 additions & 9 deletions packages/nuxt/src/vite/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import { consoleSandbox, flatten } from '@sentry/core';
import { consoleSandbox } from '@sentry/core';

/**
* Find the default SDK init file for the given type (client or server).
Expand Down Expand Up @@ -92,18 +92,21 @@ export function constructWrappedFunctionExportQuery(
entrypointWrappedFunctions: string[],
debug?: boolean,
): string {
const functionsToExport: { wrap: string[]; reexport: string[] } = {
wrap: [],
reexport: [],
};

// `exportedBindings` can look like this: `{ '.': [ 'handler' ] }` or `{ '.': [], './firebase-gen-1.mjs': [ 'server' ] }`
// The key `.` refers to exports within the current file, while other keys show from where exports were imported first.
const functionsToExport = flatten(Object.values(exportedBindings || {})).reduce(
(functions, currFunctionName) => {
if (entrypointWrappedFunctions.includes(currFunctionName)) {
functions.wrap.push(currFunctionName);
Object.values(exportedBindings || {}).forEach(functions =>
functions.forEach(fn => {
if (entrypointWrappedFunctions.includes(fn)) {
functionsToExport.wrap.push(fn);
} else {
functions.reexport.push(currFunctionName);
functionsToExport.reexport.push(fn);
}
return functions;
},
{ wrap: [], reexport: [] } as { wrap: string[]; reexport: string[] },
}),
);

if (debug && functionsToExport.wrap.length === 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ export const isNodeEnv = isNodeEnv_imported;
export const loadModule = loadModule_imported;

/** @deprecated Import from `@sentry/core` instead. */
// eslint-disable-next-line deprecation/deprecation
export const flatten = flatten_imported;

/** @deprecated Import from `@sentry/core` instead. */
Expand Down
Loading