| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer.source.analysis_options_provider; | 5 library analyzer.source.analysis_options_provider; |
| 6 | 6 |
| 7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
| 8 | 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 /// Provide the options found in [file]. | 31 /// Provide the options found in [file]. |
| 32 /// Return an empty options map if the file does not exist. | 32 /// Return an empty options map if the file does not exist. |
| 33 Map<String, YamlNode> getOptionsFromFile(File file) { | 33 Map<String, YamlNode> getOptionsFromFile(File file) { |
| 34 var optionsSource = _readAnalysisOptionsFile(file); | 34 var optionsSource = _readAnalysisOptionsFile(file); |
| 35 return getOptionsFromString(optionsSource); | 35 return getOptionsFromString(optionsSource); |
| 36 } | 36 } |
| 37 | 37 |
| 38 /// Provide the options found in [optionsSource]. | 38 /// Provide the options found in [optionsSource]. |
| 39 /// Return an empty options map if the source is null. | 39 /// Return an empty options map if the source is null. |
| 40 Map<String, YamlNode> getOptionsFromString(String optionsSource) { | 40 Map<String, YamlNode> getOptionsFromString(String optionsSource) { |
| 41 var options = <String, YamlNode>{}; | 41 Map<String, YamlNode> options = <String, YamlNode>{}; |
| 42 if (optionsSource == null) { | 42 if (optionsSource == null) { |
| 43 return options; | 43 return options; |
| 44 } | 44 } |
| 45 | 45 |
| 46 YamlNode doc; | 46 YamlNode safelyLoadYamlNode() { |
| 47 try { | 47 try { |
| 48 doc = loadYamlNode(optionsSource); | 48 return loadYamlNode(optionsSource); |
| 49 } on YamlException catch (e) { | 49 } on YamlException catch (e) { |
| 50 throw new OptionsFormatException(e.message, e.span); | 50 throw new OptionsFormatException(e.message, e.span); |
| 51 } catch (e) { | 51 } catch (e) { |
| 52 throw new OptionsFormatException('Unable to parse YAML document.'); | 52 throw new OptionsFormatException('Unable to parse YAML document.'); |
| 53 } |
| 53 } | 54 } |
| 55 YamlNode doc = safelyLoadYamlNode(); |
| 54 | 56 |
| 55 // Empty options. | 57 // Empty options. |
| 56 if (doc is YamlScalar && doc.value == null) { | 58 if (doc is YamlScalar && doc.value == null) { |
| 57 return options; | 59 return options; |
| 58 } | 60 } |
| 59 if ((doc != null) && (doc is! YamlMap)) { | 61 if ((doc != null) && (doc is! YamlMap)) { |
| 60 throw new OptionsFormatException( | 62 throw new OptionsFormatException( |
| 61 'Bad options file format (expected map, got ${doc.runtimeType})', | 63 'Bad options file format (expected map, got ${doc.runtimeType})', |
| 62 doc.span); | 64 doc.span); |
| 63 } | 65 } |
| 64 if (doc is YamlMap) { | 66 if (doc is YamlMap) { |
| 65 (doc as YamlMap).nodes.forEach((k, v) { | 67 doc.nodes.forEach((k, YamlNode v) { |
| 66 var key; | 68 var key; |
| 67 if (k is YamlScalar) { | 69 if (k is YamlScalar) { |
| 68 key = k.value; | 70 key = k.value; |
| 69 } | 71 } |
| 70 if (key is! String) { | 72 if (key is! String) { |
| 71 throw new OptionsFormatException( | 73 throw new OptionsFormatException( |
| 72 'Bad options file format (expected String scope key, ' | 74 'Bad options file format (expected String scope key, ' |
| 73 'got ${k.runtimeType})', | 75 'got ${k.runtimeType})', |
| 74 k != null ? k.span : doc.span); | 76 k != null ? k.span : doc.span); |
| 75 } | 77 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 /// Thrown on options format exceptions. | 118 /// Thrown on options format exceptions. |
| 117 class OptionsFormatException implements Exception { | 119 class OptionsFormatException implements Exception { |
| 118 final String message; | 120 final String message; |
| 119 final SourceSpan span; | 121 final SourceSpan span; |
| 120 OptionsFormatException(this.message, [this.span]); | 122 OptionsFormatException(this.message, [this.span]); |
| 121 | 123 |
| 122 @override | 124 @override |
| 123 String toString() => | 125 String toString() => |
| 124 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; | 126 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; |
| 125 } | 127 } |
| OLD | NEW |