diff --git a/.eslintrc b/.eslintrc index 18733ff..aec4ec0 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,10 +1,6 @@ { "root": true, - "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/recommended", - "plugin:@typescript-eslint/stylistic" - ], + "extends": ["eslint:recommended"], "env": { "jest": true, "node": true diff --git a/lib/rules/no-more-than-10-fields.ts b/lib/rules/no-more-than-10-fields.ts deleted file mode 100644 index 2f7e68a..0000000 --- a/lib/rules/no-more-than-10-fields.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { Kind, NameNode } from 'graphql'; -import { GraphQLESLintRule } from '@graphql-eslint/eslint-plugin/'; -//import { GraphQLESLintRuleContext,} from '@graphql-eslint/eslint-plugin/'; -const RULE_ID = 'no-more-than-10-fields'; - -export const rule: GraphQLESLintRule = { - meta: { - type: 'suggestion', - hasSuggestions: true, - docs: { - description: 'Object should have less than 11 fields.', - category: 'Operations', - recommended: true, - examples: [ - { - title: 'Incorrect', - code: /* GraphQL */ ` - query getSA($recoridId) { - uiapi { - query { - ServiceAppointment() { - edges { - node { - Id - Subject { - value - } - AccountId { - value - } - City { - value - } - Description { - value - } - OwnerId { - value - } - PostalCode { - value - } - SchedStartTime { - value - } - Status { - value - } - Street { - value - } - } - } - } - } - } - } - ` - } - ] - }, - messages: { - [RULE_ID]: 'Field number should not exceed 10!' - }, - schema: [] - }, - create(context) { - function checkNode(usedFields: Set, node: Any): void { - const fieldName = node.value; - - usedFields.add(fieldName); - if (usedFields.size === 10) { - context.report({ - node, - messageId: RULE_ID, - suggest: [ - { - desc: 'Reduce object fields to less than 10', - fix(fixer) { - return fixer.remove(node as any); - } - } - ] - }); - } - } - - return { - SelectionSet(node) { - if ( - node.parent.kind === 'Field' && - node.parent.name.kind === 'Name' && - node.parent.name.value === 'node' - ) { - const set = new Set(); - - for (const selection of node.selections) { - if (selection.kind === Kind.FIELD) { - checkNode(set, selection.alias || selection.name); - } - } - } - } - }; - } -}; diff --git a/lib/rules/no-more-than-2-fields.ts b/lib/rules/no-more-than-2-fields.ts new file mode 100644 index 0000000..cdf0c2a --- /dev/null +++ b/lib/rules/no-more-than-2-fields.ts @@ -0,0 +1,79 @@ +import { Kind, NameNode } from 'graphql'; +import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin/'; + +const RULE_ID: string = 'no-more-than-2-fields'; + +export const rule: GraphQLESLintRule = { + meta: { + type: 'suggestion', + hasSuggestions: true, + docs: { + description: 'Object should have no more than 2 fields.', + category: 'Operations', + recommended: true, + examples: [ + { + title: 'Incorrect', + code: /* GraphQL */ ` + query getSA($recoridId) { + uiapi { + query { + ServiceAppointment() { + edges { + node { + Id + Subject { + value + } + AccountId { + value + } + } + } + } + } + } + } + ` + } + ] + }, + messages: { + [RULE_ID]: 'Field number should not exceed 2!' + }, + schema: [] + }, + create(context: GraphQLESLintRuleContext) { + function checkNode(usedFields: Set, node: NameNode): void { + const fieldName = node.value; + + usedFields.add(fieldName); + if (usedFields.size === 3) { + context.report({ + node, + messageId: RULE_ID + }); + } + } + + return { + SelectionSet(node) { + if (node.type === 'SelectionSet') { + const rawNode = node.rawNode(); + // Traverse backwards to find 'node' from GraphQL ASTNode + const parentNode = node.parent.rawNode(); + + if (parentNode.kind === 'Field' && parentNode.name.value === 'node') { + const set = new Set(); + + for (const selection of rawNode.selections) { + if (selection.kind === Kind.FIELD) { + checkNode(set, selection.alias || selection.name); + } + } + } + } + } + }; + } +}; diff --git a/test/lib/rules/no-more-than-10-fields.spec.ts b/test/lib/rules/no-more-than-10-fields.spec.ts deleted file mode 100644 index 2504a3a..0000000 --- a/test/lib/rules/no-more-than-10-fields.spec.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { rule } from '../src/rules/no-more-than-then-10-fields'; -import { ruleTester } from './test-utils'; - -ruleTester.run('no-more than 10 fields', rule, { - valid: [], - invalid: [ - { - code: /* GraphQL */ ` - query getSA($recId: string) { - uiapi { - query { - ServiceAppointment( - where: { and: [{ AccountId: { eq: "1" } }, { Status: { eq: "New" } }] } - first: 2 - ) { - edges { - node { - Id - Subject { - value - } - AccountId { - value - } - City { - value - } - Description { - value - } - OwnerId { - value - } - PostalCode { - value - } - SchedStartTime { - value - } - Status { - value - } - Street { - value - } - } - } - } - } - } - } - `, - errors: [{ message: 'Field number should not exceed 10!' }], - }, - ], -}); diff --git a/test/lib/rules/no-more-than-2-fields.ts b/test/lib/rules/no-more-than-2-fields.ts new file mode 100644 index 0000000..e7895c9 --- /dev/null +++ b/test/lib/rules/no-more-than-2-fields.ts @@ -0,0 +1,65 @@ +import { RuleTester } from '@typescript-eslint/rule-tester'; +import { rule } from '../../../lib/rules/no-more-than-2-fields'; + +const ruleTester = new RuleTester({ + parser: '@graphql-eslint/eslint-plugin', + parserOptions: { + graphQLConfig: {} + } +}); + +ruleTester.run('@salesforce/lwc-mobile/no-more-than-2-fields', rule as any, { + valid: [ + { + code: /* GraphQL */ ` + query getSA { + uiapi { + query { + ServiceAppointment { + edges { + node { + Id + Subject { + value + } + } + } + } + } + } + } + ` + } + ], + invalid: [ + { + code: /* GraphQL */ ` + query getSA($recId: string) { + uiapi { + query { + ServiceAppointment( + where: { + and: [{ AccountId: { eq: "1" } }, { Status: { eq: "New" } }] + } + first: 2 + ) { + edges { + node { + Id + Subject { + value + } + AccountId { + value + } + } + } + } + } + } + } + `, + errors: [{ messageId: 'no-more-than-2-fields' }] + } + ] +});