From 671bce635021c1b1e34ddbdc7286f3e843046b87 Mon Sep 17 00:00:00 2001 From: Lucas Koehler Date: Thu, 4 May 2023 18:04:25 +0200 Subject: [PATCH] Enable ESLint no-prototype-builtins rule and fix errors Use Object.prototype..call(, ) syntax instead of directly calling the methods on the objects. This is the suggested fix in the rule documentation. Part of #2131 --- packages/angular-material/.eslintrc.js | 1 - packages/angular-test/.eslintrc.js | 1 - packages/angular/.eslintrc.js | 1 - packages/core/.eslintrc.js | 1 - packages/core/src/generators/schema.ts | 10 ++++--- packages/core/src/reducers/core.ts | 5 +++- packages/core/src/testers/testers.ts | 26 +++++++++++++------ packages/core/src/util/resolvers.ts | 5 +++- packages/core/src/util/schema.ts | 5 ++-- packages/examples-react/.eslintrc.js | 1 - packages/examples/.eslintrc.js | 1 - packages/material-renderers/.eslintrc.js | 1 - .../MaterialAnyOfStringOrEnumControl.tsx | 4 ++- packages/react/.eslintrc.js | 1 - packages/vanilla-renderers/.eslintrc.js | 1 - packages/vue/vue-vanilla/.eslintrc.js | 1 - packages/vue/vue/.eslintrc.js | 1 - 17 files changed, 39 insertions(+), 27 deletions(-) diff --git a/packages/angular-material/.eslintrc.js b/packages/angular-material/.eslintrc.js index 0bf60144d6..399fcb2b71 100644 --- a/packages/angular-material/.eslintrc.js +++ b/packages/angular-material/.eslintrc.js @@ -23,7 +23,6 @@ module.exports = { '@angular-eslint/directive-class-suffix': 'off', '@angular-eslint/no-conflicting-lifecycle': 'warn', '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/angular-test/.eslintrc.js b/packages/angular-test/.eslintrc.js index 28449d281a..2593669068 100644 --- a/packages/angular-test/.eslintrc.js +++ b/packages/angular-test/.eslintrc.js @@ -23,7 +23,6 @@ module.exports = { '@angular-eslint/directive-class-suffix': 'off', '@angular-eslint/no-conflicting-lifecycle': 'warn', '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/angular/.eslintrc.js b/packages/angular/.eslintrc.js index d3d27d3a1e..e0651d72c0 100644 --- a/packages/angular/.eslintrc.js +++ b/packages/angular/.eslintrc.js @@ -23,7 +23,6 @@ module.exports = { '@angular-eslint/directive-class-suffix': 'off', '@angular-eslint/no-conflicting-lifecycle': 'warn', '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/core/.eslintrc.js b/packages/core/.eslintrc.js index dbc146dba3..83877fd803 100644 --- a/packages/core/.eslintrc.js +++ b/packages/core/.eslintrc.js @@ -21,7 +21,6 @@ module.exports = { }, ], '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/core/src/generators/schema.ts b/packages/core/src/generators/schema.ts index ddbf85add3..f3b1d2324b 100644 --- a/packages/core/src/generators/schema.ts +++ b/packages/core/src/generators/schema.ts @@ -38,7 +38,7 @@ const distinct = ( return properties.filter((item) => { const discriminatorValue = discriminator(item); - if (known.hasOwnProperty(discriminatorValue)) { + if (Object.prototype.hasOwnProperty.call(known, discriminatorValue)) { return false; } else { known[discriminatorValue] = true; @@ -155,13 +155,17 @@ export const generateJsonSchema = ( (optionName: string): boolean | string[] => { switch (optionName) { case ADDITIONAL_PROPERTIES: - if (options.hasOwnProperty(ADDITIONAL_PROPERTIES)) { + if ( + Object.prototype.hasOwnProperty.call(options, ADDITIONAL_PROPERTIES) + ) { return options[ADDITIONAL_PROPERTIES]; } return true; case REQUIRED_PROPERTIES: - if (options.hasOwnProperty(REQUIRED_PROPERTIES)) { + if ( + Object.prototype.hasOwnProperty.call(options, REQUIRED_PROPERTIES) + ) { return options[REQUIRED_PROPERTIES](props); } diff --git a/packages/core/src/reducers/core.ts b/packages/core/src/reducers/core.ts index 65839cd419..a09ded236d 100644 --- a/packages/core/src/reducers/core.ts +++ b/packages/core/src/reducers/core.ts @@ -90,7 +90,10 @@ const initState: JsonFormsCore = { }; const reuseAjvForSchema = (ajv: Ajv, schema: JsonSchema): Ajv => { - if (schema.hasOwnProperty('id') || schema.hasOwnProperty('$id')) { + if ( + Object.prototype.hasOwnProperty.call(schema, 'id') || + Object.prototype.hasOwnProperty.call(schema, '$id') + ) { ajv.removeSchema(schema); } return ajv; diff --git a/packages/core/src/testers/testers.ts b/packages/core/src/testers/testers.ts index 708073ea1d..c14048e335 100644 --- a/packages/core/src/testers/testers.ts +++ b/packages/core/src/testers/testers.ts @@ -325,17 +325,23 @@ export const isObjectControl = and(uiTypeIs('Control'), schemaTypeIs('object')); export const isAllOfControl = and( uiTypeIs('Control'), - schemaMatches((schema) => schema.hasOwnProperty('allOf')) + schemaMatches((schema) => + Object.prototype.hasOwnProperty.call(schema, 'allOf') + ) ); export const isAnyOfControl = and( uiTypeIs('Control'), - schemaMatches((schema) => schema.hasOwnProperty('anyOf')) + schemaMatches((schema) => + Object.prototype.hasOwnProperty.call(schema, 'anyOf') + ) ); export const isOneOfControl = and( uiTypeIs('Control'), - schemaMatches((schema) => schema.hasOwnProperty('oneOf')) + schemaMatches((schema) => + Object.prototype.hasOwnProperty.call(schema, 'oneOf') + ) ); /** @@ -346,8 +352,12 @@ export const isOneOfControl = and( export const isEnumControl = and( uiTypeIs('Control'), or( - schemaMatches((schema) => schema.hasOwnProperty('enum')), - schemaMatches((schema) => schema.hasOwnProperty('const')) + schemaMatches((schema) => + Object.prototype.hasOwnProperty.call(schema, 'enum') + ), + schemaMatches((schema) => + Object.prototype.hasOwnProperty.call(schema, 'const') + ) ) ); @@ -592,9 +602,9 @@ export const isRangeControl = and( or(schemaTypeIs('number'), schemaTypeIs('integer')), schemaMatches( (schema) => - schema.hasOwnProperty('maximum') && - schema.hasOwnProperty('minimum') && - schema.hasOwnProperty('default') + Object.prototype.hasOwnProperty.call(schema, 'maximum') && + Object.prototype.hasOwnProperty.call(schema, 'minimum') && + Object.prototype.hasOwnProperty.call(schema, 'default') ), optionIs('slider', true) ); diff --git a/packages/core/src/util/resolvers.ts b/packages/core/src/util/resolvers.ts index 10deb76318..a415e252df 100644 --- a/packages/core/src/util/resolvers.ts +++ b/packages/core/src/util/resolvers.ts @@ -49,7 +49,10 @@ export const resolveData = (instance: any, dataPath: string): any => { const dataPathSegments = dataPath.split('.'); return dataPathSegments.reduce((curInstance, decodedSegment) => { - if (!curInstance || !curInstance.hasOwnProperty(decodedSegment)) { + if ( + !curInstance || + !Object.prototype.hasOwnProperty.call(curInstance, decodedSegment) + ) { return undefined; } diff --git a/packages/core/src/util/schema.ts b/packages/core/src/util/schema.ts index ea296d6761..7ec39ae70e 100644 --- a/packages/core/src/util/schema.ts +++ b/packages/core/src/util/schema.ts @@ -44,6 +44,7 @@ export const getFirstPrimitiveProp = (schema: any) => { * Tests whether the schema has an enum based on oneOf. */ export const isOneOfEnumSchema = (schema: JsonSchema) => - schema?.hasOwnProperty('oneOf') && - schema?.oneOf && + !!schema && + Object.prototype.hasOwnProperty.call(schema, 'oneOf') && + schema.oneOf && (schema.oneOf as JsonSchema[]).every((s) => s.const !== undefined); diff --git a/packages/examples-react/.eslintrc.js b/packages/examples-react/.eslintrc.js index 4db5e1f837..09f7ec2a0f 100644 --- a/packages/examples-react/.eslintrc.js +++ b/packages/examples-react/.eslintrc.js @@ -20,7 +20,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/examples/.eslintrc.js b/packages/examples/.eslintrc.js index 3de3a950a1..22942a301a 100644 --- a/packages/examples/.eslintrc.js +++ b/packages/examples/.eslintrc.js @@ -14,7 +14,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/material-renderers/.eslintrc.js b/packages/material-renderers/.eslintrc.js index f32389adb3..1bfc4966b9 100644 --- a/packages/material-renderers/.eslintrc.js +++ b/packages/material-renderers/.eslintrc.js @@ -20,7 +20,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', 'import/no-named-as-default': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', diff --git a/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx b/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx index 8a1b42bb31..d4e4642af1 100644 --- a/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx +++ b/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx @@ -133,7 +133,9 @@ const hasEnumAndText = (schemas: JsonSchema[]) => { const simpleAnyOf = and( uiTypeIs('Control'), schemaMatches( - (schema) => schema.hasOwnProperty('anyOf') && hasEnumAndText(schema.anyOf) + (schema) => + Object.prototype.hasOwnProperty.call(schema, 'anyOf') && + hasEnumAndText(schema.anyOf) ) ); export const materialAnyOfStringOrEnumControlTester: RankedTester = rankWith( diff --git a/packages/react/.eslintrc.js b/packages/react/.eslintrc.js index cc02c43070..64c535c642 100644 --- a/packages/react/.eslintrc.js +++ b/packages/react/.eslintrc.js @@ -20,7 +20,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/vanilla-renderers/.eslintrc.js b/packages/vanilla-renderers/.eslintrc.js index 58d623b3fe..83ed76d416 100644 --- a/packages/vanilla-renderers/.eslintrc.js +++ b/packages/vanilla-renderers/.eslintrc.js @@ -20,7 +20,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', 'import/no-named-as-default': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', diff --git a/packages/vue/vue-vanilla/.eslintrc.js b/packages/vue/vue-vanilla/.eslintrc.js index 227f0e72ee..1b64a113c9 100644 --- a/packages/vue/vue-vanilla/.eslintrc.js +++ b/packages/vue/vue-vanilla/.eslintrc.js @@ -21,7 +21,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [ diff --git a/packages/vue/vue/.eslintrc.js b/packages/vue/vue/.eslintrc.js index bfda198506..bcd2427654 100644 --- a/packages/vue/vue/.eslintrc.js +++ b/packages/vue/vue/.eslintrc.js @@ -21,7 +21,6 @@ module.exports = { ], rules: { '@typescript-eslint/no-explicit-any': 'off', - 'no-prototype-builtins': 'off', // Base rule must be disabled to avoid incorrect errors 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': [