Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: tools/json_schema_compiler/js_externs_generator.py

Issue 1513573012: Fix line wrapping in json_schema_compiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Generate -> Append Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 21 matching lines...) Expand all
32 self._namespace = namespace 32 self._namespace = namespace
33 self._js_util = JsUtil() 33 self._js_util = JsUtil()
34 34
35 def Generate(self): 35 def Generate(self):
36 """Generates a Code object with the schema for the entire namespace. 36 """Generates a Code object with the schema for the entire namespace.
37 """ 37 """
38 c = Code() 38 c = Code()
39 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name)) 39 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name))
40 .Append()) 40 .Append())
41 41
42 c.Cblock(self._GenerateNamespaceObject()) 42 self._AppendNamespaceObject(c)
43 43
44 for js_type in self._namespace.types.values(): 44 for js_type in self._namespace.types.values():
45 c.Cblock(self._GenerateType(js_type)) 45 self._AppendType(c, js_type)
46 46
47 for function in self._namespace.functions.values(): 47 for function in self._namespace.functions.values():
48 c.Cblock(self._GenerateFunction(function)) 48 self._AppendFunction(c, function)
49 49
50 for event in self._namespace.events.values(): 50 for event in self._namespace.events.values():
51 c.Cblock(self._GenerateEvent(event)) 51 self._AppendEvent(c, event)
52 52
53 c.TrimTrailingNewlines() 53 c.TrimTrailingNewlines()
54 54
55 return c 55 return c
56 56
57 def _GetHeader(self, tool, namespace): 57 def _GetHeader(self, tool, namespace):
58 """Returns the file header text. 58 """Returns the file header text.
59 """ 59 """
60 return (self._js_util.GetLicense() + '\n' + 60 return (self._js_util.GetLicense() + '\n' +
61 self._js_util.GetInfo(tool) + (NOTE % namespace) + '\n' + 61 self._js_util.GetInfo(tool) + (NOTE % namespace) + '\n' +
62 ('/** @fileoverview Externs generated from namespace: %s */' % 62 ('/** @fileoverview Externs generated from namespace: %s */' %
63 namespace)) 63 namespace))
64 64
65 def _AppendType(self, c, js_type):
66 """Given a Type object, generates the Code for this type's definition.
67 """
68 if js_type.property_type is PropertyType.ENUM:
69 self._AppendEnumJsDoc(c, js_type)
70 else:
71 self._AppendTypeJsDoc(c, js_type)
72 c.Append()
65 73
66 def _GenerateType(self, js_type): 74 def _AppendEnumJsDoc(self, c, js_type):
67 """Given a Type object, returns the Code for this type's definition. 75 """ Given an Enum Type object, generates the Code for the enum's definition.
68 """ 76 """
69 c = Code()
70 if js_type.property_type is PropertyType.ENUM:
71 c.Concat(self._GenerateEnumJsDoc(js_type))
72 else:
73 c.Concat(self._GenerateTypeJsDoc(js_type))
74
75 return c
76
77 def _GenerateEnumJsDoc(self, js_type):
78 """ Given an Enum Type object, returns the Code for the enum's definition.
79 """
80 c = Code()
81 (c.Sblock(line='/**', line_prefix=' * ') 77 (c.Sblock(line='/**', line_prefix=' * ')
82 .Append('@enum {string}') 78 .Append('@enum {string}')
83 .Append(self._js_util.GenerateSeeLink(self._namespace.name, 'type', 79 .Append(self._js_util.GetSeeLink(self._namespace.name, 'type',
84 js_type.simple_name)) 80 js_type.simple_name))
85 .Eblock(' */')) 81 .Eblock(' */'))
86 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name)) 82 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name))
87 83
88 def get_property_name(e): 84 def get_property_name(e):
89 # Enum properties are normified to be in ALL_CAPS_STYLE. 85 # Enum properties are normified to be in ALL_CAPS_STYLE.
90 # Assume enum '1ring-rulesThemAll'. 86 # Assume enum '1ring-rulesThemAll'.
91 # Transform to '1ring-rules_Them_All'. 87 # Transform to '1ring-rules_Them_All'.
92 e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e) 88 e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e)
93 # Transform to '1ring_rules_Them_All'. 89 # Transform to '1ring_rules_Them_All'.
94 e = re.sub(r'\W', '_', e) 90 e = re.sub(r'\W', '_', e)
95 # Transform to '_1ring_rules_Them_All'. 91 # Transform to '_1ring_rules_Them_All'.
96 e = re.sub(r'^(\d)', r'_\1', e) 92 e = re.sub(r'^(\d)', r'_\1', e)
97 # Transform to '_1RING_RULES_THEM_ALL'. 93 # Transform to '_1RING_RULES_THEM_ALL'.
98 return e.upper() 94 return e.upper()
99 95
100 c.Append('\n'.join( 96 c.Append('\n'.join(
101 [" %s: '%s'," % (get_property_name(v.name), v.name) 97 [" %s: '%s'," % (get_property_name(v.name), v.name)
102 for v in js_type.enum_values])) 98 for v in js_type.enum_values]))
103 c.Append('};') 99 c.Append('};')
104 return c
105 100
106 def _IsTypeConstructor(self, js_type): 101 def _IsTypeConstructor(self, js_type):
107 """Returns true if the given type should be a @constructor. If this returns 102 """Returns true if the given type should be a @constructor. If this returns
108 false, the type is a typedef. 103 false, the type is a typedef.
109 """ 104 """
110 return any(prop.type_.property_type is PropertyType.FUNCTION 105 return any(prop.type_.property_type is PropertyType.FUNCTION
111 for prop in js_type.properties.values()) 106 for prop in js_type.properties.values())
112 107
113 def _GenerateTypeJsDoc(self, js_type): 108 def _AppendTypeJsDoc(self, c, js_type):
114 """Generates the documentation for a type as a Code. 109 """Appends the documentation for a type as a Code.
115
116 Returns an empty code object if the object has no documentation.
117 """ 110 """
118 c = Code()
119 c.Sblock(line='/**', line_prefix=' * ') 111 c.Sblock(line='/**', line_prefix=' * ')
120 112
121 if js_type.description: 113 if js_type.description:
122 for line in js_type.description.splitlines(): 114 for line in js_type.description.splitlines():
123 c.Append(line) 115 c.Append(line)
124 116
125 is_constructor = self._IsTypeConstructor(js_type) 117 is_constructor = self._IsTypeConstructor(js_type)
126 if is_constructor: 118 if is_constructor:
127 c.Comment('@constructor', comment_prefix = ' * ', wrap_indent=4) 119 c.Comment('@constructor', comment_prefix = ' * ', wrap_indent=4)
128 else: 120 else:
129 c.Concat(self._GenerateTypedef(js_type.properties)) 121 self._AppendTypedef(c, js_type.properties)
130 122
131 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'type', 123 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'type',
132 js_type.simple_name)) 124 js_type.simple_name))
133 c.Eblock(' */') 125 c.Eblock(' */')
134 126
135 var = 'chrome.%s.%s' % (js_type.namespace.name, js_type.simple_name) 127 var = 'chrome.%s.%s' % (js_type.namespace.name, js_type.simple_name)
136 if is_constructor: var += ' = function() {}' 128 if is_constructor: var += ' = function() {}'
137 var += ';' 129 var += ';'
138 c.Append(var) 130 c.Append(var)
139 131
140 return c 132 def _AppendTypedef(self, c, properties):
133 """Given an OrderedDict of properties, Appends code containing a @typedef.
134 """
135 if not properties: return
141 136
142 def _GenerateTypedef(self, properties): 137 c.Append('@typedef {')
143 """Given an OrderedDict of properties, returns a Code containing a @typedef. 138 self._js_util.AppendObjectDefinition(c, self._namespace.name, properties,
144 """ 139 new_line=False)
145 if not properties: return Code() 140 c.Append('}', new_line=False)
146 141
147 c = Code() 142 def _AppendFunction(self, c, function):
148 c.Append('@typedef {') 143 """Appends the code representing a function, including its documentation.
149 c.Concat(self._js_util.GenerateObjectDefinition(self._namespace.name,
150 properties),
151 new_line=False)
152 c.Append('}', new_line=False)
153 return c
154
155 def _GenerateFunction(self, function):
156 """Generates the code representing a function, including its documentation.
157 For example: 144 For example:
158 145
159 /** 146 /**
160 * @param {string} title The new title. 147 * @param {string} title The new title.
161 */ 148 */
162 chrome.window.setTitle = function(title) {}; 149 chrome.window.setTitle = function(title) {};
163 """ 150 """
164 c = Code() 151 self._js_util.AppendFunctionJsDoc(c, self._namespace.name, function)
165 params = self._GenerateFunctionParams(function) 152 params = self._GetFunctionParams(function)
166 (c.Concat(self._js_util.GenerateFunctionJsDoc(self._namespace.name, 153 c.Append('chrome.%s.%s = function(%s) {};' % (self._namespace.name,
167 function)) 154 function.name, params))
168 .Append('chrome.%s.%s = function(%s) {};' % (self._namespace.name, 155 c.Append()
169 function.name,
170 params))
171 )
172 return c
173 156
174 def _GenerateEvent(self, event): 157 def _AppendEvent(self, c, event):
175 """Generates the code representing an event. 158 """Appends the code representing an event.
176 For example: 159 For example:
177 160
178 /** @type {!ChromeEvent} */ 161 /** @type {!ChromeEvent} */
179 chrome.bookmarks.onChildrenReordered; 162 chrome.bookmarks.onChildrenReordered;
180 """ 163 """
181 c = Code()
182 c.Sblock(line='/**', line_prefix=' * ') 164 c.Sblock(line='/**', line_prefix=' * ')
183 if (event.description): 165 if (event.description):
184 c.Comment(event.description, comment_prefix='') 166 c.Comment(event.description, comment_prefix='')
185 c.Append('@type {!ChromeEvent}') 167 c.Append('@type {!ChromeEvent}')
186 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'event', 168 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'event',
187 event.name)) 169 event.name))
188 c.Eblock(' */') 170 c.Eblock(' */')
189 c.Append('chrome.%s.%s;' % (self._namespace.name, event.name)) 171 c.Append('chrome.%s.%s;' % (self._namespace.name, event.name))
190 return c 172 c.Append()
191 173
192 def _GenerateNamespaceObject(self): 174 def _AppendNamespaceObject(self, c):
193 """Generates the code creating namespace object. 175 """Appends the code creating namespace object.
194 For example: 176 For example:
195 177
196 /** 178 /**
197 * @const 179 * @const
198 */ 180 */
199 chrome.bookmarks = {}; 181 chrome.bookmarks = {};
200 """ 182 """
201 c = Code() 183 c.Append("""/**
202 (c.Append("""/**
203 * @const 184 * @const
204 */""") 185 */""")
205 .Append('chrome.%s = {};' % self._namespace.name)) 186 c.Append('chrome.%s = {};' % self._namespace.name)
206 return c 187 c.Append()
207 188
208 def _GenerateFunctionParams(self, function): 189 def _GetFunctionParams(self, function):
190 """Returns the function params string for function.
191 """
209 params = function.params[:] 192 params = function.params[:]
210 if function.callback: 193 if function.callback:
211 params.append(function.callback) 194 params.append(function.callback)
212 return ', '.join(param.name for param in params) 195 return ', '.join(param.name for param in params)
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/code.py ('k') | tools/json_schema_compiler/js_externs_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698