diff --git a/eslint.config.js b/eslint.config.js index 5a83bea117..a199b1b152 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -12,6 +12,12 @@ module.exports = { }, rules: { "@typescript-eslint/no-this-alias": "off", + "@typescript-eslint/no-unused-vars": [ + "warn", + { + "argsIgnorePattern": "^_" + } + ], "eqeqeq": "off", "prefer-rest-params": "off", "@typescript-eslint/naming-convention": [ diff --git a/package.json b/package.json index 273db8acf3..cd7cbdc296 100644 --- a/package.json +++ b/package.json @@ -40,16 +40,16 @@ "devDependencies": { "@commitlint/cli": "^8.3.4", "@commitlint/config-conventional": "^8.3.4", + "@typescript-eslint/eslint-plugin": "^3.0.0", + "@typescript-eslint/parser": "^3.0.0", "beautify-benchmark": "^0.2.4", "benchmark": "^2.1.4", - "eslint": "^6.8.0", - "eslint-config-airbnb-base": "^14.0.0", - "eslint-plugin-import": "^2.19.1", + "eslint": "^7.0.0", + "eslint-config-airbnb-base": "^14.1.0", "eslint-plugin-header": "^3.0.0", - "@typescript-eslint/eslint-plugin": "^2.23.0", - "@typescript-eslint/parser": "^2.23.0", + "eslint-plugin-import": "^2.20.2", "gh-pages": "^2.1.1", - "gts": "^2.0.0", + "gts": "^2.0.2", "husky": "^3.0.9", "lerna": "^3.17.0", "lerna-changelog": "^1.0.0", diff --git a/packages/opentelemetry-api/src/context/propagation/NoopHttpTextPropagator.ts b/packages/opentelemetry-api/src/context/propagation/NoopHttpTextPropagator.ts index f36922d875..d7810109fe 100644 --- a/packages/opentelemetry-api/src/context/propagation/NoopHttpTextPropagator.ts +++ b/packages/opentelemetry-api/src/context/propagation/NoopHttpTextPropagator.ts @@ -22,9 +22,9 @@ import { HttpTextPropagator } from './HttpTextPropagator'; */ export class NoopHttpTextPropagator implements HttpTextPropagator { /** Noop inject function does nothing */ - inject(context: Context, carrier: unknown, setter: Function): void {} + inject(_context: Context, _carrier: unknown, _setter: unknown): void {} /** Noop extract function does nothing and returns the input context */ - extract(context: Context, carrier: unknown, getter: Function): Context { + extract(context: Context, _carrier: unknown, _getter: unknown): Context { return context; } } diff --git a/packages/opentelemetry-api/src/metrics/ObserverResult.ts b/packages/opentelemetry-api/src/metrics/ObserverResult.ts index 47c3d595c4..3c9da87158 100644 --- a/packages/opentelemetry-api/src/metrics/ObserverResult.ts +++ b/packages/opentelemetry-api/src/metrics/ObserverResult.ts @@ -21,5 +21,5 @@ import { MetricObservable } from './MetricObservable'; * Interface that is being used in function setCallback for Observer Metric */ export interface ObserverResult { - observe(callback: Function | MetricObservable, labels: Labels): void; + observe(callback: (() => number) | MetricObservable, labels: Labels): void; } diff --git a/packages/opentelemetry-api/test/api/api.test.ts b/packages/opentelemetry-api/test/api/api.test.ts index d02453d252..4a7a7ef92f 100644 --- a/packages/opentelemetry-api/test/api/api.test.ts +++ b/packages/opentelemetry-api/test/api/api.test.ts @@ -56,7 +56,7 @@ describe('API', () => { functions.forEach(fn => { const tracer = api.trace.getTracerProvider(); try { - ((tracer as unknown) as { [fn: string]: Function })[fn](); // Try to run the function + ((tracer as unknown) as { [fn: string]: () => unknown })[fn](); // Try to run the function assert.ok(true, fn); } catch (err) { if (err.message !== 'Method not implemented.') { diff --git a/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts b/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts index f970ab397d..f61348b742 100644 --- a/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts +++ b/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts @@ -20,6 +20,9 @@ import { EventEmitter } from 'events'; type Func = (...args: unknown[]) => T; +// eslint-disable-next-line @typescript-eslint/ban-types +type UserFunction = Function; + type PatchedEventEmitter = { /** * Store a map for each event of all original listener and their "patched" @@ -43,11 +46,11 @@ class Reference { } const ADD_LISTENER_METHODS = [ - 'addListener' as 'addListener', - 'on' as 'on', - 'once' as 'once', - 'prependListener' as 'prependListener', - 'prependOnceListener' as 'prependOnceListener', + 'addListener' as const, + 'on' as const, + 'once' as const, + 'prependListener' as const, + 'prependOnceListener' as const, ]; export class AsyncHooksContextManager implements ContextManager { @@ -141,9 +144,12 @@ export class AsyncHooksContextManager implements ContextManager { return this; } - private _bindFunction(target: T, context: Context): T { + private _bindFunction( + target: T, + context: Context + ): T { const manager = this; - const contextWrapper = function (this: {}, ...args: unknown[]) { + const contextWrapper = function (this: unknown, ...args: unknown[]) { return manager.with(context, () => target.apply(this, args)); }; Object.defineProperty(contextWrapper, 'length', { @@ -202,8 +208,11 @@ export class AsyncHooksContextManager implements ContextManager { * @param ee EventEmitter instance * @param original reference to the patched method */ - private _patchRemoveListener(ee: PatchedEventEmitter, original: Function) { - return function (this: {}, event: string, listener: Func) { + private _patchRemoveListener( + ee: PatchedEventEmitter, + original: UserFunction + ) { + return function (this: unknown, event: string, listener: Func) { if ( ee.__ot_listeners === undefined || ee.__ot_listeners[event] === undefined @@ -224,9 +233,9 @@ export class AsyncHooksContextManager implements ContextManager { */ private _patchRemoveAllListeners( ee: PatchedEventEmitter, - original: Function + original: UserFunction ) { - return function (this: {}, event: string) { + return function (this: unknown, event: string) { if ( ee.__ot_listeners === undefined || ee.__ot_listeners[event] === undefined @@ -247,11 +256,11 @@ export class AsyncHooksContextManager implements ContextManager { */ private _patchAddListener( ee: PatchedEventEmitter, - original: Function, + original: UserFunction, context: Context ) { const contextManager = this; - return function (this: {}, event: string, listener: Func) { + return function (this: unknown, event: string, listener: Func) { if (ee.__ot_listeners === undefined) ee.__ot_listeners = {}; let listeners = ee.__ot_listeners[event]; if (listeners === undefined) { diff --git a/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts b/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts index c985738ff4..21f22cc5e0 100644 --- a/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts +++ b/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts @@ -21,6 +21,9 @@ import { isListenerObject } from './util'; /* Key name to be used to save a context reference in Zone */ const ZONE_CONTEXT_KEY = 'OT_ZONE_CONTEXT'; +// eslint-disable-next-line @typescript-eslint/ban-types +type UserFunction = Function; + /** * ZoneContextManager * This module provides an easy functionality for tracing action between asynchronous operations in web. @@ -55,7 +58,10 @@ export class ZoneContextManager implements ContextManager { * @param target Function to be executed within the context * @param context A context (span) to be executed within target function */ - private _bindFunction(target: T, context: Context): T { + private _bindFunction( + target: T, + context: Context + ): T { const manager = this; const contextWrapper = function (this: any, ...args: unknown[]) { return manager.with(context, () => target.apply(this, args)); @@ -136,13 +142,13 @@ export class ZoneContextManager implements ContextManager { */ private _patchAddEventListener( target: TargetWithEvents, - original: Function, + original: UserFunction, context: Context ) { const contextManager = this; return function ( - this: {}, + this: unknown, event: string, listener: Func, opts?: any @@ -169,9 +175,9 @@ export class ZoneContextManager implements ContextManager { */ private _patchRemoveEventListener( target: TargetWithEvents, - original: Function + original: UserFunction ) { - return function (this: {}, event: string, listener: Func) { + return function (this: unknown, event: string, listener: Func) { if ( target.__ot_listeners === undefined || target.__ot_listeners[event] === undefined diff --git a/packages/opentelemetry-core/src/common/types.ts b/packages/opentelemetry-core/src/common/types.ts index eb84ba58b7..f409a6bac8 100644 --- a/packages/opentelemetry-core/src/common/types.ts +++ b/packages/opentelemetry-core/src/common/types.ts @@ -39,6 +39,6 @@ export interface TimeOriginLegacy { */ export interface ShimWrapped { __wrapped: boolean; - __unwrap: Function; - __original: Function; + __unwrap: () => unknown; + __original: () => unknown; } diff --git a/packages/opentelemetry-core/test/platform/BasePlugin.test.ts b/packages/opentelemetry-core/test/platform/BasePlugin.test.ts index 115c07627c..6607640dfc 100644 --- a/packages/opentelemetry-core/test/platform/BasePlugin.test.ts +++ b/packages/opentelemetry-core/test/platform/BasePlugin.test.ts @@ -46,7 +46,7 @@ describe('BasePlugin', () => { assert.strictEqual( (plugin['_moduleExports']![ 'externallyExportedFunction' - ] as Function)(), + ] as () => unknown)(), true ); } else { @@ -56,7 +56,7 @@ describe('BasePlugin', () => { }); }); -class TestPlugin extends BasePlugin<{ [key: string]: Function }> { +class TestPlugin extends BasePlugin<{ [key: string]: () => unknown }> { readonly moduleName = 'test-package'; readonly version = '0.1.0'; readonly _basedir = basedir; @@ -74,7 +74,7 @@ class TestPlugin extends BasePlugin<{ [key: string]: Function }> { }, }; - protected patch(): { [key: string]: Function } { + protected patch(): { [key: string]: () => unknown } { return this._moduleExports; } protected unpatch(): void {} diff --git a/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts b/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts index f9e08413ee..71172dc602 100644 --- a/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts +++ b/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts @@ -36,11 +36,11 @@ describe('BasePlugin', () => { }); }); -class TestPlugin extends BasePlugin<{ [key: string]: Function }> { +class TestPlugin extends BasePlugin<{ [key: string]: () => unknown }> { readonly moduleName = 'test-package'; readonly version = '0.1.0'; - patch(): { [key: string]: Function } { + patch(): { [key: string]: () => unknown } { return this._moduleExports; } diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/types.ts b/packages/opentelemetry-exporter-collector/src/platform/node/types.ts index b36b9a65ad..9a2b21fbed 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/types.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/types.ts @@ -14,9 +14,9 @@ * limitations under the License. */ -import * as grpc from 'grpc'; import { ReadableSpan } from '@opentelemetry/tracing'; -import { CollectorExporterError } from '../../types'; +import * as grpc from 'grpc'; +import { CollectorExporterError, opentelemetryProto } from '../../types'; /** * Queue item to be used to save temporary spans in case the GRPC service @@ -32,5 +32,10 @@ export interface GRPCQueueItem { * Trace Service Client for sending spans */ export interface TraceServiceClient extends grpc.Client { - export: (request: any, callback: Function) => {}; + export: ( + request: any, + callback: ( + err: opentelemetryProto.collector.trace.v1.ExportTraceServiceError + ) => void + ) => unknown; } diff --git a/packages/opentelemetry-metrics/src/MetricObservable.ts b/packages/opentelemetry-metrics/src/MetricObservable.ts index cd52774743..971b1c18a1 100644 --- a/packages/opentelemetry-metrics/src/MetricObservable.ts +++ b/packages/opentelemetry-metrics/src/MetricObservable.ts @@ -30,13 +30,13 @@ export class MetricObservable implements api.MetricObservable { } } - subscribe(subscriber: Function) { + subscribe(subscriber: (value: number) => void) { if (typeof subscriber === 'function') { this._subscribers.push(subscriber as Subscriber); } } - unsubscribe(subscriber?: Function) { + unsubscribe(subscriber?: (value: number) => void) { if (typeof subscriber === 'function') { for (let i = 0, j = this._subscribers.length; i < j; i++) { if (this._subscribers[i] === subscriber) { diff --git a/packages/opentelemetry-metrics/src/ObserverResult.ts b/packages/opentelemetry-metrics/src/ObserverResult.ts index 2ed593351d..356f75d6de 100644 --- a/packages/opentelemetry-metrics/src/ObserverResult.ts +++ b/packages/opentelemetry-metrics/src/ObserverResult.ts @@ -24,13 +24,16 @@ import { * Implementation of {@link TypeObserverResult} */ export class ObserverResult implements TypeObserverResult { - callbackObservers: Map = new Map(); + callbackObservers: Map number> = new Map< + Labels, + () => number + >(); observers: Map = new Map< Labels, MetricObservable >(); - observe(callback: Function | MetricObservable, labels: Labels): void { + observe(callback: (() => number) | MetricObservable, labels: Labels): void { if (typeof callback === 'function') { this.callbackObservers.set(labels, callback); } else { diff --git a/packages/opentelemetry-metrics/test/export/aggregators/histogram.test.ts b/packages/opentelemetry-metrics/test/export/aggregators/histogram.test.ts index efb1c6519a..1dd1383459 100644 --- a/packages/opentelemetry-metrics/test/export/aggregators/histogram.test.ts +++ b/packages/opentelemetry-metrics/test/export/aggregators/histogram.test.ts @@ -33,6 +33,7 @@ describe('HistogramAggregator', () => { }); it('should throw if no boundaries are defined', () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore assert.throws(() => new HistogramAggregator()); assert.throws(() => new HistogramAggregator([])); diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index 0b7de00518..7af91eb310 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -221,7 +221,7 @@ export class GrpcPlugin extends BasePlugin { original: | grpcTypes.handleCall | grpcTypes.ClientReadableStream, - self: {} + self: unknown ) { function patchedCallback( err: grpcTypes.ServiceError, @@ -259,7 +259,11 @@ export class GrpcPlugin extends BasePlugin { } plugin._tracer.bind(call); - return (original as Function).call(self, call, patchedCallback); + return (original as (...args: any[]) => unknown).call( + self, + call, + patchedCallback + ); } private _serverStreamAndBidiHandler( @@ -267,7 +271,7 @@ export class GrpcPlugin extends BasePlugin { span: Span, call: ServerCallWithMeta, original: grpcTypes.handleCall, - self: {} + self: unknown ) { let spanEnded = false; const endSpan = () => { diff --git a/packages/opentelemetry-plugin-grpc/src/utils.ts b/packages/opentelemetry-plugin-grpc/src/utils.ts index f9d5c6dd79..9a4b298c40 100644 --- a/packages/opentelemetry-plugin-grpc/src/utils.ts +++ b/packages/opentelemetry-plugin-grpc/src/utils.ts @@ -20,7 +20,7 @@ import * as grpcTypes from 'grpc'; // For types only // Equivalent to lodash _.findIndex export const findIndex: (args: T[], fn: (arg: T) => boolean) => number = ( args, - fn: Function + fn ) => { let index = -1; for (const arg of args) { diff --git a/packages/opentelemetry-plugin-http/package.json b/packages/opentelemetry-plugin-http/package.json index e59a6f3afe..b504ad5e7a 100644 --- a/packages/opentelemetry-plugin-http/package.json +++ b/packages/opentelemetry-plugin-http/package.json @@ -55,7 +55,6 @@ "@types/superagent": "^4.1.3", "axios": "^0.19.0", "codecov": "^3.6.1", - "eslint": "^6.8.0", "got": "^9.6.0", "gts": "^2.0.0", "mocha": "^7.1.2", diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index 9db4f207ab..ffa877cd46 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -272,7 +272,7 @@ export class HttpPlugin extends BasePlugin { ) { const plugin = this; return function incomingRequest( - this: {}, + this: unknown, event: string, ...args: unknown[] ): boolean { @@ -384,7 +384,7 @@ export class HttpPlugin extends BasePlugin { ): Func { const plugin = this; return function outgoingRequest( - this: {}, + this: unknown, options: url.URL | RequestOptions | string, ...args: unknown[] ): ClientRequest { diff --git a/packages/opentelemetry-plugin-http/src/utils.ts b/packages/opentelemetry-plugin-http/src/utils.ts index baa7adf90a..415615ce97 100644 --- a/packages/opentelemetry-plugin-http/src/utils.ts +++ b/packages/opentelemetry-plugin-http/src/utils.ts @@ -311,7 +311,7 @@ export const isValidOptionsType = (options: unknown): boolean => { */ export const isOpenTelemetryRequest = ( options: RequestOptions -): options is { headers: {} } & RequestOptions => { +): options is { headers: Record } & RequestOptions => { return !!(options && options.headers && options.headers[OT_REQUEST_HEADER]); }; diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts index 57cbe57427..c2550d9353 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts @@ -103,7 +103,7 @@ describe('Packages', () => { ); const result = await httpPackage.get(urlparsed.href!); if (!resHeaders) { - const res = result as AxiosResponse<{}>; + const res = result as AxiosResponse; resHeaders = res.headers; } const spans = memoryExporter.getFinishedSpans(); diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts index e22ec5c246..50b529b510 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts @@ -105,7 +105,7 @@ describe('Packages', () => { ); const result = await httpPackage.get(urlparsed.href!); if (!resHeaders) { - const res = result as AxiosResponse<{}>; + const res = result as AxiosResponse; resHeaders = res.headers; } const spans = memoryExporter.getFinishedSpans(); diff --git a/packages/opentelemetry-plugin-xml-http-request/src/types.ts b/packages/opentelemetry-plugin-xml-http-request/src/types.ts index 0fb63f676a..28830592aa 100644 --- a/packages/opentelemetry-plugin-xml-http-request/src/types.ts +++ b/packages/opentelemetry-plugin-xml-http-request/src/types.ts @@ -62,7 +62,7 @@ export interface XhrMem { entries: PerformanceResourceTiming[]; }; // callback to remove events from xhr once the span ends - callbackToRemoveEvents?: Function; + callbackToRemoveEvents?: () => unknown; } export type PropagateTraceHeaderCorsUrl = string | RegExp; diff --git a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts index 5248fd8b6f..a69edf30ff 100644 --- a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts +++ b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts @@ -40,7 +40,7 @@ class DummySpanExporter implements tracing.SpanExporter { shutdown() {} } -const getData = (url: string, callbackAfterSend: Function) => { +const getData = (url: string, callbackAfterSend: () => unknown) => { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve, reject) => { const req = new XMLHttpRequest(); diff --git a/packages/opentelemetry-web/src/StackContextManager.ts b/packages/opentelemetry-web/src/StackContextManager.ts index e91755fdeb..fb26722d4f 100644 --- a/packages/opentelemetry-web/src/StackContextManager.ts +++ b/packages/opentelemetry-web/src/StackContextManager.ts @@ -17,6 +17,9 @@ import { Context } from '@opentelemetry/api'; import { ContextManager } from '@opentelemetry/context-base'; +// eslint-disable-next-line @typescript-eslint/ban-types +type UserFunction = Function; + /** * Stack Context Manager for managing the state in web * it doesn't fully support the async calls though @@ -37,7 +40,7 @@ export class StackContextManager implements ContextManager { * @param target Function to be executed within the context * @param context */ - private _bindFunction( + private _bindFunction( target: T, context = Context.ROOT_CONTEXT ): T {