Count source-declared visibility, not a compiled ABI or dynamic export set.
(nodes: &[Node<'_>], source: &str, language: Language)
| 791 | |
| 792 | /// Count source-declared visibility, not a compiled ABI or dynamic export set. |
| 793 | pub(super) fn public_surface(nodes: &[Node<'_>], source: &str, language: Language) -> Measurement { |
| 794 | let unavailable = |reason: &str| Measurement::Unavailable { |
| 795 | reason: reason.into(), |
| 796 | }; |
| 797 | let mut explicit_python = None; |
| 798 | if language == Language::Python { |
| 799 | for &node in nodes { |
| 800 | if in_function(node, language) || owner_class(node, language).is_some() { |
| 801 | continue; |
| 802 | } |
| 803 | if node.kind() == "call" |
| 804 | && node |
| 805 | .child_by_field_name("function") |
| 806 | .is_some_and(|function| text(function, source).starts_with("__all__.")) |
| 807 | { |
| 808 | return unavailable("Python __all__ is used through a potentially mutating call"); |
| 809 | } |
| 810 | if matches!(node.kind(), "assignment" | "augmented_assignment") |
| 811 | && node |
| 812 | .child_by_field_name("left") |
| 813 | .is_some_and(|left| text(left, source).starts_with("__all__[")) |
| 814 | { |
| 815 | return unavailable("Python __all__ is mutated through indexing"); |
| 816 | } |
| 817 | if matches!(node.kind(), "assignment" | "augmented_assignment") |
| 818 | && node |
| 819 | .child_by_field_name("left") |
| 820 | .is_some_and(|left| text(left, source) == "__all__") |
| 821 | { |
| 822 | if explicit_python.is_some() || node.kind() == "augmented_assignment" { |
| 823 | return unavailable("Dynamic or multiply assigned Python __all__"); |
| 824 | } |
| 825 | let Some(value) = node |
| 826 | .child_by_field_name("right") |
| 827 | .filter(|value| matches!(value.kind(), "list" | "tuple")) |
| 828 | else { |
| 829 | return unavailable("Python __all__ is not a static literal sequence"); |
| 830 | }; |
| 831 | let mut exports = BTreeSet::new(); |
| 832 | for item in named(value) { |
| 833 | if item.kind() != "string" { |
| 834 | return unavailable("Python __all__ contains nonliteral names"); |
| 835 | } |
| 836 | let raw = text(item, source); |
| 837 | if !raw.starts_with(['\'', '"']) |
| 838 | || raw.starts_with("\"\"\"") |
| 839 | || raw.starts_with("'''") |
| 840 | { |
| 841 | return unavailable("Prefixed/triple-quoted export names are not decoded by this convention"); |
| 842 | } |
| 843 | let Some(name) = unquote(raw) else { |
| 844 | return unavailable("Python __all__ contains encoded or dynamic names"); |
| 845 | }; |
| 846 | exports.insert(name); |
| 847 | } |
| 848 | explicit_python = Some(exports); |
| 849 | } |
| 850 | } |
no test coverage detected