File: js_util.py

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (258 lines) | stat: -rw-r--r-- 9,961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re
from datetime import datetime

from code_util import Code
from model import PropertyType

LICENSE = """// Copyright %s The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
"""

INFO = """// This file was generated by:
//   %s.
"""


class JsUtil(object):
  """A helper class for generating JS Code.
  """
  def GetLicense(self):
    """Returns the license text for JS extern and interface files.
    """
    return (LICENSE % datetime.now().year)

  def GetInfo(self, tool):
    """Returns text describing how the file was generated.
    """
    return (INFO % tool.replace('\\', '/'))

  def GetPropertyName(self,e):
    # Enum properties are normified to be in ALL_CAPS_STYLE.
    # Assume enum '1ring-rulesThemAll'.
    # Transform to '1ring-rules_Them_All'.
    e = re.sub(r"([a-z])([A-Z])", r"\1_\2", e)
    # Transform to '1ring_rules_Them_All'.
    e = re.sub(r"\W", "_", e)
    # Transform to '_1ring_rules_Them_All'.
    e = re.sub(r"^(\d)", r"_\1", e)
    # Transform to '_1RING_RULES_THEM_ALL'.
    return e.upper()

  def AppendObjectDefinition(self, c, namespace_name, properties,
                               new_line=True):
    """Given an OrderedDict of properties, returns a Code containing the
       description of an object.
    """
    if not properties:
      return

    c.Sblock('{', new_line=new_line)
    first = True
    for field, prop in properties.items():
      # Avoid trailing comma.
      # TODO(devlin): This will be unneeded, if/when
      # https://github.com/google/closure-compiler/issues/796 is fixed.
      if not first:
        c.Append(',', new_line=False)
      first = False
      js_type = self._TypeToJsType(namespace_name, prop.type_)
      if prop.optional:
        js_type = (Code().Append('(')
                         .Concat(js_type, new_line=False)
                         .Append('|undefined)', new_line=False))
      c.Append('%s: ' % field, strip_right=False)
      c.Concat(js_type, new_line=False)

    c.Eblock('}')

  def AppendFunctionJsDoc(self, c, namespace_name, function):
    """Appends the documentation for a function as a Code.

    Returns an empty code object if the object has no documentation.
    """
    c.Sblock(line='/**', line_prefix=' * ')

    if function.description:
      c.Comment(function.description, comment_prefix='')

    def append_field(c, tag, js_type, name, optional, description):
      c.Append('@%s {' % tag)
      c.Concat(js_type, new_line=False)
      if optional:
        c.Append('=', new_line=False)
      c.Append('}', new_line=False)
      c.Comment(' %s' % name, comment_prefix='', wrap_indent=4, new_line=False)
      if description:
        c.Comment(' %s' % description, comment_prefix='',
                  wrap_indent=4, new_line=False)

    for i, param in enumerate(function.params):
      # Mark the parameter as optional, *only if* all following parameters are
      # also optional, to avoid JSC_OPTIONAL_ARG_AT_END errors thrown by Closure
      # Compiler.
      optional = (
          all(p.optional for p in function.params[i:]) and
          (function.returns_async is None or function.returns_async.optional))
      js_type = self._TypeToJsType(namespace_name, param.type_)

      # If the parameter was originally optional, but was followed by
      # non-optional parameters, allow it to be `null` or `undefined` instead.
      if not optional and param.optional:
        js_type_string = js_type.Render()

        # Remove the leading "!" from |js_type_string| if it exists, since "?!"
        # is not a valid type modifier.
        if js_type_string.startswith('!'):
          js_type_string = js_type_string[1:]
        js_type = Code().Append('?%s|undefined' % js_type_string)

      append_field(c, 'param', js_type, param.name, optional, param.description)

    if function.returns_async:
      append_field(
          c, 'param',
          self._ReturnsAsyncToJsFunction(namespace_name,
                                         function.returns_async),
          function.returns_async.name, function.returns_async.optional,
          function.returns_async.description)

    if function.returns:
      append_field(c, 'return',
                   self._TypeToJsType(namespace_name, function.returns),
                   '', False, function.returns.description)

    if function.deprecated:
      c.Append('@deprecated %s' % function.deprecated)

    self.AppendSeeLink(c, namespace_name, 'method', function.name)

    c.Eblock(' */')

  def AppendTypeJsDoc(self, c, namespace_name, js_type, optional):
    """Appends the documentation for a type as a Code.
    """
    c.Append('@type {')
    if optional:
      c.Append('(', new_line=False)
      c.Concat(self._TypeToJsType(namespace_name, js_type), new_line=False)
      c.Append('|undefined)', new_line=False)
    else:
      c.Concat(self._TypeToJsType(namespace_name, js_type), new_line=False)
    c.Append('}', new_line=False)

  def _FunctionToJsFunction(self, namespace_name, function):
    """Converts a model.Function to a JS type (i.e., function([params])...)"""
    c = Code()
    c.Append('function(')
    c.Concat(
        self._FunctionParamsToJsParams(namespace_name, function.params),
        new_line=False)
    c.Append('): ', new_line=False, strip_right=False)

    if function.returns:
      c.Concat(self._TypeToJsType(namespace_name, function.returns),
               new_line=False)
    else:
      c.Append('void', new_line=False)

    return c

  def _ReturnsAsyncToJsFunction(self, namespace_name, returns_async):
    """Converts a model.ReturnsAsync to a JS function equivalent"""
    # TODO(https://crbug.com/1142991) update this to generate promise-based
    # types and show that as a return from the API function itself, rather than
    # appended to the params as a callback.
    c = Code()
    c.Append('function(')
    c.Concat(
        self._FunctionParamsToJsParams(namespace_name, returns_async.params),
        new_line=False)
    c.Append('): ', new_line=False, strip_right=False)

    c.Append('void', new_line=False)
    return c

  def _FunctionParamsToJsParams(self, namespace_name, params):
    c = Code()
    for i, param in enumerate(params):
      t = self._TypeToJsType(namespace_name, param.type_)
      if param.optional:
        c.Append('(', new_line=False)
        c.Concat(t, new_line=False)
        c.Append('|undefined)', new_line=False)
      else:
        c.Concat(t, new_line=False)
      if i is not len(params) - 1:
        c.Append(', ', new_line=False, strip_right=False)
    return c

  def _TypeToJsType(self, namespace_name, js_type):
    """Converts a model.Type to a JS type (number, Array, etc.)"""
    if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE):
      return Code().Append('number')
    if js_type.property_type is PropertyType.OBJECT:
      if js_type.properties:
        c = Code()
        self.AppendObjectDefinition(c, namespace_name, js_type.properties)
        return c

      # Support instanceof for types in the same namespace and built-in
      # types. This doesn't support types in another namespace e.g. if
      # js_type.instanceof is 'tabs.Tab'.
      if js_type.instance_of:
        if js_type.instance_of in js_type.namespace.types:
          return Code().Append('chrome.%s.%s' %
                               (namespace_name, js_type.instance_of))
        return Code().Append(js_type.instance_of)
      return Code().Append('Object')
    if js_type.property_type is PropertyType.ARRAY:
      return (Code().Append('!Array<').
              Concat(self._TypeToJsType(namespace_name, js_type.item_type),
                     new_line=False).
           Append('>', new_line=False))
    if js_type.property_type is PropertyType.REF:
      ref_type = '!chrome.%s.%s' % (namespace_name, js_type.ref_type)
      return Code().Append(ref_type)
    if js_type.property_type is PropertyType.CHOICES:
      c = Code()
      c.Append('(')
      for i, choice in enumerate(js_type.choices):
        c.Concat(self._TypeToJsType(namespace_name, choice), new_line=False)
        if i is not len(js_type.choices) - 1:
          c.Append('|', new_line=False)
      c.Append(')', new_line=False)
      return c
    if js_type.property_type is PropertyType.FUNCTION:
      return self._FunctionToJsFunction(namespace_name, js_type.function)
    if js_type.property_type is PropertyType.BINARY:
      return Code().Append('ArrayBuffer')
    if js_type.property_type is PropertyType.ANY:
      return Code().Append('*')
    if js_type.property_type.is_fundamental:
      return Code().Append(js_type.property_type.name)
    return Code().Append('?') # TODO(tbreisacher): Make this more specific.

  def AppendSeeLink(self, c, namespace_name, object_type, object_name):
    """Appends a @see link for a given API 'object' (type, method, or event).
    """

    # TODO(nigeltao): this should actually be gated on if there is
    # documentation, rather than if it's a private API. Most private APIs
    # aren't documented, but some are. For example:
    #  - https://developer.chrome.com/apps/developerPrivate exists
    #  - https://developer.chrome.com/apps/mediaPlayerPrivate does not
    if namespace_name.endswith('Private'):
      return

    # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on
    # developer.chrome.com/apps/ instead of /extensions/, and some APIs have
    # '.'s in them (like app.window), which should resolve to 'app_window'.
    # Luckily, the doc server has excellent url resolution, and knows exactly
    # what we mean. This saves us from needing any complicated logic here.
    c.Append('@see https://developer.chrome.com/extensions/%s#%s-%s' %
             (namespace_name, object_type, object_name))