Skip to content

Commit c85cf60

Browse files
authored
Merge pull request #1 from Penify-dev/penify/auto_doc_61bb1ce_3b506
[Penify]: Documentation for commit - 61bb1ce
2 parents 52fe47e + cdd519e commit c85cf60

4 files changed

Lines changed: 74 additions & 51 deletions

File tree

docstring_parser/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@
1414

1515

1616
def main():
17-
"""Entry point for the command-line interface."""
17+
"""Entry point for the command-line interface.
18+
19+
This function sets up an argument parser to handle different commands (`parse`,
20+
`compose`, and `remove`) related to JSDoc strings. It processes the input data
21+
based on the specified command, performs the required operations (parsing,
22+
composing, or removing components), and handles errors appropriately. The
23+
results are then output to either a file or standard output.
24+
"""
1825
parser = argparse.ArgumentParser(description='Parse and compose JSDoc strings')
1926
subparsers = parser.add_subparsers(dest='command', required=True, help='Command to execute')
2027

docstring_parser/composer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55

66
def compose_jsdoc(jsdoc_obj: Dict[str, Any]) -> str:
7-
"""
8-
Compose a JSDoc string from a structured dictionary.
7+
"""Compose a JSDoc string from a structured dictionary.
8+
9+
This function constructs a JSDoc comment based on the provided dictionary
10+
structure, handling various components such as descriptions, parameters,
11+
returns, throws, examples, and other tags. It ensures that each section is
12+
correctly formatted and included in the final JSDoc string if present in the
13+
input dictionary.
914
1015
Args:
11-
jsdoc_obj (Dict[str, Any]): The dictionary representing the JSDoc structure
12-
16+
jsdoc_obj (Dict[str, Any]): The dictionary representing the JSDoc structure.
17+
1318
Returns:
14-
str: The formatted JSDoc string
15-
16-
Example:
17-
>>> compose_jsdoc({'description': 'Description', 'params': [{'name': 'name', 'type': 'string', 'description': 'The name'}]})
18-
'/**\\n * Description\\n * @param {string} name - The name\\n */'
19+
str: The formatted JSDoc string.
1920
"""
2021
lines = ['/**']
2122

docstring_parser/parser.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55

66

77
def parse_jsdoc(docstring: str) -> Dict[str, Any]:
8-
"""
9-
Parse a JSDoc string into a structured dictionary.
8+
# Initialize the result dictionary
9+
"""Parse a JSDoc string into a structured dictionary.
10+
11+
This function processes a JSDoc string to extract structured information such
12+
as description, parameters, return values, exceptions, examples, and other
13+
tags. It handles the removal of opening and closing markers, splits the content
14+
into lines, and categorizes each line based on whether it is part of a tag or
15+
the main description.
1016
1117
Args:
12-
docstring (str): The JSDoc string to parse
13-
14-
Returns:
15-
Dict[str, Any]: A dictionary representing the parsed JSDoc structure
18+
docstring (str): The JSDoc string to parse.
1619
17-
Example:
18-
>>> parse_jsdoc("/**\\n * Description\\n * @param {string} name - The name\\n */")
19-
{'description': 'Description', 'params': [{'name': 'name', 'type': 'string', 'description': 'The name'}]}
20+
Returns:
21+
Dict[str, Any]: A dictionary representing the parsed JSDoc structure.
2022
"""
21-
# Initialize the result dictionary
2223
result = {
2324
'description': '',
2425
'params': [],
@@ -88,13 +89,20 @@ def parse_jsdoc(docstring: str) -> Dict[str, Any]:
8889

8990

9091
def _process_tag(tag: str, content: List[str], result: Dict[str, Any]) -> None:
91-
"""
92-
Process a JSDoc tag and update the result dictionary.
92+
"""Process a JSDoc tag and update the result dictionary.
93+
94+
The function processes different types of JSDoc tags such as `param`,
95+
`returns`, `throws`, `example`, and others, updating the provided result
96+
dictionary accordingly.
9397
9498
Args:
95-
tag (str): The tag name (without the @ symbol)
96-
content (List[str]): The content lines associated with the tag
97-
result (Dict[str, Any]): The dictionary to update
99+
tag (str): The tag name (without the @ symbol).
100+
content (List[str]): The content lines associated with the tag.
101+
result (Dict[str, Any]): The dictionary to update.
102+
103+
Returns:
104+
None: The function modifies the `result` dictionary in place and does not return any
105+
value.
98106
"""
99107
content_str = ' '.join(content).strip()
100108

docstring_parser/utils.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55

66

77
def extract_type_info(type_str: str) -> Dict[str, Any]:
8-
"""
9-
Extract detailed type information from a JSDoc type string.
8+
"""Extract detailed type information from a JSDoc type string.
9+
10+
This function parses the input type string to determine if it represents a
11+
union of types, a generic/template type with parameters, or a simple type. It
12+
returns a dictionary containing the parsed type information.
1013
1114
Args:
12-
type_str (str): The type string from a JSDoc tag
13-
15+
type_str (str): The type string from a JSDoc tag.
16+
1417
Returns:
15-
Dict[str, Any]: A dictionary with parsed type information
16-
17-
Example:
18-
>>> extract_type_info('Array<string>')
19-
{'name': 'Array', 'params': ['string']}
20-
>>> extract_type_info('Object<string, number>')
21-
{'name': 'Object', 'params': ['string', 'number']}
22-
>>> extract_type_info('string|number')
23-
{'union': ['string', 'number']}
18+
Dict[str, Any]: A dictionary with parsed type information.
19+
- If the type is a union, it includes a 'union' key with a list of types.
20+
- If the type is generic/template, it includes 'name' and 'params' keys.
21+
- If the type is simple, it only includes a 'name' key.
2422
"""
2523
result = {}
2624

@@ -65,15 +63,19 @@ def extract_type_info(type_str: str) -> Dict[str, Any]:
6563

6664

6765
def merge_jsdoc_objects(base: Dict[str, Any], overlay: Dict[str, Any]) -> Dict[str, Any]:
68-
"""
69-
Merge two JSDoc objects, with the overlay taking precedence.
66+
"""Merge two JSDoc objects, with the overlay taking precedence.
67+
68+
The function merges two dictionaries representing JSDoc objects. If a key
69+
exists in both the base and overlay, the value from the overlay takes
70+
precedence. For keys like 'params' and 'throws', it combines the lists while
71+
ensuring that items with matching names are updated rather than duplicated.
7072
7173
Args:
72-
base (Dict[str, Any]): The base JSDoc object
73-
overlay (Dict[str, Any]): The overlay JSDoc object that takes precedence
74-
74+
base (Dict[str, Any]): The base JSDoc object.
75+
overlay (Dict[str, Any]): The overlay JSDoc object that takes precedence.
76+
7577
Returns:
76-
Dict[str, Any]: The merged JSDoc object
78+
Dict[str, Any]: The merged JSDoc object.
7779
"""
7880
result = base.copy()
7981

@@ -128,16 +130,21 @@ def merge_jsdoc_objects(base: Dict[str, Any], overlay: Dict[str, Any]) -> Dict[s
128130

129131

130132
def remove_jsdoc_component(jsdoc_obj: Dict[str, Any], component_type: str, identifier: Optional[str] = None) -> Dict[str, Any]:
131-
"""
132-
Remove a component from a JSDoc object.
133+
"""Remove a specified component from a JSDoc object.
134+
135+
This function modifies the provided JSDoc object by removing a component based
136+
on the given type and identifier. If no identifier is provided, it removes all
137+
instances of the component type. The function handles various component types
138+
such as 'description', 'param', 'returns', 'throws', 'example', and 'tag'.
133139
134140
Args:
135-
jsdoc_obj (Dict[str, Any]): The JSDoc object
136-
component_type (str): The component type to remove ('param', 'returns', 'throws', 'example', 'tag')
137-
identifier (Optional[str]): The identifier of the component (e.g., param name, tag name)
138-
141+
jsdoc_obj (Dict[str, Any]): The JSDoc object to be modified.
142+
component_type (str): The type of component to remove ('param', 'returns', 'throws', 'example',
143+
'tag').
144+
identifier (Optional[str]): An optional identifier to specify which instance of the component to remove.
145+
139146
Returns:
140-
Dict[str, Any]: The modified JSDoc object
147+
Dict[str, Any]: The modified JSDoc object with the specified component removed.
141148
"""
142149
result = jsdoc_obj.copy()
143150

0 commit comments

Comments
 (0)