|
| 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 | +import 'package:analyzer/dart/analysis/results.dart'; |
| 6 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 7 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 | +import 'package:analyzer/dart/element/element.dart'; |
| 9 | +import 'package:analyzer/dart/element/type.dart'; |
| 10 | + |
| 11 | +import '../utils.dart'; |
| 12 | +import 'analyze.dart'; |
| 13 | + |
| 14 | +/// Verify that no RenderBox subclasses call compute* instead of get* for |
| 15 | +/// computing the intrinsic dimensions. The full list of RenderBox intrinsic |
| 16 | +/// methods checked by this rule is listed in [candidates]. |
| 17 | +final AnalyzeRule renderBoxIntrinsicCalculation = _RenderBoxIntrinsicCalculationRule(); |
| 18 | + |
| 19 | +const Map<String, String> candidates = <String, String> { |
| 20 | + 'computeDryBaseline': 'getDryBaseline', |
| 21 | + 'computeDryLayout': 'getDryLayout', |
| 22 | + 'computeDistanceToActualBaseline': 'getDistanceToBaseline, or getDistanceToActualBaseline', |
| 23 | + 'computeMaxIntrinsicHeight': 'getMaxIntrinsicHeight', |
| 24 | + 'computeMinIntrinsicHeight': 'getMinIntrinsicHeight', |
| 25 | + 'computeMaxIntrinsicWidth': 'getMaxIntrinsicWidth', |
| 26 | + 'computeMinIntrinsicWidth': 'getMinIntrinsicWidth' |
| 27 | +}; |
| 28 | + |
| 29 | +class _RenderBoxIntrinsicCalculationRule implements AnalyzeRule { |
| 30 | + final Map<ResolvedUnitResult, List<(AstNode, String)>> _errors = <ResolvedUnitResult, List<(AstNode, String)>>{}; |
| 31 | + |
| 32 | + @override |
| 33 | + void applyTo(ResolvedUnitResult unit) { |
| 34 | + final _RenderBoxSubclassVisitor visitor = _RenderBoxSubclassVisitor(); |
| 35 | + unit.unit.visitChildren(visitor); |
| 36 | + final List<(AstNode, String)> violationsInUnit = visitor.violationNodes; |
| 37 | + if (violationsInUnit.isNotEmpty) { |
| 38 | + _errors.putIfAbsent(unit, () => <(AstNode, String)>[]).addAll(violationsInUnit); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + void reportViolations(String workingDirectory) { |
| 44 | + if (_errors.isEmpty) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + foundError(<String>[ |
| 49 | + for (final MapEntry<ResolvedUnitResult, List<(AstNode, String)>> entry in _errors.entries) |
| 50 | + for (final (AstNode node, String suggestion) in entry.value) |
| 51 | + '${locationInFile(entry.key, node, workingDirectory)}: ${node.parent}. Consider calling $suggestion instead.', |
| 52 | + '\n${bold}Typically the get* methods should be used to obtain the intrinsics of a RenderBox.$reset', |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + String toString() => 'RenderBox subclass intrinsic calculation best practices'; |
| 58 | +} |
| 59 | + |
| 60 | +class _RenderBoxSubclassVisitor extends RecursiveAstVisitor<void> { |
| 61 | + final List<(AstNode, String)> violationNodes = <(AstNode, String)>[]; |
| 62 | + |
| 63 | + static final Map<InterfaceElement, bool> _isRenderBoxClassElementCache = <InterfaceElement, bool>{}; |
| 64 | + // The cached version, call this method instead of _checkIfImplementsRenderBox. |
| 65 | + static bool _implementsRenderBox(InterfaceElement interfaceElement) { |
| 66 | + // Framework naming convention: a RenderObject subclass names have "Render" in its name. |
| 67 | + if (!interfaceElement.name.contains('Render')) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + return interfaceElement.name == 'RenderBox' |
| 71 | + || _isRenderBoxClassElementCache.putIfAbsent(interfaceElement, () => _checkIfImplementsRenderBox(interfaceElement)); |
| 72 | + } |
| 73 | + |
| 74 | + static bool _checkIfImplementsRenderBox(InterfaceElement element) { |
| 75 | + return element.allSupertypes.any((InterfaceType interface) => _implementsRenderBox(interface.element)); |
| 76 | + } |
| 77 | + |
| 78 | + // We don't care about directives, comments, or asserts. |
| 79 | + @override |
| 80 | + void visitImportDirective(ImportDirective node) { } |
| 81 | + @override |
| 82 | + void visitExportDirective(ExportDirective node) { } |
| 83 | + @override |
| 84 | + void visitComment(Comment node) { } |
| 85 | + @override |
| 86 | + void visitAssertStatement(AssertStatement node) { } |
| 87 | + |
| 88 | + @override |
| 89 | + void visitClassDeclaration(ClassDeclaration node) { |
| 90 | + // Ignore the RenderBox class implementation: that's the only place the |
| 91 | + // compute* methods are supposed to be called. |
| 92 | + if (node.name.lexeme != 'RenderBox') { |
| 93 | + super.visitClassDeclaration(node); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @override |
| 98 | + void visitSimpleIdentifier(SimpleIdentifier node) { |
| 99 | + final String? correctMethodName = candidates[node.name]; |
| 100 | + if (correctMethodName == null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + final bool isCallingSuperImplementation = switch (node.parent) { |
| 104 | + PropertyAccess(target: SuperExpression()) || |
| 105 | + MethodInvocation(target: SuperExpression()) => true, |
| 106 | + _ => false, |
| 107 | + }; |
| 108 | + if (isCallingSuperImplementation) { |
| 109 | + return; |
| 110 | + } |
| 111 | + final Element? declaredInClassElement = node.staticElement?.declaration?.enclosingElement; |
| 112 | + if (declaredInClassElement is InterfaceElement && _implementsRenderBox(declaredInClassElement)) { |
| 113 | + violationNodes.add((node, correctMethodName)); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments