| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | package rule | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-10-17 20:12:36 +01:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 	"regexp" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mgechev/revive/lint" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-01 17:44:41 +02:00
										 |  |  | // FileHeaderRule lints the header that each file should have. | 
					
						
							| 
									
										
										
										
											2021-10-17 20:34:48 +02:00
										 |  |  | type FileHeaderRule struct { | 
					
						
							|  |  |  | 	header string | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | var ( | 
					
						
							| 
									
										
										
										
											2022-04-10 11:55:13 +02:00
										 |  |  | 	multiRegexp  = regexp.MustCompile(`^/\*`) | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	singleRegexp = regexp.MustCompile("^//") | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-13 21:38:46 +01:00
										 |  |  | // Configure validates the rule configuration, and configures the rule accordingly. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Configuration implements the [lint.ConfigurableRule] interface. | 
					
						
							|  |  |  | func (r *FileHeaderRule) Configure(arguments lint.Arguments) error { | 
					
						
							| 
									
										
										
										
											2024-10-01 12:14:02 +02:00
										 |  |  | 	if len(arguments) < 1 { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:35:58 +01:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2024-10-01 12:14:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ok bool | 
					
						
							|  |  |  | 	r.header, ok = arguments[0].(string) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:35:58 +01:00
										 |  |  | 		return fmt.Errorf(`invalid argument for "file-header" rule: argument should be a string, got %T`, arguments[0]) | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-12-11 19:35:58 +01:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2022-04-10 09:06:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Apply applies the rule to given file. | 
					
						
							| 
									
										
										
										
											2024-12-13 21:38:46 +01:00
										 |  |  | func (r *FileHeaderRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | 
					
						
							| 
									
										
										
										
											2023-05-20 14:44:34 +02:00
										 |  |  | 	if r.header == "" { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	failure := []lint.Failure{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Node:       file.AST, | 
					
						
							|  |  |  | 			Confidence: 1, | 
					
						
							|  |  |  | 			Failure:    "the file doesn't have an appropriate header", | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	if len(file.AST.Comments) == 0 { | 
					
						
							|  |  |  | 		return failure | 
					
						
							| 
									
										
										
										
											2019-11-28 01:29:59 +07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	g := file.AST.Comments[0] | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 	if g == nil { | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 		return failure | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	comment := "" | 
					
						
							|  |  |  | 	for _, c := range g.List { | 
					
						
							|  |  |  | 		text := c.Text | 
					
						
							| 
									
										
										
										
											2021-09-13 02:05:42 +03:00
										 |  |  | 		if multiRegexp.MatchString(text) { | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 			text = text[2 : len(text)-2] | 
					
						
							| 
									
										
										
										
											2021-09-13 02:05:42 +03:00
										 |  |  | 		} else if singleRegexp.MatchString(text) { | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 			text = text[2:] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		comment += text | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-17 20:34:48 +02:00
										 |  |  | 	regex, err := regexp.Compile(r.header) | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:35:58 +01:00
										 |  |  | 		return newInternalFailureError(err) | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 02:05:42 +03:00
										 |  |  | 	if !regex.MatchString(comment) { | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 		return failure | 
					
						
							| 
									
										
										
										
											2018-02-04 14:51:19 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Name returns the rule name. | 
					
						
							| 
									
										
										
										
											2022-04-10 11:55:13 +02:00
										 |  |  | func (*FileHeaderRule) Name() string { | 
					
						
							| 
									
										
										
										
											2019-11-28 10:14:21 +07:00
										 |  |  | 	return "file-header" | 
					
						
							|  |  |  | } |