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 16 matching lines...) Expand all Loading... | |
27 class _Generator(object): | 27 class _Generator(object): |
28 def __init__(self, namespace): | 28 def __init__(self, namespace): |
29 self._namespace = namespace | 29 self._namespace = namespace |
30 | 30 |
31 def Generate(self): | 31 def Generate(self): |
32 """Generates a Code object with the schema for the entire namespace. | 32 """Generates a Code object with the schema for the entire namespace. |
33 """ | 33 """ |
34 c = Code() | 34 c = Code() |
35 (c.Append(LICENSE) | 35 (c.Append(LICENSE) |
36 .Append() | 36 .Append() |
37 .Append('/** @fileoverview Externs generated from namespace: %s */' % | 37 .Append('/** @fileoverview Externs generated from namespace: %s */' % |
Tyler Breisacher (Chromium)
2015/04/22 19:50:58
Can we add some text here explaining HOW it was ge
| |
38 self._namespace.name) | 38 self._namespace.name) |
39 .Append()) | 39 .Append()) |
40 | 40 |
41 c.Cblock(self._GenerateNamespaceObject()) | 41 c.Cblock(self._GenerateNamespaceObject()) |
42 | 42 |
43 for js_type in self._namespace.types.values(): | 43 for js_type in self._namespace.types.values(): |
44 c.Cblock(self._GenerateType(js_type)) | 44 c.Cblock(self._GenerateType(js_type)) |
45 | 45 |
46 for function in self._namespace.functions.values(): | 46 for function in self._namespace.functions.values(): |
47 c.Cblock(self._GenerateFunction(function)) | 47 c.Cblock(self._GenerateFunction(function)) |
(...skipping 71 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 | 205 |
206 if function.returns: | 206 if function.returns: |
207 c.Concat(self._TypeToJsType(function.returns), new_line=False) | 207 c.Concat(self._TypeToJsType(function.returns), new_line=False) |
208 else: | 208 else: |
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() | |
216 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): | 215 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): |
217 c.Append('number') | 216 return Code().Append('number') |
218 elif js_type.property_type is PropertyType.OBJECT: | 217 if js_type.property_type is PropertyType.OBJECT: |
219 c = self._GenerateObjectDefinition(js_type.properties) | 218 if js_type.properties: |
220 elif js_type.property_type is PropertyType.ARRAY: | 219 return self._GenerateObjectDefinition(js_type.properties) |
221 (c.Append('!Array<'). | 220 return Code().Append('Object') |
221 if js_type.property_type is PropertyType.ARRAY: | |
222 return (Code().Append('!Array<'). | |
222 Concat(self._TypeToJsType(js_type.item_type), new_line=False). | 223 Concat(self._TypeToJsType(js_type.item_type), new_line=False). |
223 Append('>', new_line=False)) | 224 Append('>', new_line=False)) |
224 elif js_type.property_type is PropertyType.REF: | 225 if js_type.property_type is PropertyType.REF: |
225 ref_type = js_type.ref_type | 226 ref_type = js_type.ref_type |
226 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply | 227 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply |
227 # as MyType. | 228 # as MyType. |
228 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: | 229 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: |
229 ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type) | 230 ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type) |
230 c.Append(ref_type) | 231 return Code().Append(ref_type) |
231 elif js_type.property_type is PropertyType.CHOICES: | 232 if js_type.property_type is PropertyType.CHOICES: |
233 c = Code() | |
232 c.Append('(') | 234 c.Append('(') |
233 for i, choice in enumerate(js_type.choices): | 235 for i, choice in enumerate(js_type.choices): |
234 c.Concat(self._TypeToJsType(choice), new_line=False) | 236 c.Concat(self._TypeToJsType(choice), new_line=False) |
235 if i is not len(js_type.choices) - 1: | 237 if i is not len(js_type.choices) - 1: |
236 c.Append('|', new_line=False) | 238 c.Append('|', new_line=False) |
237 c.Append(')', new_line=False) | 239 c.Append(')', new_line=False) |
238 elif js_type.property_type is PropertyType.FUNCTION: | 240 return c |
239 c = self._FunctionToJsFunction(js_type.function) | 241 if js_type.property_type is PropertyType.FUNCTION: |
240 elif js_type.property_type is PropertyType.ANY: | 242 return self._FunctionToJsFunction(js_type.function) |
241 c.Append('*') | 243 if js_type.property_type is PropertyType.ANY: |
242 elif js_type.property_type.is_fundamental: | 244 return Code().Append('*') |
243 c.Append(js_type.property_type.name) | 245 if js_type.property_type.is_fundamental: |
244 else: | 246 return Code().Append(js_type.property_type.name) |
245 c.Append('?') # TODO(tbreisacher): Make this more specific. | 247 return Code().Append('?') # TODO(tbreisacher): Make this more specific. |
246 return c | |
247 | 248 |
248 def _GenerateFunction(self, function): | 249 def _GenerateFunction(self, function): |
249 """Generates the code representing a function, including its documentation. | 250 """Generates the code representing a function, including its documentation. |
250 For example: | 251 For example: |
251 | 252 |
252 /** | 253 /** |
253 * @param {string} title The new title. | 254 * @param {string} title The new title. |
254 */ | 255 */ |
255 chrome.window.setTitle = function(title) {}; | 256 chrome.window.setTitle = function(title) {}; |
256 """ | 257 """ |
(...skipping 49 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). | 307 """Generates a @see link for a given API 'object' (type, method, or event). |
307 """ | 308 """ |
308 | 309 |
309 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on | 310 # 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 | 311 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have |
311 # '.'s in them (like app.window), which should resolve to 'app_window'. | 312 # '.'s in them (like app.window), which should resolve to 'app_window'. |
312 # Luckily, the doc server has excellent url resolution, and knows exactly | 313 # Luckily, the doc server has excellent url resolution, and knows exactly |
313 # what we mean. This saves us from needing any complicated logic here. | 314 # what we mean. This saves us from needing any complicated logic here. |
314 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' % | 315 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' % |
315 (self._namespace.name, object_type, object_name)) | 316 (self._namespace.name, object_type, object_name)) |
OLD | NEW |