Skip to content

Commit

Permalink
feat: update eslint, various improvements (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV committed Jul 2, 2021
1 parent 5a7e9a7 commit 4348dcc
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-windows-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

update eslint, various improvements
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,30 @@
"@babel/core": "7.14.6",
"@babel/preset-env": "7.14.7",
"@babel/preset-typescript": "7.14.5",
"json-schema-to-markdown": "1.1.1",
"@types/dedent": "0.7.0",
"dedent": "0.7.0",
"ts-node": "10.0.0",
"patch-package": "6.4.7",
"@changesets/cli": "2.16.0",
"@types/dedent": "0.7.0",
"@types/jest": "^26.0.23",
"@types/node": "14.17.4",
"@typescript-eslint/eslint-plugin": "4.28.1",
"@typescript-eslint/parser": "4.28.1",
"eslint": "7.28.0",
"husky": "6.0.0",
"lint-staged": "11.0.0",
"@types/node": "14.17.4",
"bob-the-bundler": "1.2.1",
"dedent": "0.7.0",
"eslint": "7.29.0",
"eslint-config-prettier": "8.3.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "5.1.0",
"eslint-plugin-standard": "5.0.0",
"eslint-config-prettier": "8.3.0",
"ts-jest": "27.0.3",
"husky": "6.0.0",
"jest": "27.0.6",
"json-schema-to-markdown": "1.1.1",
"lint-staged": "11.0.0",
"patch-package": "6.4.7",
"rimraf": "3.0.2",
"typescript": "4.3.5",
"jest": "27.0.6"
"ts-jest": "27.0.3",
"ts-node": "10.0.0",
"typescript": "4.3.5"
},
"resolutions": {
"@changesets/git": "1.1.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/processors/code-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function createGraphqlProcessor() {
if (message.endLine != null) {
message.endLine += lineOffset;
}
if (message.fix && typeof offset !== 'undefined') {
if (message.fix && offset !== undefined) {
message.fix.range[0] = offset + message.fix.range[0];
message.fix.range[1] = offset + message.fix.range[1];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/avoid-operation-name-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
import { OperationDefinitionNode, FragmentDefinitionNode } from 'graphql';

Expand All @@ -12,7 +12,7 @@ export type AvoidOperationNamePrefixConfig = [
const AVOID_OPERATION_NAME_PREFIX = 'AVOID_OPERATION_NAME_PREFIX';

function verifyRule(
context: GraphQLESlintRuleContext<AvoidOperationNamePrefixConfig>,
context: GraphQLESLintRuleContext<AvoidOperationNamePrefixConfig>,
node: GraphQLESTreeNode<OperationDefinitionNode> | GraphQLESTreeNode<FragmentDefinitionNode>
) {
const config = context.options[0] || { keywords: [], caseSensitive: false };
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/avoid-typename-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { FieldDefinitionNode } from 'graphql';
import { GraphQLESTreeNode } from '../estree-parser';
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';

const AVOID_TYPENAME_PREFIX = 'AVOID_TYPENAME_PREFIX';

function checkNode(
context: GraphQLESlintRuleContext<any>,
context: GraphQLESLintRuleContext<any>,
typeName: string,
fields: GraphQLESTreeNode<FieldDefinitionNode>[]
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin/src/rules/graphql-js-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validate, GraphQLSchema, DocumentNode, ASTNode, ValidationRule } from '
import { validateSDL } from 'graphql/validation/validate';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import fs from 'fs';
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { requireGraphQLSchemaFromContext } from '../utils';
import { GraphQLESTreeNode } from '../estree-parser';

Expand All @@ -18,7 +18,7 @@ function extractRuleName(stack: string | undefined): string | null {

export function validateDoc(
sourceNode: GraphQLESTreeNode<ASTNode>,
context: GraphQLESlintRuleContext,
context: GraphQLESLintRuleContext,
schema: GraphQLSchema | null,
documentNode: DocumentNode,
rules: ReadonlyArray<ValidationRule>,
Expand Down Expand Up @@ -54,7 +54,7 @@ const validationToRule = (
name: string,
ruleName: string,
docs: GraphQLESLintRule['meta']['docs'],
getDocumentNode?: (context: GraphQLESlintRuleContext) => DocumentNode | null
getDocumentNode?: (context: GraphQLESLintRuleContext) => DocumentNode | null
): Record<typeof name, GraphQLESLintRule<any, true>> => {
let ruleFn: null | ValidationRule = null;

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/no-operation-name-suffix.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
import { OperationDefinitionNode, FragmentDefinitionNode } from 'graphql';

const NO_OPERATION_NAME_SUFFIX = 'NO_OPERATION_NAME_SUFFIX';

function verifyRule(
context: GraphQLESlintRuleContext,
context: GraphQLESLintRuleContext,
node: GraphQLESTreeNode<OperationDefinitionNode> | GraphQLESTreeNode<FragmentDefinitionNode>
) {
if (node && node.name && node.name.value !== '') {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/require-description.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
import { ASTNode, Kind, StringValueNode } from 'graphql';

Expand All @@ -18,7 +18,7 @@ const DESCRIBABLE_NODES = [
type RequireDescriptionRuleConfig = [{ on: typeof DESCRIBABLE_NODES }];

function verifyRule(
context: GraphQLESlintRuleContext<RequireDescriptionRuleConfig>,
context: GraphQLESLintRuleContext<RequireDescriptionRuleConfig>,
node: GraphQLESTreeNode<ASTNode> & {
readonly description?: GraphQLESTreeNode<StringValueNode>;
}
Expand Down
16 changes: 8 additions & 8 deletions packages/plugin/src/testkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { readFileSync } from 'fs';
import { ASTKindToNode } from 'graphql';
import { resolve } from 'path';
import { GraphQLESTreeNode } from './estree-parser';
import { ParserOptions } from './types';
import { GraphQLESLintRule, ParserOptions } from './types';

export type GraphQLESlintRuleListener<WithTypeInfo extends boolean> = {
export type GraphQLESLintRuleListener<WithTypeInfo extends boolean> = {
[K in keyof ASTKindToNode]?: (node: GraphQLESTreeNode<ASTKindToNode[K], WithTypeInfo>) => void;
} &
Record<string, any>;
Expand All @@ -21,11 +21,11 @@ export type GraphQLInvalidTestCase<T> = GraphQLValidTestCase<T> & {
};

export class GraphQLRuleTester extends require('eslint').RuleTester {
constructor(parserOptions: ParserOptions = {}) {
constructor(parserOptions: ParserOptions) {
super({
parser: require.resolve('@graphql-eslint/eslint-plugin'),
parserOptions: {
...(parserOptions || {}),
...parserOptions,
skipGraphQLConfig: true,
},
});
Expand All @@ -37,12 +37,12 @@ export class GraphQLRuleTester extends require('eslint').RuleTester {

runGraphQLTests<Config>(
name: string,
rule: any, // Actually, it's GraphQLESLintRule, but TS has issues with that
rule: GraphQLESLintRule,
tests: {
valid?: Array<string | GraphQLValidTestCase<Config>>;
invalid?: Array<string | GraphQLInvalidTestCase<Config>>;
valid?: GraphQLValidTestCase<Config>[];
invalid?: GraphQLInvalidTestCase<Config>[];
}
): void {
super.run(name, rule as any, tests);
super.run(name, rule, tests);
}
}
6 changes: 3 additions & 3 deletions packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Rule, AST, Linter } from 'eslint';
import { GraphQLESTreeNode } from './estree-parser';
import { ASTNode, GraphQLSchema } from 'graphql';
import { GraphQLParseOptions } from '@graphql-tools/utils';
import { GraphQLESlintRuleListener } from './testkit';
import { GraphQLESLintRuleListener } from './testkit';
import { SiblingOperations } from './sibling-operations';
import { FieldsCache } from './graphql-ast';

Expand All @@ -27,7 +27,7 @@ export type GraphQLESLintParseResult = Linter.ESLintParseResult & {
services: ParserServices;
};

export type GraphQLESlintRuleContext<Options = any[]> = Omit<
export type GraphQLESLintRuleContext<Options = any[]> = Omit<
Rule.RuleContext,
'parserServices' | 'report' | 'options'
> & {
Expand All @@ -53,6 +53,6 @@ export type RuleDocsInfo<T> = {
};

export type GraphQLESLintRule<Options = any[], WithTypeInfo extends boolean = false> = {
create(context: GraphQLESlintRuleContext<Options>): GraphQLESlintRuleListener<WithTypeInfo>;
create(context: GraphQLESLintRuleContext<Options>): GraphQLESLintRuleListener<WithTypeInfo>;
meta?: Rule.RuleMetaData & RuleDocsInfo<Options>;
};
10 changes: 5 additions & 5 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Source, Lexer, GraphQLSchema, Token, DocumentNode } from 'graphql';
import { GraphQLESlintRuleContext } from './types';
import { GraphQLESLintRuleContext } from './types';
import { AST } from 'eslint';
import { SiblingOperations } from './sibling-operations';
import { FieldsCache } from './graphql-ast';

export function requireSiblingsOperations(ruleName: string, context: GraphQLESlintRuleContext<any>): SiblingOperations {
export function requireSiblingsOperations(ruleName: string, context: GraphQLESLintRuleContext<any>): SiblingOperations {
if (!context || !context.parserServices) {
throw new Error(
`Rule '${ruleName}' requires 'parserOptions.operations' to be set and loaded. See http://bit.ly/graphql-eslint-operations for more info`
Expand All @@ -22,7 +22,7 @@ export function requireSiblingsOperations(ruleName: string, context: GraphQLESli

export function requireGraphQLSchemaFromContext(
ruleName: string,
context: GraphQLESlintRuleContext<any>
context: GraphQLESLintRuleContext<any>
): GraphQLSchema {
if (!context || !context.parserServices) {
throw new Error(
Expand All @@ -41,7 +41,7 @@ export function requireGraphQLSchemaFromContext(

export function requireReachableTypesFromContext(
ruleName: string,
context: GraphQLESlintRuleContext<any>
context: GraphQLESLintRuleContext<any>
): Set<string> {
if (!context || !context.parserServices) {
throw new Error(
Expand All @@ -60,7 +60,7 @@ export function requireReachableTypesFromContext(

export function requireUsedFieldsFromContext(
ruleName: string,
context: GraphQLESlintRuleContext<any>
context: GraphQLESLintRuleContext<any>
): FieldsCache {
if (!context || !context.parserServices) {
throw new Error(
Expand Down
17 changes: 7 additions & 10 deletions patches/eslint+7.28.0.patch → patches/eslint+7.29.0.patch
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
diff --git a/node_modules/eslint/lib/rule-tester/rule-tester.js b/node_modules/eslint/lib/rule-tester/rule-tester.js
index 905f341..6ba6ae7 100644
index cac81bc..3a3dbbe 100644
--- a/node_modules/eslint/lib/rule-tester/rule-tester.js
+++ b/node_modules/eslint/lib/rule-tester/rule-tester.js
@@ -851,9 +851,19 @@ class RuleTester {
@@ -905,7 +905,17 @@ class RuleTester {
"Expected no autofixes to be suggested"
);
} else {
- assert.strictEqual(result.output, item.output, "Output is incorrect.");
+ if (item.output.includes('# normalize graphql')) {
+ const graphql = require('graphql');
+
+ function normalize(value) {
+ return graphql.print(graphql.parse(value.replace('# normalize graphql', '')));
+ }
+
+ assert.strictEqual(normalize(result.output), normalize(item.output), "Output is incorrect.");
+
+ assert.strictEqual(normalize(result.output), normalize(item.output), 'Output is incorrect.');
+ } else {
+ assert.strictEqual(result.output, item.output, "Output is incorrect.");
+ assert.strictEqual(result.output, item.output, 'Output is incorrect.');
+ }
}
- } else {
+ }
+ else {
} else {
assert.strictEqual(
result.output,
item.code,

0 comments on commit 4348dcc

Please sign in to comment.