| 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 /// Holds a couple utility functions used at various places in the system. | 5 /// Holds a couple utility functions used at various places in the system. |
| 6 library dev_compiler.src.utils; | 6 library dev_compiler.src.utils; |
| 7 | 7 |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 import 'package:analyzer/src/generated/ast.dart' | 11 import 'package:analyzer/src/generated/ast.dart' |
| 12 show | 12 show |
| 13 ImportDirective, | 13 ImportDirective, |
| 14 ExportDirective, | 14 ExportDirective, |
| 15 PartDirective, | 15 PartDirective, |
| 16 CompilationUnit, | 16 CompilationUnit, |
| 17 Identifier, | 17 Identifier, |
| 18 AnnotatedNode, | 18 AnnotatedNode, |
| 19 AstNode, | 19 AstNode, |
| 20 Expression, | 20 Expression, |
| 21 SimpleIdentifier, | 21 SimpleIdentifier, |
| 22 MethodInvocation; | 22 MethodInvocation; |
| 23 import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl; | 23 import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl; |
| 24 import 'package:analyzer/src/generated/element.dart'; | 24 import 'package:analyzer/src/generated/element.dart'; |
| 25 import 'package:analyzer/src/generated/engine.dart' | 25 import 'package:analyzer/src/generated/engine.dart' |
| 26 show ParseDartTask, AnalysisContext; | 26 show ParseDartTask, AnalysisContext; |
| 27 import 'package:analyzer/src/generated/error.dart' show ErrorCode; |
| 27 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 28 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
| 28 import 'package:analyzer/src/generated/source.dart' show Source; | 29 import 'package:analyzer/src/generated/source.dart' show LineInfo, Source; |
| 29 import 'package:analyzer/analyzer.dart' show parseDirectives; | 30 import 'package:analyzer/analyzer.dart' show parseDirectives; |
| 30 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; | 31 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; |
| 31 import 'package:source_span/source_span.dart'; | 32 import 'package:source_span/source_span.dart'; |
| 32 import 'package:yaml/yaml.dart'; | 33 import 'package:yaml/yaml.dart'; |
| 33 | 34 |
| 34 import 'codegen/js_names.dart' show invalidVariableName; | 35 import 'codegen/js_names.dart' show invalidVariableName; |
| 35 | 36 |
| 36 bool isDartPrivateLibrary(LibraryElement library) { | 37 bool isDartPrivateLibrary(LibraryElement library) { |
| 37 var uri = library.source.uri; | 38 var uri = library.source.uri; |
| 38 if (uri.scheme != "dart") return false; | 39 if (uri.scheme != "dart") return false; |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 _indent = _indent - n; | 286 _indent = _indent - n; |
| 286 assert(_indent >= 0); | 287 assert(_indent >= 0); |
| 287 _prefix = "".padRight(_indent); | 288 _prefix = "".padRight(_indent); |
| 288 } | 289 } |
| 289 | 290 |
| 290 void close() { | 291 void close() { |
| 291 new File(_path).writeAsStringSync('$_sb'); | 292 new File(_path).writeAsStringSync('$_sb'); |
| 292 } | 293 } |
| 293 } | 294 } |
| 294 | 295 |
| 295 SourceLocation locationForOffset(CompilationUnit unit, Uri uri, int offset) { | 296 SourceLocation locationForOffset(LineInfo lineInfo, Uri uri, int offset) { |
| 296 var lineInfo = unit.lineInfo.getLocation(offset); | 297 var loc = lineInfo.getLocation(offset); |
| 297 return new SourceLocation(offset, | 298 return new SourceLocation(offset, |
| 298 sourceUrl: uri, | 299 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); |
| 299 line: lineInfo.lineNumber - 1, | |
| 300 column: lineInfo.columnNumber - 1); | |
| 301 } | 300 } |
| 302 | 301 |
| 303 /// Computes a hash for the given contents. | 302 /// Computes a hash for the given contents. |
| 304 String computeHash(String contents) { | 303 String computeHash(String contents) { |
| 305 if (contents == null || contents == '') return null; | 304 if (contents == null || contents == '') return null; |
| 306 return CryptoUtils.bytesToHex((new MD5()..add(contents.codeUnits)).close()); | 305 return CryptoUtils.bytesToHex((new MD5()..add(contents.codeUnits)).close()); |
| 307 } | 306 } |
| 308 | 307 |
| 309 /// Computes a hash for the given file path (reads the contents in binary form). | 308 /// Computes a hash for the given file path (reads the contents in binary form). |
| 310 String computeHashFromFile(String filepath) { | 309 String computeHashFromFile(String filepath) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 for (var m in type.mixins.reversed) { | 428 for (var m in type.mixins.reversed) { |
| 430 if (match(m)) return m; | 429 if (match(m)) return m; |
| 431 } | 430 } |
| 432 var s = type.superclass; | 431 var s = type.superclass; |
| 433 if (s == null) return null; | 432 if (s == null) return null; |
| 434 | 433 |
| 435 if (match(s)) return type; | 434 if (match(s)) return type; |
| 436 return findSupertype(s, match); | 435 return findSupertype(s, match); |
| 437 } | 436 } |
| 438 | 437 |
| 439 SourceSpanWithContext createSpan( | |
| 440 AnalysisContext context, CompilationUnit unit, int start, int end, | |
| 441 [Source source]) { | |
| 442 if (source == null) source = unit.element.source; | |
| 443 var content = context.getContents(source).data; | |
| 444 return createSpanHelper(unit, start, end, source, content); | |
| 445 } | |
| 446 | |
| 447 SourceSpanWithContext createSpanHelper( | 438 SourceSpanWithContext createSpanHelper( |
| 448 CompilationUnit unit, int start, int end, Source source, String content) { | 439 LineInfo lineInfo, int start, int end, Source source, String content) { |
| 449 var startLoc = locationForOffset(unit, source.uri, start); | 440 var startLoc = locationForOffset(lineInfo, source.uri, start); |
| 450 var endLoc = locationForOffset(unit, source.uri, end); | 441 var endLoc = locationForOffset(lineInfo, source.uri, end); |
| 451 | 442 |
| 452 var lineStart = startLoc.offset - startLoc.column; | 443 var lineStart = startLoc.offset - startLoc.column; |
| 453 // Find the end of the line. This is not exposed directly on LineInfo, but | 444 // Find the end of the line. This is not exposed directly on LineInfo, but |
| 454 // we can find it pretty easily. | 445 // we can find it pretty easily. |
| 455 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get | 446 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get |
| 456 // some help from the LineInfo API. | 447 // some help from the LineInfo API. |
| 457 var lineInfo = unit.lineInfo; | |
| 458 int lineEnd = endLoc.offset; | 448 int lineEnd = endLoc.offset; |
| 459 int unitEnd = unit.endToken.end; | |
| 460 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; | 449 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; |
| 461 while (lineEnd < unitEnd && | 450 while (lineEnd < content.length && |
| 462 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); | 451 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); |
| 463 | 452 |
| 464 var text = content.substring(start, end); | 453 var text = content.substring(start, end); |
| 465 var lineText = content.substring(lineStart, lineEnd); | 454 var lineText = content.substring(lineStart, lineEnd); |
| 466 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); | 455 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); |
| 467 } | 456 } |
| 468 | 457 |
| 458 String errorCodeName(ErrorCode errorCode) { |
| 459 var name = errorCode.name; |
| 460 final prefix = 'dev_compiler.'; |
| 461 if (name.startsWith(prefix)) { |
| 462 return name.substring(prefix.length); |
| 463 } else { |
| 464 // TODO(jmesserly): this is for backwards compat, but not sure it's very |
| 465 // useful to log this. |
| 466 return 'AnalyzerMessage'; |
| 467 } |
| 468 } |
| 469 |
| 469 bool isInlineJS(Element e) => e is FunctionElement && | 470 bool isInlineJS(Element e) => e is FunctionElement && |
| 470 e.library.source.uri.toString() == 'dart:_foreign_helper' && | 471 e.library.source.uri.toString() == 'dart:_foreign_helper' && |
| 471 e.name == 'JS'; | 472 e.name == 'JS'; |
| OLD | NEW |