Bowler

Bowler

  • Getting Started
  • API Reference
  • Roadmap
  • Contribute
  • Github

›API Reference

Basics

  • Introduction
  • Refactoring
  • Setup
  • Usage

API Reference

  • Queries
  • Selectors
  • Filters
  • Modifiers
  • CLI Commands

Contributing

  • Developing Bowler
  • Roadmap to the Future

Modifiers

Modifiers in Bowler are functions that modify, add, remove, or replace syntax tree elements originally matched by selectors after elements have passed all filters. Modifications may occur anywhere in the syntax tree, either above or below the matched element, and may include multiple modifications.

All modifier functions must match this signature:

def modifier(node: LN, capture: Capture, filename: Filename) -> Optional[LN]:
    ...
ArgumentDescription
nodeThe matched syntax tree element.
captureSub-elements captured by the selector pattern.
filenameThe file being modified.
return valueLeaf or nodes returned will automatically replace the matched element.

Modifier Reference

All modifiers are accessed as methods on the Query class:

  • .modify()
  • .encapsulate()
  • .rename()
  • .add_argument()
  • .modify_argument()
  • .remove_argument()

Note: Bowler's API is still in a "provisional" phase. The core concepts will likely stay the same, but individual method signatures may change as the tool matures or gains new functionality.

.modify()

Add a custom modifier to the query.

query.modify(callback: Callback)
ArgumentDescription
callbackThe custom modifier function.

.encapsulate()

Encapsulate a class attribute using @property.

query.encapsulate(internal_name: str = "")
ArgumentDescription
internal_nameThe "internal" name for the underlying attribute. Defaults to _{old_name}.

.rename()

Rename a matched element to a new name. Requires using a default selector.

query.rename(new_name: str)
ArgumentDescription
new_nameNew name for element.

.add_argument()

Add an argument to a function or method, as well as callers. For positional arguments, the default value will be used to update all callers; for keyword arguments, it will be used in the function definition.

Requires use of .select_function or .select_method.

query.add_argument(
    name: str,
    value: str,
    positional: bool = False,
    after: Stringish = SENTINEL,
    type_annotation: Stringish = SENTINEL,
)
ArgumentDescription
nameThe new argument name.
valueDefault value for the argument.
positionalIf True, will be treated as a positional argument, otherwise as a keyword.
afterIf positional = True, will be placed after this argument. Defaults to the end. Use START to place at the beginning.
type_annotationOptional type annotation for the new argument.

.modify_argument()

Modify the specified argument to a function or method, as well as callers.

Requires use of .select_function or .select_method.

query.modify_argument(
    name: str,
    new_name: Stringish = SENTINEL,
    type_annotation: Stringish = SENTINEL,
    default_value: Stringish = SENTINEL,
)
ArgumentDescription
nameThe argument to modify.
new_nameOptional new name for the argument.
type_annotationOptional type annotation to set. Use DROP to remove a type annotation.
default_valueOptional default value to set.

.remove_argument()

Remove the specified argument from a function or method, as well as all callers.

Requires use of .select_function or .select_method.

query.remove_argument(name: str)
ArgumentDescription
nameThe argument to remove.
← FiltersCLI Commands →
  • Modifier Reference
    • .modify()
    • .encapsulate()
    • .rename()
    • .add_argument()
    • .modify_argument()
    • .remove_argument()
Facebook Open SourcePrivacyTermsNewsTwitterGitHubContribute to BowlerBowler
Copyright © 2022 Facebook Inc.