|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// TODO(nate-thegrate): remove this file if @protected changes, or add a test if it doesn't. |
| 6 | +// https://github.com/dart-lang/sdk/issues/57094 |
| 7 | + |
| 8 | +import 'package:analyzer/dart/analysis/results.dart'; |
| 9 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 10 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 11 | +import 'package:analyzer/dart/element/element.dart'; |
| 12 | +import 'package:analyzer/dart/element/type.dart'; |
| 13 | + |
| 14 | +import '../utils.dart'; |
| 15 | +import 'analyze.dart'; |
| 16 | + |
| 17 | +final AnalyzeRule protectPublicStateSubtypes = _ProtectPublicStateSubtypes(); |
| 18 | + |
| 19 | +class _ProtectPublicStateSubtypes implements AnalyzeRule { |
| 20 | + final Map<ResolvedUnitResult, List<MethodDeclaration>> _errors = <ResolvedUnitResult, List<MethodDeclaration>>{}; |
| 21 | + |
| 22 | + @override |
| 23 | + void applyTo(ResolvedUnitResult unit) { |
| 24 | + final _StateSubclassVisitor visitor = _StateSubclassVisitor(); |
| 25 | + unit.unit.visitChildren(visitor); |
| 26 | + final List<MethodDeclaration> unprotected = visitor.unprotectedMethods; |
| 27 | + if (unprotected.isNotEmpty) { |
| 28 | + _errors.putIfAbsent(unit, () => <MethodDeclaration>[]).addAll(unprotected); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + void reportViolations(String workingDirectory) { |
| 34 | + if (_errors.isEmpty) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + foundError( |
| 39 | + <String>[ |
| 40 | + for (final MapEntry<ResolvedUnitResult, List<MethodDeclaration>> entry in _errors.entries) |
| 41 | + for (final MethodDeclaration method in entry.value) |
| 42 | + '${locationInFile(entry.key, method, workingDirectory)}: $method - missing "@protected" annotation.', |
| 43 | + '\nPublic State subtypes should add @protected when overriding methods,', |
| 44 | + 'to avoid exposing internal logic to developers.', |
| 45 | + ], |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @override |
| 50 | + String toString() => 'Add "@protected" to public State subtypes'; |
| 51 | +} |
| 52 | + |
| 53 | +class _StateSubclassVisitor extends SimpleAstVisitor<void> { |
| 54 | + final List<MethodDeclaration> unprotectedMethods = <MethodDeclaration>[]; |
| 55 | + |
| 56 | + /// Holds the `State` class [DartType]. |
| 57 | + static DartType? stateType; |
| 58 | + |
| 59 | + static bool isPublicStateSubtype(InterfaceElement element) { |
| 60 | + if (!element.isPublic) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + if (stateType != null) { |
| 64 | + return element.allSupertypes.contains(stateType); |
| 65 | + } |
| 66 | + for (final InterfaceType superType in element.allSupertypes) { |
| 67 | + if (superType.element.name == 'State') { |
| 68 | + stateType = superType; |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + @override |
| 76 | + void visitClassDeclaration(ClassDeclaration node) { |
| 77 | + if (isPublicStateSubtype(node.declaredElement!)) { |
| 78 | + node.visitChildren(this); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// Checks whether overridden `State` methods have the `@protected` annotation, |
| 83 | + /// and adds the declaration to [unprotectedMethods] if not. |
| 84 | + @override |
| 85 | + void visitMethodDeclaration(MethodDeclaration node) { |
| 86 | + switch (node.name.lexeme) { |
| 87 | + case 'initState': |
| 88 | + case 'didUpdateWidget': |
| 89 | + case 'didChangeDependencies': |
| 90 | + case 'reassemble': |
| 91 | + case 'deactivate': |
| 92 | + case 'activate': |
| 93 | + case 'dispose': |
| 94 | + case 'build': |
| 95 | + case 'debugFillProperties': |
| 96 | + if (!node.declaredElement!.hasProtected) { |
| 97 | + unprotectedMethods.add(node); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments