OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 """ |
| 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. |
| 7 |
| 8 See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs |
| 9 """ |
| 10 |
| 11 from code import Code |
| 12 from model import * |
| 13 from schema_util import * |
| 14 |
| 15 import os |
| 16 from datetime import datetime |
| 17 |
| 18 LICENSE = ("""// Copyright %s The Chromium Authors. All rights reserved. |
| 19 // Use of this source code is governed by a BSD-style license that can be |
| 20 // found in the LICENSE file. |
| 21 """ % datetime.now().year) |
| 22 |
| 23 class JsExternsGenerator(object): |
| 24 def Generate(self, namespace): |
| 25 return _Generator(namespace).Generate() |
| 26 |
| 27 class _Generator(object): |
| 28 def __init__(self, namespace): |
| 29 self._namespace = namespace |
| 30 |
| 31 def Generate(self): |
| 32 """Generates a Code object with the schema for the entire namespace. |
| 33 """ |
| 34 c = Code() |
| 35 (c.Append(LICENSE) |
| 36 .Append() |
| 37 .Append('/** @fileoverview Externs generated from namespace: %s */' % |
| 38 self._namespace.name) |
| 39 .Append()) |
| 40 |
| 41 for js_type in self._namespace.types.values(): |
| 42 c.Cblock(self._GenerateType(js_type)) |
| 43 |
| 44 c.Cblock(self._GenerateNamespaceObject()) |
| 45 |
| 46 for function in self._namespace.functions.values(): |
| 47 c.Cblock(self._GenerateFunction(function)) |
| 48 |
| 49 for event in self._namespace.events.values(): |
| 50 c.Cblock(self._GenerateEvent(event)) |
| 51 |
| 52 return c |
| 53 |
| 54 def _GenerateType(self, js_type): |
| 55 """Given a Type object, returns the Code for this type's definition. |
| 56 |
| 57 """ |
| 58 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: |
| 63 return c |
| 64 |
| 65 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 |
| 72 return c |
| 73 |
| 74 def _IsTypeConstructor(self, js_type): |
| 75 """Returns true if the given type should be a @constructor. If this returns |
| 76 false, the type is a typedef. |
| 77 """ |
| 78 return any(prop.type_.property_type is PropertyType.FUNCTION |
| 79 for prop in js_type.properties.values()) |
| 80 |
| 81 def _GenerateTypeJsDoc(self, js_type): |
| 82 """Generates the documentation for a type as a Code. |
| 83 |
| 84 Returns an empty code object if the object has no documentation. |
| 85 """ |
| 86 c = Code() |
| 87 c.Append('/**') |
| 88 |
| 89 if js_type.description: |
| 90 for line in js_type.description.splitlines(): |
| 91 c.Comment(line, comment_prefix = ' * ') |
| 92 |
| 93 if self._IsTypeConstructor(js_type): |
| 94 c.Comment('@constructor', comment_prefix = ' * ') |
| 95 else: |
| 96 c.Concat(self._GenerateTypedef(js_type.properties)) |
| 97 |
| 98 c.Append(' */') |
| 99 return c |
| 100 |
| 101 def _GenerateTypedef(self, properties): |
| 102 """Given an OrderedDict of properties, returns a Code containing a @typedef. |
| 103 """ |
| 104 if not properties: return Code() |
| 105 |
| 106 lines = [] |
| 107 lines.append('@typedef {{') |
| 108 for field, prop in properties.items(): |
| 109 js_type = self._TypeToJsType(prop.type_) |
| 110 if prop.optional: |
| 111 js_type = '(%s|undefined)' % js_type |
| 112 lines.append(' %s: %s,' % (field, js_type)) |
| 113 |
| 114 # Remove last trailing comma. |
| 115 # TODO(devlin): This will be unneeded, if when |
| 116 # https://github.com/google/closure-compiler/issues/796 is fixed. |
| 117 lines[-1] = lines[-1][:-1] |
| 118 lines.append('}}') |
| 119 # TODO(tbreisacher): Add '@see <link to documentation>'. |
| 120 |
| 121 c = Code() |
| 122 c.Append('\n'.join([' * ' + line for line in lines])) |
| 123 return c |
| 124 |
| 125 def _GenerateFunctionJsDoc(self, function): |
| 126 """Generates the documentation for a function as a Code. |
| 127 |
| 128 Returns an empty code object if the object has no documentation. |
| 129 """ |
| 130 c = Code() |
| 131 c.Append('/**') |
| 132 |
| 133 if function.description: |
| 134 for line in function.description.split('\n'): |
| 135 c.Comment(line, comment_prefix=' * ') |
| 136 |
| 137 for param in function.params: |
| 138 js_type = self._TypeToJsType(param.type_) |
| 139 |
| 140 if param.optional: |
| 141 js_type += '=' |
| 142 |
| 143 param_doc = '@param {%s} %s %s' % (js_type, |
| 144 param.name, |
| 145 param.description or '') |
| 146 c.Comment(param_doc, comment_prefix=' * ') |
| 147 |
| 148 if function.callback: |
| 149 # TODO(tbreisacher): Convert Function to function() for better |
| 150 # typechecking. |
| 151 js_type = 'Function' |
| 152 if function.callback.optional: |
| 153 js_type += '=' |
| 154 param_doc = '@param {%s} %s %s' % (js_type, |
| 155 function.callback.name, |
| 156 function.callback.description or '') |
| 157 c.Comment(param_doc, comment_prefix=' * ') |
| 158 |
| 159 if function.returns: |
| 160 return_doc = '@return {%s} %s' % (self._TypeToJsType(function.returns), |
| 161 function.returns.description) |
| 162 c.Comment(return_doc, comment_prefix=' * ') |
| 163 |
| 164 c.Append(' */') |
| 165 return c |
| 166 |
| 167 def _TypeToJsType(self, js_type): |
| 168 """Converts a model.Type to a JS type (number, Array, etc.)""" |
| 169 if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): |
| 170 return 'number' |
| 171 elif js_type.property_type is PropertyType.OBJECT: |
| 172 return 'Object' |
| 173 elif js_type.property_type is PropertyType.ARRAY: |
| 174 return 'Array' |
| 175 elif js_type.property_type is PropertyType.REF: |
| 176 return js_type.ref_type |
| 177 elif js_type.property_type.is_fundamental: |
| 178 return js_type.property_type.name |
| 179 else: |
| 180 return '?' # TODO(tbreisacher): Make this more specific. |
| 181 |
| 182 def _GenerateFunction(self, function): |
| 183 """Generates the code representing a function, including its documentation. |
| 184 For example: |
| 185 |
| 186 /** |
| 187 * @param {string} title The new title. |
| 188 */ |
| 189 chrome.window.setTitle = function(title) {}; |
| 190 """ |
| 191 c = Code() |
| 192 params = self._GenerateFunctionParams(function) |
| 193 (c.Concat(self._GenerateFunctionJsDoc(function)) |
| 194 .Append('chrome.%s.%s = function(%s) {};' % (self._namespace.name, |
| 195 function.name, |
| 196 params)) |
| 197 ) |
| 198 return c |
| 199 |
| 200 def _GenerateEvent(self, event): |
| 201 """Generates the code representing an event. |
| 202 For example: |
| 203 |
| 204 /** @type {!ChromeEvent} */ |
| 205 chrome.bookmarks.onChildrenReordered; |
| 206 """ |
| 207 c = Code() |
| 208 (c.Append('/** @type {!ChromeEvent} */') |
| 209 .Append('chrome.%s.%s;' % (self._namespace.name, event.name))) |
| 210 return c |
| 211 |
| 212 def _GenerateNamespaceObject(self): |
| 213 """Generates the code creating namespace object. |
| 214 For example: |
| 215 |
| 216 /** |
| 217 * @const |
| 218 */ |
| 219 chrome.bookmarks = {}; |
| 220 """ |
| 221 c = Code() |
| 222 (c.Append("""/** |
| 223 * @const |
| 224 */""") |
| 225 .Append('chrome.%s = {};' % self._namespace.name)) |
| 226 return c |
| 227 |
| 228 def _GenerateFunctionParams(self, function): |
| 229 params = function.params[:] |
| 230 if function.callback: |
| 231 params.append(function.callback) |
| 232 return ', '.join(param.name for param in params) |
OLD | NEW |