OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 /// Command line tool to write checker errors as inline comments in the source | 6 /// Command line tool to write checker errors as inline comments in the source |
7 /// code of the program. This tool requires the info.json file created by | 7 /// code of the program. This tool requires the info.json file created by |
8 /// running devc.dart passing the arguments | 8 /// running devc.dart passing the arguments |
9 /// --dump-info --dump-info-file info.json | 9 /// --dump-info --dump-info-file info.json |
10 | 10 |
11 library dev_compiler.bin.edit_files; | 11 library dev_compiler.bin.edit_files; |
12 | 12 |
13 import 'dart:io'; | 13 import 'dart:io'; |
14 import 'dart:convert'; | 14 import 'dart:convert'; |
15 | 15 |
| 16 import 'package:analyzer/src/generated/source.dart' show Source; |
16 import 'package:args/args.dart'; | 17 import 'package:args/args.dart'; |
17 import 'package:cli_util/cli_util.dart' show getSdkDir; | 18 import 'package:cli_util/cli_util.dart' show getSdkDir; |
18 import 'package:source_maps/refactor.dart'; | 19 import 'package:source_maps/refactor.dart'; |
19 import 'package:source_span/source_span.dart'; | 20 import 'package:source_span/source_span.dart'; |
20 | 21 |
21 import 'package:dev_compiler/src/options.dart'; | 22 import 'package:dev_compiler/src/options.dart'; |
22 import 'package:dev_compiler/src/summary.dart'; | 23 import 'package:dev_compiler/src/summary.dart'; |
23 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; | 24 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; |
24 | 25 |
25 final ArgParser argParser = new ArgParser() | 26 final ArgParser argParser = new ArgParser() |
(...skipping 30 matching lines...) Expand all Loading... |
56 | 57 |
57 class EditFileSummaryVisitor extends RecursiveSummaryVisitor { | 58 class EditFileSummaryVisitor extends RecursiveSummaryVisitor { |
58 var _files = new Map<String, TextEditTransaction>(); | 59 var _files = new Map<String, TextEditTransaction>(); |
59 TypeResolver typeResolver; | 60 TypeResolver typeResolver; |
60 String level; | 61 String level; |
61 String checkoutFilesExecutable; | 62 String checkoutFilesExecutable; |
62 String checkoutFilesArg; | 63 String checkoutFilesArg; |
63 RegExp includePattern; | 64 RegExp includePattern; |
64 RegExp excludePattern; | 65 RegExp excludePattern; |
65 | 66 |
| 67 final Map<Uri, Source> _sources = <Uri, Source>{}; |
| 68 |
66 EditFileSummaryVisitor(this.typeResolver, this.level, | 69 EditFileSummaryVisitor(this.typeResolver, this.level, |
67 this.checkoutFilesExecutable, this.checkoutFilesArg, this.includePattern, | 70 this.checkoutFilesExecutable, this.checkoutFilesArg, this.includePattern, |
68 this.excludePattern); | 71 this.excludePattern); |
69 | 72 |
70 TextEditTransaction getEdits(String name) => _files.putIfAbsent(name, () { | 73 TextEditTransaction getEdits(String name) => _files.putIfAbsent(name, () { |
71 var fileContents = new File(name).readAsStringSync(); | 74 var fileContents = new File(name).readAsStringSync(); |
72 return new TextEditTransaction(fileContents, new SourceFile(fileContents)); | 75 return new TextEditTransaction(fileContents, new SourceFile(fileContents)); |
73 }); | 76 }); |
74 | 77 |
| 78 /// Find the corresponding [Source] for [uri]. |
| 79 Source findSource(Uri uri) { |
| 80 var source = _sources[uri]; |
| 81 if (source != null) return source; |
| 82 return _sources[uri] = typeResolver.context.sourceFactory.forUri('$uri'); |
| 83 } |
| 84 |
75 @override | 85 @override |
76 void visitMessage(MessageSummary message) { | 86 void visitMessage(MessageSummary message) { |
77 var uri = message.span.sourceUrl; | 87 var uri = message.span.sourceUrl; |
78 // Ignore dart: libraries. | 88 // Ignore dart: libraries. |
79 if (uri.scheme == 'dart') return; | 89 if (uri.scheme == 'dart') return; |
80 if (level != null) { | 90 if (level != null) { |
81 // Filter out messages with lower severity. | 91 // Filter out messages with lower severity. |
82 switch (message.level) { | 92 switch (message.level) { |
83 case "info": | 93 case "info": |
84 if (level != "info") return; | 94 if (level != "info") return; |
85 break; | 95 break; |
86 case "warning": | 96 case "warning": |
87 if (level == "severe") return; | 97 if (level == "severe") return; |
88 break; | 98 break; |
89 } | 99 } |
90 } | 100 } |
91 var fullName = typeResolver.findSource(uri).fullName; | 101 var fullName = findSource(uri).fullName; |
92 if (includePattern != null && !includePattern.hasMatch(fullName)) return; | 102 if (includePattern != null && !includePattern.hasMatch(fullName)) return; |
93 if (excludePattern != null && excludePattern.hasMatch(fullName)) return; | 103 if (excludePattern != null && excludePattern.hasMatch(fullName)) return; |
94 var edits = getEdits(fullName); | 104 var edits = getEdits(fullName); |
95 edits.edit(message.span.start.offset, message.span.start.offset, | 105 edits.edit(message.span.start.offset, message.span.start.offset, |
96 " /* DDC:${message.level}: ${message.kind}, ${message.message} */ "); | 106 " /* DDC:${message.level}: ${message.kind}, ${message.message} */ "); |
97 } | 107 } |
98 | 108 |
99 void build() { | 109 void build() { |
100 if (checkoutFilesExecutable != null) { | 110 if (checkoutFilesExecutable != null) { |
101 Process.runSync( | 111 Process.runSync( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 var includePattern = (args['include-pattern'] != null) | 150 var includePattern = (args['include-pattern'] != null) |
141 ? new RegExp(args['include-pattern']) | 151 ? new RegExp(args['include-pattern']) |
142 : null; | 152 : null; |
143 | 153 |
144 var visitor = new EditFileSummaryVisitor(typeResolver, args['level'], | 154 var visitor = new EditFileSummaryVisitor(typeResolver, args['level'], |
145 args['checkout-files-executable'], args['checkout-files-arg'], | 155 args['checkout-files-executable'], args['checkout-files-arg'], |
146 includePattern, excludePattern); | 156 includePattern, excludePattern); |
147 summary.accept(visitor); | 157 summary.accept(visitor); |
148 visitor.build(); | 158 visitor.build(); |
149 } | 159 } |
OLD | NEW |