diff options
Diffstat (limited to 'chromium/tools/json_schema_compiler')
9 files changed, 142 insertions, 37 deletions
diff --git a/chromium/tools/json_schema_compiler/cc_generator.py b/chromium/tools/json_schema_compiler/cc_generator.py index 11302a320cf..e0e1e19c831 100644 --- a/chromium/tools/json_schema_compiler/cc_generator.py +++ b/chromium/tools/json_schema_compiler/cc_generator.py @@ -42,7 +42,8 @@ class _Generator(object): .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) .Append() .Append(self._util_cc_helper.GetIncludePath()) - .Append('#include "base/logging.h"') + .Append('#include "base/check.h"') + .Append('#include "base/notreached.h"') .Append('#include "base/strings/string_number_conversions.h"') .Append('#include "base/strings/utf_string_conversions.h"') .Append('#include "base/values.h"') diff --git a/chromium/tools/json_schema_compiler/code.py b/chromium/tools/json_schema_compiler/code.py index ef41cf88407..c23fc483c50 100644 --- a/chromium/tools/json_schema_compiler/code.py +++ b/chromium/tools/json_schema_compiler/code.py @@ -124,13 +124,18 @@ class Code(object): def trim_comment(comment, max_len): if len(comment) <= max_len: return comment, '' + # If we ran out of space due to existing content, don't try to wrap. + if max_len <= 1: + return '', comment.lstrip() last_space = comment.rfind(' ', 0, max_len + 1) if last_space != -1: line = comment[0:last_space] comment = comment[last_space + 1:] else: - line = comment[0:max_len] - comment = comment[max_len:] + # If the line can't be split, then don't try. The comments might be + # important (e.g. JSDoc) where splitting it breaks things. + line = comment + comment = '' return line, comment.lstrip() # First line has the full maximum length. @@ -145,6 +150,7 @@ class Code(object): # Any subsequent lines be subject to the wrap indent. max_len = (self._comment_length - len(''.join(self._line_prefixes)) - len(comment_prefix) - wrap_indent) + assert max_len > 1 while len(comment): line, comment = trim_comment(comment, max_len) self.Append(comment_prefix + (' ' * wrap_indent) + line, substitute=False) diff --git a/chromium/tools/json_schema_compiler/code_test.py b/chromium/tools/json_schema_compiler/code_test.py index 90e28793408..6d54ecc47a4 100755 --- a/chromium/tools/json_schema_compiler/code_test.py +++ b/chromium/tools/json_schema_compiler/code_test.py @@ -141,12 +141,16 @@ class CodeTest(unittest.TestCase): 'longness, that is, using a different\n' '// word, length.', c.Render()) + # Words that cannot be broken up are left as too long. long_word = 'x' * 100 c = Code() + c.Comment('xxx') c.Comment(long_word) + c.Comment('xxx') self.assertEquals( - '// ' + 'x' * 77 + '\n' - '// ' + 'x' * 23, + '// xxx\n' + '// ' + 'x' * 100 + '\n' + '// xxx', c.Render()) c = Code(indent_size=2, comment_length=40) c.Comment('Pretend this is a Closure Compiler style comment, which should ' diff --git a/chromium/tools/json_schema_compiler/features_cc_generator.py b/chromium/tools/json_schema_compiler/features_cc_generator.py index 4af9aa71eff..c79796cbdfe 100644 --- a/chromium/tools/json_schema_compiler/features_cc_generator.py +++ b/chromium/tools/json_schema_compiler/features_cc_generator.py @@ -35,7 +35,7 @@ class _Generator(object): .Append() .Append('#include "%s.h"' % self._source_file_filename) .Append() - .Append('#include "base/logging.h"') + .Append('#include "base/notreached.h"') .Append() .Concat(cpp_util.OpenNamespace(self._namespace)) .Append() diff --git a/chromium/tools/json_schema_compiler/h_generator.py b/chromium/tools/json_schema_compiler/h_generator.py index 90e34d33fa3..04b26e4cd78 100644 --- a/chromium/tools/json_schema_compiler/h_generator.py +++ b/chromium/tools/json_schema_compiler/h_generator.py @@ -57,7 +57,6 @@ class _Generator(object): .Append('#include <string>') .Append('#include <vector>') .Append() - .Append('#include "base/logging.h"') .Append('#include "base/values.h"') .Cblock(self._type_helper.GenerateIncludes(include_soft=include_soft)) .Append() diff --git a/chromium/tools/json_schema_compiler/js_externs_generator.py b/chromium/tools/json_schema_compiler/js_externs_generator.py index 36670cd3197..f510bf5290c 100644 --- a/chromium/tools/json_schema_compiler/js_externs_generator.py +++ b/chromium/tools/json_schema_compiler/js_externs_generator.py @@ -53,6 +53,9 @@ class _Generator(object): for js_type in self._namespace.types.values(): self._AppendType(c, js_type) + for prop in self._namespace.properties.values(): + self._AppendProperty(c, prop) + for function in self._namespace.functions.values(): self._AppendFunction(c, function) @@ -162,12 +165,24 @@ class _Generator(object): c.Append('@typedef {') if properties: - self._js_util.AppendObjectDefinition( - c, self._namespace.name, properties, new_line=False) + self._js_util.AppendObjectDefinition(c, + self._namespace.name, + properties, + new_line=False) else: - c.Append('Object', new_line=False) + c.Append('Object', new_line=False) c.Append('}', new_line=False) + def _AppendProperty(self, c, prop): + """Appends the code representing a top-level property, including its + documentation. For example: + + /** @type {string} */ + chrome.runtime.id; + """ + self._AppendTypeJsDoc(c, prop.type_, prop.optional) + c.Append() + def _AppendFunction(self, c, function): """Appends the code representing a function, including its documentation. For example: @@ -203,14 +218,10 @@ class _Generator(object): """Appends the code creating namespace object. For example: - /** - * @const - */ + /** @const */ chrome.bookmarks = {}; """ - c.Append("""/** - * @const - */""") + c.Append('/** @const */') c.Append('chrome.%s = {};' % self._namespace.name) c.Append() diff --git a/chromium/tools/json_schema_compiler/js_externs_generator_test.py b/chromium/tools/json_schema_compiler/js_externs_generator_test.py index 6bd417986c2..797456e36cf 100755 --- a/chromium/tools/json_schema_compiler/js_externs_generator_test.py +++ b/chromium/tools/json_schema_compiler/js_externs_generator_test.py @@ -65,6 +65,10 @@ namespace fakeApi { callback OptionalParamCallback = void(optional Qux qux); + interface Properties { + static DOMString lastError(); + }; + interface Functions { // Does something exciting! And what's more, this is a multiline function // comment! It goes onto multiple lines! @@ -78,7 +82,26 @@ namespace fakeApi { [deprecated="Use a new method."] static DOMString returnString(); + static void instanceOfObjectParam([instanceOf=SomeType] object obj); + static void optionalParam(optional OptionalParamCallback callback); + + static void nonFinalOptionalParams( + DOMString string, + optional double num, + object obj, + [instanceOf = SomeType] object someType, + [instanceOf = SomeType] optional object optionalSomeType, + optional boolean bool, + optional Baz baz, + VoidCallback callback); + + static void multipleOptionalParams( + optional DOMString param1, + optional DOMString param2, + optional object obj, + [instanceOf = SomeType] optional object optionalSomeType, + optional VoidCallback callback); }; interface Events { @@ -102,9 +125,7 @@ fake_idl_expected = """// Copyright %s The Chromium Authors. All rights reserved /** @fileoverview Externs generated from namespace: fakeApi */ -/** - * @const - */ +/** @const */ chrome.fakeApi = {}; /** @@ -185,16 +206,22 @@ chrome.fakeApi.Qux.prototype.stop = function() {}; /** + * @type {string} + * @see https://developer.chrome.com/extensions/fakeApi#type-lastError + */ +chrome.fakeApi.lastError; + +/** * Does something exciting! And what's more, this is a multiline function * comment! It goes onto multiple lines! * @param {!chrome.fakeApi.Baz} baz The baz to use. - * @param {function():void} callback + * @param {function(): void} callback * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething */ chrome.fakeApi.doSomething = function(baz, callback) {}; /** - * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} callback + * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek): void=} callback * The callback which will most assuredly in all cases be called; that is, * of course, iff such a callback was provided and is not at all null. * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek @@ -209,12 +236,41 @@ chrome.fakeApi.bazGreek = function(callback) {}; chrome.fakeApi.returnString = function() {}; /** - * @param {function((!chrome.fakeApi.Qux|undefined)):void=} callback + * @param {SomeType} obj + * @see https://developer.chrome.com/extensions/fakeApi#method-instanceOfObjectParam + */ +chrome.fakeApi.instanceOfObjectParam = function(obj) {}; + +/** + * @param {function((!chrome.fakeApi.Qux|undefined)): void=} callback * @see https://developer.chrome.com/extensions/fakeApi#method-optionalParam */ chrome.fakeApi.optionalParam = function(callback) {}; /** + * @param {string} string + * @param {?number|undefined} num + * @param {Object} obj + * @param {SomeType} someType + * @param {?SomeType|undefined} optionalSomeType + * @param {?boolean|undefined} bool + * @param {?chrome.fakeApi.Baz|undefined} baz + * @param {function(): void} callback + * @see https://developer.chrome.com/extensions/fakeApi#method-nonFinalOptionalParams + */ +chrome.fakeApi.nonFinalOptionalParams = function(string, num, obj, someType, optionalSomeType, bool, baz, callback) {}; + +/** + * @param {string=} param1 + * @param {string=} param2 + * @param {Object=} obj + * @param {SomeType=} optionalSomeType + * @param {function(): void=} callback + * @see https://developer.chrome.com/extensions/fakeApi#method-multipleOptionalParams + */ +chrome.fakeApi.multipleOptionalParams = function(param1, param2, obj, optionalSomeType, callback) {}; + +/** * Fired when we realize it's a trap! * @type {!ChromeEvent} * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected @@ -261,9 +317,7 @@ fake_private_idl_expected = """// Copyright %s The Chromium Authors. All rights /** @fileoverview Externs generated from namespace: fakeApiPrivate */ -/** - * @const - */ +/** @const */ chrome.fakeApiPrivate = {}; /** @@ -310,6 +364,12 @@ fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved. "additionalProperties": {"type": "string"} } ], + "properties": { + "lastError": { + "type": "string", + "description": "The lastError." + } + }, "functions": [ { "name": "funcWithInlineObj", "type": "function", @@ -383,9 +443,7 @@ fake_json_expected = """// Copyright %s The Chromium Authors. All rights reserve /** @fileoverview Externs generated from namespace: fakeJson */ -/** - * @const - */ +/** @const */ chrome.fakeJson = {}; /** @@ -406,6 +464,13 @@ chrome.fakeJson.CrazyEnum = { chrome.fakeJson.CrazyObject; /** + * The lastError. + * @type {string} + * @see https://developer.chrome.com/extensions/fakeJson#type-lastError + */ +chrome.fakeJson.lastError; + +/** * @param {{ * foo: (boolean|undefined), * bar: number, @@ -417,7 +482,7 @@ chrome.fakeJson.CrazyObject; * description that causes problems! * @param {function({ * str: string - * }):void} callback The callback to this heinous method + * }): void} callback The callback to this heinous method * @return {{ * str: string, * int: number diff --git a/chromium/tools/json_schema_compiler/js_interface_generator_test.py b/chromium/tools/json_schema_compiler/js_interface_generator_test.py index 19034b909f7..a108bf5669a 100755 --- a/chromium/tools/json_schema_compiler/js_interface_generator_test.py +++ b/chromium/tools/json_schema_compiler/js_interface_generator_test.py @@ -89,13 +89,13 @@ FakeApi.prototype = { * Does something exciting! And what's more, this is a multiline function * comment! It goes onto multiple lines! * @param {!chrome.fakeApi.Baz} baz The baz to use. - * @param {function():void} callback + * @param {function(): void} callback * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething */ doSomething: function(baz, callback) {}, /** - * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} + * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek): void=} * callback The callback which will most assuredly in all cases be called; * that is, of course, iff such a callback was provided and is not at all * null. diff --git a/chromium/tools/json_schema_compiler/js_util.py b/chromium/tools/json_schema_compiler/js_util.py index 5a23c862fef..a6c72a67e39 100644 --- a/chromium/tools/json_schema_compiler/js_util.py +++ b/chromium/tools/json_schema_compiler/js_util.py @@ -35,7 +35,8 @@ class JsUtil(object): """Given an OrderedDict of properties, returns a Code containing the description of an object. """ - if not properties: return + if not properties: + return c.Sblock('{', new_line=new_line) first = True @@ -77,10 +78,26 @@ class JsUtil(object): c.Comment(' %s' % description, comment_prefix='', wrap_indent=4, new_line=False) - for param in function.params: - append_field(c, 'param', - self._TypeToJsType(namespace_name, param.type_), - param.name, param.optional, param.description) + 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.callback is None or function.callback.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.callback: append_field(c, 'param', @@ -127,7 +144,7 @@ class JsUtil(object): c.Concat(t, new_line = False) if i is not len(function.params) - 1: c.Append(', ', new_line=False, strip_right=False) - c.Append('):', new_line=False) + c.Append('): ', new_line=False, strip_right=False) if function.returns: c.Concat(self._TypeToJsType(namespace_name, function.returns), @@ -146,6 +163,8 @@ class JsUtil(object): c = Code() self.AppendObjectDefinition(c, namespace_name, js_type.properties) return c + if 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<'). |