Chore: Taint ArrayVector with `never` to further discourage (#83681)

Chore: Taint ArrayVector with never to further discourage
pull/83618/head
Josh Hunt 1 year ago committed by GitHub
parent 29d6cd8fa0
commit f0dce33034
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      .betterer.results
  2. 14
      packages/grafana-data/src/vector/ArrayVector.test.ts
  3. 10
      packages/grafana-data/src/vector/ArrayVector.ts

@ -439,11 +439,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"]
],
"packages/grafana-data/src/vector/ArrayVector.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
],
"packages/grafana-data/src/vector/CircularVector.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],

@ -2,6 +2,9 @@ import { Field, FieldType } from '../types';
import { ArrayVector } from './ArrayVector';
// There's lots of @ts-expect-error here, because we actually expect it to be a typescript error
// to further encourge developers not to use ArrayVector
describe('ArrayVector', () => {
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation();
@ -9,12 +12,14 @@ describe('ArrayVector', () => {
it('should init 150k with 65k Array.push() chonking', () => {
const arr = Array.from({ length: 150e3 }, (v, i) => i);
/// @ts-expect-error
const av = new ArrayVector(arr);
expect(av.toArray()).toEqual(arr);
});
it('should support add and push', () => {
/// @ts-expect-error
const av = new ArrayVector<number>();
av.add(1);
av.push(2);
@ -28,17 +33,26 @@ describe('ArrayVector', () => {
name: 'test',
config: {},
type: FieldType.number,
/// @ts-expect-error
values: new ArrayVector(), // this defaults to `new ArrayVector<any>()`
};
expect(field).toBeDefined();
// Before collapsing Vector, ReadWriteVector, and MutableVector these all worked fine
/// @ts-expect-error
field.values = new ArrayVector();
/// @ts-expect-error
field.values = new ArrayVector(undefined);
/// @ts-expect-error
field.values = new ArrayVector([1, 2, 3]);
/// @ts-expect-error
field.values = new ArrayVector([]);
/// @ts-expect-error
field.values = new ArrayVector([1, undefined]);
/// @ts-expect-error
field.values = new ArrayVector([null]);
/// @ts-expect-error
field.values = new ArrayVector(['a', 'b', 'c']);
expect(field.values.length).toBe(3);
});

@ -6,12 +6,12 @@ let notified = false;
*
* @deprecated use a simple Array<T>
*/
export class ArrayVector<T = any> extends Array<T> {
export class ArrayVector<T = unknown> extends Array<T> {
get buffer() {
return this;
}
set buffer(values: any[]) {
set buffer(values: T[]) {
this.length = 0;
const len = values?.length;
@ -27,10 +27,10 @@ export class ArrayVector<T = any> extends Array<T> {
}
/**
* This any type is here to make the change type changes in v10 non breaking for plugins.
* Before you could technically assign field.values any typed ArrayVector no matter what the Field<T> T type was.
* ArrayVector is deprecated and should not be used. If you get a Typescript error here, use plain arrays for field.values.
*/
constructor(buffer?: any[]) {
// `never` is used to force a build-type error from Typescript to encourage developers to move away from using this
constructor(buffer: never) {
super();
this.buffer = buffer ?? [];

Loading…
Cancel
Save