Skip to content

Commit

Permalink
Use TS ESLint's no-invalid-this instead (Azure#14458)
Browse files Browse the repository at this point in the history
Implements the approach described here: Azure#14406 (comment) to linting `this` references. Reminder to myself: look for typescript-eslint alternatives when the behavior of eslint is not satisfactory.
  • Loading branch information
deyaaeldeen authored and vindicatesociety committed Apr 26, 2021
1 parent b7a6713 commit 0770bb6
Show file tree
Hide file tree
Showing 104 changed files with 902 additions and 968 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export default {
],
"no-console": "off",
"no-dupe-class-members": "off",
"no-invalid-this": "off",
"no-empty": "error",
"no-fallthrough": "error",
"no-invalid-this": "error",
"@typescript-eslint/no-invalid-this": "error",
"@typescript-eslint/no-require-imports": "error",
"no-restricted-imports": ["error", { paths: ["rhea", "rhea/.*"] }],
"no-return-await": "error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { assert } from "chai";
import { Context } from "mocha";
import { Recorder } from "@azure/test-utils-recorder";
import { AnomalyDetectorClient } from "../src/AnomalyDetectorClient";
import { AzureKeyCredential } from "@azure/core-auth";
Expand All @@ -19,11 +20,9 @@ describe("AnomalyDetectorClient", () => {
let recorder: Recorder;
const apiKey = new AzureKeyCredential(testEnv.ANOMALY_DETECTOR_API_KEY);

beforeEach(
/** @this Mocha.Context */ function() {
({ recorder, client } = createRecordedAnomalyDetectorClient(this, apiKey));
}
);
beforeEach(function(this: Context) {
({ recorder, client } = createRecordedAnomalyDetectorClient(this, apiKey));
});

afterEach(async function() {
if (recorder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { assert, use as chaiUse } from "chai";
import { Context } from "mocha";
import chaiPromises from "chai-as-promised";
chaiUse(chaiPromises);

Expand All @@ -14,7 +15,7 @@ import { verifyAttestationToken } from "../utils/helpers";
describe("[AAD] Attestation Client", function() {
let recorder: Recorder;

beforeEach(/** @this Mocha.Context */ function() {
beforeEach(function(this: Context) {
recorder = createRecorder(this);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { assert, use as chaiUse } from "chai";
import { Context } from "mocha";
import chaiPromises from "chai-as-promised";
chaiUse(chaiPromises);

Expand All @@ -14,7 +15,7 @@ import { verifyAttestationToken } from "../utils/helpers";
describe("PolicyGetSetTests ", function() {
let recorder: Recorder;

beforeEach(/** @this Mocha.Context */ function() {
beforeEach(function(this: Context) {
recorder = createRecorder(this);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { assert, use as chaiUse } from "chai";
import { Context } from "mocha";
import chaiPromises from "chai-as-promised";
chaiUse(chaiPromises);

Expand All @@ -13,7 +14,7 @@ import { verifyAttestationToken } from "../utils/helpers";
describe("PolicyManagementTests ", function() {
let recorder: Recorder;

beforeEach(/** @this Mocha.Context */ function() {
beforeEach(function(this: Context) {
recorder = createRecorder(this);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { assert, use as chaiUse } from "chai";
import { Context } from "mocha";
import chaiPromises from "chai-as-promised";
chaiUse(chaiPromises);

Expand All @@ -13,7 +14,7 @@ import { Buffer } from "../utils/Buffer";
describe("TokenCertTests", function() {
let recorder: Recorder;

beforeEach(/** @this Mocha.Context */ function() {
beforeEach(function(this: Context) {
recorder = createRecorder(this);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,34 @@ describe.skip("ContainerRegistryClient functional tests", function() {
// NOTE: use of "function" and not ES6 arrow-style functions with the
// beforeEach hook is IMPORTANT due to the use of `this` in the function
// body.
beforeEach(
/** @this Mocha.Context */ function(this: Context) {
// The recorder has some convenience methods, and we need to store a
// reference to it so that we can `stop()` the recorder later in the
// `afterEach` hook.
recorder = record(this, {
// == Recorder Environment Setup == Add the replaceable variables from
// above
replaceableVariables,
beforeEach(function(this: Context) {
// The recorder has some convenience methods, and we need to store a
// reference to it so that we can `stop()` the recorder later in the
// `afterEach` hook.
recorder = record(this, {
// == Recorder Environment Setup == Add the replaceable variables from
// above
replaceableVariables,

// We don't use this in the template, but if we had any query parameters
// we wished to discard, we could add them here
queryParametersToSkip: [],
// We don't use this in the template, but if we had any query parameters
// we wished to discard, we could add them here
queryParametersToSkip: [],

// Finally, we need to remove the AAD `access_token` from any requests.
// This is very important, as it cannot be removed using environment
// variable or query parameter replacement. The
// `customizationsOnRecordings` field allows us to make arbitrary
// replacements within recordings.
customizationsOnRecordings: [
(recording: any): any =>
recording.replace(/"access_token":"[^"]*"/g, `"access_token":"access_token"`)
]
});
// Finally, we need to remove the AAD `access_token` from any requests.
// This is very important, as it cannot be removed using environment
// variable or query parameter replacement. The
// `customizationsOnRecordings` field allows us to make arbitrary
// replacements within recordings.
customizationsOnRecordings: [
(recording: any): any =>
recording.replace(/"access_token":"[^"]*"/g, `"access_token":"access_token"`)
]
});

// We'll be able to refer to the instantiated `client` in tests, since we
// initialize it before each test
client = createTestClient();
}
);
// We'll be able to refer to the instantiated `client` in tests, since we
// initialize it before each test
client = createTestClient();
});

// After each test, we need to stop the recording.
afterEach(async function() {
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/core-http/test/defaultHttpClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* eslint-disable no-unused-expressions */

import { assert, AssertionError } from "chai";
import { Context } from "mocha";
import * as sinon from "sinon";
import { AbortController } from "@azure/abort-controller";
import "chai/register-should";
Expand Down Expand Up @@ -236,7 +237,7 @@ describe("defaultHttpClient", function() {
}
});

it("should give a graceful error for nonexistent hosts", /** @this Mocha.Context */ async function() {
it("should give a graceful error for nonexistent hosts", async function(this: Context) {
// Increase timeout to give the request time to fail
this.timeout(10000);
const requestUrl = "http://fake.domain";
Expand Down
3 changes: 0 additions & 3 deletions sdk/core/core-lro/test/utils/testOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface TestOperationState extends PollOperationState<string> {

export interface TestOperation extends PollOperation<TestOperationState, string> {}

/** @this TestOperation */
async function update(
this: TestOperation,
options: {
Expand Down Expand Up @@ -70,7 +69,6 @@ async function update(
return makeOperation(newState);
}

/** @this TestOperation */
async function cancel(
this: TestOperation,
options: { abortSignal?: AbortSignal } = {}
Expand Down Expand Up @@ -101,7 +99,6 @@ async function cancel(
});
}

/** @this TestOperation */
function toString(this: TestOperation): string {
return JSON.stringify({
state: this.state
Expand Down
2 changes: 0 additions & 2 deletions sdk/core/logger/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ function createDebugger(namespace: string): Debugger {
return newDebugger;
}

/** @this Debugger */
function destroy(this: Debugger): boolean {
const index = debuggers.indexOf(this);
if (index >= 0) {
Expand All @@ -165,7 +164,6 @@ function destroy(this: Debugger): boolean {
return false;
}

/** @this Debugger */
function extend(this: Debugger, namespace: string): Debugger {
const newDebugger = createDebugger(`${this.namespace}:${namespace}`);
newDebugger.log = this.log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,19 @@ export class DocumentProducer {
}

public fetchFunction = async (options: FeedOptions): Promise<Response<Resource>> => {
// eslint-disable-next-line no-invalid-this
const path = getPathFromLink(this.collectionLink, ResourceType.item);
// eslint-disable-next-line no-invalid-this

const id = getIdFromLink(this.collectionLink);

// eslint-disable-next-line no-invalid-this
return this.clientContext.queryFeed({
path,
resourceType: ResourceType.item,
resourceId: id,
resultFn: (result: any) => result.Documents,
// eslint-disable-next-line no-invalid-this

query: this.query,
options,
// eslint-disable-next-line no-invalid-this

partitionKeyRangeId: this.targetPartitionKeyRange["id"]
});
};
Expand Down
6 changes: 4 additions & 2 deletions sdk/cosmosdb/cosmos/test/internal/session.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Context } from "mocha";
import { Suite } from "mocha";
import * as sinon from "sinon";
import { ClientContext } from "../../src";
import { OperationType, ResourceType, trimSlashes } from "../../src/common";
Expand Down Expand Up @@ -28,7 +30,7 @@ function getCollection2TokenMap(
return (sessionContainer as any).collectionResourceIdToSessionTokens;
}

describe("Session Token", /** @this Mocha.Context */ function() {
describe("Session Token", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 20000);

const containerId = "sessionTestColl";
Expand Down Expand Up @@ -304,7 +306,7 @@ describe("Session Token", /** @this Mocha.Context */ function() {
spy.restore();
});

it("validate 'lsn not caught up' error for higher lsn and clearing session token", /** @this Mocha.Context */ async function() {
it("validate 'lsn not caught up' error for higher lsn and clearing session token", async function(this: Context) {
this.retries(2);
const database = await getTestDatabase("session test", client);

Expand Down
3 changes: 2 additions & 1 deletion sdk/cosmosdb/cosmos/test/internal/unit/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Licensed under the MIT license.

import { getAuthorizationTokenUsingResourceTokens } from "../../../src/auth";
import { Suite } from "mocha";
import assert from "assert";

describe("NodeJS CRUD Tests", /** @this Mocha.Context */ function() {
describe("NodeJS CRUD Tests", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);

it("should find exact match", async function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { CosmosClient, PermissionMode } from "../../../src";
import { PermissionDefinition } from "../../../src/";
import { endpoint, masterKey } from "../common/_testConfig";
Expand All @@ -11,7 +12,7 @@ import {
removeAllDatabases
} from "../common/TestHelpers";

describe("NodeJS CRUD Tests", /** @this Mocha.Context */ function() {
describe("NodeJS CRUD Tests", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
beforeEach(async function() {
await removeAllDatabases();
Expand Down
3 changes: 2 additions & 1 deletion sdk/cosmosdb/cosmos/test/public/functional/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { Agent } from "http";
import { CosmosClient } from "../../../src";
import { endpoint, masterKey } from "../common/_testConfig";
Expand All @@ -13,7 +14,7 @@ import {
import AbortController from "node-abort-controller";
import { UsernamePasswordCredential } from "@azure/identity";

describe("NodeJS CRUD Tests", /** @this Mocha.Context */ function() {
describe("NodeJS CRUD Tests", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 20000);

describe("Validate client request timeout", function() {
Expand Down
3 changes: 2 additions & 1 deletion sdk/cosmosdb/cosmos/test/public/functional/conflict.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { removeAllDatabases, getTestContainer } from "../common/TestHelpers";

describe("Conflicts", /** @this Mocha.Context */ function() {
describe("Conflicts", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
beforeEach(async function() {
await removeAllDatabases();
Expand Down
3 changes: 2 additions & 1 deletion sdk/cosmosdb/cosmos/test/public/functional/container.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { Constants, ContainerResponse } from "../../../src";
import { ContainerDefinition, Database, Container } from "../../../src";
import { ContainerRequest } from "../../../src";
Expand All @@ -14,7 +15,7 @@ import {
import { SpatialType } from "../../../src";
import { GeospatialType } from "../../../src";

describe("Containers", /** @this Mocha.Context */ function() {
describe("Containers", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
beforeEach(async function() {
await removeAllDatabases();
Expand Down
3 changes: 2 additions & 1 deletion sdk/cosmosdb/cosmos/test/public/functional/database.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { CosmosClient, DatabaseDefinition, Database } from "../../../src";
import { endpoint, masterKey } from "../common/_testConfig";
import {
Expand All @@ -13,7 +14,7 @@ import { DatabaseRequest } from "../../../src";

const client = new CosmosClient({ endpoint, key: masterKey });

describe("NodeJS CRUD Tests", /** @this Mocha.Context */ function() {
describe("NodeJS CRUD Tests", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
beforeEach(async function() {
await removeAllDatabases();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import assert from "assert";
import { Context } from "mocha";
import { Suite } from "mocha";
import { CosmosClient } from "../../../src";
import { endpoint, masterKey } from "../common/_testConfig";

const client = new CosmosClient({ endpoint, key: masterKey });

describe("NodeJS CRUD Tests", /** @this Mocha.Context */ function() {
describe("NodeJS CRUD Tests", function(this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
beforeEach(
/** @this Mocha.Context */ async function() {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
}
);
beforeEach(async function(this: Context) {
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
});

describe("validate database account functionality", function() {
it("nativeApi Should get database account successfully name based", async function() {
Expand Down
Loading

0 comments on commit 0770bb6

Please sign in to comment.