From d893a71e73d1300d863c19678b9e00ecffcab670 Mon Sep 17 00:00:00 2001 From: Tuval Simha Date: Tue, 7 Feb 2023 16:18:10 +0200 Subject: [PATCH] ESlint rules fixes (#2229) * fix * fix * fix --------- Co-authored-by: Dimitri POSTOLOV --- .eslintrc.cjs | 12 ++---------- example/schemas/schema.js | 4 +--- jest.config.js | 2 +- packages/action/src/run.ts | 9 +++------ packages/cli/jest.config.js | 4 ++-- packages/commands/coverage/src/index.ts | 11 ++++------- packages/commands/diff/src/index.ts | 9 ++++----- .../__tests__/introspect-command.spec.ts | 14 +++++--------- packages/commands/introspect/src/index.ts | 4 +--- packages/commands/similar/src/index.ts | 9 +++------ packages/commands/validate/src/index.ts | 4 +--- packages/core/jest.config.js | 4 ++-- packages/github/__tests__/config.ts | 1 + packages/github/jest.config.js | 4 ++-- packages/github/src/helpers/config.ts | 4 ++-- packages/github/src/schema-change-notifications.ts | 1 + packages/testing/src/index.ts | 3 ++- 17 files changed, 37 insertions(+), 62 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 9221a6af4b..9b211bb8ac 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -11,25 +11,17 @@ module.exports = { files: ['**'], rules: { // TODO: enable following rules - '@typescript-eslint/no-namespace': 'off', - '@typescript-eslint/no-unused-vars': 'off', - '@typescript-eslint/no-var-requires': 'off', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-non-null-assertion': 'off', '@typescript-eslint/ban-types': 'off', - '@typescript-eslint/no-empty-function': 'off', + '@typescript-eslint/no-var-requires': 'off', + 'n/no-restricted-import': 'off', 'yml/no-empty-mapping-value': 'off', 'no-undef': 'off', 'unicorn/prefer-node-protocol': 'off', - 'no-restricted-syntax': 'off', - 'no-implicit-coercion': 'off', 'import/extensions': 'off', 'no-console': 'off', 'import/no-default-export': 'off', - 'no-prototype-builtins': 'off', - 'unicorn/no-useless-fallback-in-spread': 'off', - 'no-empty': 'off', - 'n/no-restricted-import': 'off', 'no-inner-declarations': 'off', }, }, diff --git a/example/schemas/schema.js b/example/schemas/schema.js index c53632e217..6d9b5f0f90 100644 --- a/example/schemas/schema.js +++ b/example/schemas/schema.js @@ -1,6 +1,4 @@ const { readFileSync } = require('fs'); const { resolve } = require('path'); -module.exports = readFileSync(resolve(__dirname, './schema.graphql'), { - encoding: 'utf-8', -}); +module.exports = readFileSync(resolve(__dirname, './schema.graphql'), 'utf8'); diff --git a/jest.config.js b/jest.config.js index 6f9882c759..267f4f5186 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,7 +4,7 @@ const { pathsToModuleNameMapper } = require('ts-jest/utils'); const ROOT_DIR = __dirname; const TSCONFIG = resolve(ROOT_DIR, 'tsconfig.json'); const tsconfig = require(TSCONFIG); -const CI = !!process.env.CI; +const CI = Boolean(process.env.CI); module.exports = { transform: { '^.+\\.tsx?$': 'ts-jest' }, diff --git a/packages/action/src/run.ts b/packages/action/src/run.ts index 194e232d98..695b4d3410 100644 --- a/packages/action/src/run.ts +++ b/packages/action/src/run.ts @@ -45,7 +45,7 @@ async function getAssociatedPullRequest(octokit: OctokitInstance, commitSha: str format: 'json', previews: ['groot'], } - }) + }) return result.data.length > 0 ? result.data[0] : null } @@ -195,7 +195,7 @@ export async function run() { let annotations = action.annotations || []; const changes = action.changes || []; - core.setOutput('changes', `${changes.length || 0}`); + core.setOutput('changes', String(changes.length || 0)); core.info(`Changes: ${changes.length || 0}`); const hasApprovedBreakingChangeLabel = pullRequest?.labels?.some( @@ -274,11 +274,8 @@ function fileLoader({ workspace?: string; }): Promise { if (file.workspace) { - return readFileSync(resolve(file.workspace, file.path), { - encoding: 'utf-8', - }); + return readFileSync(resolve(file.workspace, file.path), 'utf8'); } - const result: any = await octokit.graphql(query, { repo, owner, diff --git a/packages/cli/jest.config.js b/packages/cli/jest.config.js index 82fb2be5a7..e37577d92f 100644 --- a/packages/cli/jest.config.js +++ b/packages/cli/jest.config.js @@ -1,3 +1,3 @@ -const config = require('../../jest.config'); +import config from '../../jest.config'; -module.exports = config; +export default config; diff --git a/packages/commands/coverage/src/index.ts b/packages/commands/coverage/src/index.ts index c8a8b1f876..ea41cb5732 100644 --- a/packages/commands/coverage/src/index.ts +++ b/packages/commands/coverage/src/index.ts @@ -54,10 +54,7 @@ export function handler({ } if (output) { - writeFileSync(absPath, output, { - encoding: 'utf-8', - }); - + writeFileSync(absPath, output, 'utf8'); Logger.success(`Available at ${absPath}\n`); } else { throw new Error(`Extension ${ext} is not supported`); @@ -137,19 +134,19 @@ function renderCoverage(coverage: SchemaCoverage) { Logger.info('Schema coverage based on documents:\n'); for (const typeName in coverage.types) { - if (coverage.types.hasOwnProperty(typeName)) { + if (Object.prototype.hasOwnProperty.call(coverage.types, typeName)) { const typeCoverage = coverage.types[typeName]; Logger.log( [ chalk.grey(getTypePrefix(typeCoverage.type)), - chalk.bold(`${typeName}`), + chalk.bold(String(typeName)), chalk.grey('{'), ].join(' '), ); for (const childName in typeCoverage.children) { - if (typeCoverage.children.hasOwnProperty(childName)) { + if (Object.prototype.hasOwnProperty.call(typeCoverage.children, childName)) { const childCoverage = typeCoverage.children[childName]; if (childCoverage.hits) { diff --git a/packages/commands/diff/src/index.ts b/packages/commands/diff/src/index.ts index c110c1cbd6..274c821462 100644 --- a/packages/commands/diff/src/index.ts +++ b/packages/commands/diff/src/index.ts @@ -135,12 +135,12 @@ export default createCommand< const { headers, leftHeaders, rightHeaders, token } = parseGlobalArgs(args); const oldSchemaHeaders = { - ...(headers ?? {}), - ...(leftHeaders ?? {}), + ...headers, + ...leftHeaders, }; const newSchemaHeaders = { - ...(headers ?? {}), - ...(rightHeaders ?? {}), + ...headers, + ...rightHeaders, }; const oldSchema = await loaders.loadSchema( @@ -254,7 +254,6 @@ function resolveUsageHandler(name: string): UsageHandler | never { } catch (error) { throw new Error(`UsageHandler '${name}' does not exist!`); } - const mod = require(filepath); return mod?.default || mod; diff --git a/packages/commands/introspect/__tests__/introspect-command.spec.ts b/packages/commands/introspect/__tests__/introspect-command.spec.ts index 3863e3b633..6af4fa4a68 100644 --- a/packages/commands/introspect/__tests__/introspect-command.spec.ts +++ b/packages/commands/introspect/__tests__/introspect-command.spec.ts @@ -58,7 +58,9 @@ describe('introspect', () => { spyReporter.mockRestore(); try { unlinkSync('graphql.schema.json'); - } catch (e) {} + } catch { + /* ignore */ + } }); test('graphql api with port and ws in name using url-loader', async () => { @@ -85,10 +87,7 @@ describe('introspect', () => { done(); expect(existsSync('schema.graphql')).toBe(true); - - const printed = readFileSync('schema.graphql', { - encoding: 'utf-8', - }); + const printed = readFileSync('schema.graphql', 'utf8'); unlinkSync('schema.graphql'); const builtSchema = buildSchema(printed); @@ -111,10 +110,7 @@ describe('introspect', () => { done(); expect(existsSync('schema.graphql')).toBe(true); - - const printed = readFileSync('schema.graphql', { - encoding: 'utf-8', - }); + const printed = readFileSync('schema.graphql', 'utf8'); unlinkSync('schema.graphql'); const builtSchema = buildSchema(printed); diff --git a/packages/commands/introspect/src/index.ts b/packages/commands/introspect/src/index.ts index a04b144ab8..81fccd8628 100644 --- a/packages/commands/introspect/src/index.ts +++ b/packages/commands/introspect/src/index.ts @@ -46,9 +46,7 @@ export function handler({ throw new Error('Only .graphql, .gql and .json files are supported'); } - writeFileSync(output, content!, { - encoding: 'utf-8', - }); + writeFileSync(output, content!, 'utf8'); Logger.success(`Saved to ${filepath}`); } diff --git a/packages/commands/similar/src/index.ts b/packages/commands/similar/src/index.ts index 8f92dd9f4d..7cfac79b83 100644 --- a/packages/commands/similar/src/index.ts +++ b/packages/commands/similar/src/index.ts @@ -31,7 +31,7 @@ export function handler({ Logger.info('No similar types found'); } else { for (const typeName in similarMap) { - if (similarMap.hasOwnProperty(typeName)) { + if (Object.prototype.hasOwnProperty.call(similarMap, typeName)) { const matches = similarMap[typeName]; const prefix = getTypePrefix(schema.getType(typeName) as GraphQLNamedType); const sourceType = chalk.bold(typeName); @@ -63,10 +63,7 @@ export function handler({ } if (output) { - writeFileSync(absPath, output, { - encoding: 'utf-8', - }); - + writeFileSync(absPath, output, 'utf8'); Logger.success(`Available at ${absPath}\n`); } else { throw new Error(`Extension ${ext} is not supported`); @@ -156,7 +153,7 @@ function transformMap(similarMap: SimilarMap): SimilarResults { const results: SimilarResults = {}; for (const typename in similarMap) { - if (similarMap.hasOwnProperty(typename)) { + if (Object.prototype.hasOwnProperty.call(similarMap, typename)) { const result = similarMap[typename]; results[typename] = []; diff --git a/packages/commands/validate/src/index.ts b/packages/commands/validate/src/index.ts index ff03d137c9..a05546f3ad 100644 --- a/packages/commands/validate/src/index.ts +++ b/packages/commands/validate/src/index.ts @@ -106,9 +106,7 @@ export function handler({ null, 2, ), - { - encoding: 'utf-8', - }, + 'utf8', ); } diff --git a/packages/core/jest.config.js b/packages/core/jest.config.js index 82fb2be5a7..e37577d92f 100644 --- a/packages/core/jest.config.js +++ b/packages/core/jest.config.js @@ -1,3 +1,3 @@ -const config = require('../../jest.config'); +import config from '../../jest.config'; -module.exports = config; +export default config; diff --git a/packages/github/__tests__/config.ts b/packages/github/__tests__/config.ts index 3daedebbb7..57321121e1 100644 --- a/packages/github/__tests__/config.ts +++ b/packages/github/__tests__/config.ts @@ -1,5 +1,6 @@ import { createConfig } from '../src/helpers/config'; +// eslint-disable-next-line @typescript-eslint/no-empty-function const dummySetConfig = () => {}; describe('multiple environments', () => { describe('when branch matches environment', () => { diff --git a/packages/github/jest.config.js b/packages/github/jest.config.js index 82fb2be5a7..e37577d92f 100644 --- a/packages/github/jest.config.js +++ b/packages/github/jest.config.js @@ -1,3 +1,3 @@ -const config = require('../../jest.config'); +import config from '../../jest.config'; -module.exports = config; +export default config; diff --git a/packages/github/src/helpers/config.ts b/packages/github/src/helpers/config.ts index 7b2af273dd..bf8f9597e5 100644 --- a/packages/github/src/helpers/config.ts +++ b/packages/github/src/helpers/config.ts @@ -143,7 +143,7 @@ function normalizeConfig(config: Config): { const normalized: NormalizedConfig = {}; for (const envName in config.env) { - if (config.env.hasOwnProperty(envName)) { + if (Object.prototype.hasOwnProperty.call(config.env, envName)) { const env = config.env[envName]; normalized[envName] = { @@ -260,7 +260,7 @@ function findConfigByBranch( const branches: string[] = []; for (const name in config) { - if (config.hasOwnProperty(name)) { + if (Object.prototype.hasOwnProperty.call(config, name)) { const env = config[name]; if (env.branch === branch) { diff --git a/packages/github/src/schema-change-notifications.ts b/packages/github/src/schema-change-notifications.ts index cf7e5ac63d..a50dbb07e2 100644 --- a/packages/github/src/schema-change-notifications.ts +++ b/packages/github/src/schema-change-notifications.ts @@ -56,6 +56,7 @@ export async function handleSchemaChangeNotifications({ } const branch = ref.replace('refs/heads/', ''); + // eslint-disable-next-line @typescript-eslint/no-empty-function const config = createConfig(rawConfig as any, () => {}, [branch]); if (!config.notifications) { diff --git a/packages/testing/src/index.ts b/packages/testing/src/index.ts index 7bf5fafe80..544858c1b9 100644 --- a/packages/testing/src/index.ts +++ b/packages/testing/src/index.ts @@ -8,8 +8,9 @@ export function nonTTY(msg: string) { } declare global { + // eslint-disable-next-line @typescript-eslint/no-namespace namespace jest { - interface Matchers { + interface Matchers { /** * Strips and normalizes logs */