How to use the libcst._nodes.whitespace.SimpleWhitespace function in libcst

To help you get started, we’ve selected a few libcst examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Instagram / LibCST / libcst / _nodes / expression.py View on Github external
@dataclass(frozen=True)
class From(CSTNode):
    """
    A ``from x`` stanza in a :class:`Yield` or :class:`Raise`.
    """

    #: The expression that we are yielding/raising from.
    item: BaseExpression

    #: The whitespace at the very start of this node.
    whitespace_before_from: Union[
        BaseParenthesizableWhitespace, MaybeSentinel
    ] = MaybeSentinel.DEFAULT

    #: The whitespace after the ``from`` keyword, but before the ``item``.
    whitespace_after_from: BaseParenthesizableWhitespace = SimpleWhitespace.field(" ")

    def _validate(self) -> None:
        if (
            isinstance(self.whitespace_after_from, BaseParenthesizableWhitespace)
            and self.whitespace_after_from.empty
            and not self.item._safe_to_use_with_word_operator(ExpressionPosition.RIGHT)
        ):
            raise CSTValidationError(
                "Must have at least one space after 'from' keyword."
            )

    def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "From":
        return From(
            whitespace_before_from=visit_sentinel(
                self, "whitespace_before_from", self.whitespace_before_from, visitor
            ),
github Instagram / LibCST / libcst / _parser / whitespace_parser.py View on Github external
ws_line_list = [ws_line]
    while "\\" in ws_line:
        # continuation character
        state.line += 1
        state.column = 0
        ws_line = SIMPLE_WHITESPACE_RE.match(lines[state.line - 1], state.column).group(
            0
        )
        ws_line_list.append(ws_line)

    # TODO: we could special-case the common case where there's no continuation
    # character to avoid list construction and joining.

    # once we've finished collecting continuation characters
    state.column += len(ws_line)
    return SimpleWhitespace("".join(ws_line_list))
github Instagram / LibCST / libcst / _parser / conversions / expression.py View on Github external
if comma_token is None:
            comma = MaybeSentinel.DEFAULT
        else:
            comma = Comma(
                whitespace_before=parse_parenthesizable_whitespace(
                    config, comma_token.whitespace_before
                ),
                # Only compute whitespace_after if we're not a trailing comma.
                # If we're a trailing comma, that whitespace should be consumed by the
                # TrailingWhitespace, parenthesis, etc.
                whitespace_after=(
                    parse_parenthesizable_whitespace(
                        config, comma_token.whitespace_after
                    )
                    if comma_token is not children[-1]
                    else SimpleWhitespace("")
                ),
            )

        if isinstance(expr_or_starred_element, StarredElement):
            starred_element = expr_or_starred_element
            elements.append(starred_element.with_changes(comma=comma))
        else:
            expr = expr_or_starred_element
            elements.append(Element(value=expr, comma=comma))

    # lpar/rpar are the responsibility of our parent
    return WithLeadingWhitespace(
        # pyre-ignore[29]: `Union[Type[List], Type[Set], Type[Tuple]]` is not a function.
        sequence_type(elements, lpar=(), rpar=()),
        children[0].whitespace_before,
    )
github Instagram / LibCST / libcst / _parser / conversions / expression.py View on Github external
try:
        comma_token = next(children_iter)
        element = element.with_changes(
            comma=Comma(
                whitespace_before=parse_parenthesizable_whitespace(
                    config, comma_token.whitespace_before
                ),
                # Only compute whitespace_after if we're not a trailing comma.
                # If we're a trailing comma, that whitespace should be consumed by the
                # RightBracket.
                whitespace_after=(
                    parse_parenthesizable_whitespace(
                        config, comma_token.whitespace_after
                    )
                    if comma_token is not last_child
                    else SimpleWhitespace("")
                ),
            )
        )
    except StopIteration:
        pass
    return element
github Instagram / LibCST / libcst / _nodes / statement.py View on Github external
class Decorator(CSTNode):
    """
    A single decorator that decorates a :class:`FunctionDef` or a :class:`ClassDef`.
    """

    #: The decorator that will return a new function wrapping the parent
    #: of this decorator.
    decorator: Union[Name, Attribute, Call]

    #: Line comments and empty lines before this decorator. The parent
    #: :class:`FunctionDef` or :class:`ClassDef` node owns leading lines before
    #: the first decorator so that if the first decorator is removed, spacing is preserved.
    leading_lines: Sequence[EmptyLine] = ()

    #: Whitespace after the ``@`` and before the decorator expression itself.
    whitespace_after_at: SimpleWhitespace = SimpleWhitespace.field("")

    #: Optional trailing comment and newline following the decorator before the next line.
    trailing_whitespace: TrailingWhitespace = TrailingWhitespace.field()

    def _validate(self) -> None:
        decorator = self.decorator
        if len(decorator.lpar) > 0 or len(decorator.rpar) > 0:
            raise CSTValidationError(
                "Cannot have parens around decorator in a Decorator."
            )
        if isinstance(decorator, Call) and not isinstance(
            decorator.func, (Name, Attribute)
        ):
            raise CSTValidationError(
                "Decorator call function must be Name or Attribute node."
            )
github Instagram / LibCST / libcst / _parser / conversions / statement.py View on Github external
def convert_except_clause(config: ParserConfig, children: Sequence[Any]) -> Any:
    if len(children) == 1:
        (except_token,) = children
        whitespace_after_except = SimpleWhitespace("")
        test = None
        name = None
    elif len(children) == 2:
        (except_token, test_node) = children
        whitespace_after_except = parse_simple_whitespace(
            config, except_token.whitespace_after
        )
        test = test_node.value
        name = None
    else:
        (except_token, test_node, as_token, name_token) = children
        whitespace_after_except = parse_simple_whitespace(
            config, except_token.whitespace_after
        )
        test = test_node.value
        name = AsName(
github Instagram / LibCST / libcst / matchers / _return_types.py View on Github external
Power: BaseBinaryOp,
    PowerAssign: BaseAugOp,
    Raise: Union[BaseSmallStatement, RemovalSentinel],
    Return: Union[BaseSmallStatement, RemovalSentinel],
    RightCurlyBrace: RightCurlyBrace,
    RightParen: Union[RightParen, MaybeSentinel, RemovalSentinel],
    RightShift: BaseBinaryOp,
    RightShiftAssign: BaseAugOp,
    RightSquareBracket: RightSquareBracket,
    Semicolon: Union[Semicolon, MaybeSentinel],
    Set: BaseExpression,
    SetComp: BaseExpression,
    SimpleStatementLine: Union[BaseStatement, RemovalSentinel],
    SimpleStatementSuite: BaseSuite,
    SimpleString: BaseExpression,
    SimpleWhitespace: Union[BaseParenthesizableWhitespace, MaybeSentinel],
    Slice: Slice,
    StarredDictElement: Union[BaseDictElement, RemovalSentinel],
    StarredElement: Union[BaseElement, RemovalSentinel],
    Subscript: BaseExpression,
    SubscriptElement: Union[SubscriptElement, RemovalSentinel],
    Subtract: BaseBinaryOp,
    SubtractAssign: BaseAugOp,
    TrailingWhitespace: TrailingWhitespace,
    Try: Union[BaseStatement, RemovalSentinel],
    Tuple: BaseExpression,
    UnaryOperation: BaseExpression,
    While: Union[BaseStatement, RemovalSentinel],
    With: Union[BaseStatement, RemovalSentinel],
    WithItem: Union[WithItem, RemovalSentinel],
    Yield: BaseExpression,
}