11import re
2+ from typing import List , Optional
23
34CONVENTIONAL_TYPES = ["feat" , "fix" ]
45DEFAULT_TYPES = [
@@ -26,8 +27,20 @@ def r_types(types):
2627 return "|" .join (types )
2728
2829
29- def r_scope (optional = True ):
30+ def _get_scope_pattern (scopes : Optional [List [str ]] = None ):
31+ scopes_str = r_types (scopes )
32+ escaped_delimiters = list (map (re .escape , [":" , "," , "-" , "/" ])) # type: ignore
33+ delimiters_pattern = r_types (escaped_delimiters )
34+ return rf"\(\s*(?:{ scopes_str } )(?:\s*(?:{ delimiters_pattern } )\s*(?:{ scopes_str } ))*\s*\)"
35+
36+
37+ def r_scope (optional = True , scopes : Optional [List [str ]] = None ):
3038 """Regex str for an optional (scope)."""
39+
40+ if scopes :
41+ scopes_pattern = _get_scope_pattern (scopes )
42+ return scopes_pattern
43+
3144 if optional :
3245 return r"(\([\w \/:,-]+\))?"
3346 else :
@@ -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