@graphql-eslint/eslint-plugin 3.8.0-alpha-db02c77.0 → 3.8.0-alpha-2d2b247.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.
@@ -9,8 +9,6 @@
9
9
 
10
10
  Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.
11
11
 
12
- > Note: autofix will work only for fields without comments (between or around)
13
-
14
12
  ## Usage Examples
15
13
 
16
14
  ### Incorrect
package/index.js CHANGED
@@ -692,10 +692,7 @@ const rule = {
692
692
  fixable: 'code',
693
693
  docs: {
694
694
  category: ['Schema', 'Operations'],
695
- description: [
696
- 'Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.',
697
- '> Note: autofix will work only for fields without comments (between or around)',
698
- ].join('\n\n'),
695
+ description: `Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.`,
699
696
  url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/alphabetize.md',
700
697
  examples: [
701
698
  {
@@ -853,8 +850,27 @@ const rule = {
853
850
  },
854
851
  create(context) {
855
852
  var _a, _b, _c, _d, _e;
856
- function isOnSameLineNodeAndComment(beforeNode, afterNode) {
857
- return beforeNode.loc.end.line === afterNode.loc.start.line;
853
+ const sourceCode = context.getSourceCode();
854
+ function isNodeAndCommentOnSameLine(node, comment) {
855
+ return node.loc.end.line === comment.loc.start.line;
856
+ }
857
+ function getBeforeComments(node) {
858
+ const commentsBefore = sourceCode.getCommentsBefore(node);
859
+ if (commentsBefore.length === 0) {
860
+ return [];
861
+ }
862
+ const tokenBefore = sourceCode.getTokenBefore(node);
863
+ if (tokenBefore) {
864
+ return commentsBefore.filter(comment => !isNodeAndCommentOnSameLine(tokenBefore, comment));
865
+ }
866
+ return commentsBefore;
867
+ }
868
+ function getRangeWithComments(node) {
869
+ const [firstBeforeComment] = getBeforeComments(node);
870
+ const [firstAfterComment] = sourceCode.getCommentsAfter(node);
871
+ const from = firstBeforeComment || node;
872
+ const to = firstAfterComment && isNodeAndCommentOnSameLine(node, firstAfterComment) ? firstAfterComment : node;
873
+ return [from.range[0], to.range[1]];
858
874
  }
859
875
  function checkNodes(nodes) {
860
876
  // Starts from 1, ignore nodes.length <= 1
@@ -863,39 +879,27 @@ const rule = {
863
879
  const currNode = nodes[i];
864
880
  const prevName = prevNode.name.value;
865
881
  const currName = currNode.name.value;
866
- if (prevName.localeCompare(currName) === 1) {
867
- const isVariableNode = currNode.kind === graphql.Kind.VARIABLE;
868
- context.report({
869
- loc: getLocation(currNode.loc, currName, { offsetEnd: isVariableNode ? 0 : 1 }),
870
- messageId: ALPHABETIZE,
871
- data: isVariableNode
872
- ? {
873
- currName: `$${currName}`,
874
- prevName: `$${prevName}`,
875
- }
876
- : { currName, prevName },
877
- *fix(fixer) {
878
- const prev = prevNode;
879
- const curr = currNode;
880
- const sourceCode = context.getSourceCode();
881
- const beforeComments = sourceCode.getCommentsBefore(prev);
882
- if (beforeComments.length > 0) {
883
- const tokenBefore = sourceCode.getTokenBefore(prev);
884
- const lastBeforeComment = beforeComments[beforeComments.length - 1];
885
- if (!tokenBefore || !isOnSameLineNodeAndComment(tokenBefore, lastBeforeComment))
886
- return;
887
- }
888
- const betweenComments = sourceCode.getCommentsBefore(curr);
889
- if (betweenComments.length > 0)
890
- return;
891
- const [firstAfterComment] = sourceCode.getCommentsAfter(curr);
892
- if (firstAfterComment && isOnSameLineNodeAndComment(curr, firstAfterComment))
893
- return;
894
- yield fixer.replaceText(prev, sourceCode.getText(curr));
895
- yield fixer.replaceText(curr, sourceCode.getText(prev));
896
- },
897
- });
882
+ // Compare with lexicographic order
883
+ if (prevName.localeCompare(currName) !== 1) {
884
+ continue;
898
885
  }
886
+ const isVariableNode = currNode.kind === graphql.Kind.VARIABLE;
887
+ context.report({
888
+ loc: getLocation(currNode.name.loc, currName, { offsetStart: isVariableNode ? 2 : 1 }),
889
+ messageId: ALPHABETIZE,
890
+ data: isVariableNode
891
+ ? {
892
+ currName: `$${currName}`,
893
+ prevName: `$${prevName}`,
894
+ }
895
+ : { currName, prevName },
896
+ *fix(fixer) {
897
+ const prevRange = getRangeWithComments(prevNode);
898
+ const currRange = getRangeWithComments(currNode);
899
+ yield fixer.replaceTextRange(prevRange, sourceCode.getText({ range: currRange }));
900
+ yield fixer.replaceTextRange(currRange, sourceCode.getText({ range: prevRange }));
901
+ },
902
+ });
899
903
  }
900
904
  }
901
905
  const opts = context.options[0];
package/index.mjs CHANGED
@@ -686,10 +686,7 @@ const rule = {
686
686
  fixable: 'code',
687
687
  docs: {
688
688
  category: ['Schema', 'Operations'],
689
- description: [
690
- 'Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.',
691
- '> Note: autofix will work only for fields without comments (between or around)',
692
- ].join('\n\n'),
689
+ description: `Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.`,
693
690
  url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/alphabetize.md',
694
691
  examples: [
695
692
  {
@@ -847,8 +844,27 @@ const rule = {
847
844
  },
848
845
  create(context) {
849
846
  var _a, _b, _c, _d, _e;
850
- function isOnSameLineNodeAndComment(beforeNode, afterNode) {
851
- return beforeNode.loc.end.line === afterNode.loc.start.line;
847
+ const sourceCode = context.getSourceCode();
848
+ function isNodeAndCommentOnSameLine(node, comment) {
849
+ return node.loc.end.line === comment.loc.start.line;
850
+ }
851
+ function getBeforeComments(node) {
852
+ const commentsBefore = sourceCode.getCommentsBefore(node);
853
+ if (commentsBefore.length === 0) {
854
+ return [];
855
+ }
856
+ const tokenBefore = sourceCode.getTokenBefore(node);
857
+ if (tokenBefore) {
858
+ return commentsBefore.filter(comment => !isNodeAndCommentOnSameLine(tokenBefore, comment));
859
+ }
860
+ return commentsBefore;
861
+ }
862
+ function getRangeWithComments(node) {
863
+ const [firstBeforeComment] = getBeforeComments(node);
864
+ const [firstAfterComment] = sourceCode.getCommentsAfter(node);
865
+ const from = firstBeforeComment || node;
866
+ const to = firstAfterComment && isNodeAndCommentOnSameLine(node, firstAfterComment) ? firstAfterComment : node;
867
+ return [from.range[0], to.range[1]];
852
868
  }
853
869
  function checkNodes(nodes) {
854
870
  // Starts from 1, ignore nodes.length <= 1
@@ -857,39 +873,27 @@ const rule = {
857
873
  const currNode = nodes[i];
858
874
  const prevName = prevNode.name.value;
859
875
  const currName = currNode.name.value;
860
- if (prevName.localeCompare(currName) === 1) {
861
- const isVariableNode = currNode.kind === Kind.VARIABLE;
862
- context.report({
863
- loc: getLocation(currNode.loc, currName, { offsetEnd: isVariableNode ? 0 : 1 }),
864
- messageId: ALPHABETIZE,
865
- data: isVariableNode
866
- ? {
867
- currName: `$${currName}`,
868
- prevName: `$${prevName}`,
869
- }
870
- : { currName, prevName },
871
- *fix(fixer) {
872
- const prev = prevNode;
873
- const curr = currNode;
874
- const sourceCode = context.getSourceCode();
875
- const beforeComments = sourceCode.getCommentsBefore(prev);
876
- if (beforeComments.length > 0) {
877
- const tokenBefore = sourceCode.getTokenBefore(prev);
878
- const lastBeforeComment = beforeComments[beforeComments.length - 1];
879
- if (!tokenBefore || !isOnSameLineNodeAndComment(tokenBefore, lastBeforeComment))
880
- return;
881
- }
882
- const betweenComments = sourceCode.getCommentsBefore(curr);
883
- if (betweenComments.length > 0)
884
- return;
885
- const [firstAfterComment] = sourceCode.getCommentsAfter(curr);
886
- if (firstAfterComment && isOnSameLineNodeAndComment(curr, firstAfterComment))
887
- return;
888
- yield fixer.replaceText(prev, sourceCode.getText(curr));
889
- yield fixer.replaceText(curr, sourceCode.getText(prev));
890
- },
891
- });
876
+ // Compare with lexicographic order
877
+ if (prevName.localeCompare(currName) !== 1) {
878
+ continue;
892
879
  }
880
+ const isVariableNode = currNode.kind === Kind.VARIABLE;
881
+ context.report({
882
+ loc: getLocation(currNode.name.loc, currName, { offsetStart: isVariableNode ? 2 : 1 }),
883
+ messageId: ALPHABETIZE,
884
+ data: isVariableNode
885
+ ? {
886
+ currName: `$${currName}`,
887
+ prevName: `$${prevName}`,
888
+ }
889
+ : { currName, prevName },
890
+ *fix(fixer) {
891
+ const prevRange = getRangeWithComments(prevNode);
892
+ const currRange = getRangeWithComments(currNode);
893
+ yield fixer.replaceTextRange(prevRange, sourceCode.getText({ range: currRange }));
894
+ yield fixer.replaceTextRange(currRange, sourceCode.getText({ range: prevRange }));
895
+ },
896
+ });
893
897
  }
894
898
  }
895
899
  const opts = context.options[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-eslint/eslint-plugin",
3
- "version": "3.8.0-alpha-db02c77.0",
3
+ "version": "3.8.0-alpha-2d2b247.0",
4
4
  "description": "GraphQL plugin for ESLint",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {