mirror of
https://github.com/unclebob/crapper.git
synced 2026-11-26 18:18:49 +00:00
Skip Swift requirements that have no statements.
Protocol subscripts and `{ get set }` properties were scored as functions
even though they contain no executable body, unlike protocol methods.
The new test lists those requirements and a real subscript, and failed
until entries required a statements node. Accessor tests still pass.
Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
co-authored by
Robert C. Martin
parent
10c4e43f7b
commit
ccbf312645
@@ -72,7 +72,7 @@ Python counts `if`, `elif`, `for`, `while`, `except`, each `match` case, a compr
|
|||||||
|
|
||||||
Swift counts `if` (each `else if` is another `if`), `guard`, `for`, `while`, `repeat-while`, each `catch`, `?:`, each `switch` case including `default`, `&&`, `||`, `??`, and optional chaining (`a?.b`, `a?[i]`, `a?()`). These are not decisions: `try` / `try?` / `try!`, `as?` / `as!` / `is`, a `where` clause, `#if`, and the `?` on an optional type (`Int?`) or a failable `init?`. One `if` or `guard` is one decision even when it lists several conditions. `case 1, 2` is one case. Operators inside a string or a comment are not decisions; an interpolated expression is. `#if` does not hide the branches the grammar still parses.
|
Swift counts `if` (each `else if` is another `if`), `guard`, `for`, `while`, `repeat-while`, each `catch`, `?:`, each `switch` case including `default`, `&&`, `||`, `??`, and optional chaining (`a?.b`, `a?[i]`, `a?()`). These are not decisions: `try` / `try?` / `try!`, `as?` / `as!` / `is`, a `where` clause, `#if`, and the `?` on an optional type (`Int?`) or a failable `init?`. One `if` or `guard` is one decision even when it lists several conditions. `case 1, 2` is one case. Operators inside a string or a comment are not decisions; an interpolated expression is. `#if` does not hide the branches the grammar still parses.
|
||||||
|
|
||||||
Entries are top-level functions, methods on classes, structs, enums, actors, extensions, and protocols, initializers, deinitializers, subscripts, computed properties, and property observers. `get`, `set`, `willSet`, and `didSet` stay inside the property. A closure stored directly in a `let` or `var` is an entry. Any other closure stays inside the enclosing function, the same way a nested callback stays inside its function in TypeScript. A type declared inside a function keeps its own methods, and those methods are not also charged to the outer function.
|
Entries are top-level functions, methods on classes, structs, enums, actors, extensions, and protocols, initializers, deinitializers, subscripts, computed properties, and property observers. A requirement or accessor with no statements, such as `{ get set }`, is not an entry. `get`, `set`, `willSet`, and `didSet` stay inside the property. A closure stored directly in a `let` or `var` is an entry. Any other closure stays inside the enclosing function, the same way a nested callback stays inside its function in TypeScript. A type declared inside a function keeps its own methods, and those methods are not also charged to the outer function.
|
||||||
|
|
||||||
The namespace is the module, or `Module.Type` for a member (`Module.Outer.Inner` for a nested type, `Module.Array` for an extension). The module is the SwiftPM target whose path contains the file, otherwise the directory under `Sources/`, otherwise the nearest `.xcodeproj` or `.xcworkspace` name, otherwise the dotted directory path with `src/` removed. uml-viewer groups on that namespace and joins `:name`. mutator uses the same function list, including each entry's byte span, so a nested closure's mutations belong to the enclosing function.
|
The namespace is the module, or `Module.Type` for a member (`Module.Outer.Inner` for a nested type, `Module.Array` for an extension). The module is the SwiftPM target whose path contains the file, otherwise the directory under `Sources/`, otherwise the nearest `.xcodeproj` or `.xcworkspace` name, otherwise the dotted directory path with `src/` removed. uml-viewer groups on that namespace and joins `:name`. mutator uses the same function list, including each entry's byte span, so a nested closure's mutations belong to the enclosing function.
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ branches; an interpolated expression is.
|
|||||||
|
|
||||||
A top-level function, a method, an initializer, a deinitializer, a
|
A top-level function, a method, an initializer, a deinitializer, a
|
||||||
subscript, and a computed property or property observer are entries.
|
subscript, and a computed property or property observer are entries.
|
||||||
`get`, `set`, `willSet`, and `didSet` stay inside that entry. A closure
|
`get`, `set`, `willSet`, and `didSet` stay inside that entry. A
|
||||||
|
requirement or accessor with no statements, such as `{ get set }`, is
|
||||||
|
not an entry. A closure
|
||||||
bound directly with `let` or `var` is an entry, at file scope or on a type.
|
bound directly with `let` or `var` is an entry, at file scope or on a type.
|
||||||
Any other closure stays inside the enclosing function, the way a nested
|
Any other closure stays inside the enclosing function, the way a nested
|
||||||
callback stays inside its function in TypeScript. A local type's methods
|
callback stays inside its function in TypeScript. A local type's methods
|
||||||
@@ -154,10 +156,19 @@ def _direct_lambda(node):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _property_is_entry(node) -> bool:
|
def _has_statements(node) -> bool:
|
||||||
if child_of_type(node, "computed_property", "willset_didset_block") is not None:
|
if node.type == "statements":
|
||||||
return True
|
return True
|
||||||
return _direct_lambda(node) is not None
|
return any(_has_statements(child) for child in node.children)
|
||||||
|
|
||||||
|
|
||||||
|
def _property_is_entry(node) -> bool:
|
||||||
|
if _direct_lambda(node) is not None:
|
||||||
|
return True
|
||||||
|
accessor = child_of_type(node, "computed_property", "willset_didset_block")
|
||||||
|
if accessor is None:
|
||||||
|
return False
|
||||||
|
return _has_statements(accessor)
|
||||||
|
|
||||||
|
|
||||||
def _type_identifiers(data: bytes, node) -> list[str]:
|
def _type_identifiers(data: bytes, node) -> list[str]:
|
||||||
@@ -222,7 +233,7 @@ def _record_function(data: bytes, node, module: str, path: str) -> Function | No
|
|||||||
|
|
||||||
|
|
||||||
def _record_subscript(data: bytes, node, module: str, path: str) -> Function | None:
|
def _record_subscript(data: bytes, node, module: str, path: str) -> Function | None:
|
||||||
if _is_nested(node):
|
if _is_nested(node) or not _has_statements(node):
|
||||||
return None
|
return None
|
||||||
return _make(node, "subscript", _namespace(module, data, node), path)
|
return _make(node, "subscript", _namespace(module, data, node), path)
|
||||||
|
|
||||||
|
|||||||
@@ -164,6 +164,28 @@ func outer(flag: Bool) -> Int {
|
|||||||
assert "map" in source.encode()[make.start_byte : make.end_byte].decode()
|
assert "map" in source.encode()[make.start_byte : make.end_byte].decode()
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_requirement_without_a_body_is_not_a_function():
|
||||||
|
source = """
|
||||||
|
protocol P {
|
||||||
|
func req()
|
||||||
|
var name: String { get set }
|
||||||
|
subscript(i: Int) -> Int { get set }
|
||||||
|
init()
|
||||||
|
}
|
||||||
|
struct S {
|
||||||
|
var name: String { get set }
|
||||||
|
subscript(i: Int) -> Int { i > 0 ? i : 0 }
|
||||||
|
}
|
||||||
|
class C {
|
||||||
|
var label: String { get set }
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
functions = functions_in_source(source, "Sources/Demo/Box.swift", "/proj")
|
||||||
|
assert [(fn.namespace, fn.name, fn.complexity) for fn in functions] == [
|
||||||
|
("Demo.S", "subscript", 2)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_swiftui_body_is_the_view_type():
|
def test_swiftui_body_is_the_view_type():
|
||||||
source = """
|
source = """
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|||||||
Reference in New Issue
Block a user