10000 Display params default values (+ test case) · pyodide/sphinx-js@78c70c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 78c70c4

Browse files
committed
Display params default values (+ test case)
1 parent b31c62b commit 78c70c4

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

sphinx_js/renderers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ def _formal_params(self, doclet):
115115

116116
# Harvest params from the @param tag unless they collide with an
117117
# explicit formal param. Even look at params that are really
118-
# documenting subproperties of formal params.
119-
params = reduce(
120-
lambda l, v: l + [v] if not v in l else l,
121-
[param['name'].split('.')[0] for param in doclet.get('params', [])],
122-
[])
118+
# documenting subproperties of formal params. Also handles params
119+
# default values.
120+
params = ["%s=%s" % (param, value) if value else param for param, value in reduce(
121+
lambda params, param: params + [param] if not param[0] in [i[0] for i in params] else params,
122+
[(param['name'].split(".")[0], param.get('defaultvalue')) for param in doclet.get('params', [])],
123+
[])]
123124

124125
# Use params from JS code if there are no documented params:
125126
if not params:

tests/test_build/source/code.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,9 @@ const ExampleAttribute = null;
149149
* @param {String} p2.bar
150150
*/
151151
function destructuredParams(p1, {foo, bar}) {}
152+
153+
/**
154+
* @param {number} [p1=42]
155+
* @param {string} [p2="foobar"]
156+
*/
157+
function defaultValues(p1=42, p2="foobar") {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.. js:autofunction:: defaultValues

tests/test_build/test_build.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def test_autofunction_destructured_params(self):
7272
' * **p2.foo** (*String*) --\n\n'
7373
' * **p2.bar** (*String*) --\n')
7474

75+
def test_autofunction_defaut_values(self):
76+
"Make shure params default values appear in the function definition."""
77+
self._file_contents_eq(
78+
'autofunction_default_values',
79+
u'defaultValues(p1=42, p2="foobar")\n\n'
80+
' Arguments:\n'
81+
' * **p1** (*number*) --\n\n'
82+
' * **p2** (*string*) --\n')
83+
7584
def test_autoclass(self):
7685
"""Make sure classes show their class comment and constructor
7786
comment."""

0 commit comments

Comments
 (0)
0