From 65df36c95b3ee7f9c7f4a198700df1b7eb25ce0c Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 8 Nov 2021 16:54:29 +0800 Subject: [PATCH] chore: align linter rules with repo open-telemetry/opentelemetry-js (#127) --- api/.eslintrc.js | 127 +++++++++++++++------------- api/package.json | 9 +- api/src/api/diag.ts | 9 +- api/src/diag/ComponentLogger.ts | 4 +- api/src/diag/consoleLogger.ts | 12 +-- api/test/diag/consoleLogger.test.ts | 2 + 6 files changed, 83 insertions(+), 80 deletions(-) diff --git a/api/.eslintrc.js b/api/.eslintrc.js index cf8bb3c246..c78e7b660d 100644 --- a/api/.eslintrc.js +++ b/api/.eslintrc.js @@ -1,64 +1,69 @@ module.exports = { - "env": { - "mocha": true, - "commonjs": true, - "shared-node-browser": true - }, - plugins: [ - "@typescript-eslint", - "header" + plugins: [ + "@typescript-eslint", + "header", + "node" + ], + extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + parser: "@typescript-eslint/parser", + parserOptions: { + "project": "./tsconfig.json" + }, + rules: { + "quotes": [2, "single", { "avoidEscape": true }], + "@typescript-eslint/no-floating-promises": 2, + "@typescript-eslint/no-this-alias": "off", + "brace-style": ["error", "1tbs"], + "eqeqeq": [ + "error", + "smart" ], - extends: [ - "./node_modules/gts", + "prefer-rest-params": "off", + "@typescript-eslint/naming-convention": [ + "error", + { + "selector": "memberLike", + "modifiers": ["private", "protected"], + "format": ["camelCase"], + "leadingUnderscore": "require" + } ], - parser: "@typescript-eslint/parser", - parserOptions: { - "project": [ - "./tsconfig.json" - ] - }, - ignorePatterns: [ - 'build', - ], - rules: { - "@typescript-eslint/no-this-alias": "off", - "eqeqeq": [ - "error", - "smart" - ], - "prefer-rest-params": "off", - "@typescript-eslint/naming-convention": [ - "error", - { - "selector": "memberLike", - "modifiers": ["private", "protected"], - "format": ["camelCase"], - "leadingUnderscore": "require" - } - ], - "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "args": "after-used" }], - "@typescript-eslint/no-inferrable-types": ["error", { ignoreProperties: true }], - "arrow-parens": ["error", "as-needed"], - "prettier/prettier": ["error", { "singleQuote": true, "arrowParens": "avoid" }], - "prefer-spread": "off", - "node/no-deprecated-api": ["warn"], - "header/header": [2, "block", [{ - pattern: / \* Copyright The OpenTelemetry Authors[\r\n]+ \*[\r\n]+ \* Licensed under the Apache License, Version 2\.0 \(the \"License\"\);[\r\n]+ \* you may not use this file except in compliance with the License\.[\r\n]+ \* You may obtain a copy of the License at[\r\n]+ \*[\r\n]+ \* https:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0[\r\n]+ \*[\r\n]+ \* Unless required by applicable law or agreed to in writing, software[\r\n]+ \* distributed under the License is distributed on an \"AS IS\" BASIS,[\r\n]+ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.[\r\n]+ \* See the License for the specific language governing permissions and[\r\n]+ \* limitations under the License\./gm, - template: - `\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n ` - }]] - }, - overrides: [ - { - "files": ["test/**/*.ts"], - "rules": { - "no-empty": "off", - "@typescript-eslint/ban-ts-ignore": "off", - "@typescript-eslint/no-empty-function": "off", - "@typescript-eslint/no-explicit-any": "off", - "@typescript-eslint/no-unused-vars": "off", - "@typescript-eslint/no-var-requires": "off" - } - } - ] -} + "no-console": "error", + "no-shadow": "off", + "@typescript-eslint/no-shadow": ["warn"], + "@typescript-eslint/no-unused-vars": ["error", {"argsIgnorePattern": "^_", "args": "after-used"}], + "@typescript-eslint/no-inferrable-types": ["error", { ignoreProperties: true }], + "@typescript-eslint/no-empty-function": ["off"], + "@typescript-eslint/ban-types": ["warn", { + "types": { + "Function": null, + } + }], + "@typescript-eslint/no-shadow": ["warn"], + "arrow-parens": ["error", "as-needed"], + "node/no-deprecated-api": ["warn"], + "header/header": [2, "block", [{ + pattern: / \* Copyright The OpenTelemetry Authors[\r\n]+ \*[\r\n]+ \* Licensed under the Apache License, Version 2\.0 \(the \"License\"\);[\r\n]+ \* you may not use this file except in compliance with the License\.[\r\n]+ \* You may obtain a copy of the License at[\r\n]+ \*[\r\n]+ \* https:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0[\r\n]+ \*[\r\n]+ \* Unless required by applicable law or agreed to in writing, software[\r\n]+ \* distributed under the License is distributed on an \"AS IS\" BASIS,[\r\n]+ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.[\r\n]+ \* See the License for the specific language governing permissions and[\r\n]+ \* limitations under the License\./gm, + template: + `\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n ` + }]] + }, + overrides: [ + { + "files": ["test/**/*.ts"], + "rules": { + "no-empty": "off", + "@typescript-eslint/ban-ts-ignore": "off", + "@typescript-eslint/no-empty-function": "off", + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-floating-promises": 1, + "@typescript-eslint/no-unused-vars": "off", + "@typescript-eslint/no-var-requires": "off", + "@typescript-eslint/no-shadow": ["off"], + "@typescript-eslint/no-floating-promises": ["off"], + "@typescript-eslint/no-non-null-assertion": ["off"], + "@typescript-eslint/explicit-module-boundary-types": ["off"] + } + } + ] +}; diff --git a/api/package.json b/api/package.json index 904b0849e1..1da8fe8868 100644 --- a/api/package.json +++ b/api/package.json @@ -61,15 +61,14 @@ "@types/node": "14.17.4", "@types/sinon": "10.0.2", "@types/webpack-env": "1.16.0", - "@typescript-eslint/eslint-plugin": "4.28.1", - "@typescript-eslint/parser": "4.28.1", + "@typescript-eslint/eslint-plugin": "5.0.0", + "@typescript-eslint/parser": "5.0.0", "codecov": "3.8.2", "dpdm": "3.7.1", - "eslint": "7.30.0", + "eslint": "7.32.0", "eslint-plugin-header": "3.1.1", - "eslint-plugin-import": "2.23.4", + "eslint-plugin-node": "11.1.0", "gh-pages": "3.2.0", - "gts": "3.1.0", "istanbul-instrumenter-loader": "3.0.1", "karma": "5.2.3", "karma-chrome-launcher": "3.1.0", diff --git a/api/src/api/diag.ts b/api/src/api/diag.ts index 32c28b1dfb..3fd1027a62 100644 --- a/api/src/api/diag.ts +++ b/api/src/api/diag.ts @@ -52,16 +52,11 @@ export class DiagAPI implements DiagLogger { */ private constructor() { function _logProxy(funcName: keyof DiagLogger): DiagLogFunction { - return function () { + return function (...args) { const logger = getGlobal('diag'); // shortcut if logger not set if (!logger) return; - return logger[funcName].apply( - logger, - // work around Function.prototype.apply types - // eslint-disable-next-line @typescript-eslint/no-explicit-any - arguments as any - ); + return logger[funcName](...args); }; } diff --git a/api/src/diag/ComponentLogger.ts b/api/src/diag/ComponentLogger.ts index aaddb9de50..2253501863 100644 --- a/api/src/diag/ComponentLogger.ts +++ b/api/src/diag/ComponentLogger.ts @@ -15,7 +15,7 @@ */ import { getGlobal } from '../internal/global-utils'; -import { ComponentLoggerOptions, DiagLogger } from './types'; +import { ComponentLoggerOptions, DiagLogger, DiagLogFunction } from './types'; /** * Component Logger which is meant to be used as part of any component which @@ -66,5 +66,5 @@ function logProxy( } args.unshift(namespace); - return logger[funcName].apply(logger, args); + return logger[funcName](...(args as Parameters)); } diff --git a/api/src/diag/consoleLogger.ts b/api/src/diag/consoleLogger.ts index 8e18e0fac5..76e9855732 100644 --- a/api/src/diag/consoleLogger.ts +++ b/api/src/diag/consoleLogger.ts @@ -16,7 +16,8 @@ import { DiagLogger, DiagLogFunction } from './types'; -const consoleMap: { n: keyof DiagLogger; c: keyof Console }[] = [ +type ConsoleMapKeys = 'error' | 'warn' | 'info' | 'debug' | 'trace'; +const consoleMap: { n: keyof DiagLogger; c: ConsoleMapKeys }[] = [ { n: 'error', c: 'error' }, { n: 'warn', c: 'warn' }, { n: 'info', c: 'info' }, @@ -31,20 +32,21 @@ const consoleMap: { n: keyof DiagLogger; c: keyof Console }[] = [ */ export class DiagConsoleLogger implements DiagLogger { constructor() { - function _consoleFunc(funcName: keyof Console): DiagLogFunction { - return function () { - const orgArguments = arguments; + function _consoleFunc(funcName: ConsoleMapKeys): DiagLogFunction { + return function (...args) { if (console) { // Some environments only expose the console when the F12 developer console is open + // eslint-disable-next-line no-console let theFunc = console[funcName]; if (typeof theFunc !== 'function') { // Not all environments support all functions + // eslint-disable-next-line no-console theFunc = console.log; } // One last final check if (typeof theFunc === 'function') { - return theFunc.apply(console, orgArguments); + return theFunc.apply(console, args); } } }; diff --git a/api/test/diag/consoleLogger.test.ts b/api/test/diag/consoleLogger.test.ts index 56b8e4c28b..2210a7c68b 100644 --- a/api/test/diag/consoleLogger.test.ts +++ b/api/test/diag/consoleLogger.test.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +/* eslint-disable no-console */ + import * as assert from 'assert'; import { DiagConsoleLogger } from '../../src/diag/consoleLogger';