diff --git a/README.md b/README.md index c49e3c7..bb16874 100644 --- a/README.md +++ b/README.md @@ -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. -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. diff --git a/src/crapper/languages/swift.py b/src/crapper/languages/swift.py index 052b710..aa2471e 100644 --- a/src/crapper/languages/swift.py +++ b/src/crapper/languages/swift.py @@ -17,7 +17,9 @@ branches; an interpolated expression is. A top-level function, a method, an initializer, a deinitializer, a 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. Any other closure stays inside the enclosing function, the way a nested callback stays inside its function in TypeScript. A local type's methods @@ -154,10 +156,19 @@ def _direct_lambda(node): return None -def _property_is_entry(node) -> bool: - if child_of_type(node, "computed_property", "willset_didset_block") is not None: +def _has_statements(node) -> bool: + if node.type == "statements": 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]: @@ -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: - if _is_nested(node): + if _is_nested(node) or not _has_statements(node): return None return _make(node, "subscript", _namespace(module, data, node), path) diff --git a/tests/test_swift.py b/tests/test_swift.py index e3698f8..8f2844d 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -164,6 +164,28 @@ func outer(flag: Bool) -> Int { 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(): source = """ import SwiftUI