From 68411116575f7afcaf5491ed893019da12b649ae Mon Sep 17 00:00:00 2001 From: "rdevlin.cronin" Date: Tue, 7 Apr 2015 10:20:59 -0700 Subject: [PATCH] [Extension API Extern Generation] Fix a few bugs in extern generation 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} --- tools/json_schema_compiler/code.py | 2 +- tools/json_schema_compiler/code_test.py | 11 +++++- .../js_externs_generator.py | 39 ++++++++++--------- .../js_externs_generator_test.py | 4 +- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/tools/json_schema_compiler/code.py b/tools/json_schema_compiler/code.py index 5f0a98a23df580..e454e5d4440398 100644 --- a/tools/json_schema_compiler/code.py +++ b/tools/json_schema_compiler/code.py @@ -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)) diff --git a/tools/json_schema_compiler/code_test.py b/tools/json_schema_compiler/code_test.py index 7d64b6c0a04490..90e28793408617 100755 --- a/tools/json_schema_compiler/code_test.py +++ b/tools/json_schema_compiler/code_test.py @@ -191,7 +191,7 @@ 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) @@ -199,6 +199,15 @@ def testSameLineAppendAndConcat(self): 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() diff --git a/tools/json_schema_compiler/js_externs_generator.py b/tools/json_schema_compiler/js_externs_generator.py index 34552a7a888a10..786f05838d8f73 100644 --- a/tools/json_schema_compiler/js_externs_generator.py +++ b/tools/json_schema_compiler/js_externs_generator.py @@ -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('{') @@ -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. diff --git a/tools/json_schema_compiler/js_externs_generator_test.py b/tools/json_schema_compiler/js_externs_generator_test.py index 004b79c1f8d9c4..b74260daf86e5f 100755 --- a/tools/json_schema_compiler/js_externs_generator_test.py +++ b/tools/json_schema_compiler/js_externs_generator_test.py @@ -43,6 +43,7 @@ Bar obj; long? maybe; (DOMString or Greek or long[]) choice; + object plainObj; }; callback VoidCallback = void(); @@ -114,7 +115,8 @@ * anythingGoes: !Array<*>, * obj: Bar, * maybe: (number|undefined), - * choice: (string|!chrome.fakeApi.Greek|!Array) + * choice: (string|!chrome.fakeApi.Greek|!Array), + * plainObj: Object * }} * @see https://developer.chrome.com/extensions/fakeApi#type-Baz */