Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SaveMachedNode now matches with trailing empty wildcards #356

Merged
merged 3 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions libcst/matchers/_matcher_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,24 @@ def SaveMatchedNode(matcher: _OtherNodeT, name: str) -> _OtherNodeT:
return cast(_OtherNodeT, _ExtractMatchingNode(matcher, name))


def _matches_zero_nodes(
matcher: Union[
BaseMatcherNode,
_BaseWildcardNode,
MatchIfTrue[Callable[[object], bool]],
_BaseMetadataMatcher,
DoNotCareSentinel,
]
) -> bool:
if isinstance(matcher, AtLeastN) and matcher.n == 0:
return True
if isinstance(matcher, AtMostN):
return True
if isinstance(matcher, _ExtractMatchingNode):
return _matches_zero_nodes(matcher.matcher)
return False


@dataclass(frozen=True)
class _SequenceMatchesResult:
sequence_capture: Optional[
Expand Down Expand Up @@ -960,14 +978,13 @@ def _sequence_matches( # noqa: C901
return _SequenceMatchesResult({}, None)
if not nodes and matchers:
# Base case, we have one or more matcher that wasn't matched
return (
_SequenceMatchesResult({}, [])
if all(
(isinstance(m, AtLeastN) and m.n == 0) or isinstance(m, AtMostN)
for m in matchers
if all(_matches_zero_nodes(m) for m in matchers):
return _SequenceMatchesResult(
{m.name: () for m in matchers if isinstance(m, _ExtractMatchingNode)},
(),
)
else _SequenceMatchesResult(None, None)
)
else:
return _SequenceMatchesResult(None, None)
if nodes and not matchers:
# Base case, we have nodes left that don't match any matcher
return _SequenceMatchesResult(None, None)
Expand Down
28 changes: 28 additions & 0 deletions libcst/matchers/tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,34 @@ def test_extract_optional_wildcard(self) -> None:
)
self.assertEqual(nodes, {})

def test_extract_optional_wildcard_head(self) -> None:
expression = cst.parse_expression("[3]")
nodes = m.extract(
expression,
m.List(
elements=[
m.SaveMatchedNode(m.ZeroOrMore(), "head1"),
m.SaveMatchedNode(m.ZeroOrMore(), "head2"),
m.Element(value=m.Integer(value="3")),
]
),
)
self.assertEqual(nodes, {"head1": (), "head2": ()})

def test_extract_optional_wildcard_tail(self) -> None:
expression = cst.parse_expression("[3]")
nodes = m.extract(
expression,
m.List(
elements=[
m.Element(value=m.Integer(value="3")),
m.SaveMatchedNode(m.ZeroOrMore(), "tail1"),
m.SaveMatchedNode(m.ZeroOrMore(), "tail2"),
]
),
)
self.assertEqual(nodes, {"tail1": (), "tail2": ()})

def test_extract_optional_wildcard_present(self) -> None:
expression = cst.parse_expression("a + b[c], d(e, f * g, h.i.j)")
nodes = m.extract(
Expand Down