Skip to content
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

[Abort Controller] Update linting and fix linting errors #11269

Merged
merged 14 commits into from
Sep 21, 2020
4 changes: 2 additions & 2 deletions sdk/core/abort-controller/api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "types/src/aborter.d.ts",
"mainEntryPointFilePath": "types/src/index.d.ts",
"docModel": {
"enabled": true
},
Expand All @@ -11,7 +11,7 @@
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./types/aborter.d.ts"
"publicTrimmedFilePath": "./types/abort-controller.d.ts"
},
"messages": {
"tsdocMessageReporting": {
Expand Down
24 changes: 13 additions & 11 deletions sdk/core/abort-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"version": "1.0.1",
"description": "Microsoft Azure SDK for JavaScript - Aborter",
"main": "./dist/index.js",
"module": "./dist-esm/src/aborter.js",
"module": "dist-esm/src/index.js",
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:es6": "tsc -p tsconfig.json",
"build:nodebrowser": "rollup -c 2>&1",
"build:nodebrowser": "rollup -c 2>&1 && api-extractor run --local",
"build:test": "rollup -c rollup.test.config.js 2>&1",
"build": "npm run build:es6 && npm run build:nodebrowser",
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
Expand All @@ -19,8 +19,8 @@
"integration-test:browser": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"lint:fix": "eslint \"src/**/*.ts\" \"test/**/*.ts\" -c ../../.eslintrc.old.json --fix --fix-type [problem,suggestion]",
"lint": "eslint -c ../../.eslintrc.old.json src test --ext .ts -f html -o abort-controller-lintReport.html || exit 0",
"lint:fix": "eslint -c ../../../.eslintrc.json package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved
"lint": "eslint -c ../../../.eslintrc.json package.json api-extractor.json src test --ext .ts",
"pack": "npm pack 2>&1",
"prebuild": "npm run clean",
"pretest": "npm run build:test",
Expand All @@ -31,21 +31,21 @@
"unit-test:node": "cross-env TS_NODE_FILES=true TS_NODE_COMPILER_OPTIONS=\"{\\\"module\\\": \\\"commonjs\\\"}\" mocha --require ts-node/register --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace --no-timeouts test/*.spec.ts",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
},
"types": "./types/src/aborter.d.ts",
"types": "./types/abort-controller.d.ts",
"engine": {
"node": ">=8.0.0"
},
"files": [
"dist/",
"dist-esm/src/",
"types/src",
"types/abort-controller.d.ts",
"README.md",
"LICENSE"
],
"repository": {
"type": "git",
"url": "git+https://github.com/Azure/azure-sdk-for-js.git"
"engines": {
"node": ">=8.0.0"
},
"repository": "github:Azure/azure-sdk-for-js",
"keywords": [
"azure",
"aborter",
Expand All @@ -54,19 +54,21 @@
"node.js",
"typescript",
"javascript",
"browser"
"browser",
"cloud"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/core/abort-controller",
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/core/abort-controller/README.md",
"sideEffects": false,
"dependencies": {
"tslib": "^2.0.0"
},
"devDependencies": {
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@microsoft/api-extractor": "7.7.11",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-multi-entry": "^3.0.0",
Expand Down
9 changes: 5 additions & 4 deletions sdk/core/abort-controller/review/abort-controller.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AbortController {
constructor(parentSignals?: AbortSignalLike[]);
constructor(...parentSignals: AbortSignalLike[]);
abort(): void;
readonly signal: AbortSignal;
get signal(): AbortSignal;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically downlevel TypeScript get accessor signatures to readonly properties. Was that something we were doing on the older types file that didn't get carried in?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize this was an accessor before, yes we should use downlevel-dts here if so

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willmtemple and @xirzec
So to be clear, we should not ship a get accessor at all? if so, why?
If not, would #11319 take care of this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to avoid them since:

  1. They have TS compatibility concerns before/after 3.7
  2. They are harder for developers to reason about, as property accesses are usually assumed to be cheap and without side-effects.

There are times where for compat purposes we can't avoid them (and maybe this is one of those times) but for new code I'd like to steer away from using getters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I turned off extract-api again. We already have an issue opened for it: #10320.

static timeout(ms: number): AbortSignal;
}

Expand All @@ -21,10 +21,11 @@ export class AbortError extends Error {
// @public
export class AbortSignal implements AbortSignalLike {
constructor();
readonly aborted: boolean;
get aborted(): boolean;
addEventListener(_type: "abort", listener: (this: AbortSignalLike, ev: any) => any): void;
static readonly none: AbortSignal;
onabort?: (ev?: Event) => any;
dispatchEvent(_event: Event): boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other four changes seem reasonable if weird, but here dispatchEvent has just appeared out of thin air. Did we modify this signature somewhere to export it?

static get none(): AbortSignal;
onabort: ((ev?: Event) => any) | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we change an undefined to null? The change in this signature doesn't seem compatible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also seems like the forward-declaration of Event fell out from shims-public.d.ts. API Extractor doesn't yet support rolling up file-based references, so we'll have to work around it. I think core-http has a solution of some kind, but I can't remember exactly. I think @jeremymeng knows more.

removeEventListener(_type: "abort", listener: (this: AbortSignalLike, ev: any) => any): void;
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/abort-controller/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import sourcemaps from "rollup-plugin-sourcemaps";

const pkg = require("./package.json");
const depNames = Object.keys(pkg.dependencies);
const input = "./dist-esm/src/aborter.js";
const input = "./dist-esm/src/index.js";
const production = process.env.NODE_ENV === "production";

export function nodeConfig(test = false) {
Expand Down
4 changes: 4 additions & 0 deletions sdk/core/abort-controller/src/AbortController.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { AbortSignal, abortSignal, AbortSignalLike } from "./AbortSignal";

/**
Expand Down Expand Up @@ -76,6 +79,7 @@ export class AbortController {
}
// coerce parentSignals into an array
if (!Array.isArray(parentSignals)) {
// eslint-disable-next-line prefer-rest-params
parentSignals = arguments;
deyaaeldeen marked this conversation as resolved.
Show resolved Hide resolved
}
for (const parentSignal of parentSignals) {
Expand Down
4 changes: 4 additions & 0 deletions sdk/core/abort-controller/src/AbortSignal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./shims-public.d.ts" />
type AbortEventListener = (this: AbortSignalLike, ev?: any) => any;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

// Changes to Aborter
// * Rename Aborter to AbortSignal
// * Remove withValue and getValue - async context should be solved differently/wholistically, not tied to cancellation
Expand Down
3 changes: 3 additions & 0 deletions sdk/core/abort-controller/src/shims-public.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

// forward declaration of Event in case DOM libs are not present.
interface Event {}
17 changes: 10 additions & 7 deletions sdk/core/abort-controller/test/aborter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as assert from "assert";
import { AbortController, AbortSignal, AbortError } from "../src/aborter";
import { AbortController, AbortSignal, AbortError } from "../src";

describe("AbortController", () => {
function doAsyncOperation(aborter: AbortSignal, runningTimeinMs: number = 100): Promise<number> {
const s = Date.now();
return new Promise((res, rej) => {
return new Promise((resolve, reject) => {
// check status every 10 ms.
const handle = setInterval(() => {
// check if we're aborted.
if (aborter.aborted) {
clearInterval(handle);
return rej(new AbortError());
return reject(new AbortError());
}

// if we're completed, resolve.
if (Date.now() - s > runningTimeinMs) {
clearInterval(handle);
return res();
return resolve();
}

// else, continue trying.
Expand All @@ -34,7 +37,7 @@ describe("AbortController", () => {
const response = doAsyncOperation(aborter);
controller.abort();
try {
let rs = await response;
const rs = await response;
console.log("got result", rs);
assert.fail();
} catch (err) {
Expand All @@ -48,7 +51,7 @@ describe("AbortController", () => {
const response = doAsyncOperation(aborter, 500);
setTimeout(() => controller.abort(), 50);
try {
let r = await response;
const r = await response;
console.log("got, r", r);
assert.fail();
} catch (err) {
Expand Down Expand Up @@ -83,7 +86,7 @@ describe("AbortController", () => {
it("should invoke abort listener callbacks when aborting", async () => {
const controller = new AbortController();
const aborter = controller.signal;
let s: string[] = [];
const s: string[] = [];
try {
aborter.addEventListener("abort", () => {
s.push("aborted");
Expand Down