Skip to content

Commit 3742bd3

Browse files
authored
feat(core): Deprecate flatten (#14454)
Ref: #14267 From my investigation, redis does not actually support arrays/nested arrays for keys and ioredis is wrapping thinly over redis functions, but otel has `any[]` as one of their possible values for command line arguments so I opted for inlining the implementation and adding a note to use `Array.flat` in v9.
1 parent 1b7815a commit 3742bd3

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

packages/core/src/utils-hoist/array.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export type NestedArray<T> = Array<NestedArray<T> | T>;
22

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

packages/core/src/utils-hoist/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { applyAggregateErrorsToEvent } from './aggregate-errors';
2+
// eslint-disable-next-line deprecation/deprecation
23
export { flatten } from './array';
34
export { getBreadcrumbLogLevelFromHttpStatusCode } from './breadcrumb-log-level';
45
export { getComponentName, getDomElement, getLocationHref, htmlTreeAsString } from './browser';

packages/core/test/utils-hoist/array.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import type { NestedArray } from '../../src/utils-hoist/array';
2+
// eslint-disable-next-line deprecation/deprecation
23
import { flatten } from '../../src/utils-hoist/array';
34

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

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

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

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

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

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

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

5361
it('should return the same array when input is a flat array', () => {
5462
const input = [1, 'a', { b: 2 }, 'c', 3];
5563
const expected = [1, 'a', { b: 2 }, 'c', 3];
64+
// eslint-disable-next-line deprecation/deprecation
5665
expect(flatten(input)).toEqual(expected);
5766
});
5867
});

packages/node/src/utils/redisCache.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { CommandArgs as IORedisCommandArgs } from '@opentelemetry/instrumentation-ioredis';
2-
import { flatten } from '@sentry/core';
32

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

@@ -95,3 +94,23 @@ export function calculateCacheItemSize(response: unknown): number | undefined {
9594
}, 0)
9695
: getSize(response);
9796
}
97+
98+
// TODO(v9): This is inlined from core so we can deprecate `flatten`.
99+
// It's usage can be replaced with `Array.flat` in v9.
100+
type NestedArray<T> = Array<NestedArray<T> | T>;
101+
function flatten<T>(input: NestedArray<T>): T[] {
102+
const result: T[] = [];
103+
104+
const flattenHelper = (input: NestedArray<T>): void => {
105+
input.forEach((el: T | NestedArray<T>) => {
106+
if (Array.isArray(el)) {
107+
flattenHelper(el as NestedArray<T>);
108+
} else {
109+
result.push(el as T);
110+
}
111+
});
112+
};
113+
114+
flattenHelper(input);
115+
return result;
116+
}

packages/nuxt/src/vite/utils.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { consoleSandbox, flatten } from '@sentry/core';
3+
import { consoleSandbox } from '@sentry/core';
44

55
/**
66
* Find the default SDK init file for the given type (client or server).
@@ -92,18 +92,21 @@ export function constructWrappedFunctionExportQuery(
9292
entrypointWrappedFunctions: string[],
9393
debug?: boolean,
9494
): string {
95+
const functionsToExport: { wrap: string[]; reexport: string[] } = {
96+
wrap: [],
97+
reexport: [],
98+
};
99+
95100
// `exportedBindings` can look like this: `{ '.': [ 'handler' ] }` or `{ '.': [], './firebase-gen-1.mjs': [ 'server' ] }`
96101
// The key `.` refers to exports within the current file, while other keys show from where exports were imported first.
97-
const functionsToExport = flatten(Object.values(exportedBindings || {})).reduce(
98-
(functions, currFunctionName) => {
99-
if (entrypointWrappedFunctions.includes(currFunctionName)) {
100-
functions.wrap.push(currFunctionName);
102+
Object.values(exportedBindings || {}).forEach(functions =>
103+
functions.forEach(fn => {
104+
if (entrypointWrappedFunctions.includes(fn)) {
105+
functionsToExport.wrap.push(fn);
101106
} else {
102-
functions.reexport.push(currFunctionName);
107+
functionsToExport.reexport.push(fn);
103108
}
104-
return functions;
105-
},
106-
{ wrap: [], reexport: [] } as { wrap: string[]; reexport: string[] },
109+
}),
107110
);
108111

109112
if (debug && functionsToExport.wrap.length === 0) {

packages/utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ export const isNodeEnv = isNodeEnv_imported;
610610
export const loadModule = loadModule_imported;
611611

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

615616
/** @deprecated Import from `@sentry/core` instead. */

0 commit comments

Comments
 (0)