| // Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:analyzer/dart/ast/token.dart'; | |
| import 'package:analyzer/dart/ast/visitor.dart'; | |
| import 'package:analyzer/src/dart/ast/ast.dart'; | |
| import 'package:analyzer/src/dart/ast/token.dart'; | |
| const _notSerializableName = '_notSerializableExpression'; | |
| bool isNotSerializableMarker(UnqualifiedNameExpression node) { | |
| return node.name.lexeme == _notSerializableName; | |
| } | |
| /// If [node] is fully serializable, returns it. | |
| /// Otherwise returns a marker node. | |
| ExpressionImpl replaceNotSerializableExpression(ExpressionImpl node) { | |
| var visitor = _IsSerializableNodeVisitor(); | |
| node.accept2(visitor); | |
| if (visitor.result) { | |
| return node; | |
| } | |
| return UnqualifiedNameExpressionImpl( | |
| name: StringToken(TokenType.STRING, _notSerializableName, -1), | |
| ); | |
| } | |
| class _IsSerializableNodeVisitor extends RecursiveAstVisitor2<void> { | |
| bool result = true; | |
| @override | |
| void visitAnonymousMethodInvocation(AnonymousMethodInvocation node) { | |
| result = false; | |
| } | |
| @override | |
| void visitForElement(ForElement node) { | |
| result = false; | |
| } | |
| @override | |
| void visitFunctionExpression(FunctionExpression node) { | |
| result = false; | |
| } | |
| @override | |
| void visitPatternAssignment(PatternAssignment node) { | |
| result = false; | |
| } | |
| @override | |
| void visitSwitchExpression(SwitchExpression node) { | |
| result = false; | |
| } | |
| } |