Skip to content

Commit

Permalink
eslint - no-unused-variables, prefer-optional-chain, `object-has-…
Browse files Browse the repository at this point in the history
…own` (#8800)

* no-unused-variables, prefer-optional-chain, object-has-own

* commit (#8801)

* enable `prefer-object-has-own` in v3, disable for now
  • Loading branch information
dimaMachina committed Jan 7, 2023
1 parent 5c66efc commit 4e323ca
Show file tree
Hide file tree
Showing 47 changed files with 151 additions and 315 deletions.
12 changes: 7 additions & 5 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module.exports = {
extends: ['@theguild'],
rules: {
'no-empty': 'off',
'no-prototype-builtins': 'off',
'no-useless-constructor': 'off',
'@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',
'@typescript-eslint/no-empty-interface': 'off',
Expand All @@ -28,9 +25,9 @@ module.exports = {
'import/no-default-export': 'off',
// todo: enable in v3
'unicorn/prefer-node-protocol': 'off',
'prefer-object-has-own': 'off',
},
env: {
es6: true,
node: true,
},
overrides: [
Expand All @@ -44,10 +41,15 @@ module.exports = {
jest: true,
},
rules: {
'@typescript-eslint/no-unused-vars': 'off',
'import/no-extraneous-dependencies': 'off',
},
},
{
files: '**/tests/fixtures/*.ts',
rules: {
'@typescript-eslint/no-unused-vars': 'off',
},
},
],
ignorePatterns: ['dev-test', 'examples/front-end', 'website'],
};
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
coverage
*.log
node_modules/
temp
temp/
junit.xml
dist/
.bob/
out.txt
.cache
tsconfig.tsbuildinfo
.yarn
.yarn/
recompile.sh

.next/
out
out/
website/public/sitemap.xml
website/public/_redirects
.eslintcache
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ website/src/pages/plugins/presets/near-operation-file-preset.mdx
examples/**/gql/**

website/algolia-lockfile.json
temp/
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@
"@graphql-typed-document-node/core": "3.1.1",
"@reduxjs/toolkit": "1.8.5",
"@tanstack/react-query": "4.2.3",
"@theguild/eslint-config": "0.4.2",
"@theguild/eslint-config": "0.5.0",
"@theguild/prettier-config": "0.1.1",
"@types/common-tags": "1.8.1",
"@types/jest": "28.1.8",
"@types/mkdirp": "1.0.2",
"@types/node": "18.11.18",
"@types/react": "18.0.18",
"@types/webpack-env": "1.18.0",
"@typescript-eslint/eslint-plugin": "5.36.2",
"@typescript-eslint/parser": "5.36.2",
"@urql/exchange-graphcache": "5.0.5",
"@vue/apollo-composable": "4.0.0-alpha.19",
"@vue/composition-api": "1.7.0",
Expand All @@ -59,13 +57,7 @@
"bob-the-bundler": "4.0.0",
"chalk": "4.1.2",
"dotenv": "16.0.2",
"eslint": "8.23.0",
"eslint-config-prettier": "8.5.0",
"eslint-config-standard": "17.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-n": "15.2.5",
"eslint-plugin-promise": "6.0.1",
"eslint-plugin-react-hooks": "4.6.0",
"eslint": "8.31.0",
"flow-bin": "0.186.0",
"flow-parser": "0.186.0",
"fs-extra": "10.1.0",
Expand Down
12 changes: 5 additions & 7 deletions packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
const cache = createCache();

function wrapTask(task: () => void | Promise<void>, source: string, taskName: string, ctx: Ctx) {
return () => {
return context.profiler.run(async () => {
return () =>
context.profiler.run(async () => {
try {
await Promise.resolve().then(() => task());
} catch (error) {
Expand All @@ -95,7 +95,6 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
throw error;
}
}, taskName);
};
}

async function normalize() {
Expand Down Expand Up @@ -203,7 +202,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom

return {
title,
task: async (_, subTask) => {
async task(_, subTask) {
let outputSchemaAst: GraphQLSchema;
let outputSchema: DocumentNode;
const outputFileTemplateConfig = outputConfig.config || {};
Expand All @@ -217,7 +216,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
: outputConfig.preset
: null;

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

Expand Down Expand Up @@ -314,7 +313,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
...(typeof outputFileTemplateConfig === 'string'
? { value: outputFileTemplateConfig }
: outputFileTemplateConfig),
emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config, filename),
emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config),
};

const outputs: Types.GenerateOptions[] = preset
Expand Down Expand Up @@ -400,7 +399,6 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
// All the errors throw in `listr2` are collected in context
// Running tasks doesn't throw anything
const executedContext = await tasks.run();

if (config.debug) {
// if we have debug logs, make sure to print them before throwing the errors
printLogs();
Expand Down
14 changes: 6 additions & 8 deletions packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ export async function loadCodegenConfig({
packageProp,
loaders: customLoaders,
}: LoadCodegenConfigOptions): Promise<LoadCodegenConfigResult> {
configFilePath = configFilePath || process.cwd();
moduleName = moduleName || 'codegen';
packageProp = packageProp || moduleName;
configFilePath ||= process.cwd();
moduleName ||= 'codegen';
packageProp ||= moduleName;
const cosmi = cosmiconfig(moduleName, {
searchPlaces: generateSearchPlaces(moduleName).concat(additionalSearchPlaces || []),
packageProp,
Expand All @@ -145,9 +145,7 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);

if (graphqlConfig) {
return new CodegenContext({
graphqlConfig,
});
return new CodegenContext({ graphqlConfig });
}

const result = await loadCodegenConfig({ configFilePath });
Expand Down Expand Up @@ -203,7 +201,7 @@ export function buildOptions() {
alias: 'watch',
describe:
'Watch for changes and execute generation automatically. You can also specify a glob expression for custom watch list.',
coerce: (watch: any) => {
coerce(watch: any) {
if (watch === 'false') {
return false;
}
Expand Down Expand Up @@ -489,7 +487,7 @@ function addHashToDocumentFiles(documentFilesPromise: Promise<Types.DocumentFile
);
}

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

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/graphql-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function isGraphQLConfig(config: GraphQLConfig): config is GraphQLConfig {

try {
for (const projectName in config.projects) {
if (config.projects.hasOwnProperty(projectName)) {
if (Object.prototype.hasOwnProperty.call(config.projects, projectName)) {
const project = config.projects[projectName];

if (project.hasExtension('codegen')) {
Expand Down
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 @@ -76,16 +76,12 @@ export async function writePackage(answers: Answers, configLocation: string) {
const pkg = JSON.parse(pkgContent);
const { indent } = detectIndent(pkgContent);

if (!pkg.scripts) {
pkg.scripts = {};
}
pkg.scripts ||= {};

pkg.scripts[answers.script] = `graphql-codegen --config ${configLocation}`;

// plugin
if (!pkg.devDependencies) {
pkg.devDependencies = {};
}
pkg.devDependencies ||= {};

await Promise.all(
(answers.plugins || []).map(async plugin => {
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export async function getPresetByName(
try {
const loaded = await loader(moduleName);

if (loaded && loaded.preset) {
if (loaded?.preset) {
return loaded.preset;
}
if (loaded && loaded.default) {
if (loaded?.default) {
return loaded.default;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function setSilentLogger() {
}

export function useWinstonLogger() {
if (logger && logger.levels) {
if (logger?.levels) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/utils/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const createWatcher = (
.map(filename => ({ filename, config: normalizeOutputParam(config.generates[filename]) }))
.forEach(entry => {
if (entry.config.preset) {
const extension = entry.config.presetConfig && entry.config.presetConfig.extension;
const extension = entry.config.presetConfig?.extension;
if (extension) {
ignored.push(join(entry.filename, '**', '*' + extension));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { parse } = require('graphql');
const { readFileSync } = require('fs');
const { join } = require('path');

module.exports = function (docString, config) {
module.exports = function (docString, _config) {
global.CUSTOM_DOCUMENT_LOADER_CALLED = true;

return parse(readFileSync(join(process.cwd(), docString), 'utf8'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { buildSchema } = require('graphql');
const { readFileSync } = require('fs');
const { join } = require('path');

module.exports = function (schemaString, config) {
module.exports = function (schemaString, _config) {
global.CUSTOM_SCHEMA_LOADER_CALLED = true;

return buildSchema(readFileSync(join(process.cwd(), schemaString), 'utf8'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function (schemaString, config) {
module.exports = function (schemaString, _config) {
return schemaString;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function (schemaString, config) {
module.exports = function (schemaString, _config) {
return schemaString;
};
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/tests/custom-plugins/basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
plugin: (schema, documents, config) => {
plugin(_schema, _documents, _config) {
return 'plugin';
},
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
plugin: (schema, documents, config) => {
plugin(schema, _documents, _config) {
return `
Should have the Extension type: '${schema.getType('Extension')}'
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
plugin: (schema, documents, config) => {
plugin(schema, _documents, _config) {
return Object.keys(schema.getTypeMap()).join(', ');
},
addToSchema: c => `type ${c.test} { f: String }`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
plugin: (schema, documents, config) => {
plugin(schema, _documents, _config) {
return Object.keys(schema.getTypeMap()).join(',');
},
addToSchema: `type Extension { f: String }`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
plugin: (schema, documents, config) => {
plugin(_schema, _documents, _config) {
return 'plugin';
},
validate: () => {
validate() {
throw new Error('Invalid!');
},
};
2 changes: 0 additions & 2 deletions packages/graphql-codegen-cli/tests/test-files/16.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
const other = 'asd';
export const endpoint = `${other}/graphql`;

const anotherVariable = `asdfasdf`;
14 changes: 5 additions & 9 deletions packages/graphql-codegen-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,11 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) {
deduplicatedDefinitions: Set<DefinitionNode>
) {
if (typeof node.name !== 'undefined') {
if (!definitionMap[node.kind]) {
definitionMap[node.kind] = {};
}
if (!definitionMap[node.kind][node.name.value]) {
definitionMap[node.kind][node.name.value] = {
paths: new Set(),
contents: new Set(),
};
}
definitionMap[node.kind] ||= {};
definitionMap[node.kind][node.name.value] ||= {
paths: new Set(),
contents: new Set(),
};

const definitionKindMap = definitionMap[node.kind];

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-core/src/execute-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ExecutePluginOptions {
}

export async function executePlugin(options: ExecutePluginOptions, plugin: CodegenPlugin): Promise<Types.PluginOutput> {
if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') {
if (!plugin?.plugin || typeof plugin.plugin !== 'function') {
throw new Error(
`Invalid Custom Plugin "${options.name}" \n
Plugin ${options.name} does not export a valid JS object with "plugin" function.
Expand Down
4 changes: 1 addition & 3 deletions packages/graphql-codegen-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export function hasFederationSpec(schemaOrAST: GraphQLSchema | DocumentNode) {
}

export function extractHashFromSchema(schema: GraphQLSchema): string | null {
if (!schema.extensions) {
schema.extensions = {};
}
schema.extensions ||= {};

return (schema.extensions['hash'] as string) ?? null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class BaseDocumentsVisitor<
}

private handleAnonymousOperation(node: OperationDefinitionNode): string {
const name = node.name && node.name.value;
const name = node.name?.value;

if (name) {
return this.convertName(name, {
Expand Down
Loading

0 comments on commit 4e323ca

Please sign in to comment.