Skip to content

Commit

Permalink
[Extension API Extern Generation] Fix a few bugs in extern generation
Browse files Browse the repository at this point in the history
Properly handle the case of an "object" (one deliberately left undefined in the
idl/json), and fix an off-by-one error in the comment wrapping.
Add tests for each.

BUG=469920

Review URL: https://codereview.chromium.org/1062573004

Cr-Commit-Position: refs/heads/master@{#324063}
  • Loading branch information
rdcronin authored and Commit bot committed Apr 7, 2015
1 parent 155d7e5 commit 6841111
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tools/json_schema_compiler/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def trim_comment(comment, max_len):

# First line has the full maximum length.
if not new_line and self._code:
max_len = self._comment_length - len(self._code[-1].value) - 1
max_len = self._comment_length - len(self._code[-1].value)
else:
max_len = (self._comment_length - len(''.join(self._line_prefixes)) -
len(comment_prefix))
Expand Down
11 changes: 10 additions & 1 deletion tools/json_schema_compiler/code_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,23 @@ def testLinePrefixes(self):
' */',
output)

def testSameLineAppendAndConcat(self):
def testSameLineAppendConcatComment(self):
c = Code()
c.Append('This is a line.')
c.Append('This too.', new_line=False)
d = Code()
d.Append('And this.')
c.Concat(d, new_line=False)
self.assertEquals('This is a line.This too.And this.', c.Render())
c = Code()
c.Append('This is a')
c.Comment(' spectacular 80-character line thingy ' +
'that fits wonderfully everywhere.',
comment_prefix='',
new_line=False)
self.assertEquals('This is a spectacular 80-character line thingy that ' +
'fits wonderfully everywhere.',
c.Render())

if __name__ == '__main__':
unittest.main()
39 changes: 20 additions & 19 deletions tools/json_schema_compiler/js_externs_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _GenerateObjectDefinition(self, properties):
"""Given an OrderedDict of properties, returns a Code containing the
description of an object.
"""
if not properties: return ''
if not properties: return Code()

c = Code()
c.Sblock('{')
Expand Down Expand Up @@ -212,38 +212,39 @@ def _FunctionToJsFunction(self, function):

def _TypeToJsType(self, js_type):
"""Converts a model.Type to a JS type (number, Array, etc.)"""
c = Code()
if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE):
c.Append('number')
elif js_type.property_type is PropertyType.OBJECT:
c = self._GenerateObjectDefinition(js_type.properties)
elif js_type.property_type is PropertyType.ARRAY:
(c.Append('!Array<').
return Code().Append('number')
if js_type.property_type is PropertyType.OBJECT:
if js_type.properties:
return self._GenerateObjectDefinition(js_type.properties)
return Code().Append('Object')
if js_type.property_type is PropertyType.ARRAY:
return (Code().Append('!Array<').
Concat(self._TypeToJsType(js_type.item_type), new_line=False).
Append('>', new_line=False))
elif js_type.property_type is PropertyType.REF:
if js_type.property_type is PropertyType.REF:
ref_type = js_type.ref_type
# Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply
# as MyType.
if self._namespace.types[ref_type].property_type is PropertyType.ENUM:
ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type)
c.Append(ref_type)
elif js_type.property_type is PropertyType.CHOICES:
return Code().Append(ref_type)
if js_type.property_type is PropertyType.CHOICES:
c = Code()
c.Append('(')
for i, choice in enumerate(js_type.choices):
c.Concat(self._TypeToJsType(choice), new_line=False)
if i is not len(js_type.choices) - 1:
c.Append('|', new_line=False)
c.Append(')', new_line=False)
elif js_type.property_type is PropertyType.FUNCTION:
c = self._FunctionToJsFunction(js_type.function)
elif js_type.property_type is PropertyType.ANY:
c.Append('*')
elif js_type.property_type.is_fundamental:
c.Append(js_type.property_type.name)
else:
c.Append('?') # TODO(tbreisacher): Make this more specific.
return c
return c
if js_type.property_type is PropertyType.FUNCTION:
return self._FunctionToJsFunction(js_type.function)
if js_type.property_type is PropertyType.ANY:
return Code().Append('*')
if js_type.property_type.is_fundamental:
return Code().Append(js_type.property_type.name)
return Code().Append('?') # TODO(tbreisacher): Make this more specific.

def _GenerateFunction(self, function):
"""Generates the code representing a function, including its documentation.
Expand Down
4 changes: 3 additions & 1 deletion tools/json_schema_compiler/js_externs_generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
Bar obj;
long? maybe;
(DOMString or Greek or long[]) choice;
object plainObj;
};
callback VoidCallback = void();
Expand Down Expand Up @@ -114,7 +115,8 @@
* anythingGoes: !Array<*>,
* obj: Bar,
* maybe: (number|undefined),
* choice: (string|!chrome.fakeApi.Greek|!Array<number>)
* choice: (string|!chrome.fakeApi.Greek|!Array<number>),
* plainObj: Object
* }}
* @see https://developer.chrome.com/extensions/fakeApi#type-Baz
*/
Expand Down

0 comments on commit 6841111

Please sign in to comment.