Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: pkg/analyzer/lib/src/dart/ast/utilities.dart

Issue 2407553002: Guard against exceptions thrown by linters (issue 27545) (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/dart/ast/visitor.dart ('k') | pkg/analyzer/lib/src/task/dart.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.src.dart.ast.utilities; 5 library analyzer.src.dart.ast.utilities;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
11 import 'package:analyzer/dart/ast/visitor.dart'; 11 import 'package:analyzer/dart/ast/visitor.dart';
12 import 'package:analyzer/dart/element/element.dart'; 12 import 'package:analyzer/dart/element/element.dart';
13 import 'package:analyzer/exception/exception.dart'; 13 import 'package:analyzer/exception/exception.dart';
14 import 'package:analyzer/src/dart/ast/ast.dart'; 14 import 'package:analyzer/src/dart/ast/ast.dart';
15 import 'package:analyzer/src/dart/ast/token.dart'; 15 import 'package:analyzer/src/dart/ast/token.dart';
16 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine; 16 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
17 import 'package:analyzer/src/generated/java_core.dart'; 17 import 'package:analyzer/src/generated/java_core.dart';
18 import 'package:analyzer/src/generated/utilities_collection.dart' show TokenMap; 18 import 'package:analyzer/src/generated/utilities_collection.dart' show TokenMap;
19 import 'package:analyzer/src/generated/utilities_dart.dart'; 19 import 'package:analyzer/src/generated/utilities_dart.dart';
20 20
21 /** 21 /**
22 * A function used to handle exceptions that are thrown by delegates while using
23 * an [ExceptionHandlingDelegatingAstVisitor].
24 */
25 typedef void ExceptionInDelagateHandler(
scheglov 2016/10/07 21:16:44 Delegate
26 AstNode node, AstVisitor visitor, dynamic exception, StackTrace stackTrace);
27
28 /**
22 * An AST visitor that will clone any AST structure that it visits. The cloner 29 * An AST visitor that will clone any AST structure that it visits. The cloner
23 * will only clone the structure, it will not preserve any resolution results or 30 * will only clone the structure, it will not preserve any resolution results or
24 * properties associated with the nodes. 31 * properties associated with the nodes.
25 */ 32 */
26 class AstCloner implements AstVisitor<AstNode> { 33 class AstCloner implements AstVisitor<AstNode> {
27 /** 34 /**
28 * A flag indicating whether tokens should be cloned while cloning an AST 35 * A flag indicating whether tokens should be cloned while cloning an AST
29 * structure. 36 * structure.
30 */ 37 */
31 final bool cloneTokens; 38 final bool cloneTokens;
(...skipping 2592 matching lines...) Expand 10 before | Expand all | Expand 10 after
2624 return parent.uriElement; 2631 return parent.uriElement;
2625 } 2632 }
2626 return null; 2633 return null;
2627 } 2634 }
2628 2635
2629 @override 2636 @override
2630 Element visitVariableDeclaration(VariableDeclaration node) => node.element; 2637 Element visitVariableDeclaration(VariableDeclaration node) => node.element;
2631 } 2638 }
2632 2639
2633 /** 2640 /**
2641 * A [DelegatingAstVisitor] that will additionally catch all exceptions from the
2642 * delegates without stopping the visiting. A function must be provided that
2643 * will be invoked for each such exception.
2644 *
2645 * Clients may not extend, implement or mix-in this class.
2646 */
2647 class ExceptionHandlingDelegatingAstVisitor<T> extends DelegatingAstVisitor<T> {
2648 /**
2649 * The function that will be executed for each exception that is thrown by one
2650 * of the visit methods on the delegate.
2651 */
2652 final ExceptionInDelagateHandler handler;
2653
2654 /**
2655 * Initialize a newly created visitor to use each of the given delegate
2656 * visitors to visit the nodes of an AST structure.
2657 */
2658 ExceptionHandlingDelegatingAstVisitor(
2659 Iterable<AstVisitor<T>> delegates, this.handler)
2660 : super(delegates) {
2661 if (handler == null) {
2662 throw new ArgumentError('A handler must be provided');
2663 }
2664 }
2665
2666 @override
2667 T visitNode(AstNode node) {
2668 delegates.forEach((delegate) {
2669 try {
2670 node.accept(delegate);
2671 } catch (exception, stackTrace) {
2672 handler(node, delegate, exception, stackTrace);
2673 }
2674 });
2675 node.visitChildren(this);
2676 return null;
2677 }
2678
2679 /**
2680 * A function that can be used with instances of this class to log and then
2681 * ignore any exceptions that are thrown by any of the delegates.
2682 */
2683 static void logException(AstNode node, AstVisitor visitor, dynamic exception,
2684 StackTrace stackTrace) {
2685 StringBuffer buffer = new StringBuffer();
2686 buffer.write('Exception while using a ${visitor.runtimeType} to visit a ');
2687 AstNode currentNode = node;
2688 bool first = true;
2689 while (currentNode != null) {
2690 if (first) {
2691 first = false;
2692 } else {
2693 buffer.write('in ');
2694 }
2695 buffer.write(currentNode.runtimeType);
2696 currentNode = currentNode.parent;
2697 }
2698 AnalysisEngine.instance.logger.logError(
2699 buffer.toString(), new CaughtException(exception, stackTrace));
2700 }
2701 }
2702
2703 /**
2634 * An object that will clone any AST structure that it visits. The cloner will 2704 * An object that will clone any AST structure that it visits. The cloner will
2635 * clone the structure, replacing the specified ASTNode with a new ASTNode, 2705 * clone the structure, replacing the specified ASTNode with a new ASTNode,
2636 * mapping the old token stream to a new token stream, and preserving resolution 2706 * mapping the old token stream to a new token stream, and preserving resolution
2637 * results. 2707 * results.
2638 */ 2708 */
2639 @deprecated 2709 @deprecated
2640 class IncrementalAstCloner implements AstVisitor<AstNode> { 2710 class IncrementalAstCloner implements AstVisitor<AstNode> {
2641 /** 2711 /**
2642 * The node to be replaced during the cloning process. 2712 * The node to be replaced during the cloning process.
2643 */ 2713 */
(...skipping 5192 matching lines...) Expand 10 before | Expand all | Expand 10 after
7836 * Safely visit the given [token], printing the [suffix] after the token if it 7906 * Safely visit the given [token], printing the [suffix] after the token if it
7837 * is non-`null`. 7907 * is non-`null`.
7838 */ 7908 */
7839 void _visitTokenWithSuffix(Token token, String suffix) { 7909 void _visitTokenWithSuffix(Token token, String suffix) {
7840 if (token != null) { 7910 if (token != null) {
7841 _writer.print(token.lexeme); 7911 _writer.print(token.lexeme);
7842 _writer.print(suffix); 7912 _writer.print(suffix);
7843 } 7913 }
7844 } 7914 }
7845 } 7915 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/dart/ast/visitor.dart ('k') | pkg/analyzer/lib/src/task/dart.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698