Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Put "static" in front of static methods. Close #174.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrose committed May 6, 2021
2 parents 91d35b8 + ca2eed5 commit a1ee456
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 4 deletions.
7 changes: 5 additions & 2 deletions sphinx_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from .directives import (auto_class_directive_bound_to_app,
auto_function_directive_bound_to_app,
auto_attribute_directive_bound_to_app)
auto_attribute_directive_bound_to_app,
JSStaticFunction)
from .jsdoc import Analyzer as JsAnalyzer
from .typedoc import Analyzer as TsAnalyzer


def setup(app):
# I believe this is the best place to run jsdoc. I was tempted to use
# app.add_source_parser(), but I think the kind of source it's referring to
Expand All @@ -17,6 +17,9 @@ def setup(app):

app.connect('env-before-read-docs', read_all_docs)

app.add_directive_to_domain('js',
'staticfunction',
JSStaticFunction)
app.add_directive_to_domain('js',
'autofunction',
auto_function_directive_bound_to_app(app))
Expand Down
10 changes: 9 additions & 1 deletion sphinx_js/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
"""
from docutils.parsers.rst import Directive
from docutils.parsers.rst.directives import flag
from sphinx.domains.javascript import JSCallable

from .renderers import AutoFunctionRenderer, AutoClassRenderer, AutoAttributeRenderer
from .renderers import (AutoFunctionRenderer,
AutoClassRenderer,
AutoAttributeRenderer)


class JsDirective(Directive):
Expand Down Expand Up @@ -83,3 +86,8 @@ def _members_to_exclude(arg):
"""
return set(a.strip() for a in (arg or '').split(','))


class JSStaticFunction(JSCallable):
"""Like a callable but with a different prefix."""
display_prefix = 'static '
6 changes: 5 additions & 1 deletion sphinx_js/jsdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _doclet_as_function(doclet, full_path):
exported_from=None,
is_abstract=False,
is_optional=False,
is_static=False,
is_static=is_static(doclet),
is_private=is_private(doclet),
exceptions=exceptions_to_ir(doclet.get('exceptions', [])),
returns=returns_to_ir(doclet.get('returns', [])),
Expand All @@ -156,6 +156,10 @@ def is_private(doclet):
return doclet.get('access') == 'private'


def is_static(doclet):
return doclet.get('scope') == 'static'


def full_path_segments(d, base_dir, longname_field='longname'):
"""Return the full, unambiguous list of path segments that points to an
entity described by a doclet.
Expand Down
1 change: 1 addition & 0 deletions sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def _template_vars(self, name, obj):
examples=obj.examples,
deprecated=obj.deprecated,
is_optional=obj.is_optional,
is_static=obj.is_static,
see_also=obj.see_alsos,
content='\n'.join(self._content))

Expand Down
4 changes: 4 additions & 0 deletions sphinx_js/templates/function.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% import 'common.rst' as common %}

{% if is_static %}
.. js:staticfunction:: {{ name }}{{ '?' if is_optional else '' }}{{ params }}
{% else %}
.. js:function:: {{ name }}{{ '?' if is_optional else '' }}{{ params }}
{% endif %}

{{ common.deprecated(deprecated)|indent(3) }}

Expand Down
11 changes: 11 additions & 0 deletions tests/test_build_js/source/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,14 @@ function union(fnodeA) {
*/
function longDescriptions(a, b) {
}

/**
* Class doc.
*/
class SimpleClass {

/**
* Static.
*/
static noUseOfThis() {}
}
2 changes: 2 additions & 0 deletions tests/test_build_js/source/docs/autofunction_static.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.. js:autoclass:: SimpleClass
:members:
11 changes: 11 additions & 0 deletions tests/test_build_js/test_build_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ def test_autofunction_see(self):
' * "deprecatedFunction"\n\n'
' * "DeprecatedAttribute"\n')

def test_autofunction_static(self):
"""Make sure the static function gets its prefix ``static``."""
self._file_contents_eq(
'autofunction_static',
'class SimpleClass()\n\n'
' Class doc.\n'
'\n'
' static SimpleClass.noUseOfThis()\n'
'\n'
' Static.\n')

def test_autoclass(self):
"""Make sure classes show their class comment and constructor
comment."""
Expand Down

0 comments on commit a1ee456

Please sign in to comment.