11import re
2+ from typing import List , Optional
23
34CONVENTIONAL_TYPES = ["feat" , "fix" ]
45DEFAULT_TYPES = [
@@ -26,11 +27,23 @@ def r_types(types):
2627 return "|" .join (types )
2728
2829
29- def r_scope (optional = True ):
30+ def r_scope (optional = True , scopes : Optional [ List [ str ]] = None ):
3031 """Regex str for an optional (scope)."""
32+
3133 if optional :
34+ if scopes :
35+ scopes_str = r_types (scopes )
36+ # delims_str = r_types([":", ",", "-"])
37+ escaped_delimiters = list (map (re .escape , [":" , "," ])) # type: ignore
38+ delimiters_pattern = r_types (escaped_delimiters )
39+ return rf"\(\s*(?:{ scopes_str } )(?:\s*(?:{ delimiters_pattern } )\s*(?:{ scopes_str } ))*\s*\)"
3240 return r"(\([\w \/:,-]+\))?"
3341 else :
42+ if scopes :
43+ scopes_str = r_types (scopes )
44+ escaped_delimiters = list (map (re .escape , [":" , "," ])) # type: ignore
45+ delimiters_pattern = r_types (escaped_delimiters )
46+ return rf"\(\s*(?:{ scopes_str } )(?:\s*(?:{ delimiters_pattern } )\s*(?:{ scopes_str } ))*\s*\)"
3447 return r"(\([\w \/:,-]+\))"
3548
3649
@@ -79,7 +92,7 @@ def conventional_types(types=[]):
7992 return types
8093
8194
82- def is_conventional (input , types = DEFAULT_TYPES , optional_scope = True ):
95+ def is_conventional (input , types = DEFAULT_TYPES , optional_scope = True , scopes : Optional [ list [ str ]] = None ):
8396 """
8497 Returns True if input matches Conventional Commits formatting
8598 https://www.conventionalcommits.org
@@ -89,7 +102,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
89102 input = strip_verbose_diff (input )
90103 input = strip_comments (input )
91104 types = conventional_types (types )
92- pattern = f"^({ r_types (types )} ){ r_scope (optional_scope )} { r_delim ()} { r_subject ()} { r_body ()} "
105+ pattern = f"^({ r_types (types )} ){ r_scope (optional_scope , scopes = scopes )} { r_delim ()} { r_subject ()} { r_body ()} "
93106 regex = re .compile (pattern , re .MULTILINE )
94107
95108 result = regex .match (input )
0 commit comments