From ae730752f246ab05ceb16336346e757556a2a69a Mon Sep 17 00:00:00 2001 From: "haifeng.li" Date: Fri, 7 Jun 2024 16:15:44 -0700 Subject: [PATCH] fix: avoid source code for GraphQLESTreeNode --- jest.config.ts | 9 + .../no-fiscal-date-filtering-supported.ts | 2 +- src/rules/graphql/unsupported-scope.ts | 3 +- src/util/entity-stats.ts | 2 +- src/util/graphql-ast-utils.ts | 4 +- src/util/types.ts | 169 ------------------ test/rules/graphql/entity-stats.spec.ts | 3 +- tsconfig.json | 8 +- 8 files changed, 23 insertions(+), 177 deletions(-) delete mode 100644 src/util/types.ts diff --git a/jest.config.ts b/jest.config.ts index d9490c2..5fb57d3 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -13,12 +13,21 @@ const config: Config = { preset: 'ts-jest', testMatch: ['/test/**/*.ts'], moduleFileExtensions: ['ts', 'js', 'json'], + globals: { + 'ts-jest': { + tsconfig: 'tsconfig.json' + } + }, testResultsProcessor: 'jest-sonar-reporter', testPathIgnorePatterns: ['/node_modules/', '/lib/', '/test/shared.ts'], moduleDirectories: ['node_modules'], collectCoverage: true, collectCoverageFrom: ['src/**/*.ts', '!src/index.ts', '!src/configs/*.ts'], coverageDirectory: 'reports/coverage', + moduleNameMapper: { + '^@graphql-eslint/eslint-plugin/(.*)$': + '/node_modules/@graphql-eslint/eslint-plugin/$1' + }, reporters: [ 'default', [ diff --git a/src/rules/graphql/no-fiscal-date-filtering-supported.ts b/src/rules/graphql/no-fiscal-date-filtering-supported.ts index ef6f9d9..2c18b01 100644 --- a/src/rules/graphql/no-fiscal-date-filtering-supported.ts +++ b/src/rules/graphql/no-fiscal-date-filtering-supported.ts @@ -2,7 +2,7 @@ import { ASTNode, Kind, ArgumentNode } from 'graphql'; import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin'; import { getDocUrl } from '../../util/rule-helpers'; import { getClosestAncestorByType } from '../../util/graphql-ast-utils'; -import { GraphQLESTreeNode } from '../../util/types'; +import { GraphQLESTreeNode } from '@graphql-eslint/eslint-plugin/estree-converter/types'; export const NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID = 'offline-graphql-no-fiscal-date-filter-supported'; diff --git a/src/rules/graphql/unsupported-scope.ts b/src/rules/graphql/unsupported-scope.ts index 55b11da..25a3918 100644 --- a/src/rules/graphql/unsupported-scope.ts +++ b/src/rules/graphql/unsupported-scope.ts @@ -13,8 +13,7 @@ export const SCOPE_SUPPORTED_FOR_CERTAIN_ENTITIES_ONLY = 'ASSIGNED_TO_ME__SERVIC export const OTHER_UNSUPPORTED_SCOPE = 'OTHER_UNSUPPORTED_SCOPE'; import { getDocUrl } from '../../util/rule-helpers'; - -import { GraphQLESTreeNode } from '../../util/types'; +import { GraphQLESTreeNode } from '@graphql-eslint/eslint-plugin/estree-converter/types'; // Record key is scope name, value is the array of supported entities. Empty array // means that all entities are supported. diff --git a/src/util/entity-stats.ts b/src/util/entity-stats.ts index 33ec437..8df6dc6 100644 --- a/src/util/entity-stats.ts +++ b/src/util/entity-stats.ts @@ -6,7 +6,7 @@ */ import { FieldNode } from 'graphql'; -import { GraphQLESTreeNode } from './types'; +import { GraphQLESTreeNode } from '@graphql-eslint/eslint-plugin/estree-converter/types'; import { getEntityNodeForEdges, diff --git a/src/util/graphql-ast-utils.ts b/src/util/graphql-ast-utils.ts index 8b95bc0..d3389d5 100644 --- a/src/util/graphql-ast-utils.ts +++ b/src/util/graphql-ast-utils.ts @@ -7,7 +7,7 @@ import { Position } from 'estree'; import { AST } from 'eslint'; -import { GraphQLESTreeNode, ParentNode } from './types'; +import { GraphQLESTreeNode } from '@graphql-eslint/eslint-plugin/estree-converter/types'; import { ASTNode, FieldNode, Kind, DocumentNode, OperationDefinitionNode } from 'graphql'; import { DEFAULT_PAGE_SIZE } from './entity-stats'; @@ -41,7 +41,7 @@ export function getClosestAncestorByType( if (parentNode === null || parentNode === undefined) { return undefined; } - const astParentNode = parentNode as GraphQLESTreeNode, unknown>>; + const astParentNode = parentNode as any; if (astParentNode.type === type) { return astParentNode as GraphQLESTreeNode; } diff --git a/src/util/types.ts b/src/util/types.ts deleted file mode 100644 index 4c0e08a..0000000 --- a/src/util/types.ts +++ /dev/null @@ -1,169 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2022 Dimitri POSTOLOV - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* - * File: types.ts - * Author: Dimitri POSTOLOV - * Description: Copied types.ts from graphql-eslint to simplify custom graphql eslint rule development. - * License: MIT License - */ - -import { AST } from 'eslint'; -import { Comment, SourceLocation } from 'estree'; -import { - ArgumentNode, - ASTNode, - DefinitionNode, - DirectiveDefinitionNode, - DirectiveNode, - DocumentNode, - EnumTypeDefinitionNode, - EnumTypeExtensionNode, - EnumValueDefinitionNode, - ExecutableDefinitionNode, - FieldDefinitionNode, - FieldNode, - FragmentSpreadNode, - InlineFragmentNode, - InputObjectTypeDefinitionNode, - InputObjectTypeExtensionNode, - InputValueDefinitionNode, - InterfaceTypeDefinitionNode, - InterfaceTypeExtensionNode, - ListTypeNode, - NamedTypeNode, - NameNode, - NonNullTypeNode, - ObjectTypeDefinitionNode, - ObjectTypeExtensionNode, - OperationTypeDefinitionNode, - SelectionNode, - SelectionSetNode, - TypeDefinitionNode, - TypeExtensionNode, - TypeInfo, - TypeNode, - VariableDefinitionNode, - VariableNode -} from 'graphql'; - -type SafeGraphQLType = T extends { type: TypeNode } - ? Omit & { gqlType: T['type'] } - : Omit; - -type Writeable = { -readonly [K in keyof T]: T[K] }; - -export type TypeInformation = { - argument: ReturnType; - defaultValue: ReturnType; - directive: ReturnType; - enumValue: ReturnType; - fieldDef: ReturnType; - inputType: ReturnType; - parentInputType: ReturnType; - parentType: ReturnType; - gqlType: ReturnType; -}; - -type NodeWithName = - | ArgumentNode - | DirectiveDefinitionNode - | EnumValueDefinitionNode - | ExecutableDefinitionNode - | FieldDefinitionNode - | FieldNode - | FragmentSpreadNode - | NamedTypeNode - | TypeDefinitionNode - | TypeExtensionNode - | VariableNode; - -type NodeWithType = - | FieldDefinitionNode - | InputValueDefinitionNode - | ListTypeNode - | NonNullTypeNode - | OperationTypeDefinitionNode - | VariableDefinitionNode; - -export type ParentNode = T extends DocumentNode - ? AST.Program - : T extends DefinitionNode - ? DocumentNode - : T extends EnumValueDefinitionNode - ? EnumTypeDefinitionNode | EnumTypeExtensionNode - : T extends InputValueDefinitionNode - ? - | DirectiveDefinitionNode - | FieldDefinitionNode - | InputObjectTypeDefinitionNode - | InputObjectTypeExtensionNode - : T extends FieldDefinitionNode - ? - | InterfaceTypeDefinitionNode - | InterfaceTypeExtensionNode - | ObjectTypeDefinitionNode - | ObjectTypeExtensionNode - : T extends SelectionSetNode - ? ExecutableDefinitionNode | FieldNode | InlineFragmentNode - : T extends SelectionNode - ? SelectionSetNode - : T extends TypeNode - ? NodeWithType - : T extends NameNode - ? NodeWithName - : T extends DirectiveNode - ? InputObjectTypeDefinitionNode | ObjectTypeDefinitionNode - : T extends VariableNode - ? VariableDefinitionNode - : unknown; // Explicitly show error to add new ternary with parent nodes - -type Node = - // Remove readonly for friendly editor popup - Writeable> & { - type: T['kind']; - loc: SourceLocation; - range: AST.Range; - leadingComments: Comment[]; - typeInfo: () => WithTypeInfo extends true ? TypeInformation : Record; - rawNode: () => T; - parent: GraphQLESTreeNode>; - }; - -export type GraphQLESTreeNode = - // if value is ASTNode => convert to Node type - T extends ASTNode - ? { - // Loop recursively over object values - [K in keyof Node]: Node[K] extends - | ReadonlyArray - | undefined // If optional readonly array => loop over array items - ? GraphQLESTreeNode[] - : GraphQLESTreeNode[K], W>; - } - : // If Program node => add `parent: null` field - T extends AST.Program - ? T & { parent: null } - : // Return value as is - T; diff --git a/test/rules/graphql/entity-stats.spec.ts b/test/rules/graphql/entity-stats.spec.ts index 443e3e6..22dee68 100644 --- a/test/rules/graphql/entity-stats.spec.ts +++ b/test/rules/graphql/entity-stats.spec.ts @@ -2,7 +2,8 @@ import { describe } from 'node:test'; import { expect } from '@jest/globals'; import { FieldNode } from 'graphql'; -import { GraphQLESTreeNode } from '../../../src/util/types'; +// @ts-ignore +import { GraphQLESTreeNode } from '@graphql-eslint/eslint-plugin/estree-converter/types'; import { mock } from 'jest-mock-extended'; import { diff --git a/tsconfig.json b/tsconfig.json index c7a80b9..be882b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,13 @@ "noUnusedLocals": true /* Enable error reporting when local variables aren't read. */, "noUnusedParameters": true /* Raise an error when a function parameter isn't read. */, "noImplicitReturns": true /* Enable error reporting for codepaths that do not explicitly return in a function. */, - "skipLibCheck": true /* Skip type checking all .d.ts files. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */, + "paths": { + /* TS complains not about to infer GraphQLESTreeNode */ + "@graphql-eslint/eslint-plugin/estree-converter/*": [ + "./node_modules/@graphql-eslint/eslint-plugin/cjs/estree-converter/*" + ] + } }, "include": ["src/**/*", "./package.json"] }