| OLD | NEW |
| 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 computer.error; | 5 library computer.error; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/computer/element.dart'; | 7 import 'package:analysis_server/src/protocol2.dart'; |
| 8 import 'package:analysis_server/src/constants.dart'; | |
| 9 import 'package:analysis_server/src/services/json.dart'; | |
| 10 import 'package:analyzer/src/generated/engine.dart' as engine; | 8 import 'package:analyzer/src/generated/engine.dart' as engine; |
| 11 import 'package:analyzer/src/generated/error.dart' as engine; | 9 import 'package:analyzer/src/generated/error.dart' as engine; |
| 12 import 'package:analyzer/src/generated/source.dart' as engine; | 10 import 'package:analyzer/src/generated/source.dart' as engine; |
| 13 | 11 |
| 14 | 12 |
| 15 /** | 13 /** |
| 16 * Returns a JSON correponding to the given list of Engine errors. | 14 * Returns a JSON correponding to the given list of Engine errors. |
| 17 */ | 15 */ |
| 18 List<Map<String, Object>> engineErrorInfoToJson(engine.AnalysisErrorInfo info) { | 16 List<Map<String, Object>> engineErrorInfoToJson(engine.AnalysisErrorInfo info) { |
| 19 return engineErrorsToJson(info.lineInfo, info.errors); | 17 return engineErrorsToJson(info.lineInfo, info.errors); |
| 20 } | 18 } |
| 21 | 19 |
| 22 | 20 |
| 23 /** | 21 /** |
| 24 * Returns a JSON correponding to the given list of Engine errors. | 22 * Returns a JSON correponding to the given list of Engine errors. |
| 25 */ | 23 */ |
| 26 List<Map<String, Object>> engineErrorsToJson(engine.LineInfo lineInfo, | 24 List<Map<String, Object>> engineErrorsToJson(engine.LineInfo lineInfo, |
| 27 List<engine.AnalysisError> errors) { | 25 List<engine.AnalysisError> errors) { |
| 28 return errors.map((engine.AnalysisError error) { | 26 return errors.map((engine.AnalysisError error) { |
| 29 return new AnalysisError.fromEngine(lineInfo, error).toJson(); | 27 return analysisErrorFromEngine(lineInfo, error).toJson(); |
| 30 }).toList(); | 28 }).toList(); |
| 31 } | 29 } |
| 32 | 30 |
| 33 | 31 |
| 34 /** | 32 AnalysisError analysisErrorFromEngine(engine.LineInfo lineInfo, |
| 35 * An indication of an error, warning, or hint that was produced by the | 33 engine.AnalysisError error) { |
| 36 * analysis. | 34 engine.ErrorCode errorCode = error.errorCode; |
| 37 */ | 35 // prepare location |
| 38 class AnalysisError implements HasToJson { | 36 Location location; |
| 39 final String severity; | 37 { |
| 40 final String type; | 38 String file = error.source.fullName; |
| 41 final Location location; | 39 int offset = error.offset; |
| 42 final String message; | 40 int length = error.length; |
| 43 final String correction; | 41 int startLine = -1; |
| 44 | 42 int startColumn = -1; |
| 45 AnalysisError(this.severity, this.type, this.location, this.message, | 43 if (lineInfo != null) { |
| 46 this.correction); | 44 engine.LineInfo_Location lineLocation = lineInfo.getLocation(offset); |
| 47 | 45 if (lineLocation != null) { |
| 48 factory AnalysisError.fromEngine(engine.LineInfo lineInfo, | 46 startLine = lineLocation.lineNumber; |
| 49 engine.AnalysisError error) { | 47 startColumn = lineLocation.columnNumber; |
| 50 engine.ErrorCode errorCode = error.errorCode; | |
| 51 // prepare location | |
| 52 Location location; | |
| 53 { | |
| 54 String file = error.source.fullName; | |
| 55 int offset = error.offset; | |
| 56 int length = error.length; | |
| 57 int startLine = -1; | |
| 58 int startColumn = -1; | |
| 59 if (lineInfo != null) { | |
| 60 engine.LineInfo_Location lineLocation = lineInfo.getLocation(offset); | |
| 61 if (lineLocation != null) { | |
| 62 startLine = lineLocation.lineNumber; | |
| 63 startColumn = lineLocation.columnNumber; | |
| 64 } | |
| 65 } | 48 } |
| 66 location = new Location(file, offset, length, startLine, startColumn); | |
| 67 } | 49 } |
| 68 // done | 50 location = new Location(file, offset, length, startLine, startColumn); |
| 69 String severity = errorCode.errorSeverity.toString(); | |
| 70 String type = errorCode.type.toString(); | |
| 71 String message = error.message; | |
| 72 String correction = error.correction; | |
| 73 return new AnalysisError(severity, type, location, message, correction); | |
| 74 } | 51 } |
| 75 | 52 // done |
| 76 @override | 53 var severity = new ErrorSeverity(errorCode.errorSeverity.name); |
| 77 Map<String, Object> toJson() { | 54 var type = new ErrorType(errorCode.type.name); |
| 78 Map<String, Object> json = { | 55 String message = error.message; |
| 79 SEVERITY: severity, | 56 String correction = error.correction; |
| 80 TYPE: type, | 57 return new AnalysisError(severity, type, location, message, |
| 81 LOCATION: location.toJson(), | 58 correction: correction); |
| 82 MESSAGE: message | |
| 83 }; | |
| 84 if (correction != null) { | |
| 85 json[CORRECTION] = correction; | |
| 86 } | |
| 87 return json; | |
| 88 } | |
| 89 | |
| 90 @override | |
| 91 String toString() { | |
| 92 return 'AnalysisError(location=$location message=$message; ' | |
| 93 'severity=$severity; type=$type; correction=$correction'; | |
| 94 } | |
| 95 | |
| 96 static AnalysisError fromJson(Map<String, Object> json) { | |
| 97 return new AnalysisError( | |
| 98 json[SEVERITY], | |
| 99 json[TYPE], | |
| 100 new Location.fromJson(json[LOCATION]), | |
| 101 json[MESSAGE], | |
| 102 json[CORRECTION]); | |
| 103 } | |
| 104 } | 59 } |
| OLD | NEW |