github.com/Zenithar/prototool@v1.3.0/internal/lint/check_comments_no_c_style.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package lint
    22  
    23  import (
    24  	"text/scanner"
    25  
    26  	"github.com/emicklei/proto"
    27  	"github.com/uber/prototool/internal/text"
    28  )
    29  
    30  var commentsNoCStyleLinter = NewLinter(
    31  	"COMMENTS_NO_C_STYLE",
    32  	"Verifies that there are no /* c-style */ comments.",
    33  	checkCommentsNoCStyle,
    34  )
    35  
    36  func checkCommentsNoCStyle(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error {
    37  	return runVisitor(&commentsNoCStyleVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
    38  }
    39  
    40  type commentsNoCStyleVisitor struct {
    41  	baseAddVisitor
    42  }
    43  
    44  func (v commentsNoCStyleVisitor) VisitMessage(element *proto.Message) {
    45  	v.checkComments(element.Position, element.Comment)
    46  	for _, child := range element.Elements {
    47  		child.Accept(v)
    48  	}
    49  }
    50  
    51  func (v commentsNoCStyleVisitor) VisitService(element *proto.Service) {
    52  	v.checkComments(element.Position, element.Comment)
    53  	for _, child := range element.Elements {
    54  		child.Accept(v)
    55  	}
    56  }
    57  
    58  func (v commentsNoCStyleVisitor) VisitSyntax(element *proto.Syntax) {
    59  	v.checkComments(element.Position, element.Comment, element.InlineComment)
    60  }
    61  
    62  func (v commentsNoCStyleVisitor) VisitPackage(element *proto.Package) {
    63  	v.checkComments(element.Position, element.Comment, element.InlineComment)
    64  }
    65  
    66  func (v commentsNoCStyleVisitor) VisitOption(element *proto.Option) {
    67  	v.checkComments(element.Position, element.Comment, element.InlineComment)
    68  }
    69  
    70  func (v commentsNoCStyleVisitor) VisitImport(element *proto.Import) {
    71  	v.checkComments(element.Position, element.Comment, element.InlineComment)
    72  }
    73  
    74  func (v commentsNoCStyleVisitor) VisitNormalField(element *proto.NormalField) {
    75  	v.checkComments(element.Position, element.Comment, element.InlineComment)
    76  	for _, child := range element.Options {
    77  		child.Accept(v)
    78  	}
    79  }
    80  
    81  func (v commentsNoCStyleVisitor) VisitEnumField(element *proto.EnumField) {
    82  	v.checkComments(element.Position, element.Comment, element.InlineComment)
    83  	if element.ValueOption != nil {
    84  		element.ValueOption.Accept(v)
    85  	}
    86  }
    87  
    88  func (v commentsNoCStyleVisitor) VisitEnum(element *proto.Enum) {
    89  	v.checkComments(element.Position, element.Comment)
    90  	for _, child := range element.Elements {
    91  		child.Accept(v)
    92  	}
    93  }
    94  
    95  func (v commentsNoCStyleVisitor) VisitComment(element *proto.Comment) {
    96  	v.checkComments(element.Position, element)
    97  }
    98  
    99  func (v commentsNoCStyleVisitor) VisitOneof(element *proto.Oneof) {
   100  	v.checkComments(element.Position, element.Comment)
   101  	for _, child := range element.Elements {
   102  		child.Accept(v)
   103  	}
   104  }
   105  
   106  func (v commentsNoCStyleVisitor) VisitOneofField(element *proto.OneOfField) {
   107  	v.checkComments(element.Position, element.Comment, element.InlineComment)
   108  	for _, child := range element.Options {
   109  		child.Accept(v)
   110  	}
   111  }
   112  
   113  func (v commentsNoCStyleVisitor) VisitReserved(element *proto.Reserved) {
   114  	v.checkComments(element.Position, element.Comment, element.InlineComment)
   115  }
   116  
   117  func (v commentsNoCStyleVisitor) VisitRPC(element *proto.RPC) {
   118  	v.checkComments(element.Position, element.Comment, element.InlineComment)
   119  	for _, child := range element.Elements {
   120  		child.Accept(v)
   121  	}
   122  }
   123  
   124  func (v commentsNoCStyleVisitor) VisitMapField(element *proto.MapField) {
   125  	v.checkComments(element.Position, element.Comment, element.InlineComment)
   126  	for _, child := range element.Options {
   127  		child.Accept(v)
   128  	}
   129  }
   130  
   131  func (v commentsNoCStyleVisitor) VisitGroup(element *proto.Group) {
   132  	v.checkComments(element.Position, element.Comment)
   133  	for _, child := range element.Elements {
   134  		child.Accept(v)
   135  	}
   136  }
   137  
   138  func (v commentsNoCStyleVisitor) VisitExtensions(element *proto.Extensions) {
   139  	v.checkComments(element.Position, element.Comment, element.InlineComment)
   140  }
   141  
   142  func (v commentsNoCStyleVisitor) checkComments(position scanner.Position, comments ...*proto.Comment) {
   143  	for _, comment := range comments {
   144  		if comment != nil {
   145  			if comment.Cstyle {
   146  				v.AddFailuref(position, "C-Style comments are not allowed.")
   147  			}
   148  		}
   149  	}
   150  }