|  | 
|  | 1 | +import { GraphQLESLintRule } from '../types'; | 
|  | 2 | +import { TokenKind } from 'graphql'; | 
|  | 3 | + | 
|  | 4 | +const HASHTAG_COMMENT = 'HASHTAG_COMMENT'; | 
|  | 5 | + | 
|  | 6 | +const rule: GraphQLESLintRule = { | 
|  | 7 | +  meta: { | 
|  | 8 | +    messages: { | 
|  | 9 | +      [HASHTAG_COMMENT]: `Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.`, | 
|  | 10 | +    }, | 
|  | 11 | +    docs: { | 
|  | 12 | +      description: `Requires to use """ or " for adding a GraphQL description instead of #.\nThis rule allows you to use hashtag for comments, as long as it's not attached to a AST definition.`, | 
|  | 13 | +      category: 'Best Practices', | 
|  | 14 | +      url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-hashtag-description.md`, | 
|  | 15 | +      requiresSchema: false, | 
|  | 16 | +      requiresSiblings: false, | 
|  | 17 | +      examples: [ | 
|  | 18 | +        { | 
|  | 19 | +          title: 'Incorrect', | 
|  | 20 | +          code: /* GraphQL */ ` | 
|  | 21 | +            # Represents a user | 
|  | 22 | +            type User { | 
|  | 23 | +              id: ID! | 
|  | 24 | +              name: String | 
|  | 25 | +            } | 
|  | 26 | +          `, | 
|  | 27 | +        }, | 
|  | 28 | +        { | 
|  | 29 | +          title: 'Correct', | 
|  | 30 | +          code: /* GraphQL */ ` | 
|  | 31 | +            " Represents a user " | 
|  | 32 | +            type User { | 
|  | 33 | +              id: ID! | 
|  | 34 | +              name: String | 
|  | 35 | +            } | 
|  | 36 | +          `, | 
|  | 37 | +        }, | 
|  | 38 | +        { | 
|  | 39 | +          title: 'Correct', | 
|  | 40 | +          code: /* GraphQL */ ` | 
|  | 41 | +            # This file defines the basic User type. | 
|  | 42 | +            # This comment is valid because it's not attached specifically to an AST object. | 
|  | 43 | +
 | 
|  | 44 | +            " Represents a user " | 
|  | 45 | +            type User { | 
|  | 46 | +              id: ID! # This one is also valid, since it comes after the AST object | 
|  | 47 | +              name: String | 
|  | 48 | +            } | 
|  | 49 | +          `, | 
|  | 50 | +        }, | 
|  | 51 | +      ], | 
|  | 52 | +    }, | 
|  | 53 | +    type: 'suggestion', | 
|  | 54 | +  }, | 
|  | 55 | +  create(context) { | 
|  | 56 | +    return { | 
|  | 57 | +      Document(node) { | 
|  | 58 | +        if (node) { | 
|  | 59 | +          const rawNode = node.rawNode(); | 
|  | 60 | + | 
|  | 61 | +          if (rawNode && rawNode.loc && rawNode.loc.startToken) { | 
|  | 62 | +            let token = rawNode.loc.startToken; | 
|  | 63 | + | 
|  | 64 | +            while (token !== null) { | 
|  | 65 | +              if (token.kind === TokenKind.COMMENT && token.next && token.prev) { | 
|  | 66 | +                if ( | 
|  | 67 | +                  token.prev.kind !== TokenKind.SOF && | 
|  | 68 | +                  token.prev.kind !== TokenKind.COMMENT && | 
|  | 69 | +                  token.next.kind !== TokenKind.COMMENT && | 
|  | 70 | +                  token.next.line - token.line > 1 && | 
|  | 71 | +                  token.prev.line !== token.line | 
|  | 72 | +                ) { | 
|  | 73 | +                  context.report({ | 
|  | 74 | +                    messageId: HASHTAG_COMMENT, | 
|  | 75 | +                    loc: { | 
|  | 76 | +                      start: { | 
|  | 77 | +                        line: token.line, | 
|  | 78 | +                        column: token.column, | 
|  | 79 | +                      }, | 
|  | 80 | +                      end: { | 
|  | 81 | +                        line: token.line, | 
|  | 82 | +                        column: token.column, | 
|  | 83 | +                      }, | 
|  | 84 | +                    }, | 
|  | 85 | +                  }); | 
|  | 86 | +                } else if ( | 
|  | 87 | +                  token.next.kind !== TokenKind.COMMENT && | 
|  | 88 | +                  token.next.kind !== TokenKind.EOF && | 
|  | 89 | +                  token.next.line - token.line < 2 && | 
|  | 90 | +                  token.prev.line !== token.line | 
|  | 91 | +                ) { | 
|  | 92 | +                  context.report({ | 
|  | 93 | +                    messageId: HASHTAG_COMMENT, | 
|  | 94 | +                    loc: { | 
|  | 95 | +                      start: { | 
|  | 96 | +                        line: token.line, | 
|  | 97 | +                        column: token.column, | 
|  | 98 | +                      }, | 
|  | 99 | +                      end: { | 
|  | 100 | +                        line: token.line, | 
|  | 101 | +                        column: token.column, | 
|  | 102 | +                      }, | 
|  | 103 | +                    }, | 
|  | 104 | +                  }); | 
|  | 105 | +                } | 
|  | 106 | +              } | 
|  | 107 | + | 
|  | 108 | +              token = token.next; | 
|  | 109 | +            } | 
|  | 110 | +          } | 
|  | 111 | +        } | 
|  | 112 | +      }, | 
|  | 113 | +    }; | 
|  | 114 | +  }, | 
|  | 115 | +}; | 
|  | 116 | + | 
|  | 117 | +export default rule; | 
0 commit comments