From 7146fb54a1d9a1e40705822aa304d18def8a168b Mon Sep 17 00:00:00 2001 From: bzhang0 Date: Sun, 7 Nov 2021 10:51:51 -0800 Subject: [PATCH 1/4] add a linter rule to check if exists in package.json --- .../rules/ts-package-json-sdktype-exists.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts new file mode 100644 index 000000000000..7f157299c83d --- /dev/null +++ b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * @file Rule to check if package.json includes 'sdk-type' + * @author Ben Zhang + */ + +import { Rule } from "eslint"; +import { getRuleMetaData, getVerifiers, stripPath } from "../utils"; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +export = { + meta: getRuleMetaData( + "ts-package-json-sdktype-exists", + "check if package.json includes 'sdk-type'", + "code" + ), + create: (context: Rule.RuleContext): Rule.RuleListener => { + const verifiers = getVerifiers(context, { + outer: "sdk-type", + }); + return stripPath(context.getFilename()) === "package.json" + ? ({ + // callback functions + + // check to see if package.json includes 'sdk-type' + "ExpressionStatement > ObjectExpression": verifiers.existsInFile, + } as Rule.RuleListener) + : {}; + } +}; From d098623eeaf40700950f6738768f72971e2b326a Mon Sep 17 00:00:00 2001 From: bzhang0 Date: Sun, 7 Nov 2021 10:55:31 -0800 Subject: [PATCH 2/4] Revert "add a linter rule to check if exists in package.json" This reverts commit 7146fb54a1d9a1e40705822aa304d18def8a168b. --- .../rules/ts-package-json-sdktype-exists.ts | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts deleted file mode 100644 index 7f157299c83d..000000000000 --- a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * @file Rule to check if package.json includes 'sdk-type' - * @author Ben Zhang - */ - -import { Rule } from "eslint"; -import { getRuleMetaData, getVerifiers, stripPath } from "../utils"; - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -export = { - meta: getRuleMetaData( - "ts-package-json-sdktype-exists", - "check if package.json includes 'sdk-type'", - "code" - ), - create: (context: Rule.RuleContext): Rule.RuleListener => { - const verifiers = getVerifiers(context, { - outer: "sdk-type", - }); - return stripPath(context.getFilename()) === "package.json" - ? ({ - // callback functions - - // check to see if package.json includes 'sdk-type' - "ExpressionStatement > ObjectExpression": verifiers.existsInFile, - } as Rule.RuleListener) - : {}; - } -}; From 90576853e436c1f061582858c1855156810ae813 Mon Sep 17 00:00:00 2001 From: bzhang0 Date: Sun, 7 Nov 2021 11:04:53 -0800 Subject: [PATCH 3/4] Add a linter rule to check if sdk-type exists in package.json --- .../src/configs/index.ts | 3 +- .../src/rules/index.ts | 4 +- .../rules/ts-package-json-sdktype-exists.ts | 35 ++ .../eslint-plugin-azure-sdk/tests/plugin.ts | 3 +- .../rules/ts-package-json-sdktype-exists.ts | 393 ++++++++++++++++++ 5 files changed, 435 insertions(+), 3 deletions(-) create mode 100644 common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts create mode 100644 common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-sdktype-exists.ts diff --git a/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts b/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts index 89977fe833cc..514861113d20 100644 --- a/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts +++ b/common/tools/eslint-plugin-azure-sdk/src/configs/index.ts @@ -48,7 +48,6 @@ export = { "@azure/azure-sdk/ts-no-const-enums": "warn", "@azure/azure-sdk/ts-no-namespaces": "error", "@azure/azure-sdk/ts-no-window": "error", - "@azure/azure-sdk/ts-package-json-sdktype": "error", "@azure/azure-sdk/ts-package-json-author": "error", "@azure/azure-sdk/ts-package-json-bugs": "error", "@azure/azure-sdk/ts-package-json-engine-is-present": "error", @@ -61,6 +60,8 @@ export = { "@azure/azure-sdk/ts-package-json-name": "error", "@azure/azure-sdk/ts-package-json-repo": "error", "@azure/azure-sdk/ts-package-json-required-scripts": "error", + "@azure/azure-sdk/ts-package-json-sdktype": "error", + "@azure/azure-sdk/ts-package-json-sdktype-exists": "error", "@azure/azure-sdk/ts-package-json-sideeffects": "error", "@azure/azure-sdk/ts-package-json-types": "error", "@azure/azure-sdk/ts-pagination-list": "error", diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/index.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/index.ts index 51c0cd8b11a6..081f48a7cf8a 100644 --- a/common/tools/eslint-plugin-azure-sdk/src/rules/index.ts +++ b/common/tools/eslint-plugin-azure-sdk/src/rules/index.ts @@ -33,7 +33,6 @@ import tsNoConstEnums from "./ts-no-const-enums"; import tsNoNamespaces from "./ts-no-namespaces"; import tsNoWindow from "./ts-no-window"; import tsPackageJsonAuthor from "./ts-package-json-author"; -import tsPackageJsonSdkType from "./ts-package-json-sdktype"; import tsPackageJsonBugs from "./ts-package-json-bugs"; import tsPackageJsonEngineIsPresent from "./ts-package-json-engine-is-present"; import tsPackageJsonFilesRequired from "./ts-package-json-files-required"; @@ -45,6 +44,8 @@ import tsPackageJsonModule from "./ts-package-json-module"; import tsPackageJsonName from "./ts-package-json-name"; import tsPackageJsonRepo from "./ts-package-json-repo"; import tsPackageJsonRequiredScripts from "./ts-package-json-required-scripts"; +import tsPackageJsonSdkType from "./ts-package-json-sdktype"; +import tsPackageJsonSdkTypeExists from "./ts-package-json-sdktype-exists"; import tsPackageJsonSideEffects from "./ts-package-json-sideeffects"; import tsPackageJsonTypes from "./ts-package-json-types"; import tsPaginationList from "./ts-pagination-list"; @@ -95,6 +96,7 @@ export = { "ts-package-json-name": tsPackageJsonName, "ts-package-json-repo": tsPackageJsonRepo, "ts-package-json-required-scripts": tsPackageJsonRequiredScripts, + "ts-package-json-sdktype-exists": tsPackageJsonSdkTypeExists, "ts-package-json-sideeffects": tsPackageJsonSideEffects, "ts-package-json-types": tsPackageJsonTypes, "ts-pagination-list": tsPaginationList, diff --git a/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts new file mode 100644 index 000000000000..7f157299c83d --- /dev/null +++ b/common/tools/eslint-plugin-azure-sdk/src/rules/ts-package-json-sdktype-exists.ts @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * @file Rule to check if package.json includes 'sdk-type' + * @author Ben Zhang + */ + +import { Rule } from "eslint"; +import { getRuleMetaData, getVerifiers, stripPath } from "../utils"; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +export = { + meta: getRuleMetaData( + "ts-package-json-sdktype-exists", + "check if package.json includes 'sdk-type'", + "code" + ), + create: (context: Rule.RuleContext): Rule.RuleListener => { + const verifiers = getVerifiers(context, { + outer: "sdk-type", + }); + return stripPath(context.getFilename()) === "package.json" + ? ({ + // callback functions + + // check to see if package.json includes 'sdk-type' + "ExpressionStatement > ObjectExpression": verifiers.existsInFile, + } as Rule.RuleListener) + : {}; + } +}; diff --git a/common/tools/eslint-plugin-azure-sdk/tests/plugin.ts b/common/tools/eslint-plugin-azure-sdk/tests/plugin.ts index 603dc30647fe..ac6b63ef5d27 100644 --- a/common/tools/eslint-plugin-azure-sdk/tests/plugin.ts +++ b/common/tools/eslint-plugin-azure-sdk/tests/plugin.ts @@ -41,7 +41,6 @@ const ruleList = [ "ts-no-namespaces", "ts-no-window", "ts-package-json-author", - "ts-package-json-sdktype", "ts-package-json-bugs", "ts-package-json-engine-is-present", "ts-package-json-files-required", @@ -53,6 +52,8 @@ const ruleList = [ "ts-package-json-name", "ts-package-json-repo", "ts-package-json-required-scripts", + "ts-package-json-sdktype", + "ts-package-json-sdktype-exists", "ts-package-json-sideeffects", "ts-package-json-types", "ts-pagination-list", diff --git a/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-sdktype-exists.ts b/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-sdktype-exists.ts new file mode 100644 index 000000000000..37e561003877 --- /dev/null +++ b/common/tools/eslint-plugin-azure-sdk/tests/rules/ts-package-json-sdktype-exists.ts @@ -0,0 +1,393 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * @file Testing the ts-package-json-sdktype-exists rule. + * @author Ben Zhang + */ + +import rule from "../../src/rules/ts-package-json-sdktype-exists"; +import { RuleTester } from "eslint"; + +//------------------------------------------------------------------------------ +// Example files +//------------------------------------------------------------------------------ + +const examplePackageGood = `{ + "name": "@azure/event-hubs", + "sdk-type": "client", + "version": "5.7.0-beta.1", + "description": "Azure Event Hubs SDK for JS.", + "author": "Microsoft Corporation", + "license": "MIT", + "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/eventhub/event-hubs/", + "repository": "github:Azure/azure-sdk-for-js", + "sideEffects": false, + "keywords": [ + "azure", + "cloud", + "event hubs", + "events", + "Azure" + ], + "bugs": { + "url": "https://github.com/Azure/azure-sdk-for-js/issues" + }, + "engines": { + "node": ">=12.0.0" + }, + "main": "./dist/index.js", + "module": "dist-esm/src/index.js", + "types": "./types/latest/event-hubs.d.ts", + "typesVersions": { + "<3.6": { + "types/latest/*": [ + "types/3.1/*" + ] + } + }, + "browser": { + "./dist-esm/src/util/runtimeInfo.js": "./dist-esm/src/util/runtimeInfo.browser.js", + "./dist-esm/test/public/utils/mockService.js": "./dist-esm/test/public/utils/mockService.browser.js" + }, + "files": [ + "dist/", + "dist-esm/src/", + "types/latest/", + "types/3.1/", + "README.md", + "LICENSE" + ], + "//metadata": { + "constantPaths": [ + { + "path": "src/util/constants.ts", + "prefix": "version" + } + ] + }, + "//sampleConfiguration": { + "extraFiles": { + "./samples-browser": [ + "browser" + ], + "./samples-express": [ + "express" + ] + }, + "skip": [ + "iothubConnectionString.js", + "iothubConnectionStringWebsockets.js", + "useWithIotHub.js", + "usingAadAuth.js" + ], + "productName": "Azure Event Hubs", + "productSlugs": [ + "azure", + "azure-event-hubs" + ], + "requiredResources": { + "Azure Event Hub": "https://docs.microsoft.com/azure/event-hubs/event-hubs-create" + } + }, + "dependencies": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-amqp": "^3.0.0", + "@azure/core-asynciterator-polyfill": "^1.0.0", + "@azure/core-auth": "^1.3.0", + "@azure/core-tracing": "1.0.0-preview.13", + "@azure/logger": "^1.0.0", + "buffer": "^6.0.0", + "is-buffer": "^2.0.3", + "jssha": "^3.1.0", + "process": "^0.11.10", + "rhea-promise": "^2.1.0", + "tslib": "^2.2.0", + "uuid": "^8.3.0" + }, + "devDependencies": { + "@azure/dev-tool": "^1.0.0", + "@azure/eslint-plugin-azure-sdk": "^3.0.0", + "@azure/identity": "^2.0.1", + "@azure/mock-hub": "^1.0.0", + "@azure/test-utils": "^1.0.0", + "@azure/test-utils-perf": "^1.0.0", + "@microsoft/api-extractor": "^7.18.11", + "@rollup/plugin-commonjs": "11.0.2", + "@rollup/plugin-inject": "^4.0.0", + "@rollup/plugin-json": "^4.0.0", + "@rollup/plugin-multi-entry": "^3.0.0", + "@rollup/plugin-node-resolve": "^8.0.0", + "@rollup/plugin-replace": "^2.2.0", + "@types/async-lock": "^1.1.0", + "@types/chai": "^4.1.6", + "@types/chai-as-promised": "^7.1.0", + "@types/chai-string": "^1.4.1", + "@types/debug": "^4.1.4", + "@types/long": "^4.0.0", + "@types/mocha": "^7.0.2", + "@types/node": "^12.0.0", + "@types/sinon": "^9.0.4", + "@types/uuid": "^8.0.0", + "@types/ws": "^7.2.4", + "assert": "^1.4.1", + "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", + "chai-exclude": "^2.0.2", + "chai-string": "^1.5.0", + "cross-env": "^7.0.2", + "debug": "^4.1.1", + "dotenv": "^8.2.0", + "downlevel-dts": "~0.4.0", + "eslint": "^7.15.0", + "esm": "^3.2.18", + "https-proxy-agent": "^5.0.0", + "karma": "^6.2.0", + "karma-chrome-launcher": "^3.0.0", + "karma-coverage": "^2.0.0", + "karma-edge-launcher": "^0.4.2", + "karma-env-preprocessor": "^0.1.1", + "karma-firefox-launcher": "^1.1.0", + "karma-ie-launcher": "^1.0.0", + "karma-junit-reporter": "^2.0.1", + "karma-mocha": "^2.0.1", + "karma-mocha-reporter": "^2.2.5", + "karma-sourcemap-loader": "^0.3.8", + "mocha": "^7.1.1", + "mocha-junit-reporter": "^1.18.0", + "moment": "^2.24.0", + "nyc": "^14.0.0", + "prettier": "^1.16.4", + "puppeteer": "^10.2.0", + "rimraf": "^3.0.0", + "rollup": "^1.16.3", + "rollup-plugin-shim": "^1.0.0", + "rollup-plugin-sourcemaps": "^0.4.2", + "rollup-plugin-terser": "^5.1.1", + "sinon": "^9.0.2", + "ts-node": "^10.0.0", + "typescript": "~4.2.0", + "ws": "^7.1.1", + "typedoc": "0.15.2" + } +}`; + +const examplePackageBad = `{ + "name": "@azure/event-hubs", + "version": "5.7.0-beta.1", + "description": "Azure Event Hubs SDK for JS.", + "author": "Microsoft Corporation", + "license": "MIT", + "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/eventhub/event-hubs/", + "repository": "github:Azure/azure-sdk-for-js", + "sideEffects": false, + "keywords": [ + "azure", + "cloud", + "event hubs", + "events", + "Azure" + ], + "bugs": { + "url": "https://github.com/Azure/azure-sdk-for-js/issues" + }, + "engines": { + "node": ">=12.0.0" + }, + "main": "./dist/index.js", + "module": "dist-esm/src/index.js", + "types": "./types/latest/event-hubs.d.ts", + "typesVersions": { + "<3.6": { + "types/latest/*": [ + "types/3.1/*" + ] + } + }, + "browser": { + "./dist-esm/src/util/runtimeInfo.js": "./dist-esm/src/util/runtimeInfo.browser.js", + "./dist-esm/test/public/utils/mockService.js": "./dist-esm/test/public/utils/mockService.browser.js" + }, + "files": [ + "dist/", + "dist-esm/src/", + "types/latest/", + "types/3.1/", + "README.md", + "LICENSE" + ], + "//metadata": { + "constantPaths": [ + { + "path": "src/util/constants.ts", + "prefix": "version" + } + ] + }, + "//sampleConfiguration": { + "extraFiles": { + "./samples-browser": [ + "browser" + ], + "./samples-express": [ + "express" + ] + }, + "skip": [ + "iothubConnectionString.js", + "iothubConnectionStringWebsockets.js", + "useWithIotHub.js", + "usingAadAuth.js" + ], + "productName": "Azure Event Hubs", + "productSlugs": [ + "azure", + "azure-event-hubs" + ], + "requiredResources": { + "Azure Event Hub": "https://docs.microsoft.com/azure/event-hubs/event-hubs-create" + } + }, + "dependencies": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-amqp": "^3.0.0", + "@azure/core-asynciterator-polyfill": "^1.0.0", + "@azure/core-auth": "^1.3.0", + "@azure/core-tracing": "1.0.0-preview.13", + "@azure/logger": "^1.0.0", + "buffer": "^6.0.0", + "is-buffer": "^2.0.3", + "jssha": "^3.1.0", + "process": "^0.11.10", + "rhea-promise": "^2.1.0", + "tslib": "^2.2.0", + "uuid": "^8.3.0" + }, + "devDependencies": { + "@azure/dev-tool": "^1.0.0", + "@azure/eslint-plugin-azure-sdk": "^3.0.0", + "@azure/identity": "^2.0.1", + "@azure/mock-hub": "^1.0.0", + "@azure/test-utils": "^1.0.0", + "@azure/test-utils-perf": "^1.0.0", + "@microsoft/api-extractor": "^7.18.11", + "@rollup/plugin-commonjs": "11.0.2", + "@rollup/plugin-inject": "^4.0.0", + "@rollup/plugin-json": "^4.0.0", + "@rollup/plugin-multi-entry": "^3.0.0", + "@rollup/plugin-node-resolve": "^8.0.0", + "@rollup/plugin-replace": "^2.2.0", + "@types/async-lock": "^1.1.0", + "@types/chai": "^4.1.6", + "@types/chai-as-promised": "^7.1.0", + "@types/chai-string": "^1.4.1", + "@types/debug": "^4.1.4", + "@types/long": "^4.0.0", + "@types/mocha": "^7.0.2", + "@types/node": "^12.0.0", + "@types/sinon": "^9.0.4", + "@types/uuid": "^8.0.0", + "@types/ws": "^7.2.4", + "assert": "^1.4.1", + "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", + "chai-exclude": "^2.0.2", + "chai-string": "^1.5.0", + "cross-env": "^7.0.2", + "debug": "^4.1.1", + "dotenv": "^8.2.0", + "downlevel-dts": "~0.4.0", + "eslint": "^7.15.0", + "esm": "^3.2.18", + "https-proxy-agent": "^5.0.0", + "karma": "^6.2.0", + "karma-chrome-launcher": "^3.0.0", + "karma-coverage": "^2.0.0", + "karma-edge-launcher": "^0.4.2", + "karma-env-preprocessor": "^0.1.1", + "karma-firefox-launcher": "^1.1.0", + "karma-ie-launcher": "^1.0.0", + "karma-junit-reporter": "^2.0.1", + "karma-mocha": "^2.0.1", + "karma-mocha-reporter": "^2.2.5", + "karma-sourcemap-loader": "^0.3.8", + "mocha": "^7.1.1", + "mocha-junit-reporter": "^1.18.0", + "moment": "^2.24.0", + "nyc": "^14.0.0", + "prettier": "^1.16.4", + "puppeteer": "^10.2.0", + "rimraf": "^3.0.0", + "rollup": "^1.16.3", + "rollup-plugin-shim": "^1.0.0", + "rollup-plugin-sourcemaps": "^0.4.2", + "rollup-plugin-terser": "^5.1.1", + "sinon": "^9.0.2", + "ts-node": "^10.0.0", + "typescript": "~4.2.0", + "ws": "^7.1.1", + "typedoc": "0.15.2" + } +}`; + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({ + parser: require.resolve("@typescript-eslint/parser"), + parserOptions: { + createDefaultProgram: true, + project: "./tsconfig.json" + } +}); + +ruleTester.run("ts-package-json-sdktype-exists", rule, { + valid: [ + { + // only the fields we care about + code: '{"sdk-type": "client"}', + filename: "package.json" + }, + { + // a full example package.json (taken from https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/eventhub/event-hubs/package.json with "scripts" removed for testing purposes) + code: examplePackageGood, + filename: "package.json" + }, + { + // incorrect format but in a file we don't care about + code: '{"types": "typings/index.d.ts"}', + filename: "not_package.json" + } + ], + invalid: [ + { + code: '{"not-sdk-type": "client"}', + filename: "package.json", + errors: [ + { + message: "sdk-type does not exist at the outermost level" + } + ] + }, + { + // sdk-type is in a nested object + code: '{"outer": {"sdk-type": "client"}}', + filename: "package.json", + errors: [ + { + message: "sdk-type does not exist at the outermost level" + } + ] + }, + { + code: examplePackageBad, + filename: "package.json", + errors: [ + { + message: "sdk-type does not exist at the outermost level" + } + ] + } + ] +}); From c651e91a8a81787484f44faaf19886aef806f658 Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Sun, 7 Nov 2021 12:50:20 -0800 Subject: [PATCH 4/4] fixed some errors with rush lint after calling rush rebuild --- sdk/keyvault/keyvault-common/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/keyvault/keyvault-common/package.json b/sdk/keyvault/keyvault-common/package.json index a19e03450391..79039e353ec0 100644 --- a/sdk/keyvault/keyvault-common/package.json +++ b/sdk/keyvault/keyvault-common/package.json @@ -1,6 +1,7 @@ { "name": "@azure/keyvault-common", "sideEffects": false, + "sdk-type": "client", "private": true, "author": "Microsoft Corporation", "version": "1.0.0",