Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: tools/json_schema_compiler/js_externs_generator.py

Issue 1036593004: [Extension API Extern Generation] Add support for function types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/json_schema_compiler/js_externs_generator_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/json_schema_compiler/js_externs_generator.py
diff --git a/tools/json_schema_compiler/js_externs_generator.py b/tools/json_schema_compiler/js_externs_generator.py
index 50a4f4d8655cc4d51bd92353afade8f4402da443..8512ac4661aef6b0f8b826f09232cd9ba1eedcb3 100644
--- a/tools/json_schema_compiler/js_externs_generator.py
+++ b/tools/json_schema_compiler/js_externs_generator.py
@@ -139,40 +139,46 @@ class _Generator(object):
c = Code()
c.Append('/**')
+ lines = []
if function.description:
- for line in function.description.split('\n'):
- c.Comment(line, comment_prefix=' * ')
+ lines.extend(function.description.splitlines())
for param in function.params:
js_type = self._TypeToJsType(param.type_)
-
if param.optional:
js_type += '='
-
- param_doc = '@param {%s} %s %s' % (js_type,
- param.name,
- param.description or '')
- c.Comment(param_doc, comment_prefix=' * ')
+ lines.append('@param {%s} %s %s' % (js_type,
+ param.name,
+ param.description or ''))
if function.callback:
- # TODO(tbreisacher): Convert Function to function() for better
- # typechecking.
- js_type = 'Function'
- if function.callback.optional:
- js_type += '='
- param_doc = '@param {%s} %s %s' % (js_type,
- function.callback.name,
- function.callback.description or '')
- c.Comment(param_doc, comment_prefix=' * ')
+ lines.append('@param {%s} %s %s' % (
+ self._FunctionToJsFunction(function.callback),
+ function.callback.name,
+ function.callback.description or ''))
if function.returns:
- return_doc = '@return {%s} %s' % (self._TypeToJsType(function.returns),
- function.returns.description)
- c.Comment(return_doc, comment_prefix=' * ')
+ lines.append('@return {%s} %s' % (self._TypeToJsType(function.returns),
+ function.returns.description or ''))
+
+ if function.deprecated:
+ lines.append('@deprecated %s' % function.deprecated)
+
+ for line in lines:
+ c.Comment(line, comment_prefix=' * ');
c.Append(' */')
return c
+ def _FunctionToJsFunction(self, function):
+ """Converts a model.Function to a JS type (i.e., function([params])...)"""
+ params = ', '.join(
+ [self._TypeToJsType(param.type_) for param in function.params])
+ return_type = (
+ self._TypeToJsType(function.returns) if function.returns else 'void')
+ optional = '=' if function.optional else ''
+ return 'function(%s):%s%s' % (params, return_type, optional)
+
def _TypeToJsType(self, js_type):
"""Converts a model.Type to a JS type (number, Array, etc.)"""
if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE):
@@ -191,6 +197,8 @@ class _Generator(object):
elif js_type.property_type is PropertyType.CHOICES:
return '(%s)' % '|'.join(
[self._TypeToJsType(choice) for choice in js_type.choices])
+ elif js_type.property_type is PropertyType.FUNCTION:
+ return self._FunctionToJsFunction(js_type.function)
elif js_type.property_type is PropertyType.ANY:
return '*'
elif js_type.property_type.is_fundamental:
« no previous file with comments | « no previous file | tools/json_schema_compiler/js_externs_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698