Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: lib/src/formatter.dart

Issue 2061383003: Fix NSME when file contents cannot be read (#260). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: better_handling Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 linter.src.formatter; 5 library linter.src.formatter;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:math'; 8 import 'dart:math';
9 9
10 import 'package:analyzer/analyzer.dart'; 10 import 'package:analyzer/analyzer.dart';
11 import 'package:analyzer/src/generated/engine.dart'; 11 import 'package:analyzer/src/generated/engine.dart';
12 import 'package:analyzer/src/generated/source.dart'; 12 import 'package:analyzer/src/generated/source.dart';
13 import 'package:analyzer/src/services/lint.dart'; 13 import 'package:analyzer/src/services/lint.dart';
14 import 'package:linter/src/linter.dart'; 14 import 'package:linter/src/linter.dart';
15 15
16 String getLineContents(int lineNumber, AnalysisError error) { 16 String getLineContents(int lineNumber, AnalysisError error) {
17 var path = error.source.fullName; 17 String path = error.source.fullName;
18 var file = new File(path); 18 File file = new File(path);
19 if (file.existsSync()) { 19 String failureDetails;
20 if (!file.existsSync()) {
21 failureDetails = 'file at $path does not exist';
22 } else {
20 var lines = file.readAsLinesSync(); 23 var lines = file.readAsLinesSync();
21 var lineIndex = lineNumber - 1; 24 var lineIndex = lineNumber - 1;
22 if (lines.length > lineIndex) { 25 if (lines.length > lineIndex) {
23 return lines[lineIndex]; 26 return lines[lineIndex];
24 } 27 }
28 failureDetails =
29 'line index ($lineIndex), outside of file line range (${lines.length})';
25 } 30 }
26 return null; 31 throw new StateError('Unable to get contents for line: $failureDetails');
27 } 32 }
28 33
29 String pluralize(String word, int count) => 34 String pluralize(String word, int count) =>
30 "$count ${count == 1 ? '$word' : '${word}s'}"; 35 "$count ${count == 1 ? '$word' : '${word}s'}";
31 36
32 String shorten(String fileRoot, String fullName) { 37 String shorten(String fileRoot, String fullName) {
33 if (fileRoot == null || !fullName.startsWith(fileRoot)) { 38 if (fileRoot == null || !fullName.startsWith(fileRoot)) {
34 return fullName; 39 return fullName;
35 } 40 }
36 return fullName.substring(fileRoot.length); 41 return fullName.substring(fileRoot.length);
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 274
270 void _writeLint(AnalysisError error, LineInfo lineInfo) { 275 void _writeLint(AnalysisError error, LineInfo lineInfo) {
271 var offset = error.offset; 276 var offset = error.offset;
272 var location = lineInfo.getLocation(offset); 277 var location = lineInfo.getLocation(offset);
273 var line = location.lineNumber; 278 var line = location.lineNumber;
274 var column = location.columnNumber; 279 var column = location.columnNumber;
275 280
276 writeLint(error, offset: offset, column: column, line: line); 281 writeLint(error, offset: offset, column: column, line: line);
277 } 282 }
278 } 283 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698