0

[Extensions WebIDL] Fix descriptions with preceding extended attributes

This CL fixes an issue with extracting descriptions from comments when
the node the comment was being extracted for had an extended attribute
that was formatted to the preceding line. In these cases we use the
line number from the extended attribute instead, as the comment will
instead be above that.

Also adds test, which will need to be added to once function
description comment parsing is implemented.

Bug: 340297705
Change-Id: I402d45bf7ae3a35bdbe7a5516fbfb08fbc1455e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5979615
Commit-Queue: Tim <tjudkins@chromium.org>
Reviewed-by: Kelvin Jiang <kelvinjiang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1385810}
This commit is contained in:
Tim Judkins
2024-11-20 19:27:55 +00:00
committed by Chromium LUCI CQ
parent 163af13633
commit 5d70ca611f
3 changed files with 48 additions and 7 deletions

@@ -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;
};

@@ -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()

@@ -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()