Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 """ | 4 """ |
| 5 Generator that produces an externs file for the Closure Compiler. | 5 Generator that produces an externs file for the Closure Compiler. |
| 6 Note: This is a work in progress, and generated externs may require tweaking. | 6 Note: This is a work in progress, and generated externs may require tweaking. |
| 7 | 7 |
| 8 See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs | 8 See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs |
| 9 """ | 9 """ |
| 10 | 10 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 c = Code() | 119 c = Code() |
| 120 c.Append('@typedef {') | 120 c.Append('@typedef {') |
| 121 c.Concat(self._GenerateObjectDefinition(properties), new_line=False) | 121 c.Concat(self._GenerateObjectDefinition(properties), new_line=False) |
| 122 c.Append('}', new_line=False) | 122 c.Append('}', new_line=False) |
| 123 return c | 123 return c |
| 124 | 124 |
| 125 def _GenerateObjectDefinition(self, properties): | 125 def _GenerateObjectDefinition(self, properties): |
| 126 """Given an OrderedDict of properties, returns a Code containing the | 126 """Given an OrderedDict of properties, returns a Code containing the |
| 127 description of an object. | 127 description of an object. |
| 128 """ | 128 """ |
| 129 if not properties: return '' | 129 if not properties: return Code() |
| 130 | 130 |
| 131 c = Code() | 131 c = Code() |
| 132 c.Sblock('{') | 132 c.Sblock('{') |
| 133 first = True | 133 first = True |
| 134 for field, prop in properties.items(): | 134 for field, prop in properties.items(): |
| 135 # Avoid trailing comma. | 135 # Avoid trailing comma. |
| 136 # TODO(devlin): This will be unneeded, if/when | 136 # TODO(devlin): This will be unneeded, if/when |
| 137 # https://github.com/google/closure-compiler/issues/796 is fixed. | 137 # https://github.com/google/closure-compiler/issues/796 is fixed. |
| 138 if not first: | 138 if not first: |
| 139 c.Append(',', new_line=False) | 139 c.Append(',', new_line=False) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 c.Append('void', new_line=False) | 209 c.Append('void', new_line=False) |
| 210 | 210 |
| 211 return c | 211 return c |
| 212 | 212 |
| 213 def _TypeToJsType(self, js_type): | 213 def _TypeToJsType(self, js_type): |
| 214 """Converts a model.Type to a JS type (number, Array, etc.)""" | 214 """Converts a model.Type to a JS type (number, Array, etc.)""" |
| 215 c = Code() | 215 c = Code() |
| 216 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): | 216 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): |
| 217 c.Append('number') | 217 c.Append('number') |
| 218 elif js_type.property_type is PropertyType.OBJECT: | 218 elif js_type.property_type is PropertyType.OBJECT: |
| 219 c = self._GenerateObjectDefinition(js_type.properties) | 219 if js_type.properties: |
| 220 c = self._GenerateObjectDefinition(js_type.properties) | |
|
Dan Beam
2015/04/07 01:54:46
this is OK i guess but it might be mildly better i
Devlin
2015/04/07 16:13:27
Yeah, I guess "return Code().Append('foo')" isn't
| |
| 221 else: | |
| 222 c.Append('Object') | |
| 220 elif js_type.property_type is PropertyType.ARRAY: | 223 elif js_type.property_type is PropertyType.ARRAY: |
| 221 (c.Append('!Array<'). | 224 (c.Append('!Array<'). |
| 222 Concat(self._TypeToJsType(js_type.item_type), new_line=False). | 225 Concat(self._TypeToJsType(js_type.item_type), new_line=False). |
| 223 Append('>', new_line=False)) | 226 Append('>', new_line=False)) |
| 224 elif js_type.property_type is PropertyType.REF: | 227 elif js_type.property_type is PropertyType.REF: |
| 225 ref_type = js_type.ref_type | 228 ref_type = js_type.ref_type |
| 226 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply | 229 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply |
| 227 # as MyType. | 230 # as MyType. |
| 228 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: | 231 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: |
| 229 ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type) | 232 ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type) |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 """Generates a @see link for a given API 'object' (type, method, or event). | 309 """Generates a @see link for a given API 'object' (type, method, or event). |
| 307 """ | 310 """ |
| 308 | 311 |
| 309 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on | 312 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on |
| 310 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have | 313 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have |
| 311 # '.'s in them (like app.window), which should resolve to 'app_window'. | 314 # '.'s in them (like app.window), which should resolve to 'app_window'. |
| 312 # Luckily, the doc server has excellent url resolution, and knows exactly | 315 # Luckily, the doc server has excellent url resolution, and knows exactly |
| 313 # what we mean. This saves us from needing any complicated logic here. | 316 # what we mean. This saves us from needing any complicated logic here. |
| 314 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' % | 317 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' % |
| 315 (self._namespace.name, object_type, object_name)) | 318 (self._namespace.name, object_type, object_name)) |
| OLD | NEW |