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 12 matching lines...) Expand all Loading... | |
23 // See https://chromium.googlesource.com/chromium/src/+/master/docs/closure_comp ilation.md | 23 // See https://chromium.googlesource.com/chromium/src/+/master/docs/closure_comp ilation.md |
24 """ | 24 """ |
25 | 25 |
26 class JsExternsGenerator(object): | 26 class JsExternsGenerator(object): |
27 def Generate(self, namespace): | 27 def Generate(self, namespace): |
28 return _Generator(namespace).Generate() | 28 return _Generator(namespace).Generate() |
29 | 29 |
30 class _Generator(object): | 30 class _Generator(object): |
31 def __init__(self, namespace): | 31 def __init__(self, namespace): |
32 self._namespace = namespace | 32 self._namespace = namespace |
33 self._class_name = None | |
33 self._js_util = JsUtil() | 34 self._js_util = JsUtil() |
34 | 35 |
35 def Generate(self): | 36 def Generate(self): |
36 """Generates a Code object with the schema for the entire namespace. | 37 """Generates a Code object with the schema for the entire namespace. |
37 """ | 38 """ |
38 c = Code() | 39 c = Code() |
39 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name)) | 40 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name)) |
40 .Append()) | 41 .Append()) |
41 | 42 |
42 self._AppendNamespaceObject(c) | 43 self._AppendNamespaceObject(c) |
(...skipping 29 matching lines...) Expand all Loading... | |
72 c.Append() | 73 c.Append() |
73 | 74 |
74 def _AppendEnumJsDoc(self, c, js_type): | 75 def _AppendEnumJsDoc(self, c, js_type): |
75 """ Given an Enum Type object, generates the Code for the enum's definition. | 76 """ Given an Enum Type object, generates the Code for the enum's definition. |
76 """ | 77 """ |
77 (c.Sblock(line='/**', line_prefix=' * ') | 78 (c.Sblock(line='/**', line_prefix=' * ') |
78 .Append('@enum {string}') | 79 .Append('@enum {string}') |
79 .Append(self._js_util.GetSeeLink(self._namespace.name, 'type', | 80 .Append(self._js_util.GetSeeLink(self._namespace.name, 'type', |
80 js_type.simple_name)) | 81 js_type.simple_name)) |
81 .Eblock(' */')) | 82 .Eblock(' */')) |
82 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name)) | 83 c.Append('%s.%s = {' % (self._GetNamespace(), js_type.name)) |
83 | 84 |
84 def get_property_name(e): | 85 def get_property_name(e): |
85 # Enum properties are normified to be in ALL_CAPS_STYLE. | 86 # Enum properties are normified to be in ALL_CAPS_STYLE. |
86 # Assume enum '1ring-rulesThemAll'. | 87 # Assume enum '1ring-rulesThemAll'. |
87 # Transform to '1ring-rules_Them_All'. | 88 # Transform to '1ring-rules_Them_All'. |
88 e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e) | 89 e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e) |
89 # Transform to '1ring_rules_Them_All'. | 90 # Transform to '1ring_rules_Them_All'. |
90 e = re.sub(r'\W', '_', e) | 91 e = re.sub(r'\W', '_', e) |
91 # Transform to '_1ring_rules_Them_All'. | 92 # Transform to '_1ring_rules_Them_All'. |
92 e = re.sub(r'^(\d)', r'_\1', e) | 93 e = re.sub(r'^(\d)', r'_\1', e) |
(...skipping 15 matching lines...) Expand all Loading... | |
108 def _AppendTypeJsDoc(self, c, js_type): | 109 def _AppendTypeJsDoc(self, c, js_type): |
109 """Appends the documentation for a type as a Code. | 110 """Appends the documentation for a type as a Code. |
110 """ | 111 """ |
111 c.Sblock(line='/**', line_prefix=' * ') | 112 c.Sblock(line='/**', line_prefix=' * ') |
112 | 113 |
113 if js_type.description: | 114 if js_type.description: |
114 for line in js_type.description.splitlines(): | 115 for line in js_type.description.splitlines(): |
115 c.Append(line) | 116 c.Append(line) |
116 | 117 |
117 is_constructor = self._IsTypeConstructor(js_type) | 118 is_constructor = self._IsTypeConstructor(js_type) |
118 if is_constructor: | 119 if js_type.property_type is not PropertyType.OBJECT: |
119 c.Comment('@constructor', comment_prefix = ' * ', wrap_indent=4) | 120 self._js_util.AppendTypeJsDoc(c, self._namespace.name, js_type) |
121 elif is_constructor: | |
122 c.Comment('@constructor', comment_prefix = '', wrap_indent=4) | |
120 else: | 123 else: |
121 self._AppendTypedef(c, js_type.properties) | 124 self._AppendTypedef(c, js_type.properties) |
122 | 125 |
123 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'type', | 126 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'type', |
124 js_type.simple_name)) | 127 js_type.simple_name)) |
125 c.Eblock(' */') | 128 c.Eblock(' */') |
126 | 129 |
127 var = 'chrome.%s.%s' % (js_type.namespace.name, js_type.simple_name) | 130 var = '%s.%s' % (self._GetNamespace(), js_type.simple_name) |
128 if is_constructor: var += ' = function() {}' | 131 if is_constructor: var += ' = function() {}' |
129 var += ';' | 132 var += ';' |
130 c.Append(var) | 133 c.Append(var) |
131 | 134 |
135 if is_constructor: | |
136 c.Append() | |
137 self._class_name = js_type.name | |
138 for prop in js_type.properties.values(): | |
139 if prop.type_.property_type is PropertyType.FUNCTION: | |
140 self._AppendFunction(c, prop.type_.function) | |
141 else: | |
142 self._AppendTypeJsDoc(c, prop.type_) | |
143 c.Append() | |
144 self._class_name = None | |
145 | |
132 def _AppendTypedef(self, c, properties): | 146 def _AppendTypedef(self, c, properties): |
133 """Given an OrderedDict of properties, Appends code containing a @typedef. | 147 """Given an OrderedDict of properties, Appends code containing a @typedef. |
134 """ | 148 """ |
135 if not properties: return | 149 if not properties: return |
136 | 150 |
137 c.Append('@typedef {') | 151 c.Append('@typedef {') |
138 self._js_util.AppendObjectDefinition(c, self._namespace.name, properties, | 152 self._js_util.AppendObjectDefinition(c, self._namespace.name, properties, |
139 new_line=False) | 153 new_line=False) |
140 c.Append('}', new_line=False) | 154 c.Append('}', new_line=False) |
141 | 155 |
142 def _AppendFunction(self, c, function): | 156 def _AppendFunction(self, c, function): |
143 """Appends the code representing a function, including its documentation. | 157 """Appends the code representing a function, including its documentation. |
144 For example: | 158 For example: |
145 | 159 |
146 /** | 160 /** |
147 * @param {string} title The new title. | 161 * @param {string} title The new title. |
148 */ | 162 */ |
149 chrome.window.setTitle = function(title) {}; | 163 chrome.window.setTitle = function(title) {}; |
150 """ | 164 """ |
151 self._js_util.AppendFunctionJsDoc(c, self._namespace.name, function) | 165 self._js_util.AppendFunctionJsDoc(c, self._namespace.name, function) |
152 params = self._GetFunctionParams(function) | 166 params = self._GetFunctionParams(function) |
153 c.Append('chrome.%s.%s = function(%s) {};' % (self._namespace.name, | 167 c.Append('%s.%s = function(%s) {};' % (self._GetNamespace(), |
154 function.name, params)) | 168 function.name, params)) |
155 c.Append() | 169 c.Append() |
156 | 170 |
157 def _AppendEvent(self, c, event): | 171 def _AppendEvent(self, c, event): |
158 """Appends the code representing an event. | 172 """Appends the code representing an event. |
159 For example: | 173 For example: |
160 | 174 |
161 /** @type {!ChromeEvent} */ | 175 /** @type {!ChromeEvent} */ |
162 chrome.bookmarks.onChildrenReordered; | 176 chrome.bookmarks.onChildrenReordered; |
163 """ | 177 """ |
164 c.Sblock(line='/**', line_prefix=' * ') | 178 c.Sblock(line='/**', line_prefix=' * ') |
165 if (event.description): | 179 if (event.description): |
166 c.Comment(event.description, comment_prefix='') | 180 c.Comment(event.description, comment_prefix='') |
167 c.Append('@type {!ChromeEvent}') | 181 c.Append('@type {!ChromeEvent}') |
168 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'event', | 182 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'event', |
169 event.name)) | 183 event.name)) |
170 c.Eblock(' */') | 184 c.Eblock(' */') |
171 c.Append('chrome.%s.%s;' % (self._namespace.name, event.name)) | 185 c.Append('%s.%s;' % (self._GetNamespace(), event.name)) |
172 c.Append() | 186 c.Append() |
173 | 187 |
174 def _AppendNamespaceObject(self, c): | 188 def _AppendNamespaceObject(self, c): |
175 """Appends the code creating namespace object. | 189 """Appends the code creating namespace object. |
176 For example: | 190 For example: |
177 | 191 |
178 /** | 192 /** |
179 * @const | 193 * @const |
180 */ | 194 */ |
181 chrome.bookmarks = {}; | 195 chrome.bookmarks = {}; |
182 """ | 196 """ |
183 c.Append("""/** | 197 c.Append("""/** |
184 * @const | 198 * @const |
185 */""") | 199 */""") |
186 c.Append('chrome.%s = {};' % self._namespace.name) | 200 c.Append('chrome.%s = {};' % self._namespace.name) |
187 c.Append() | 201 c.Append() |
188 | 202 |
189 def _GetFunctionParams(self, function): | 203 def _GetFunctionParams(self, function): |
190 """Returns the function params string for function. | 204 """Returns the function params string for function. |
191 """ | 205 """ |
192 params = function.params[:] | 206 params = function.params[:] |
193 if function.callback: | 207 if function.callback: |
194 params.append(function.callback) | 208 params.append(function.callback) |
195 return ', '.join(param.name for param in params) | 209 return ', '.join(param.name for param in params) |
210 | |
211 def _GetNamespace(self): | |
212 """Returns the namespace to be prepended to a top-level typedef. | |
213 | |
214 For example, it might return "chrome.namespace". | |
215 | |
216 Also optionally includes the class name if this is in the context | |
217 of outputting the members of a class. | |
218 | |
219 For example, "chrome.namespace.ClassName.prototype" | |
220 """ | |
221 if self._class_name: | |
222 return 'chrome.%s.%s.prototype' % (self._namespace.name, self._class_name) | |
223 else: | |
Dan Beam
2017/01/10 22:54:43
nit: need this else?
dmazzoni
2017/01/11 22:20:42
Done.
| |
224 return 'chrome.%s' % self._namespace.name | |
OLD | NEW |