Chromium Code Reviews| 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 import 'dart:io'; | 5 import 'dart:io'; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
| 10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
| 11 import 'package:source_maps/source_maps.dart' hide SourceFile; | 11 import 'package:source_maps/source_maps.dart' hide SourceFile; |
| 12 import 'package:compiler/src/apiimpl.dart'; | 12 import 'package:compiler/src/apiimpl.dart'; |
| 13 import 'package:compiler/src/elements/elements.dart' | 13 import 'package:compiler/src/elements/elements.dart' |
| 14 show LibraryElement, | 14 show LibraryElement, |
| 15 CompilationUnitElement, | 15 CompilationUnitElement, |
| 16 ClassElement, | 16 ClassElement, |
| 17 AstElement; | 17 AstElement; |
| 18 import 'package:compiler/src/io/source_file.dart' show SourceFile; | 18 import 'package:compiler/src/io/source_file.dart' show SourceFile; |
| 19 | 19 |
| 20 validateSourceMap(Uri targetUri, [Compiler compiler]) { | 20 validateSourceMap(Uri targetUri, |
| 21 {Uri mainUri, | |
| 22 Position mainPosition, | |
| 23 Compiler compiler}) { | |
| 21 Uri mapUri = getMapUri(targetUri); | 24 Uri mapUri = getMapUri(targetUri); |
| 25 List<String> targetLines = | |
| 26 new File.fromUri(targetUri).readAsStringSync().split('\n'); | |
|
floitsch
2015/02/02 10:17:57
readAsLinesSync ?
Johnni Winther
2015/02/02 10:49:26
Done.
| |
| 22 SingleMapping sourceMap = getSourceMap(mapUri); | 27 SingleMapping sourceMap = getSourceMap(mapUri); |
| 23 checkFileReferences(targetUri, mapUri, sourceMap); | 28 checkFileReferences(targetUri, mapUri, sourceMap); |
| 24 checkIndexReferences(targetUri, mapUri, sourceMap); | 29 checkIndexReferences(targetLines, mapUri, sourceMap); |
| 25 checkRedundancy(sourceMap); | 30 checkRedundancy(sourceMap); |
| 26 if (compiler != null) { | 31 if (compiler != null) { |
| 27 checkNames(targetUri, mapUri, sourceMap, compiler); | 32 checkNames(targetUri, mapUri, sourceMap, compiler); |
| 28 } | 33 } |
| 34 if (mainUri != null && mainPosition != null) { | |
| 35 checkMainPosition(targetUri, targetLines ,sourceMap, mainUri, mainPosition); | |
| 36 } | |
| 29 } | 37 } |
| 30 | 38 |
| 31 checkIndexReferences(Uri targetUri, Uri mapUri, SingleMapping sourceMap) { | 39 checkIndexReferences(List<String> targetLines, |
| 32 List<String> target = | 40 Uri mapUri, |
| 33 new File.fromUri(targetUri).readAsStringSync().split('\n'); | 41 SingleMapping sourceMap) { |
| 34 int urlsLength = sourceMap.urls.length; | 42 int urlsLength = sourceMap.urls.length; |
| 35 List<List<String>> sources = new List(urlsLength); | 43 List<List<String>> sources = new List(urlsLength); |
| 36 print('Reading sources'); | 44 print('Reading sources'); |
| 37 for (int i = 0; i < urlsLength; i++) { | 45 for (int i = 0; i < urlsLength; i++) { |
| 38 sources[i] = new File.fromUri(mapUri.resolve(sourceMap.urls[i])). | 46 sources[i] = new File.fromUri(mapUri.resolve(sourceMap.urls[i])). |
| 39 readAsStringSync().split('\n'); | 47 readAsStringSync().split('\n'); |
| 40 } | 48 } |
| 41 | 49 |
| 42 sourceMap.lines.forEach((TargetLineEntry line) { | 50 sourceMap.lines.forEach((TargetLineEntry line) { |
| 43 Expect.isTrue(line.line >= 0); | 51 Expect.isTrue(line.line >= 0); |
| 44 Expect.isTrue(line.line < target.length); | 52 Expect.isTrue(line.line < targetLines.length); |
| 45 for (TargetEntry entry in line.entries) { | 53 for (TargetEntry entry in line.entries) { |
| 46 int urlIndex = entry.sourceUrlId; | 54 int urlIndex = entry.sourceUrlId; |
| 47 | 55 |
| 48 // TODO(zarah): Entry columns sometimes point one or more characters too | 56 // TODO(zarah): Entry columns sometimes point one or more characters too |
| 49 // far. Incomment this check when this is fixed. | 57 // far. Incomment this check when this is fixed. |
| 50 // | 58 // |
| 51 // Expect.isTrue(entry.column < target[line.line].length); | 59 // Expect.isTrue(entry.column < target[line.line].length); |
| 52 Expect.isTrue(entry.column >= 0); | 60 Expect.isTrue(entry.column >= 0); |
| 53 Expect.isTrue(urlIndex == null || | 61 Expect.isTrue(urlIndex == null || |
| 54 (urlIndex >= 0 && urlIndex < urlsLength)); | 62 (urlIndex >= 0 && urlIndex < urlsLength)); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 classElement.forEachLocalMember(match); | 164 classElement.forEachLocalMember(match); |
| 157 } else { | 165 } else { |
| 158 match(element); | 166 match(element); |
| 159 } | 167 } |
| 160 }); | 168 }); |
| 161 } | 169 } |
| 162 } | 170 } |
| 163 }); | 171 }); |
| 164 } | 172 } |
| 165 | 173 |
| 174 RegExp mainSignaturePrefix = new RegExp(r'main: \[?function\('); | |
| 175 | |
| 176 // Check that the line pointing to by [mainPosition] in [mainUri] contains | |
| 177 // the main function signature. | |
| 178 checkMainPosition(Uri targetUri, | |
| 179 List<String> targetLines, | |
| 180 SingleMapping sourceMap, | |
| 181 Uri mainUri, | |
| 182 Position mainPosition) { | |
| 183 bool mainPositionFound = false; | |
| 184 sourceMap.lines.forEach((TargetLineEntry lineEntry) { | |
| 185 lineEntry.entries.forEach((TargetEntry entry) { | |
| 186 if (entry.sourceLine == null || entry.sourceUrlId == null) return; | |
| 187 Uri sourceUri = targetUri.resolve(sourceMap.urls[entry.sourceUrlId]); | |
| 188 if (sourceUri != mainUri) return; | |
| 189 if (entry.sourceLine + 1 == mainPosition.line && | |
| 190 entry.sourceColumn + 1 == mainPosition.column) { | |
| 191 Expect.isNotNull(entry.sourceNameId, | |
| 192 "Main position has no name."); | |
| 193 String name = sourceMap.names[entry.sourceNameId]; | |
| 194 Expect.equals('main', name, | |
| 195 "Main position name is not '$name', not 'main'."); | |
| 196 String line = targetLines[lineEntry.line]; | |
| 197 Expect.isTrue(line.contains(mainSignaturePrefix), | |
| 198 "Line mapped to main position " | |
| 199 "([${lineEntry.line + 1},${entry.column + 1}]) " | |
| 200 "expected to contain '${mainSignaturePrefix.pattern}':\n$line\n"); | |
| 201 mainPositionFound = true; | |
| 202 } | |
| 203 }); | |
| 204 }); | |
| 205 Expect.isTrue(mainPositionFound, | |
| 206 'No main position $mainPosition found in $mainUri'); | |
| 207 } | |
| 208 | |
| 209 | |
| 166 sameSourcePoint(TargetEntry entry, TargetEntry otherEntry) { | 210 sameSourcePoint(TargetEntry entry, TargetEntry otherEntry) { |
| 167 return | 211 return |
| 168 (entry.sourceUrlId == otherEntry.sourceUrlId) && | 212 (entry.sourceUrlId == otherEntry.sourceUrlId) && |
| 169 (entry.sourceLine == otherEntry.sourceLine) && | 213 (entry.sourceLine == otherEntry.sourceLine) && |
| 170 (entry.sourceColumn == otherEntry.sourceColumn) && | 214 (entry.sourceColumn == otherEntry.sourceColumn) && |
| 171 (entry.sourceNameId == otherEntry.sourceNameId); | 215 (entry.sourceNameId == otherEntry.sourceNameId); |
| 172 } | 216 } |
| 173 | 217 |
| 174 Uri getMapUri(Uri targetUri) { | 218 Uri getMapUri(Uri targetUri) { |
| 175 print('Accessing $targetUri'); | 219 print('Accessing $targetUri'); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 .createTemp('sourceMap_test-') | 253 .createTemp('sourceMap_test-') |
| 210 .then((Directory dir) { | 254 .then((Directory dir) { |
| 211 return dir; | 255 return dir; |
| 212 }); | 256 }); |
| 213 } | 257 } |
| 214 | 258 |
| 215 class Position { | 259 class Position { |
| 216 final int line; | 260 final int line; |
| 217 final int column; | 261 final int column; |
| 218 | 262 |
| 219 Position(this.line, this.column); | 263 const Position(this.line, this.column); |
| 220 | 264 |
| 221 bool operator <=(Position other) { | 265 bool operator <=(Position other) { |
| 222 return line < other.line || | 266 return line < other.line || |
| 223 line == other.line && column <= other.column; | 267 line == other.line && column <= other.column; |
| 224 } | 268 } |
| 225 | 269 |
| 226 String toString() => '[$line,$column]'; | 270 String toString() => '[$line,$column]'; |
| 227 } | 271 } |
| 228 | 272 |
| 229 class Interval { | 273 class Interval { |
| 230 final Position begin; | 274 final Position begin; |
| 231 final Position end; | 275 final Position end; |
| 232 | 276 |
| 233 Interval(this.begin, this.end); | 277 Interval(this.begin, this.end); |
| 234 | 278 |
| 235 bool contains(Position other) { | 279 bool contains(Position other) { |
| 236 return begin <= other && other <= end; | 280 return begin <= other && other <= end; |
| 237 } | 281 } |
| 238 | 282 |
| 239 String toString() => '$begin-$end'; | 283 String toString() => '$begin-$end'; |
| 240 } | 284 } |
| OLD | NEW |