Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 032419e

Browse files
author
John Messerly
committed
fixes #6, refactor to use AnalysisError/Listener throughout dev_compiler
Main goal here was to get rid of the enter/exit compilation unit tracking, to allow further Compiler/Server refactoring. We can get the Uri/LineInfo/contents off Source, which AnalysisError already has SummaryReporter is now fairly generic. TODOs for the future: * figure out what to do about ErrorCodes, in particular dev_compiler prefix feels off. * skip the intermediate StaticInfo types? They're really just builders now for AnalysisError. * don't classify all existing messages as "AnalyzerError" ... just print them ... * skip logger. For our command line program, stdout is part of its "API", IMO it shouldn't use logging for those messages. Consider Pub, which uses stdout/err for its messages, and uses logging for more detailed troubleshooting. [email protected], [email protected] Review URL: https://codereview.chromium.org/1230903002.
1 parent f722e56 commit 032419e

15 files changed

+254
-310
lines changed

lib/devc.dart

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import 'dart:async';
99
import 'dart:convert';
1010
import 'dart:io';
1111

12-
import 'package:analyzer/src/generated/error.dart'
13-
show AnalysisError, ErrorSeverity, ErrorType;
1412
import 'package:analyzer/src/generated/engine.dart'
1513
show AnalysisContext, ChangeSet;
14+
import 'package:analyzer/src/generated/error.dart'
15+
show AnalysisError, AnalysisErrorListener, ErrorSeverity, ErrorType;
1616
import 'package:analyzer/src/generated/source.dart' show Source;
1717
import 'package:logging/logging.dart' show Level, Logger, LogRecord;
1818
import 'package:path/path.dart' as path;
@@ -54,7 +54,7 @@ abstract class AbstractCompiler {
5454
class Compiler implements AbstractCompiler {
5555
final CompilerOptions options;
5656
final AnalysisContext context;
57-
final CompilerReporter _reporter;
57+
final AnalysisErrorListener _reporter;
5858
final TypeRules rules;
5959
final CodeChecker _checker;
6060
final SourceNode _entryNode;
@@ -64,7 +64,7 @@ class Compiler implements AbstractCompiler {
6464
bool _failure = false;
6565

6666
factory Compiler(CompilerOptions options,
67-
{AnalysisContext context, CompilerReporter reporter}) {
67+
{AnalysisContext context, AnalysisErrorListener reporter}) {
6868
var strongOpts = options.strongOptions;
6969
var sourceOpts = options.sourceOptions;
7070
if (context == null) {
@@ -124,14 +124,11 @@ class Compiler implements AbstractCompiler {
124124

125125
void _buildHtmlFile(HtmlSourceNode node) {
126126
if (outputDir == null) return;
127-
var uri = node.source.uri;
128-
_reporter.enterHtml(uri);
129127
var output = generateEntryHtml(node, options);
130128
if (output == null) {
131129
_failure = true;
132130
return;
133131
}
134-
_reporter.leaveHtml();
135132
var filename = path.basename(node.uri.path);
136133
String outputFile = path.join(outputDir, filename);
137134
new File(outputFile).writeAsStringSync(output);
@@ -169,7 +166,6 @@ class Compiler implements AbstractCompiler {
169166
} else {
170167
node.info = current = new LibraryInfo(lib, _isEntry(node));
171168
}
172-
_reporter.enterLibrary(source.uri);
173169
_libraries.add(current);
174170
rules.currentLibraryInfo = current;
175171

@@ -180,12 +176,9 @@ class Compiler implements AbstractCompiler {
180176
bool failureInLib = false;
181177
for (var unit in libraryUnit.libraryThenParts) {
182178
var unitSource = unit.element.source;
183-
_reporter.enterCompilationUnit(unit);
184-
// TODO(sigmund): integrate analyzer errors with static-info (issue #6).
185179
failureInLib = logErrors(unitSource) || failureInLib;
186180
_checker.visitCompilationUnit(unit);
187181
if (_checker.failure) failureInLib = true;
188-
_reporter.leaveCompilationUnit();
189182
}
190183
if (failureInLib) {
191184
_failure = true;
@@ -196,7 +189,6 @@ class Compiler implements AbstractCompiler {
196189
var hash = cg.generateLibrary(libraryUnit, current);
197190
if (_hashing) node.cachingHash = hash;
198191
}
199-
_reporter.leaveLibrary();
200192
}
201193

202194
/// Log any errors encountered when resolving [source] and return whether any
@@ -216,9 +208,9 @@ class Compiler implements AbstractCompiler {
216208
continue;
217209
}
218210

219-
var message = new AnalyzerMessage.from(error);
220-
if (message.level == Level.SEVERE) failure = true;
221-
_reporter.log(message);
211+
// All analyzer warnings or errors are errors for DDC.
212+
failure = true;
213+
_reporter.onError(error);
222214
}
223215
}
224216
return failure;

lib/src/checker/checker.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import 'package:analyzer/analyzer.dart';
88
import 'package:analyzer/src/generated/ast.dart';
99
import 'package:analyzer/src/generated/element.dart';
1010
import 'package:analyzer/src/generated/scanner.dart' show Token, TokenType;
11-
import 'package:logging/logging.dart' as logger;
1211

1312
import 'package:dev_compiler/src/info.dart';
14-
import 'package:dev_compiler/src/report.dart' show CheckerReporter;
1513
import 'package:dev_compiler/src/utils.dart' show getMemberType;
1614
import 'package:dev_compiler/strong_mode.dart' show StrongModeOptions;
1715
import 'rules.dart';
@@ -22,7 +20,7 @@ import 'rules.dart';
2220
class _OverrideChecker {
2321
bool _failure = false;
2422
final TypeRules _rules;
25-
final CheckerReporter _reporter;
23+
final AnalysisErrorListener _reporter;
2624
final bool _inferFromOverrides;
2725
_OverrideChecker(this._rules, this._reporter, StrongModeOptions options)
2826
: _inferFromOverrides = options.inferFromOverrides;
@@ -327,29 +325,30 @@ class _OverrideChecker {
327325

328326
void _recordMessage(StaticInfo info) {
329327
if (info == null) return;
330-
if (info.level >= logger.Level.SEVERE) _failure = true;
331-
_reporter.log(info);
328+
var error = info.toAnalysisError();
329+
if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) _failure = true;
330+
_reporter.onError(error);
332331
}
333332
}
334333

335334
/// Checks the body of functions and properties.
336335
class CodeChecker extends RecursiveAstVisitor {
337336
final TypeRules _rules;
338-
final CheckerReporter _reporter;
337+
final AnalysisErrorListener _reporter;
339338
final _OverrideChecker _overrideChecker;
340339
bool _failure = false;
341340
bool get failure => _failure || _overrideChecker._failure;
342341

343-
CodeChecker(
344-
TypeRules rules, CheckerReporter reporter, StrongModeOptions options)
342+
CodeChecker(TypeRules rules, AnalysisErrorListener reporter,
343+
StrongModeOptions options)
345344
: _rules = rules,
346345
_reporter = reporter,
347346
_overrideChecker = new _OverrideChecker(rules, reporter, options);
348347

349348
@override
350349
void visitCompilationUnit(CompilationUnit unit) {
351350
void report(Expression expr) {
352-
_reporter.log(new MissingTypeError(expr));
351+
_reporter.onError(new MissingTypeError(expr).toAnalysisError());
353352
}
354353
var callback = _rules.reportMissingType;
355354
_rules.reportMissingType = report;
@@ -988,8 +987,7 @@ class CodeChecker extends RecursiveAstVisitor {
988987
}
989988

990989
void _recordDynamicInvoke(AstNode node, AstNode target) {
991-
var dinvoke = new DynamicInvoke(_rules, node);
992-
_reporter.log(dinvoke);
990+
_reporter.onError(new DynamicInvoke(_rules, node).toAnalysisError());
993991
// TODO(jmesserly): we may eventually want to record if the whole operation
994992
// (node) was dynamic, rather than the target, but this is an easier fit
995993
// with what we used to do.
@@ -998,8 +996,10 @@ class CodeChecker extends RecursiveAstVisitor {
998996

999997
void _recordMessage(StaticInfo info) {
1000998
if (info == null) return;
1001-
if (info.level >= logger.Level.SEVERE) _failure = true;
1002-
_reporter.log(info);
999+
var error = info.toAnalysisError();
1000+
if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) _failure = true;
1001+
_reporter.onError(error);
1002+
10031003
if (info is CoercionInfo) {
10041004
assert(CoercionInfo.get(info.node) == null);
10051005
CoercionInfo.set(info.node, info);

lib/src/codegen/js_printer.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class SourceMapPrintingContext extends JS.JavaScriptPrintingContext {
8484
}
8585
}
8686

87-
SourceLocation _location(int offset) => locationForOffset(unit, uri, offset);
87+
SourceLocation _location(int offset) =>
88+
locationForOffset(unit.lineInfo, uri, offset);
8889

8990
Uri _makeRelativeUri(Uri src) {
9091
return new Uri(path: path.relative(src.path, from: outputDir));

lib/src/dependency_graph.dart

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'package:analyzer/src/generated/ast.dart'
2121
UriBasedDirective;
2222
import 'package:analyzer/src/generated/engine.dart'
2323
show ParseDartTask, AnalysisContext;
24+
import 'package:analyzer/src/generated/error.dart';
2425
import 'package:analyzer/src/generated/source.dart' show Source, SourceKind;
2526
import 'package:html/dom.dart' show Document, Node, Element;
2627
import 'package:html/parser.dart' as html;
@@ -44,7 +45,7 @@ class SourceGraph {
4445

4546
/// Analyzer used to resolve source files.
4647
final AnalysisContext _context;
47-
final CompilerReporter _reporter;
48+
final AnalysisErrorListener _reporter;
4849
final CompilerOptions _options;
4950

5051
SourceGraph(this._context, this._reporter, this._options) {
@@ -181,7 +182,10 @@ class HtmlSourceNode extends SourceNode {
181182
void update() {
182183
super.update();
183184
if (needsRebuild) {
184-
graph._reporter.clearHtml(uri);
185+
var reporter = graph._reporter;
186+
if (reporter is SummaryReporter) {
187+
reporter.clearHtml(uri);
188+
}
185189
document = html.parse(contents, generateSpans: true);
186190
var newScripts = new Set<DartSourceNode>();
187191
var tags = document.querySelectorAll('script[type="application/dart"]');
@@ -232,11 +236,12 @@ class HtmlSourceNode extends SourceNode {
232236
}
233237

234238
void _reportError(SourceGraph graph, String message, Node node) {
235-
graph._reporter.enterHtml(_source.uri);
236239
var span = node.sourceSpan;
237-
graph._reporter.log(
238-
new Message(message, Level.SEVERE, span.start.offset, span.end.offset));
239-
graph._reporter.leaveHtml();
240+
241+
// TODO(jmesserly): should these be errors or warnings?
242+
var errorCode = new HtmlWarningCode('dev_compiler.$runtimeType', message);
243+
graph._reporter.onError(new AnalysisError(
244+
_source, span.start.offset, span.length, errorCode));
240245
}
241246
}
242247

@@ -281,7 +286,11 @@ class DartSourceNode extends SourceNode {
281286
super.update();
282287

283288
if (needsRebuild) {
284-
graph._reporter.clearLibrary(uri);
289+
var reporter = graph._reporter;
290+
if (reporter is SummaryReporter) {
291+
reporter.clearLibrary(uri);
292+
}
293+
285294
// If the defining compilation-unit changed, the structure might have
286295
// changed.
287296
var unit = parseDirectives(contents, name: _source.fullName);
@@ -306,7 +315,7 @@ class DartSourceNode extends SourceNode {
306315
targetUri, () => new DartSourceNode(graph, targetUri, target));
307316
//var node = graph.nodeFromUri(targetUri);
308317
if (node._source == null || !node._source.exists()) {
309-
_reportError(graph, 'File $targetUri not found', unit, d);
318+
_reportError(graph, 'File $targetUri not found', d);
310319
}
311320

312321
if (d is ImportDirective) {
@@ -360,14 +369,9 @@ class DartSourceNode extends SourceNode {
360369
}
361370
}
362371

363-
void _reportError(
364-
SourceGraph graph, String message, CompilationUnit unit, AstNode node) {
365-
graph._reporter
366-
..enterLibrary(_source.uri)
367-
..enterCompilationUnit(unit, _source)
368-
..log(new Message(message, Level.SEVERE, node.offset, node.end))
369-
..leaveCompilationUnit()
370-
..leaveLibrary();
372+
void _reportError(SourceGraph graph, String message, AstNode node) {
373+
graph._reporter.onError(new AnalysisError(_source, node.offset,
374+
node.length, new CompileTimeErrorCode('dev_compiler.$runtimeType', message)));
371375
}
372376
}
373377

0 commit comments

Comments
 (0)