Skip to content

Commit

Permalink
fix: Added linting rule to disable use of object prototypes (#2939)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh authored Apr 17, 2020
1 parent 5b92f7e commit 0e9621c
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 57 deletions.
95 changes: 50 additions & 45 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -24,7 +25,7 @@ const typescriptOverrides = {
settings: {
react: {
version: "detect",
}
},
},
rules: Object.assign(
typescriptEslintRecommended.rules,
Expand All @@ -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",
Expand All @@ -46,8 +50,8 @@ const typescriptOverrides = {
"@typescript-eslint/explicit-member-accessibility": [
"error",
{
"overrides": {
"constructors": "off",
overrides: {
constructors: "off",
},
},
],
Expand All @@ -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",
Expand All @@ -85,32 +92,36 @@ 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",
"named-imports-order": "case-insensitive-legacy",
},
},
},
}],
// 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 = {
Expand All @@ -119,8 +130,8 @@ const jestOverrides = {
},
files: ["test/**/*.ts", "test/**/*.tsx"],
globals: {
"expectAndFail": "readonly",
"fail": "readonly",
expectAndFail: "readonly",
fail: "readonly",
},
};

Expand All @@ -144,33 +155,27 @@ 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",
"jsdoc/require-param": "off",
"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",
Expand All @@ -187,22 +192,22 @@ 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",
"no-undef-init": "error",
"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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const persistedQueriesGetMethodMiddleware: Middleware = (next) => async (
// Rebuild the query parameters for GET.
const params: Record<string, string> = { query: "" };
for (const key in body) {
if (!body.hasOwnProperty(key)) {
if (!Object.prototype.hasOwnProperty.call(body, key)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/common/utils/dotize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 11 additions & 3 deletions src/core/server/app/helpers/entrypoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/graph/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function mapFieldsetToErrorCodes<T>(
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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/graph/persisted/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/models/action/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/server/models/comment/counts/counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/models/comment/counts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/services/stories/scraper/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 0e9621c

Please sign in to comment.