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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 c.Append(' */') | 173 c.Append(' */') |
174 return c | 174 return c |
175 | 175 |
176 def _TypeToJsType(self, js_type): | 176 def _TypeToJsType(self, js_type): |
177 """Converts a model.Type to a JS type (number, Array, etc.)""" | 177 """Converts a model.Type to a JS type (number, Array, etc.)""" |
178 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): | 178 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): |
179 return 'number' | 179 return 'number' |
180 elif js_type.property_type is PropertyType.OBJECT: | 180 elif js_type.property_type is PropertyType.OBJECT: |
181 return 'Object' | 181 return 'Object' |
182 elif js_type.property_type is PropertyType.ARRAY: | 182 elif js_type.property_type is PropertyType.ARRAY: |
183 return 'Array' | 183 return '!Array<%s>' % self._TypeToJsType(js_type.item_type) |
184 elif js_type.property_type is PropertyType.REF: | 184 elif js_type.property_type is PropertyType.REF: |
185 ref_type = js_type.ref_type | 185 ref_type = js_type.ref_type |
186 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply | 186 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply |
187 # as MyType. | 187 # as MyType. |
188 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: | 188 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: |
189 ref_type = 'chrome.%s.%s' % (self._namespace.name, ref_type) | 189 ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type) |
190 return ref_type | 190 return ref_type |
| 191 elif js_type.property_type is PropertyType.CHOICES: |
| 192 return '(%s)' % '|'.join( |
| 193 [self._TypeToJsType(choice) for choice in js_type.choices]) |
| 194 elif js_type.property_type is PropertyType.ANY: |
| 195 return '*' |
191 elif js_type.property_type.is_fundamental: | 196 elif js_type.property_type.is_fundamental: |
192 return js_type.property_type.name | 197 return js_type.property_type.name |
193 else: | 198 else: |
194 return '?' # TODO(tbreisacher): Make this more specific. | 199 return '?' # TODO(tbreisacher): Make this more specific. |
195 | 200 |
196 def _GenerateFunction(self, function): | 201 def _GenerateFunction(self, function): |
197 """Generates the code representing a function, including its documentation. | 202 """Generates the code representing a function, including its documentation. |
198 For example: | 203 For example: |
199 | 204 |
200 /** | 205 /** |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 * @const | 242 * @const |
238 */""") | 243 */""") |
239 .Append('chrome.%s = {};' % self._namespace.name)) | 244 .Append('chrome.%s = {};' % self._namespace.name)) |
240 return c | 245 return c |
241 | 246 |
242 def _GenerateFunctionParams(self, function): | 247 def _GenerateFunctionParams(self, function): |
243 params = function.params[:] | 248 params = function.params[:] |
244 if function.callback: | 249 if function.callback: |
245 params.append(function.callback) | 250 params.append(function.callback) |
246 return ', '.join(param.name for param in params) | 251 return ', '.join(param.name for param in params) |
OLD | NEW |