Skip to content

Commit 9e1d18a

Browse files
authored
Upgrade the analyzer package (#1974)
1 parent 247098d commit 9e1d18a

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies:
3636
watcher: ^1.0.0
3737

3838
dev_dependencies:
39-
analyzer: ^4.7.0
39+
analyzer: ^5.13.0
4040
archive: ^3.1.2
4141
cli_pkg: ^2.4.4
4242
crypto: ^3.0.0

tool/grind/synchronize.dart

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
import 'dart:convert';
66
import 'dart:io';
7+
import 'dart:math' as math;
78

89
import 'package:analyzer/dart/analysis/features.dart';
910
import 'package:analyzer/dart/analysis/utilities.dart';
1011
import 'package:analyzer/dart/ast/ast.dart';
1112
import 'package:analyzer/dart/ast/token.dart';
1213
import 'package:analyzer/dart/ast/visitor.dart';
14+
import 'package:collection/collection.dart';
1315
import 'package:crypto/crypto.dart';
1416
import 'package:dart_style/dart_style.dart';
1517
import 'package:grinder/grinder.dart';
1618
import 'package:path/path.dart' as p;
19+
import 'package:source_span/source_span.dart';
1720

1821
import 'package:sass/src/util/nullable.dart';
1922

@@ -69,37 +72,46 @@ class _Visitor extends RecursiveAstVisitor<void> {
6972
/// The source of the original asynchronous file.
7073
final String _source;
7174

75+
/// The path from which [_source] was loaded.
76+
final String _path;
77+
7278
/// The current position in [_source].
7379
var _position = 0;
7480

7581
/// The buffer in which the text of the synchronous file is built up.
7682
final _buffer = StringBuffer();
7783

84+
/// Returns the [SourceFile] which is being rewritten.
85+
///
86+
/// This is only used for debugging and error reporting.
87+
SourceFile get _sourceFile =>
88+
SourceFile.fromString(_source, url: p.toUri(_path));
89+
7890
/// The synchronous text.
7991
String get result {
8092
_buffer.write(_source.substring(_position));
8193
_position = _source.length;
8294
return _buffer.toString();
8395
}
8496

85-
_Visitor(this._source, String path) {
97+
_Visitor(this._source, this._path) {
8698
var afterHeader = "\n".allMatches(_source).skip(3).first.end;
8799
_buffer.writeln(_source.substring(0, afterHeader));
88100
_buffer.writeln("""
89-
// DO NOT EDIT. This file was generated from ${p.basename(path)}.
101+
// DO NOT EDIT. This file was generated from ${p.basename(_path)}.
90102
// See tool/grind/synchronize.dart for details.
91103
//
92104
// Checksum: ${sha1.convert(utf8.encode(_source))}
93105
//
94106
// ignore_for_file: unused_import
95107
""");
96108

97-
if (p.basename(path) == 'async_evaluate.dart') {
109+
if (p.basename(_path) == 'async_evaluate.dart') {
98110
_buffer.writeln();
99111
_buffer.writeln("import 'async_evaluate.dart' show EvaluateResult;");
100112
_buffer.writeln("export 'async_evaluate.dart' show EvaluateResult;");
101113
_buffer.writeln();
102-
} else if (p.basename(path) == 'async_compile.dart') {
114+
} else if (p.basename(_path) == 'async_compile.dart') {
103115
_buffer.writeln();
104116
_buffer.writeln("export 'async_compile.dart';");
105117
_buffer.writeln();
@@ -132,10 +144,19 @@ class _Visitor extends RecursiveAstVisitor<void> {
132144
}
133145

134146
void visitClassDeclaration(ClassDeclaration node) {
135-
if (_sharedClasses.contains(node.name2.lexeme)) {
147+
if (_sharedClasses.contains(node.name.lexeme)) {
136148
_skipNode(node);
137149
} else {
138-
super.visitClassDeclaration(node);
150+
for (var child in node.sortedCommentAndAnnotations) {
151+
child.accept(this);
152+
}
153+
_rename(node.name);
154+
node.typeParameters?.accept(this);
155+
node.extendsClause?.accept(this);
156+
node.withClause?.accept(this);
157+
node.implementsClause?.accept(this);
158+
node.nativeClause?.accept(this);
159+
node.members.accept(this);
139160
}
140161
}
141162

@@ -144,8 +165,17 @@ class _Visitor extends RecursiveAstVisitor<void> {
144165
node.visitChildren(this);
145166
}
146167

168+
void visitFunctionDeclaration(FunctionDeclaration node) {
169+
for (var child in node.sortedCommentAndAnnotations) {
170+
child.accept(this);
171+
}
172+
node.returnType?.accept(this);
173+
_rename(node.name);
174+
node.functionExpression.accept(this);
175+
}
176+
147177
void visitMethodDeclaration(MethodDeclaration node) {
148-
if (_synchronizeName(node.name2.lexeme) != node.name2.lexeme) {
178+
if (_synchronizeName(node.name.lexeme) != node.name.lexeme) {
149179
// If the file defines any asynchronous versions of synchronous functions,
150180
// remove them.
151181
_skipNode(node);
@@ -189,8 +219,8 @@ class _Visitor extends RecursiveAstVisitor<void> {
189219
}
190220

191221
void visitNamedType(NamedType node) {
192-
if (["Future", "FutureOr"].contains(node.name.name)) {
193-
_skip(node.name.beginToken);
222+
if (["Future", "FutureOr"].contains(node.name2.lexeme)) {
223+
_skip(node.name2);
194224
var typeArguments = node.typeArguments;
195225
if (typeArguments != null) {
196226
_skip(typeArguments.leftBracket);
@@ -199,18 +229,30 @@ class _Visitor extends RecursiveAstVisitor<void> {
199229
} else {
200230
_buffer.write("void");
201231
}
202-
} else if (node.name.name == "Module") {
232+
} else if (node.name2.lexeme == "Module") {
203233
_skipNode(node);
204234
_buffer.write("Module<Callable>");
205235
} else {
206236
super.visitNamedType(node);
207237
}
208238
}
209239

240+
/// Writes through [node]'s (synchronized) name.
241+
///
242+
/// Assumes [node] has a name field with type [Token].
243+
void _rename(Token token) {
244+
_skip(token);
245+
_buffer.write(_synchronizeName(token.lexeme));
246+
}
247+
210248
/// Writes [_source] to [_buffer] up to the beginning of [token], then puts
211249
/// [_position] after [token] so it doesn't get written.
212250
void _skip(Token? token) {
213251
if (token == null) return;
252+
if (token.offset < _position) {
253+
throw _alreadyEmittedException(_spanForToken(token));
254+
}
255+
214256
_buffer.write(_source.substring(_position, token.offset));
215257
_position = token.end;
216258
}
@@ -224,6 +266,10 @@ class _Visitor extends RecursiveAstVisitor<void> {
224266

225267
/// Writes [_source] to [_buffer] up to the beginning of [node].
226268
void _writeTo(AstNode node) {
269+
if (node.beginToken.offset < _position) {
270+
throw _alreadyEmittedException(_spanForNode(node));
271+
}
272+
227273
_buffer.write(_source.substring(_position, node.beginToken.offset));
228274
_position = node.beginToken.offset;
229275
}
@@ -232,6 +278,10 @@ class _Visitor extends RecursiveAstVisitor<void> {
232278
///
233279
/// This leaves [_position] at the end of [node].
234280
void _write(AstNode node) {
281+
if (node.beginToken.offset < _position) {
282+
throw _alreadyEmittedException(_spanForNode(node));
283+
}
284+
235285
_position = node.beginToken.offset;
236286
node.accept(this);
237287
_buffer.write(_source.substring(_position, node.endToken.end));
@@ -248,4 +298,26 @@ class _Visitor extends RecursiveAstVisitor<void> {
248298
return name;
249299
}
250300
}
301+
302+
SourceSpanException _alreadyEmittedException(SourceSpan span) {
303+
var lines = _buffer.toString().split("\n");
304+
return SourceSpanException(
305+
"Node was already emitted. Last 3 lines:\n\n" +
306+
lines
307+
.slice(math.max(lines.length - 3, 0))
308+
.map((line) => " $line")
309+
.join("\n") +
310+
"\n",
311+
span);
312+
}
313+
314+
/// Returns a [FileSpan] that represents [token]'s position in the source
315+
/// file.
316+
SourceSpan _spanForToken(Token token) =>
317+
_sourceFile.span(token.offset, token.end);
318+
319+
/// Returns a [FileSpan] that represents [token]'s position in the source
320+
/// file.
321+
SourceSpan _spanForNode(AstNode node) =>
322+
_sourceFile.span(node.beginToken.offset, node.endToken.end);
251323
}

0 commit comments

Comments
 (0)