script_binding: Add type check on servo script bindings (#38161)

Introduce type checking with Pyrefly in `components/script_bindings`

This commit adds Pyrefly-based type checking to the
`components/script_bindings` directory. The primary goal is to catch
type inconsistencies early and reduce the likelihood of unexpected
runtime errors.

This change affects the `webidl` component, as these script bindings are
responsible for connecting WebIDL specifications to the Rust codebase.

Testing: `./mach test-wpt webidl`
Fixes: *Link to an issue this pull requests fixes or remove this line if
there is no issue*

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
This commit is contained in:
Jerens Lensun 2025-08-01 12:34:24 +08:00 committed by GitHub
parent 4ce5b17605
commit b05d265de5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 176 additions and 88 deletions

View file

@ -13,7 +13,7 @@ SERVO_ROOT = os.path.abspath(os.path.join(SCRIPT_PATH, "..", "..", ".."))
FILTER_PATTERN = re.compile("// skip-unless ([A-Z_]+)\n")
def main():
def main() -> None:
os.chdir(os.path.join(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(SERVO_ROOT, "third_party", "WebIDL"))
sys.path.insert(0, os.path.join(SERVO_ROOT, "third_party", "ply"))
@ -84,13 +84,13 @@ def main():
f.write(module.encode("utf-8"))
def make_dir(path):
def make_dir(path: str):
if not os.path.exists(path):
os.makedirs(path)
return path
def generate(config, name, filename):
def generate(config, name: str, filename: str) -> None:
from codegen import GlobalGenRoots
root = getattr(GlobalGenRoots, name)(config)
code = root.define()
@ -98,8 +98,8 @@ def generate(config, name, filename):
f.write(code.encode("utf-8"))
def add_css_properties_attributes(css_properties_json, parser):
def map_preference_name(preference_name: str):
def add_css_properties_attributes(css_properties_json: str, parser) -> None:
def map_preference_name(preference_name: str) -> str:
"""Map between Stylo preference names and Servo preference names as the
`css-properties.json` file is generated by Stylo. This should be kept in sync with the
preference mapping done in `components/servo_config/prefs.rs`, which handles the runtime version of
@ -132,7 +132,7 @@ def add_css_properties_attributes(css_properties_json, parser):
parser.parse(idl, "CSSStyleDeclaration_generated.webidl")
def attribute_names(property_name):
def attribute_names(property_name: str):
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-dashed-attribute
if property_name != "float":
yield property_name
@ -145,11 +145,11 @@ def attribute_names(property_name):
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-webkit-cased-attribute
if property_name.startswith("-webkit-"):
yield "".join(camel_case(property_name), True)
yield "".join(camel_case(property_name, True))
# https://drafts.csswg.org/cssom/#css-property-to-idl-attribute
def camel_case(chars, webkit_prefixed=False):
def camel_case(chars: str, webkit_prefixed: bool = False):
if webkit_prefixed:
chars = chars[1:]
next_is_uppercase = False