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 20 matching lines...) Expand all Loading... |
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 */' % |
38 self._namespace.name) | 38 self._namespace.name) |
39 .Append()) | 39 .Append()) |
40 | 40 |
| 41 c.Cblock(self._GenerateNamespaceObject()) |
| 42 |
41 for js_type in self._namespace.types.values(): | 43 for js_type in self._namespace.types.values(): |
42 c.Cblock(self._GenerateType(js_type)) | 44 c.Cblock(self._GenerateType(js_type)) |
43 | 45 |
44 c.Cblock(self._GenerateNamespaceObject()) | |
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)) |
48 | 48 |
49 for event in self._namespace.events.values(): | 49 for event in self._namespace.events.values(): |
50 c.Cblock(self._GenerateEvent(event)) | 50 c.Cblock(self._GenerateEvent(event)) |
51 | 51 |
52 return c | 52 return c |
53 | 53 |
54 def _GenerateType(self, js_type): | 54 def _GenerateType(self, js_type): |
55 """Given a Type object, returns the Code for this type's definition. | 55 """Given a Type object, returns the Code for this type's definition. |
56 | |
57 """ | 56 """ |
58 c = Code() | 57 c = Code() |
59 | |
60 # Since enums are just treated as strings for now, don't generate their | |
61 # type. | |
62 if js_type.property_type is PropertyType.ENUM: | 58 if js_type.property_type is PropertyType.ENUM: |
63 return c | 59 c.Concat(self._GenerateEnumJsDoc(js_type)) |
64 | 60 else: |
65 c.Concat(self._GenerateTypeJsDoc(js_type)) | 61 c.Concat(self._GenerateTypeJsDoc(js_type)) |
66 | |
67 var = 'var ' + js_type.simple_name | |
68 if self._IsTypeConstructor(js_type): var += ' = function()' | |
69 var += ';' | |
70 c.Append(var) | |
71 | 62 |
72 return c | 63 return c |
73 | 64 |
| 65 def _GenerateEnumJsDoc(self, js_type): |
| 66 """ Given an Enum Type object, returns the Code for the enum's definition. |
| 67 """ |
| 68 c = Code() |
| 69 c.Append('/**').Append(' * @enum {string}').Append(' */') |
| 70 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name)) |
| 71 c.Append('\n'.join( |
| 72 [" %s: '%s'," % (v.name, v.name) for v in js_type.enum_values])) |
| 73 c.Append('};') |
| 74 return c |
| 75 |
74 def _IsTypeConstructor(self, js_type): | 76 def _IsTypeConstructor(self, js_type): |
75 """Returns true if the given type should be a @constructor. If this returns | 77 """Returns true if the given type should be a @constructor. If this returns |
76 false, the type is a typedef. | 78 false, the type is a typedef. |
77 """ | 79 """ |
78 return any(prop.type_.property_type is PropertyType.FUNCTION | 80 return any(prop.type_.property_type is PropertyType.FUNCTION |
79 for prop in js_type.properties.values()) | 81 for prop in js_type.properties.values()) |
80 | 82 |
81 def _GenerateTypeJsDoc(self, js_type): | 83 def _GenerateTypeJsDoc(self, js_type): |
82 """Generates the documentation for a type as a Code. | 84 """Generates the documentation for a type as a Code. |
83 | 85 |
84 Returns an empty code object if the object has no documentation. | 86 Returns an empty code object if the object has no documentation. |
85 """ | 87 """ |
86 c = Code() | 88 c = Code() |
87 c.Append('/**') | 89 c.Append('/**') |
88 | 90 |
89 if js_type.description: | 91 if js_type.description: |
90 for line in js_type.description.splitlines(): | 92 for line in js_type.description.splitlines(): |
91 c.Comment(line, comment_prefix = ' * ') | 93 c.Comment(line, comment_prefix = ' * ') |
92 | 94 |
93 if self._IsTypeConstructor(js_type): | 95 is_constructor = self._IsTypeConstructor(js_type) |
| 96 if is_constructor: |
94 c.Comment('@constructor', comment_prefix = ' * ') | 97 c.Comment('@constructor', comment_prefix = ' * ') |
95 else: | 98 else: |
96 c.Concat(self._GenerateTypedef(js_type.properties)) | 99 c.Concat(self._GenerateTypedef(js_type.properties)) |
97 | 100 |
98 c.Append(' */') | 101 c.Append(' */') |
| 102 |
| 103 var = 'var ' + js_type.simple_name |
| 104 if is_constructor: var += ' = function() {}' |
| 105 var += ';' |
| 106 c.Append(var) |
| 107 |
99 return c | 108 return c |
100 | 109 |
101 def _GenerateTypedef(self, properties): | 110 def _GenerateTypedef(self, properties): |
102 """Given an OrderedDict of properties, returns a Code containing a @typedef. | 111 """Given an OrderedDict of properties, returns a Code containing a @typedef. |
103 """ | 112 """ |
104 if not properties: return Code() | 113 if not properties: return Code() |
105 | 114 |
106 lines = [] | 115 lines = [] |
107 lines.append('@typedef {{') | 116 lines.append('@typedef {{') |
108 for field, prop in properties.items(): | 117 for field, prop in properties.items(): |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 175 |
167 def _TypeToJsType(self, js_type): | 176 def _TypeToJsType(self, js_type): |
168 """Converts a model.Type to a JS type (number, Array, etc.)""" | 177 """Converts a model.Type to a JS type (number, Array, etc.)""" |
169 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): | 178 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): |
170 return 'number' | 179 return 'number' |
171 elif js_type.property_type is PropertyType.OBJECT: | 180 elif js_type.property_type is PropertyType.OBJECT: |
172 return 'Object' | 181 return 'Object' |
173 elif js_type.property_type is PropertyType.ARRAY: | 182 elif js_type.property_type is PropertyType.ARRAY: |
174 return 'Array' | 183 return 'Array' |
175 elif js_type.property_type is PropertyType.REF: | 184 elif js_type.property_type is PropertyType.REF: |
176 return js_type.ref_type | 185 ref_type = js_type.ref_type |
| 186 # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply |
| 187 # as MyType. |
| 188 if self._namespace.types[ref_type].property_type is PropertyType.ENUM: |
| 189 ref_type = 'chrome.%s.%s' % (self._namespace.name, ref_type) |
| 190 return ref_type |
177 elif js_type.property_type.is_fundamental: | 191 elif js_type.property_type.is_fundamental: |
178 return js_type.property_type.name | 192 return js_type.property_type.name |
179 else: | 193 else: |
180 return '?' # TODO(tbreisacher): Make this more specific. | 194 return '?' # TODO(tbreisacher): Make this more specific. |
181 | 195 |
182 def _GenerateFunction(self, function): | 196 def _GenerateFunction(self, function): |
183 """Generates the code representing a function, including its documentation. | 197 """Generates the code representing a function, including its documentation. |
184 For example: | 198 For example: |
185 | 199 |
186 /** | 200 /** |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 * @const | 237 * @const |
224 */""") | 238 */""") |
225 .Append('chrome.%s = {};' % self._namespace.name)) | 239 .Append('chrome.%s = {};' % self._namespace.name)) |
226 return c | 240 return c |
227 | 241 |
228 def _GenerateFunctionParams(self, function): | 242 def _GenerateFunctionParams(self, function): |
229 params = function.params[:] | 243 params = function.params[:] |
230 if function.callback: | 244 if function.callback: |
231 params.append(function.callback) | 245 params.append(function.callback) |
232 return ', '.join(param.name for param in params) | 246 return ', '.join(param.name for param in params) |
OLD | NEW |