From 92b6c900025c57e7670a977939a333f2bdfeaa69 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 17 Apr 2020 10:50:43 -0600 Subject: [PATCH] fix: Added linting rule to disable use of object prototypes --- .eslintrc.js | 95 ++++++++++--------- .../persistedQueriesGetMethodMiddleware.ts | 2 +- src/core/common/utils/dotize.ts | 2 +- src/core/server/app/helpers/entrypoints.ts | 14 ++- src/core/server/graph/errors.ts | 2 +- src/core/server/graph/persisted/loader.ts | 2 +- src/core/server/models/action/comment.ts | 2 +- .../server/models/comment/counts/counts.ts | 4 +- .../server/models/comment/counts/shared.ts | 2 +- .../services/stories/scraper/scraper.ts | 2 +- 10 files changed, 70 insertions(+), 57 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 4f46be7c8e..4d515ead76 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,10 +1,11 @@ -const typescriptEslintRecommended = require('@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended').default.overrides[0]; -const typescriptRecommended = require('@typescript-eslint/eslint-plugin/dist/configs/recommended.json'); -const typescriptRecommendedTypeChecking = require('@typescript-eslint/eslint-plugin/dist/configs/recommended-requiring-type-checking.json'); -const typescriptEslintPrettier = require('eslint-config-prettier/@typescript-eslint'); -const react = require('eslint-plugin-react').configs.recommended; -const jsxA11y = require('eslint-plugin-jsx-a11y').configs.recommended; -const reactPrettier = require('eslint-config-prettier/react'); +const typescriptEslintRecommended = require("@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended") + .default.overrides[0]; +const typescriptRecommended = require("@typescript-eslint/eslint-plugin/dist/configs/recommended.json"); +const typescriptRecommendedTypeChecking = require("@typescript-eslint/eslint-plugin/dist/configs/recommended-requiring-type-checking.json"); +const typescriptEslintPrettier = require("eslint-config-prettier/@typescript-eslint"); +const react = require("eslint-plugin-react").configs.recommended; +const jsxA11y = require("eslint-plugin-jsx-a11y").configs.recommended; +const reactPrettier = require("eslint-config-prettier/react"); const typescriptOverrides = { files: ["*.ts", "*.tsx"], @@ -24,7 +25,7 @@ const typescriptOverrides = { settings: { react: { version: "detect", - } + }, }, rules: Object.assign( typescriptEslintRecommended.rules, @@ -36,7 +37,10 @@ const typescriptOverrides = { { "@typescript-eslint/adjacent-overload-signatures": "error", // TODO: (cvle) change `readonly` param to `array-simple` when upgraded typescript. - "@typescript-eslint/array-type": ["error", { "default": "array-simple", "readonly": "generic"}], + "@typescript-eslint/array-type": [ + "error", + { default: "array-simple", readonly: "generic" }, + ], "@typescript-eslint/ban-types": "error", "@typescript-eslint/camelcase": "off", "@typescript-eslint/consistent-type-assertions": "error", @@ -46,8 +50,8 @@ const typescriptOverrides = { "@typescript-eslint/explicit-member-accessibility": [ "error", { - "overrides": { - "constructors": "off", + overrides: { + constructors: "off", }, }, ], @@ -61,7 +65,10 @@ const typescriptOverrides = { "@typescript-eslint/no-namespace": "error", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-parameter-properties": "off", - "@typescript-eslint/no-unused-vars": ["error", {"args": "none", "ignoreRestSiblings": true}], + "@typescript-eslint/no-unused-vars": [ + "error", + { args: "none", ignoreRestSiblings: true }, + ], "@typescript-eslint/no-use-before-define": "off", // TODO: (cvle) Should be on? "@typescript-eslint/no-use-before-declare": "off", "@typescript-eslint/no-var-requires": "error", @@ -85,17 +92,21 @@ const typescriptOverrides = { let typescriptTypeCheckingOverrides = { files: ["*.ts", "*.tsx"], parserOptions: { - project: ["./tsconfig.json", "./src/tsconfig.json", "./src/core/client/tsconfig.json"], + project: [ + "./tsconfig.json", + "./src/tsconfig.json", + "./src/core/client/tsconfig.json", + ], // TODO: (cvle) this is a workaround, see: https://github.com/typescript-eslint/typescript-eslint/issues/1091. createDefaultProgram: true, }, - rules: Object.assign( - typescriptRecommendedTypeChecking.rules, - { - "@typescript-eslint/tslint/config": ["error", { - "rules": { + rules: Object.assign(typescriptRecommendedTypeChecking.rules, { + "@typescript-eslint/tslint/config": [ + "error", + { + rules: { "ordered-imports": { - "options": { + options: { // Legacy sorting until this is fixed: https://github.com/SoominHan/import-sorter/issues/60 "import-sources-order": "case-insensitive-legacy", "module-source-path": "full", @@ -103,14 +114,14 @@ let typescriptTypeCheckingOverrides = { }, }, }, - }], - // 28.11.19: (cvle) Disabled because behavior of regexp.exec seems different than str.match? - "@typescript-eslint/prefer-regexp-exec": "off", - "@typescript-eslint/require-await": "off", - "@typescript-eslint/no-misused-promises": "off", - "@typescript-eslint/unbound-method": "off", // 10.10.19: (cvle) seems to give false positive. - } - ), + }, + ], + // 28.11.19: (cvle) Disabled because behavior of regexp.exec seems different than str.match? + "@typescript-eslint/prefer-regexp-exec": "off", + "@typescript-eslint/require-await": "off", + "@typescript-eslint/no-misused-promises": "off", + "@typescript-eslint/unbound-method": "off", // 10.10.19: (cvle) seems to give false positive. + }), }; const jestOverrides = { @@ -119,8 +130,8 @@ const jestOverrides = { }, files: ["test/**/*.ts", "test/**/*.tsx"], globals: { - "expectAndFail": "readonly", - "fail": "readonly", + expectAndFail: "readonly", + fail: "readonly", }, }; @@ -144,22 +155,19 @@ module.exports = { "plugin:prettier/recommended", ], parserOptions: { - "ecmaVersion": 2018, + ecmaVersion: 2018, }, rules: { "arrow-body-style": "off", - "arrow-parens": [ - "off", - "as-needed", - ], - "camelcase": "off", - "complexity": "off", + "arrow-parens": ["off", "as-needed"], + camelcase: "off", + complexity: "off", "constructor-super": "error", "spaced-comment": ["error", "always"], - "curly": "error", + curly: "error", "dot-notation": "error", "eol-last": "off", - "eqeqeq": "error", + eqeqeq: "error", "guard-for-in": "error", "jsdoc/require-jsdoc": "off", "jsdoc/require-returns": "off", @@ -167,10 +175,7 @@ module.exports = { "jsdoc/require-param-type": "off", "jsdoc/require-returns-type": "off", "linebreak-style": "off", - "max-classes-per-file": [ - "error", - 1, - ], + "max-classes-per-file": ["error", 1], "member-ordering": "off", "new-parens": "off", "newline-per-chained-call": "off", @@ -187,7 +192,7 @@ module.exports = { "no-irregular-whitespace": "off", "no-multiple-empty-lines": "off", "no-new-wrappers": "error", - "no-prototype-builtins": "off", + "no-prototype-builtins": "error", "no-shadow": "error", "no-throw-literal": "error", "no-undef": "off", @@ -195,14 +200,14 @@ module.exports = { "no-unsafe-finally": "error", "no-unused-expressions": "error", "no-unused-labels": "error", - "no-unused-vars": ["error", {"args": "none", "ignoreRestSiblings": true}], + "no-unused-vars": ["error", { args: "none", ignoreRestSiblings: true }], "no-var": "error", "object-shorthand": "error", "one-var": "off", "prefer-arrow-callback": "off", "prefer-const": "error", "quote-props": "off", - "radix": "error", + radix: "error", "require-atomic-updates": "off", "space-before-function-paren": "off", "sort-imports": "off", diff --git a/src/core/client/framework/lib/network/persistedQueriesGetMethodMiddleware.ts b/src/core/client/framework/lib/network/persistedQueriesGetMethodMiddleware.ts index a6bf409028..368d89585b 100644 --- a/src/core/client/framework/lib/network/persistedQueriesGetMethodMiddleware.ts +++ b/src/core/client/framework/lib/network/persistedQueriesGetMethodMiddleware.ts @@ -31,7 +31,7 @@ const persistedQueriesGetMethodMiddleware: Middleware = (next) => async ( // Rebuild the query parameters for GET. const params: Record = { query: "" }; for (const key in body) { - if (!body.hasOwnProperty(key)) { + if (!Object.prototype.hasOwnProperty.call(body, key)) { continue; } diff --git a/src/core/common/utils/dotize.ts b/src/core/common/utils/dotize.ts index 4b9c30f691..67f773029d 100644 --- a/src/core/common/utils/dotize.ts +++ b/src/core/common/utils/dotize.ts @@ -45,7 +45,7 @@ function reduce({ if (isObject(obj)) { for (const property in obj) { - if (!obj.hasOwnProperty(property)) { + if (!Object.prototype.hasOwnProperty.call(obj, property)) { continue; } diff --git a/src/core/server/app/helpers/entrypoints.ts b/src/core/server/app/helpers/entrypoints.ts index dd0341050f..110c1814a7 100644 --- a/src/core/server/app/helpers/entrypoints.ts +++ b/src/core/server/app/helpers/entrypoints.ts @@ -37,7 +37,7 @@ export default class Entrypoints { constructor(manifest: Manifest) { for (const entry in manifest.entrypoints) { - if (!manifest.entrypoints.hasOwnProperty(entry)) { + if (!Object.prototype.hasOwnProperty.call(manifest.entrypoints, entry)) { continue; } @@ -47,7 +47,12 @@ export default class Entrypoints { // Itterate over the extension's in the entrypoint. for (const extension in manifest.entrypoints[entry]) { - if (!manifest.entrypoints[entry].hasOwnProperty(extension)) { + if ( + !Object.prototype.hasOwnProperty.call( + manifest.entrypoints[entry], + extension + ) + ) { continue; } @@ -61,7 +66,10 @@ export default class Entrypoints { for (const src of assets) { // Search for the entry in the assets. for (const name in manifest) { - if (name !== "entrypoints" && !manifest.hasOwnProperty(name)) { + if ( + name !== "entrypoints" && + !Object.prototype.hasOwnProperty.call(manifest, name) + ) { continue; } diff --git a/src/core/server/graph/errors.ts b/src/core/server/graph/errors.ts index ccfaf05102..5f62282380 100644 --- a/src/core/server/graph/errors.ts +++ b/src/core/server/graph/errors.ts @@ -20,7 +20,7 @@ export async function mapFieldsetToErrorCodes( if (err instanceof CoralError) { // Then loop over all the fieldSpecs... for (const param in errorMap) { - if (!errorMap.hasOwnProperty(param)) { + if (!Object.prototype.hasOwnProperty.call(errorMap, param)) { continue; } diff --git a/src/core/server/graph/persisted/loader.ts b/src/core/server/graph/persisted/loader.ts index cbed515df0..a7e5f81789 100644 --- a/src/core/server/graph/persisted/loader.ts +++ b/src/core/server/graph/persisted/loader.ts @@ -36,7 +36,7 @@ export function loadPersistedQueries(): PersistedQuery[] { // Go over each of the persisted queries and collect the ID and query to // merge in. for (const id in persistedQueries) { - if (!persistedQueries.hasOwnProperty(id)) { + if (!Object.prototype.hasOwnProperty.call(persistedQueries, id)) { continue; } diff --git a/src/core/server/models/action/comment.ts b/src/core/server/models/action/comment.ts index b26a79ec9c..a2f23e3889 100644 --- a/src/core/server/models/action/comment.ts +++ b/src/core/server/models/action/comment.ts @@ -464,7 +464,7 @@ export function invertEncodedActionCounts( actionCounts: EncodedCommentActionCounts ): EncodedCommentActionCounts { for (const key in actionCounts) { - if (!actionCounts.hasOwnProperty(key)) { + if (!Object.prototype.hasOwnProperty.call(actionCounts, key)) { continue; } diff --git a/src/core/server/models/comment/counts/counts.ts b/src/core/server/models/comment/counts/counts.ts index 1a8bf3ce13..13b77b8ea2 100644 --- a/src/core/server/models/comment/counts/counts.ts +++ b/src/core/server/models/comment/counts/counts.ts @@ -101,7 +101,7 @@ export function mergeCommentStatusCount( const mergedStatusCounts = createEmptyCommentStatusCounts(); for (const commentCounts of statusCounts) { for (const status in commentCounts) { - if (!commentCounts.hasOwnProperty(status)) { + if (!Object.prototype.hasOwnProperty.call(commentCounts, status)) { continue; } @@ -147,7 +147,7 @@ export function calculateTotalCommentCount( ): number { let count = 0; for (const status in commentCounts) { - if (!commentCounts.hasOwnProperty(status)) { + if (!Object.prototype.hasOwnProperty.call(commentCounts, status)) { continue; } diff --git a/src/core/server/models/comment/counts/shared.ts b/src/core/server/models/comment/counts/shared.ts index 4676d7c96c..0fc3cec100 100644 --- a/src/core/server/models/comment/counts/shared.ts +++ b/src/core/server/models/comment/counts/shared.ts @@ -126,7 +126,7 @@ function fillAndConvertStringToNumber< >(input: T, initial: U): U { const result: U = Object.assign({}, initial); for (const key in input) { - if (!input.hasOwnProperty(key)) { + if (!Object.prototype.hasOwnProperty.call(input, key)) { continue; } diff --git a/src/core/server/services/stories/scraper/scraper.ts b/src/core/server/services/stories/scraper/scraper.ts index 3b51b0b81e..56b2fa9eff 100644 --- a/src/core/server/services/stories/scraper/scraper.ts +++ b/src/core/server/services/stories/scraper/scraper.ts @@ -51,7 +51,7 @@ class Scraper { for (const rule of this.rules) { for (const property in rule) { - if (!rule.hasOwnProperty(property)) { + if (!Object.prototype.hasOwnProperty.call(rule, property)) { continue; }