mirror of
https://github.com/unclebob/dryer.git
synced 2026-11-26 18:58:51 +00:00
Normalize Swift members, operators, labels, and literals.
Call names were kept, and everything else in a Swift expression was still a generic word. filter matched select, >= matched !=, an argument label disappeared, and a string did not match a number. A navigation_expression is an attribute, so the called member stays and the field name does not. A custom operator and an argument label keep their spelling. Swift strings, nil, and real numbers are literals, an interpolation is not, and a block comment is skipped. Proved: the Swift tests for methods, operators, labels, literals, comments, and tuple indexes pass, and the other languages still pass. 92 tests. Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
co-authored by
Robert C. Martin
parent
16b5db55b2
commit
41384b723d
+36
-8
@@ -18,6 +18,7 @@ _SKIP = {
|
||||
"block_comment",
|
||||
"documentation_comment",
|
||||
"doc_comment",
|
||||
"multiline_comment",
|
||||
}
|
||||
|
||||
_IDENTIFIERS = {
|
||||
@@ -63,6 +64,10 @@ _LITERALS = {
|
||||
"template_string",
|
||||
"regex",
|
||||
"regex_literal",
|
||||
"line_string_literal",
|
||||
"multi_line_string_literal",
|
||||
"nil_literal",
|
||||
"real_literal",
|
||||
}
|
||||
|
||||
_OPERATORS = {
|
||||
@@ -128,8 +133,14 @@ _ATTRIBUTES = {
|
||||
"selector_expression",
|
||||
"field_access",
|
||||
"field_expression",
|
||||
"navigation_expression",
|
||||
}
|
||||
|
||||
# The spelling is the structure: Swift spells >= as custom_operator, and an
|
||||
# argument label is part of the call name.
|
||||
_TEXT_SYMBOLS = {"custom_operator", "value_argument_label"}
|
||||
_INTERPOLATED = {"interpolation", "interpolated_expression"}
|
||||
|
||||
_SCOPED = {
|
||||
"scoped_identifier",
|
||||
"scoped_type_identifier",
|
||||
@@ -159,9 +170,7 @@ _PENDING = object()
|
||||
def _is_literal(node) -> bool:
|
||||
if node.type not in _LITERALS:
|
||||
return False
|
||||
if node.type == "string" and any(child.type == "interpolation" for child in node.children):
|
||||
return False
|
||||
return True
|
||||
return not any(child.type in _INTERPOLATED for child in node.children)
|
||||
|
||||
|
||||
def _named_before_args(node):
|
||||
@@ -204,6 +213,8 @@ def _try_leaf(node, mode: str, head: bool):
|
||||
return None
|
||||
if node.type in _OPERATORS:
|
||||
return [K("symbol"), node.type]
|
||||
if node.type in _TEXT_SYMBOLS:
|
||||
return [K("symbol"), _text(node)]
|
||||
if not node.is_named:
|
||||
return None
|
||||
return _named_leaf(node, head)
|
||||
@@ -223,11 +234,29 @@ def _call_jobs(node):
|
||||
return jobs
|
||||
|
||||
|
||||
def _attr_jobs(node, head: bool):
|
||||
def _member_name(node):
|
||||
"""Swift wraps the member in navigation_suffix. The name inside is the member."""
|
||||
|
||||
if node.type != "navigation_suffix":
|
||||
return node
|
||||
for child in node.named_children:
|
||||
if child.type in _IDENTIFIERS:
|
||||
return child
|
||||
return node
|
||||
|
||||
|
||||
def _attr_ends(node):
|
||||
named = list(node.named_children)
|
||||
if not named:
|
||||
return []
|
||||
return [], None
|
||||
*objects, name = named
|
||||
return objects, _member_name(name)
|
||||
|
||||
|
||||
def _attr_jobs(node, head: bool):
|
||||
objects, name = _attr_ends(node)
|
||||
if name is None:
|
||||
return []
|
||||
jobs = [(obj, "norm", False) for obj in objects]
|
||||
if name.type in _IDENTIFIERS:
|
||||
return jobs
|
||||
@@ -307,10 +336,9 @@ def _assemble_call(node, cache):
|
||||
|
||||
|
||||
def _assemble_attr(node, head: bool, cache):
|
||||
named = list(node.named_children)
|
||||
if not named:
|
||||
objects, name = _attr_ends(node)
|
||||
if name is None:
|
||||
return [K(node.type)]
|
||||
*objects, name = named
|
||||
parts = [K(node.type)]
|
||||
for obj in objects:
|
||||
norm = cache[(id(obj), "norm", False)]
|
||||
|
||||
Reference in New Issue
Block a user