diff --git a/tools/json_schema_compiler/test/web_idl/preceding_extended_attributes.idl b/tools/json_schema_compiler/test/web_idl/preceding_extended_attributes.idl new file mode 100644 index 0000000000000..ceb5c1c4d6bf0 --- /dev/null +++ b/tools/json_schema_compiler/test/web_idl/preceding_extended_attributes.idl @@ -0,0 +1,15 @@ +// Copyright 2024 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Comment on a schema that has extended attributes on a previous line. +[nodoc] +interface PrecedingExtendedAttributes { + + // Blank for now. + // TODO(crbug.com/340297705): Add similar here for functions. +}; + +partial interface Browser { + static attribute PrecedingExtendedAttributes precedingExtendedAttributes; +}; diff --git a/tools/json_schema_compiler/web_idl_schema.py b/tools/json_schema_compiler/web_idl_schema.py index 57ce2a1ffdb1b..7878669dc3997 100755 --- a/tools/json_schema_compiler/web_idl_schema.py +++ b/tools/json_schema_compiler/web_idl_schema.py @@ -113,6 +113,13 @@ def GetNodeDescription(node: IDLNode) -> str: The formatted string expected for the description of the node. """ + # Extended attributes for a node can actually be formatted onto a preceding + # line, so if this node has an extended attribute we instead look for the + # description relative to the extended attribute node. + ext_attribute_node = node.GetOneOf('ExtAttributes') + if ext_attribute_node is not None: + return GetNodeDescription(ext_attribute_node) + # Look through the lines above the current node and extract every consecutive # line that is a comment until a blank or non-comment line is found. filename, line_number = node.GetFileAndLine() diff --git a/tools/json_schema_compiler/web_idl_schema_test.py b/tools/json_schema_compiler/web_idl_schema_test.py index 9f436774a0cf5..43631f5d8bd02 100755 --- a/tools/json_schema_compiler/web_idl_schema_test.py +++ b/tools/json_schema_compiler/web_idl_schema_test.py @@ -180,8 +180,6 @@ class WebIdlSchemaTest(unittest.TestCase): 'type': 'promise' }, getFunctionAsyncReturn(schema, 'undefinedPromiseReturn')) - - # Tests function parameters are processed as expected. def testFunctionParameters(self): schema = self.idl_basics @@ -226,7 +224,6 @@ class WebIdlSchemaTest(unittest.TestCase): '$ref': 'ExampleType' }], getFunctionParameters(schema, 'takesOptionalCustomType')) - # Tests that Dictionaries defined on the top level of the IDL file are # processed into types on the resulting namespace. def testApiTypesOnNamespace(self): @@ -344,11 +341,33 @@ class WebIdlSchemaTest(unittest.TestCase): # Tests that an API interface that uses the nodoc extended attribute has the # related nodoc attribute set to true after processing. def testNoDocOnNamespace(self): - nodoc_schema = web_idl_schema.Load('test/web_idl/nodoc_on_namespace.idl') - self.assertEqual(1, len(nodoc_schema)) - self.assertEqual('nodocAPI', nodoc_schema[0]['namespace']) - self.assertTrue(nodoc_schema[0]['nodoc']) + idl = web_idl_schema.Load('test/web_idl/nodoc_on_namespace.idl') + self.assertEqual(1, len(idl)) + schema = idl[0] + self.assertEqual('nodocAPI', schema['namespace']) + self.assertTrue(schema['nodoc']) + # Also ensure the description comes through correctly on the node with + # 'nodoc' as an extended attribute. + self.assertEqual( + 'The nodoc API. This exists to demonstrate nodoc on the main interface' + ' itself.', + schema['description'], + ) + # Tests that extended attributes being listed on the the line previous to a + # node come through correctly and don't throw off and associated descriptions. + # TODO(crbug.com/340297705): Add checks for functions here once support for + # processing their descriptions is complete. + def testPreviousLineExtendedAttributes(self): + idl = web_idl_schema.Load('test/web_idl/preceding_extended_attributes.idl') + self.assertEqual(1, len(idl)) + schema = idl[0] + self.assertEqual('precedingExtendedAttributes', schema['namespace']) + self.assertTrue(schema['nodoc']) + self.assertEqual( + 'Comment on a schema that has extended attributes on a previous line.', + schema['description'], + ) if __name__ == '__main__': unittest.main()