@graphql-eslint/eslint-plugin 2.3.2-alpha-53e82da.0 → 2.3.2-alpha-78e0315.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,11 +14,11 @@ Checks for duplicate fields in selection set, variables in operation definition,
14
14
  ```graphql
15
15
  # eslint @graphql-eslint/avoid-duplicate-fields: 'error'
16
16
 
17
- query getUserDetails {
17
+ query {
18
18
  user {
19
- name # first
19
+ name
20
20
  email
21
- name # second
21
+ name # duplicate field
22
22
  }
23
23
  }
24
24
  ```
@@ -28,7 +28,7 @@ query getUserDetails {
28
28
  ```graphql
29
29
  # eslint @graphql-eslint/avoid-duplicate-fields: 'error'
30
30
 
31
- query getUsers {
31
+ query {
32
32
  users(
33
33
  first: 100
34
34
  skip: 50
@@ -45,9 +45,11 @@ query getUsers {
45
45
  ```graphql
46
46
  # eslint @graphql-eslint/avoid-duplicate-fields: 'error'
47
47
 
48
- query getUsers($first: Int!, $first: Int!) {
49
- # Duplicate variable
50
- users(first: 100, skip: 50, after: "cji629tngfgou0b73kt7vi5jo") {
48
+ query (
49
+ $first: Int!
50
+ $first: Int! # duplicate variable
51
+ ) {
52
+ users(first: $first, skip: 50) {
51
53
  id
52
54
  }
53
55
  }
package/index.js CHANGED
@@ -744,35 +744,22 @@ const rule = {
744
744
  };
745
745
 
746
746
  const AVOID_DUPLICATE_FIELDS = 'AVOID_DUPLICATE_FIELDS';
747
- const ensureUnique = () => {
748
- const set = new Set();
749
- return {
750
- add: (item, onError) => {
751
- if (set.has(item)) {
752
- onError();
753
- }
754
- else {
755
- set.add(item);
756
- }
757
- },
758
- };
759
- };
760
747
  const rule$1 = {
761
748
  meta: {
762
749
  type: 'suggestion',
763
750
  docs: {
764
- description: 'Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.',
751
+ description: `Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.`,
765
752
  category: 'Stylistic Issues',
766
753
  url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/avoid-duplicate-fields.md',
767
754
  examples: [
768
755
  {
769
756
  title: 'Incorrect',
770
757
  code: /* GraphQL */ `
771
- query getUserDetails {
758
+ query {
772
759
  user {
773
- name # first
760
+ name
774
761
  email
775
- name # second
762
+ name # duplicate field
776
763
  }
777
764
  }
778
765
  `,
@@ -780,7 +767,7 @@ const rule$1 = {
780
767
  {
781
768
  title: 'Incorrect',
782
769
  code: /* GraphQL */ `
783
- query getUsers {
770
+ query {
784
771
  users(
785
772
  first: 100
786
773
  skip: 50
@@ -795,9 +782,11 @@ const rule$1 = {
795
782
  {
796
783
  title: 'Incorrect',
797
784
  code: /* GraphQL */ `
798
- query getUsers($first: Int!, $first: Int!) {
799
- # Duplicate variable
800
- users(first: 100, skip: 50, after: "cji629tngfgou0b73kt7vi5jo") {
785
+ query (
786
+ $first: Int!
787
+ $first: Int! # duplicate variable
788
+ ) {
789
+ users(first: $first, skip: 50) {
801
790
  id
802
791
  }
803
792
  }
@@ -806,58 +795,47 @@ const rule$1 = {
806
795
  ],
807
796
  },
808
797
  messages: {
809
- [AVOID_DUPLICATE_FIELDS]: `{{ type }} "{{ fieldName }}" defined multiple times.`,
798
+ [AVOID_DUPLICATE_FIELDS]: `{{ type }} "{{ fieldName }}" defined multiple times`,
810
799
  },
811
800
  schema: [],
812
801
  },
813
802
  create(context) {
803
+ function checkNode(usedFields, fieldName, type, node) {
804
+ if (usedFields.has(fieldName)) {
805
+ context.report({
806
+ loc: getLocation((node.kind === graphql.Kind.FIELD && node.alias ? node.alias : node).loc, fieldName, {
807
+ offsetEnd: node.kind === graphql.Kind.VARIABLE_DEFINITION ? 0 : 1,
808
+ }),
809
+ messageId: AVOID_DUPLICATE_FIELDS,
810
+ data: {
811
+ type,
812
+ fieldName,
813
+ },
814
+ });
815
+ }
816
+ else {
817
+ usedFields.add(fieldName);
818
+ }
819
+ }
814
820
  return {
815
821
  OperationDefinition(node) {
816
- const uniqueCheck = ensureUnique();
817
- for (const arg of node.variableDefinitions || []) {
818
- uniqueCheck.add(arg.variable.name.value, () => {
819
- context.report({
820
- messageId: AVOID_DUPLICATE_FIELDS,
821
- data: {
822
- type: 'Operation variable',
823
- fieldName: arg.variable.name.value,
824
- },
825
- node: arg,
826
- });
827
- });
822
+ const set = new Set();
823
+ for (const varDef of node.variableDefinitions) {
824
+ checkNode(set, varDef.variable.name.value, 'Operation variable', varDef);
828
825
  }
829
826
  },
830
827
  Field(node) {
831
- const uniqueCheck = ensureUnique();
832
- for (const arg of node.arguments || []) {
833
- uniqueCheck.add(arg.name.value, () => {
834
- context.report({
835
- messageId: AVOID_DUPLICATE_FIELDS,
836
- data: {
837
- type: 'Field argument',
838
- fieldName: arg.name.value,
839
- },
840
- node: arg,
841
- });
842
- });
828
+ const set = new Set();
829
+ for (const arg of node.arguments) {
830
+ checkNode(set, arg.name.value, 'Field argument', arg);
843
831
  }
844
832
  },
845
833
  SelectionSet(node) {
846
834
  var _a;
847
- const uniqueCheck = ensureUnique();
848
- for (const selection of node.selections || []) {
835
+ const set = new Set();
836
+ for (const selection of node.selections) {
849
837
  if (selection.kind === graphql.Kind.FIELD) {
850
- const nameToCheck = ((_a = selection.alias) === null || _a === void 0 ? void 0 : _a.value) || selection.name.value;
851
- uniqueCheck.add(nameToCheck, () => {
852
- context.report({
853
- messageId: AVOID_DUPLICATE_FIELDS,
854
- data: {
855
- type: 'Field',
856
- fieldName: nameToCheck,
857
- },
858
- node: selection,
859
- });
860
- });
838
+ checkNode(set, ((_a = selection.alias) === null || _a === void 0 ? void 0 : _a.value) || selection.name.value, 'Field', selection);
861
839
  }
862
840
  }
863
841
  },
@@ -1626,7 +1604,7 @@ const rule$8 = {
1626
1604
  });
1627
1605
  if (result.ok === false) {
1628
1606
  context.report({
1629
- loc: getLocation(node.loc, node.value),
1607
+ node,
1630
1608
  message: result.errorMessage,
1631
1609
  data: {
1632
1610
  prefix,
@@ -1653,16 +1631,10 @@ const rule$8 = {
1653
1631
  return {
1654
1632
  Name: node => {
1655
1633
  if (node.value.startsWith('_') && options.leadingUnderscore === 'forbid') {
1656
- context.report({
1657
- loc: getLocation(node.loc, node.value),
1658
- message: 'Leading underscores are not allowed',
1659
- });
1634
+ context.report({ node, message: 'Leading underscores are not allowed' });
1660
1635
  }
1661
1636
  if (node.value.endsWith('_') && options.trailingUnderscore === 'forbid') {
1662
- context.report({
1663
- loc: getLocation(node.loc, node.value),
1664
- message: 'Trailing underscores are not allowed',
1665
- });
1637
+ context.report({ node, message: 'Trailing underscores are not allowed' });
1666
1638
  }
1667
1639
  },
1668
1640
  ObjectTypeDefinition: node => {
@@ -2036,10 +2008,7 @@ const rule$c = {
2036
2008
  if (!isEslintComment && line !== prev.line && next.kind === graphql.TokenKind.NAME && linesAfter < 2) {
2037
2009
  context.report({
2038
2010
  messageId: HASHTAG_COMMENT,
2039
- loc: {
2040
- start: { line, column },
2041
- end: { line, column },
2042
- },
2011
+ loc: getLocation({ start: { line, column } }),
2043
2012
  });
2044
2013
  }
2045
2014
  }
@@ -3195,16 +3164,15 @@ const rule$m = {
3195
3164
  }
3196
3165
  return isValidIdName && isValidIdType;
3197
3166
  });
3198
- const typeName = node.name.value;
3199
3167
  // Usually, there should be only one unique identifier field per type.
3200
3168
  // Some clients allow multiple fields to be used. If more people need this,
3201
3169
  // we can extend this rule later.
3202
3170
  if (validIds.length !== 1) {
3203
3171
  context.report({
3204
- loc: getLocation(node.name.loc, typeName),
3205
- message: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }} ; Accepted type(s): {{ acceptedTypesString }}`,
3172
+ node,
3173
+ message: '{{nodeName}} must have exactly one non-nullable unique identifier. Accepted name(s): {{acceptedNamesString}} ; Accepted type(s): {{acceptedTypesString}}',
3206
3174
  data: {
3207
- typeName,
3175
+ nodeName: node.name.value,
3208
3176
  acceptedNamesString: options.acceptedIdNames.join(','),
3209
3177
  acceptedTypesString: options.acceptedIdTypes.join(','),
3210
3178
  },
package/index.mjs CHANGED
@@ -738,35 +738,22 @@ const rule = {
738
738
  };
739
739
 
740
740
  const AVOID_DUPLICATE_FIELDS = 'AVOID_DUPLICATE_FIELDS';
741
- const ensureUnique = () => {
742
- const set = new Set();
743
- return {
744
- add: (item, onError) => {
745
- if (set.has(item)) {
746
- onError();
747
- }
748
- else {
749
- set.add(item);
750
- }
751
- },
752
- };
753
- };
754
741
  const rule$1 = {
755
742
  meta: {
756
743
  type: 'suggestion',
757
744
  docs: {
758
- description: 'Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.',
745
+ description: `Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.`,
759
746
  category: 'Stylistic Issues',
760
747
  url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/avoid-duplicate-fields.md',
761
748
  examples: [
762
749
  {
763
750
  title: 'Incorrect',
764
751
  code: /* GraphQL */ `
765
- query getUserDetails {
752
+ query {
766
753
  user {
767
- name # first
754
+ name
768
755
  email
769
- name # second
756
+ name # duplicate field
770
757
  }
771
758
  }
772
759
  `,
@@ -774,7 +761,7 @@ const rule$1 = {
774
761
  {
775
762
  title: 'Incorrect',
776
763
  code: /* GraphQL */ `
777
- query getUsers {
764
+ query {
778
765
  users(
779
766
  first: 100
780
767
  skip: 50
@@ -789,9 +776,11 @@ const rule$1 = {
789
776
  {
790
777
  title: 'Incorrect',
791
778
  code: /* GraphQL */ `
792
- query getUsers($first: Int!, $first: Int!) {
793
- # Duplicate variable
794
- users(first: 100, skip: 50, after: "cji629tngfgou0b73kt7vi5jo") {
779
+ query (
780
+ $first: Int!
781
+ $first: Int! # duplicate variable
782
+ ) {
783
+ users(first: $first, skip: 50) {
795
784
  id
796
785
  }
797
786
  }
@@ -800,58 +789,47 @@ const rule$1 = {
800
789
  ],
801
790
  },
802
791
  messages: {
803
- [AVOID_DUPLICATE_FIELDS]: `{{ type }} "{{ fieldName }}" defined multiple times.`,
792
+ [AVOID_DUPLICATE_FIELDS]: `{{ type }} "{{ fieldName }}" defined multiple times`,
804
793
  },
805
794
  schema: [],
806
795
  },
807
796
  create(context) {
797
+ function checkNode(usedFields, fieldName, type, node) {
798
+ if (usedFields.has(fieldName)) {
799
+ context.report({
800
+ loc: getLocation((node.kind === Kind.FIELD && node.alias ? node.alias : node).loc, fieldName, {
801
+ offsetEnd: node.kind === Kind.VARIABLE_DEFINITION ? 0 : 1,
802
+ }),
803
+ messageId: AVOID_DUPLICATE_FIELDS,
804
+ data: {
805
+ type,
806
+ fieldName,
807
+ },
808
+ });
809
+ }
810
+ else {
811
+ usedFields.add(fieldName);
812
+ }
813
+ }
808
814
  return {
809
815
  OperationDefinition(node) {
810
- const uniqueCheck = ensureUnique();
811
- for (const arg of node.variableDefinitions || []) {
812
- uniqueCheck.add(arg.variable.name.value, () => {
813
- context.report({
814
- messageId: AVOID_DUPLICATE_FIELDS,
815
- data: {
816
- type: 'Operation variable',
817
- fieldName: arg.variable.name.value,
818
- },
819
- node: arg,
820
- });
821
- });
816
+ const set = new Set();
817
+ for (const varDef of node.variableDefinitions) {
818
+ checkNode(set, varDef.variable.name.value, 'Operation variable', varDef);
822
819
  }
823
820
  },
824
821
  Field(node) {
825
- const uniqueCheck = ensureUnique();
826
- for (const arg of node.arguments || []) {
827
- uniqueCheck.add(arg.name.value, () => {
828
- context.report({
829
- messageId: AVOID_DUPLICATE_FIELDS,
830
- data: {
831
- type: 'Field argument',
832
- fieldName: arg.name.value,
833
- },
834
- node: arg,
835
- });
836
- });
822
+ const set = new Set();
823
+ for (const arg of node.arguments) {
824
+ checkNode(set, arg.name.value, 'Field argument', arg);
837
825
  }
838
826
  },
839
827
  SelectionSet(node) {
840
828
  var _a;
841
- const uniqueCheck = ensureUnique();
842
- for (const selection of node.selections || []) {
829
+ const set = new Set();
830
+ for (const selection of node.selections) {
843
831
  if (selection.kind === Kind.FIELD) {
844
- const nameToCheck = ((_a = selection.alias) === null || _a === void 0 ? void 0 : _a.value) || selection.name.value;
845
- uniqueCheck.add(nameToCheck, () => {
846
- context.report({
847
- messageId: AVOID_DUPLICATE_FIELDS,
848
- data: {
849
- type: 'Field',
850
- fieldName: nameToCheck,
851
- },
852
- node: selection,
853
- });
854
- });
832
+ checkNode(set, ((_a = selection.alias) === null || _a === void 0 ? void 0 : _a.value) || selection.name.value, 'Field', selection);
855
833
  }
856
834
  }
857
835
  },
@@ -1620,7 +1598,7 @@ const rule$8 = {
1620
1598
  });
1621
1599
  if (result.ok === false) {
1622
1600
  context.report({
1623
- loc: getLocation(node.loc, node.value),
1601
+ node,
1624
1602
  message: result.errorMessage,
1625
1603
  data: {
1626
1604
  prefix,
@@ -1647,16 +1625,10 @@ const rule$8 = {
1647
1625
  return {
1648
1626
  Name: node => {
1649
1627
  if (node.value.startsWith('_') && options.leadingUnderscore === 'forbid') {
1650
- context.report({
1651
- loc: getLocation(node.loc, node.value),
1652
- message: 'Leading underscores are not allowed',
1653
- });
1628
+ context.report({ node, message: 'Leading underscores are not allowed' });
1654
1629
  }
1655
1630
  if (node.value.endsWith('_') && options.trailingUnderscore === 'forbid') {
1656
- context.report({
1657
- loc: getLocation(node.loc, node.value),
1658
- message: 'Trailing underscores are not allowed',
1659
- });
1631
+ context.report({ node, message: 'Trailing underscores are not allowed' });
1660
1632
  }
1661
1633
  },
1662
1634
  ObjectTypeDefinition: node => {
@@ -2030,10 +2002,7 @@ const rule$c = {
2030
2002
  if (!isEslintComment && line !== prev.line && next.kind === TokenKind.NAME && linesAfter < 2) {
2031
2003
  context.report({
2032
2004
  messageId: HASHTAG_COMMENT,
2033
- loc: {
2034
- start: { line, column },
2035
- end: { line, column },
2036
- },
2005
+ loc: getLocation({ start: { line, column } }),
2037
2006
  });
2038
2007
  }
2039
2008
  }
@@ -3189,16 +3158,15 @@ const rule$m = {
3189
3158
  }
3190
3159
  return isValidIdName && isValidIdType;
3191
3160
  });
3192
- const typeName = node.name.value;
3193
3161
  // Usually, there should be only one unique identifier field per type.
3194
3162
  // Some clients allow multiple fields to be used. If more people need this,
3195
3163
  // we can extend this rule later.
3196
3164
  if (validIds.length !== 1) {
3197
3165
  context.report({
3198
- loc: getLocation(node.name.loc, typeName),
3199
- message: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }} ; Accepted type(s): {{ acceptedTypesString }}`,
3166
+ node,
3167
+ message: '{{nodeName}} must have exactly one non-nullable unique identifier. Accepted name(s): {{acceptedNamesString}} ; Accepted type(s): {{acceptedTypesString}}',
3200
3168
  data: {
3201
- typeName,
3169
+ nodeName: node.name.value,
3202
3170
  acceptedNamesString: options.acceptedIdNames.join(','),
3203
3171
  acceptedTypesString: options.acceptedIdTypes.join(','),
3204
3172
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-eslint/eslint-plugin",
3
- "version": "2.3.2-alpha-53e82da.0",
3
+ "version": "2.3.2-alpha-78e0315.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0"
@@ -1,3 +1,3 @@
1
1
  import { GraphQLESLintRule } from '../types';
2
- declare const rule: GraphQLESLintRule<[], false>;
2
+ declare const rule: GraphQLESLintRule;
3
3
  export default rule;
package/rules/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export declare const rules: {
6
6
  variables?: "OperationDefinition"[];
7
7
  arguments?: ("Field" | "Directive" | "FieldDefinition" | "DirectiveDefinition")[];
8
8
  }], false>;
9
- 'avoid-duplicate-fields': import("..").GraphQLESLintRule<[], false>;
9
+ 'avoid-duplicate-fields': import("..").GraphQLESLintRule<any[], false>;
10
10
  'avoid-operation-name-prefix': import("..").GraphQLESLintRule<import("./avoid-operation-name-prefix").AvoidOperationNamePrefixConfig, false>;
11
11
  'avoid-scalar-result-type-on-mutation': import("..").GraphQLESLintRule<any[], false>;
12
12
  'avoid-typename-prefix': import("..").GraphQLESLintRule<any[], false>;