github.com/googleapis/api-linter@v1.65.2/rules/aip0192/no_html.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0192
    16  
    17  import (
    18  	"regexp"
    19  
    20  	"github.com/googleapis/api-linter/lint"
    21  	"github.com/googleapis/api-linter/rules/internal/utils"
    22  	"github.com/jhump/protoreflect/desc"
    23  )
    24  
    25  var noHTML = &lint.DescriptorRule{
    26  	Name: lint.NewRuleName(192, "no-html"),
    27  	OnlyIf: func(d desc.Descriptor) bool {
    28  		return d.GetSourceInfo() != nil
    29  	},
    30  	LintDescriptor: func(d desc.Descriptor) []lint.Problem {
    31  		for _, comment := range utils.SeparateInternalComments(d.GetSourceInfo().GetLeadingComments()).External {
    32  			if htmlTag.MatchString(comment) {
    33  				return []lint.Problem{{
    34  					Message:    "Comments must not include raw HTML.",
    35  					Descriptor: d,
    36  				}}
    37  			}
    38  		}
    39  		return nil
    40  	},
    41  }
    42  
    43  // Yes, yes, I know: https://stackoverflow.com/questions/1732348/
    44  // Even Jon Skeet cannot parse HTML using regular expressions.
    45  //
    46  // That said, we really only want to pick up "basic HTML smell", and are
    47  // disinterested in actually doing any manipulation, and we can be at least
    48  // a little tolerant of false positives/negatives. To do this, we'll look for
    49  // closing tags (e.g., <foo /> or </foo>) since opening tags are likely to be
    50  // used for variable placeholders (e.g., http://<server>/<path>).
    51  //
    52  // TL;DR: In this case, a regex seems better than taking a dependency just for
    53  // checking if HTML exists in comments.
    54  var htmlTag = regexp.MustCompile(`(</ *[a-zA-Z-]+>|<[a-zA-Z-]+ */>)`)