|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:analyzer/dart/analysis/utilities.dart'; |
| 4 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 5 | +import 'package:path/path.dart' as p; |
| 6 | + |
| 7 | +import 'ignore_matcher.dart'; |
| 8 | +import 'parsed_symbol.dart'; |
| 9 | + |
| 10 | +/// Source file suffixes for generated Dart that is never authored public API. |
| 11 | +const _generatedSuffixes = ['.g.dart', '.freezed.dart', '.gr.dart']; |
| 12 | + |
| 13 | +/// Extracts public API symbols from a single Dart source string. |
| 14 | +/// |
| 15 | +/// Parsing is purely syntactic (no element resolution), so it needs neither |
| 16 | +/// `pub get` nor a resolvable package graph. Dart privacy is name-based, so a |
| 17 | +/// declaration is public when its name does not start with `_`. This matches |
| 18 | +/// the altitude of the TypeScript and Swift parsers, which collect declarations |
| 19 | +/// per file without following exports. |
| 20 | +List<ParsedSymbol> extractFromSource(String source, String relPath) { |
| 21 | + final symbols = <ParsedSymbol>[]; |
| 22 | + final unit = parseString( |
| 23 | + content: source, |
| 24 | + path: relPath, |
| 25 | + throwIfDiagnostics: false, |
| 26 | + ).unit; |
| 27 | + |
| 28 | + for (final declaration in unit.declarations) { |
| 29 | + _visitTopLevel(declaration, relPath, symbols); |
| 30 | + } |
| 31 | + return symbols; |
| 32 | +} |
| 33 | + |
| 34 | +void _visitTopLevel( |
| 35 | + CompilationUnitMember declaration, |
| 36 | + String relPath, |
| 37 | + List<ParsedSymbol> out, |
| 38 | +) { |
| 39 | + switch (declaration) { |
| 40 | + // Class-like containers expose their name and members identically. Unnamed |
| 41 | + // extensions (name == null) fall through, as they have no qualifiable |
| 42 | + // surface. |
| 43 | + case ClassDeclaration(:final name, :final members): |
| 44 | + case MixinDeclaration(:final name, :final members): |
| 45 | + case EnumDeclaration(:final name, :final members): |
| 46 | + case ExtensionTypeDeclaration(:final name, :final members): |
| 47 | + case ExtensionDeclaration(name: final name?, :final members): |
| 48 | + _emitContainer(name.lexeme, members, relPath, out); |
| 49 | + |
| 50 | + case FunctionDeclaration(:final name, :final isGetter, :final isSetter) |
| 51 | + when !isGetter && !isSetter: |
| 52 | + _emit(name.lexeme, SymbolKind.function, relPath, out); |
| 53 | + |
| 54 | + // `class C = A with M;` is a class, not a typedef, so it precedes TypeAlias. |
| 55 | + case ClassTypeAlias(:final name): |
| 56 | + _emit(name.lexeme, SymbolKind.classKind, relPath, out); |
| 57 | + |
| 58 | + case TypeAlias(:final name): |
| 59 | + _emit(name.lexeme, SymbolKind.variable, relPath, out); |
| 60 | + |
| 61 | + case TopLevelVariableDeclaration(:final variables): |
| 62 | + for (final variable in variables.variables) { |
| 63 | + _emit(variable.name.lexeme, SymbolKind.variable, relPath, out); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void _emit( |
| 69 | + String name, SymbolKind kind, String relPath, List<ParsedSymbol> out) { |
| 70 | + if (_isPrivate(name)) return; |
| 71 | + out.add(ParsedSymbol(name: name, kind: kind, file: relPath)); |
| 72 | +} |
| 73 | + |
| 74 | +void _emitContainer( |
| 75 | + String containerName, |
| 76 | + List<ClassMember> members, |
| 77 | + String relPath, |
| 78 | + List<ParsedSymbol> out, |
| 79 | +) { |
| 80 | + if (_isPrivate(containerName)) return; |
| 81 | + out.add( |
| 82 | + ParsedSymbol( |
| 83 | + name: containerName, |
| 84 | + kind: SymbolKind.classKind, |
| 85 | + file: relPath, |
| 86 | + ), |
| 87 | + ); |
| 88 | + |
| 89 | + for (final member in members) { |
| 90 | + if (member is MethodDeclaration) { |
| 91 | + final name = member.name.lexeme; |
| 92 | + if (_isPrivate(name)) continue; |
| 93 | + final kind = (member.isGetter || member.isSetter) |
| 94 | + ? SymbolKind.property |
| 95 | + : SymbolKind.method; |
| 96 | + out.add( |
| 97 | + ParsedSymbol(name: '$containerName.$name', kind: kind, file: relPath), |
| 98 | + ); |
| 99 | + } else if (member is FieldDeclaration) { |
| 100 | + for (final field in member.fields.variables) { |
| 101 | + final name = field.name.lexeme; |
| 102 | + if (_isPrivate(name)) continue; |
| 103 | + out.add( |
| 104 | + ParsedSymbol( |
| 105 | + name: '$containerName.$name', |
| 106 | + kind: SymbolKind.property, |
| 107 | + file: relPath, |
| 108 | + ), |
| 109 | + ); |
| 110 | + } |
| 111 | + } else if (member is ConstructorDeclaration) { |
| 112 | + // The unnamed constructor reuses the class name, matching the |
| 113 | + // `ClassName.ClassName` form the other parsers emit. |
| 114 | + final ctorName = member.name?.lexeme ?? containerName; |
| 115 | + if (_isPrivate(ctorName)) continue; |
| 116 | + out.add( |
| 117 | + ParsedSymbol( |
| 118 | + name: '$containerName.$ctorName', |
| 119 | + kind: SymbolKind.method, |
| 120 | + file: relPath, |
| 121 | + ), |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +bool _isPrivate(String name) => name.startsWith('_'); |
| 128 | + |
| 129 | +/// Discovers every package under [projectRoot] (a directory containing a |
| 130 | +/// `pubspec.yaml` with a `lib/` directory) and extracts the public API surface |
| 131 | +/// of its `lib/**.dart` files. Paths are reported relative to [projectRoot]. |
| 132 | +List<ParsedSymbol> parseDartProject(String projectRoot) { |
| 133 | + final root = p.normalize(p.absolute(projectRoot)); |
| 134 | + final ignore = IgnoreMatcher.load(root); |
| 135 | + final symbols = <ParsedSymbol>[]; |
| 136 | + |
| 137 | + for (final packageDir in _findPackageDirs(root, ignore)) { |
| 138 | + final libDir = Directory(p.join(packageDir, 'lib')); |
| 139 | + if (!libDir.existsSync()) continue; |
| 140 | + |
| 141 | + for (final entity in libDir.listSync(recursive: true, followLinks: false)) { |
| 142 | + if (entity is! File || !entity.path.endsWith('.dart')) continue; |
| 143 | + if (_generatedSuffixes.any(entity.path.endsWith)) continue; |
| 144 | + |
| 145 | + final relPath = p.relative(entity.path, from: root); |
| 146 | + if (ignore.ignores(p.split(relPath).join('/'))) continue; |
| 147 | + |
| 148 | + try { |
| 149 | + symbols.addAll(extractFromSource(entity.readAsStringSync(), relPath)); |
| 150 | + } catch (error) { |
| 151 | + // A single unreadable or unparseable file must not fail the whole |
| 152 | + // check; skip it and surface a warning on stderr. |
| 153 | + stderr.writeln('warning: skipped $relPath: $error'); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + symbols.sort((a, b) => a.name.compareTo(b.name)); |
| 159 | + return symbols; |
| 160 | +} |
| 161 | + |
| 162 | +Iterable<String> _findPackageDirs(String root, IgnoreMatcher ignore) sync* { |
| 163 | + final rootDir = Directory(root); |
| 164 | + if (!rootDir.existsSync()) return; |
| 165 | + |
| 166 | + for (final entity in rootDir.listSync(recursive: true, followLinks: false)) { |
| 167 | + if (entity is! File || p.basename(entity.path) != 'pubspec.yaml') continue; |
| 168 | + |
| 169 | + final dir = p.dirname(entity.path); |
| 170 | + final relDir = p.relative(dir, from: root); |
| 171 | + if (relDir != '.') { |
| 172 | + final segments = p.split(relDir); |
| 173 | + if (segments.any((s) => s.startsWith('.') || s == 'build')) continue; |
| 174 | + if (ignore.ignores(segments.join('/'), isDirectory: true)) continue; |
| 175 | + } |
| 176 | + yield dir; |
| 177 | + } |
| 178 | +} |
0 commit comments