Skip to content

Commit

Permalink
eslint fixes (#8798)
Browse files Browse the repository at this point in the history
* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* update yarn.lock

* prettier

* remove `node:` protocol (not available in node 12)
  • Loading branch information
dimaMachina committed Jan 7, 2023
1 parent 2672c95 commit 5c66efc
Show file tree
Hide file tree
Showing 59 changed files with 173 additions and 310 deletions.
30 changes: 8 additions & 22 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,51 @@ module.exports = {
extends: ['@theguild'],
rules: {
'no-empty': 'off',
'no-console': 'error',
'no-prototype-builtins': 'off',
'no-useless-constructor': 'off',
'no-unused-vars': 'off',
'object-shorthand': ['error', 'always'],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-namespace': 'off',
'no-unreachable-loop': 'error',
'@typescript-eslint/no-empty-interface': 'off',
'prefer-arrow-callback': 'error',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/ban-types': 'off',
'no-lonely-if': 'error',
'import/no-extraneous-dependencies': [
'error',
{ devDependencies: ['**/*.test.ts', '**/*.spec.ts', '**/test/**/*.ts'] },
],

// todo: enable
'unicorn/prefer-node-protocol': 'off',
'no-restricted-syntax': 'off',
'unicorn/filename-case': 'off',
'import/extensions': 'off',
'no-implicit-coercion': 'off',
'unicorn/numeric-separators-style': 'off',
'import/no-default-export': 'off',
'unicorn/no-lonely-if': 'off',
'unicorn/no-useless-spread': 'off',
'simple-import-sort/exports': 'off',
'unicorn/no-array-push-push': 'off',
'no-else-return': 'off',
'no-undef': 'off',
'import/first': 'off',
'n/no-restricted-import': 'off',
'import/no-duplicates': 'off',
// todo: enable in v3
'unicorn/prefer-node-protocol': 'off',
},
env: {
es6: true,
node: true,
},
overrides: [
{
files: ['**/tests/**/*.ts', '**/graphql-codegen-testing/**/*.ts', '*.spec.ts'],
files: ['website/**'],
extends: '@theguild/eslint-config/react',
},
{
files: ['**/tests/**/*.{js,ts,tsx}', '**/graphql-codegen-testing/**/*.ts', '*.spec.ts'],
env: {
jest: true,
},
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'import/no-extraneous-dependencies': 'off',
},
},
],
ignorePatterns: ['dist', 'node_modules', 'dev-test', 'website', 'test-files', 'examples/front-end', '.bob'],
ignorePatterns: ['dev-test', 'examples/front-end', 'website'],
};
6 changes: 2 additions & 4 deletions examples/programmatic-typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { promises } from 'fs';
import { promises } from 'node:fs';
import { codegen } from '@graphql-codegen/core';
import { getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
import * as typedDocumentNode from '@graphql-codegen/typed-document-node';
Expand Down Expand Up @@ -76,9 +76,7 @@ const schema = makeExecutableSchema({
...(await prettier.resolveConfig(process.cwd())),
parser: 'typescript',
}),
{
encoding: 'utf-8',
}
'utf8'
);
console.log('done generating.');
})().catch(err => {
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const PROJECTS = false;
const CI = !!process.env.CI;
const CI = Boolean(process.env.CI);

module.exports =
!PROJECTS || CI
Expand Down
5 changes: 3 additions & 2 deletions jest.project.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { resolve } = require('path');
// eslint-disable-next-line import/no-extraneous-dependencies -- false positive
const { pathsToModuleNameMapper } = require('ts-jest');

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 = ({ dirname, projectMode = true }) => {
const pkg = require(resolve(dirname, 'package.json'));
Expand All @@ -21,7 +22,7 @@ module.exports = ({ dirname, projectMode = true }) => {
cacheDirectory: resolve(ROOT_DIR, `${CI ? '' : 'node_modules/'}.cache/jest`),
setupFiles: [`${ROOT_DIR}/dev-test/setup.js`],
collectCoverage: false,
testTimeout: 20000,
testTimeout: 20_000,
resolver: 'bob-the-bundler/jest-resolver.js',
snapshotFormat: {
escapeString: false,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"build": "bob build",
"watch-build": "npx tsc-watch --project tsconfig.json --onSuccess \"bob build\"",
"test": "jest --forceExit --no-watchman",
"lint": "eslint --cache --ext .ts .",
"lint": "eslint --cache --ignore-path .gitignore .",
"prettier": "prettier --cache --write --list-different .",
"prettier:check": "prettier --cache --check .",
"types:check": "tsc --noEmit",
"test-and-build": "yarn build && yarn test",
"ci:lint": "eslint --ext .ts . --output-file eslint_report.json --format json",
"ci:lint": "eslint --cache --ignore-path .gitignore --output-file eslint_report.json --format json .",
"prerelease": "yarn build",
"release": "changeset publish",
"generate:examples:esm": "node packages/graphql-codegen-cli/dist/esm/bin.js --require dotenv/config --config ./dev-test/codegen.ts dotenv_config_path=dev-test/.env",
Expand Down
17 changes: 7 additions & 10 deletions packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
task: (ctx, task) => {
const generateTasks: ListrTask<Ctx>[] = Object.keys(generates).map(filename => {
const outputConfig = generates[filename];
const hasPreset = !!outputConfig.preset;
const hasPreset = Boolean(outputConfig.preset);

const title = `Generate to ${filename}`;

Expand All @@ -217,10 +217,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
: outputConfig.preset
: null;

if (preset) {
if (preset.prepareDocuments) {
outputSpecificDocuments = await preset.prepareDocuments(filename, outputSpecificDocuments);
}
if (preset && preset.prepareDocuments) {
outputSpecificDocuments = await preset.prepareDocuments(filename, outputSpecificDocuments);
}

return subTask.newListr(
Expand Down Expand Up @@ -352,10 +350,9 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom

const process = async (outputArgs: Types.GenerateOptions) => {
const output = await codegen({
...{
...outputArgs,
emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config, outputArgs.filename),
},
...outputArgs,
// @ts-expect-error todo: fix 'emitLegacyCommonJSImports' does not exist in type 'GenerateOptions'
emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config, outputArgs.filename),
cache,
});
result.push({
Expand Down Expand Up @@ -411,7 +408,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom

if (executedContext.errors.length > 0) {
const errors = executedContext.errors.map(subErr => subErr.message || subErr.toString());
const newErr = new AggregateError(executedContext.errors, `${errors.join('\n\n')}`);
const newErr = new AggregateError(executedContext.errors, String(errors.join('\n\n')));
// Best-effort to all stack traces for debugging
newErr.stack = `${newErr.stack}\n\n${executedContext.errors.map(subErr => subErr.stack).join('\n\n')}`;
throw newErr;
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export function buildOptions() {
if (typeof watch === 'string' || Array.isArray(watch)) {
return watch;
}
return !!watch;
return Boolean(watch);
},
},
r: {
Expand Down Expand Up @@ -490,7 +490,7 @@ function addHashToDocumentFiles(documentFilesPromise: Promise<Types.DocumentFile
}

export function shouldEmitLegacyCommonJSImports(config: Types.Config, outputPath: string): boolean {
const globalValue = config.emitLegacyCommonJSImports === undefined ? true : !!config.emitLegacyCommonJSImports;
const globalValue = config.emitLegacyCommonJSImports === undefined ? true : Boolean(config.emitLegacyCommonJSImports);
// const outputConfig = config.generates[outputPath];

// if (!outputConfig) {
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/generate-and-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export async function generate(
}

function shouldOverwrite(config: Types.Config, outputPath: string): boolean {
const globalValue = config.overwrite === undefined ? true : !!config.overwrite;
const globalValue = config.overwrite === undefined ? true : Boolean(config.overwrite);
const outputConfig = config.generates[outputPath];

if (!outputConfig) {
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql-codegen-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './cli.js';
export { executeCodegen } from './codegen.js';
export { generate } from './generate-and-save.js';
export * from './config.js';
export { CodegenConfig } from './config.js';
export { generate } from './generate-and-save.js';
export * from './graphql-config.js';
export * from './init/index.js';
export * from './utils/cli-error.js';
export * from './cli.js';
export * from './graphql-config.js';
export { CodegenConfig } from './config.js';
8 changes: 2 additions & 6 deletions packages/graphql-codegen-cli/src/init/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export default config;
} else {
content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
}
writeFileSync(fullPath, content, {
encoding: 'utf-8',
});
writeFileSync(fullPath, content, 'utf8');

return {
relativePath,
Expand All @@ -74,9 +72,7 @@ export default config;
export async function writePackage(answers: Answers, configLocation: string) {
// script
const pkgPath = resolve(process.cwd(), 'package.json');
const pkgContent = readFileSync(pkgPath, {
encoding: 'utf-8',
});
const pkgContent = readFileSync(pkgPath, 'utf8');
const pkg = JSON.parse(pkgContent);
const { indent } = detectIndent(pkgContent);

Expand Down
12 changes: 6 additions & 6 deletions packages/graphql-codegen-cli/src/init/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ export function getApplicationTypeChoices(possibleTargets: Record<Tags, boolean>
} else if (possibleTargets.Flow) {
tags.push(Tags.flow);
} else if (possibleTargets.Node) {
tags.push(Tags.typescript);
tags.push(Tags.flow);
tags.push(Tags.typescript, Tags.flow);
}
return tags;
}
Expand Down Expand Up @@ -187,11 +186,12 @@ export function getOutputDefaultValue(answers: Answers) {
export function getDocumentsDefaultValue(answers: Answers) {
if (answers.targets.includes(Tags.vue)) {
return 'src/**/*.vue';
} else if (answers.targets.includes(Tags.angular)) {
}
if (answers.targets.includes(Tags.angular)) {
return 'src/**/*.ts';
} else if (answers.targets.includes(Tags.client)) {
}
if (answers.targets.includes(Tags.client)) {
return 'src/**/*.tsx';
} else {
return 'src/**/*.graphql';
}
return 'src/**/*.graphql';
}
6 changes: 1 addition & 5 deletions packages/graphql-codegen-cli/src/init/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { resolve } from 'path';
import { Tags } from './types.js';

export async function guessTargets(): Promise<Record<Tags, boolean>> {
const pkg = JSON.parse(
readFileSync(resolve(process.cwd(), 'package.json'), {
encoding: 'utf-8',
})
);
const pkg = JSON.parse(readFileSync(resolve(process.cwd(), 'package.json'), 'utf8'));
const dependencies = Object.keys({
...pkg.dependencies,
...pkg.devDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const { join } = require('path');
module.exports = function (docString, config) {
global.CUSTOM_DOCUMENT_LOADER_CALLED = true;

return parse(readFileSync(join(process.cwd(), docString), { encoding: 'utf-8' }));
return parse(readFileSync(join(process.cwd(), docString), 'utf8'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { join } = require('path');

module.exports = function (schemaString, config) {
config.pluginContext.hello = 'world';
return buildSchema(readFileSync(join(process.cwd(), schemaString), { encoding: 'utf-8' }));
return buildSchema(readFileSync(join(process.cwd(), schemaString), 'utf8'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const { join } = require('path');
module.exports = function (schemaString, config) {
global.CUSTOM_SCHEMA_LOADER_CALLED = true;

return buildSchema(readFileSync(join(process.cwd(), schemaString), { encoding: 'utf-8' }));
return buildSchema(readFileSync(join(process.cwd(), schemaString), 'utf8'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('generate-and-save', () => {
import gql from 'graphql-tag';
const MyQuery = gql\`query MyQuery { f }\`;
`,
{}
'utf8'
);
const generateOnce: () => Promise<Types.FileOutput[]> = () =>
generate(
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql-codegen-cli/tests/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
jest.mock('../src/utils/get-latest-version.ts', () => {
return { getLatestVersion: () => Promise.resolve('1.0.0') };
});

import { resolve } from 'path';
import bddStdin from 'bdd-stdin';
import { bold } from '../src/init/helpers.js';
Expand All @@ -10,6 +6,10 @@ import { getApplicationTypeChoices, getPluginChoices } from '../src/init/questio
import { guessTargets } from '../src/init/targets.js';
import { Tags } from '../src/init/types.js';

jest.mock('../src/utils/get-latest-version.ts', () => {
return { getLatestVersion: () => Promise.resolve('1.0.0') };
});

jest.mock('fs');
const { version } = require('../package.json');

Expand Down
3 changes: 1 addition & 2 deletions packages/graphql-codegen-cli/tests/test-files/10.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import graphql from 'graphql-tag';

import { buildQuery } from 'graph/buildQuery';
import { GPlayerList } from 'graph/types';
import graphql from 'graphql-tag';

export const playerListQuery = buildQuery<GPlayerList.Query, GPlayerList.Variables>(graphql`
query GPlayerList {
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql-codegen-cli/tests/test-files/5.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import gql from 'graphql-tag';

import { buildQuery } from 'graph/buildQuery';
import { GPlayerList } from 'graph/types';
import gql from 'graphql-tag';

export const playerListQuery = buildQuery<GPlayerList.Query, GPlayerList.Variables>(gql`
query GPlayerList {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/other/fragment-matcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const plugin: PluginFunction = async (

const filterUnionAndInterfaceTypes = type => type.kind === 'UNION' || type.kind === 'INTERFACE';
const createPossibleTypesCollection = (acc, type) => {
return { ...acc, ...{ [type.name]: type.possibleTypes.map(possibleType => possibleType.name) } };
return { ...acc, [type.name]: type.possibleTypes.map(possibleType => possibleType.name) };
};

const filteredData: IntrospectionResultData | PossibleTypesResultData =
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/other/time/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extname } from 'path';
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
import { GraphQLSchema } from 'graphql';
// eslint-disable-next-line n/no-restricted-import -- todo: replace moment in v3
import moment from 'moment';
import { TimePluginConfig } from './config.js';

Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/other/time/tests/time.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { plugin } from '../src/index.js';

describe('Time', () => {
it('Should use default comment when extenion is unknown', async () => {
it('Should use default comment when extension is unknown', async () => {
const result = await plugin(null as any, [], null, { outputFile: null });
expect(result).toContain('// Generated on');
});

it('Should use # prefix for comment when extenion is graphql', async () => {
it('Should use # prefix for comment when extension is graphql', async () => {
const result = await plugin(null as any, [], null, { outputFile: 'schema.graphql' });
expect(result).toContain('# Generated on');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class BaseDocumentsVisitor<
namespacedImportName: getConfigValue(rawConfig.namespacedImportName, null),
experimentalFragmentVariables: getConfigValue(rawConfig.experimentalFragmentVariables, false),
addTypename: !rawConfig.skipTypename,
globalNamespace: !!rawConfig.globalNamespace,
globalNamespace: Boolean(rawConfig.globalNamespace),
operationResultSuffix: getConfigValue(rawConfig.operationResultSuffix, ''),
scalars: buildScalarsFromConfig(_schema, rawConfig, defaultScalars),
...((additionalConfig || {}) as any),
Expand Down Expand Up @@ -223,7 +223,7 @@ export class BaseDocumentsVisitor<
});
}

return this.convertName(this._unnamedCounter++ + '', {
return this.convertName(String(this._unnamedCounter++), {
prefix: 'Unnamed_',
suffix: '_',
useTypesPrefix: false,
Expand Down
Loading

0 comments on commit 5c66efc

Please sign in to comment.