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

Side by Side Diff: pkg/analyzer/lib/src/dart/analysis/analysis_impl.dart

Issue 2684793003: Resolve import/export directives in analyzer itself, report non-libraries. (Closed)
Patch Set: Created 3 years, 10 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) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 'package:analyzer/context/declared_variables.dart'; 5 import 'package:analyzer/context/declared_variables.dart';
6 import 'package:analyzer/dart/ast/ast.dart'; 6 import 'package:analyzer/dart/ast/ast.dart';
7 import 'package:analyzer/dart/ast/token.dart'; 7 import 'package:analyzer/dart/ast/token.dart';
8 import 'package:analyzer/dart/element/element.dart'; 8 import 'package:analyzer/dart/element/element.dart';
9 import 'package:analyzer/error/error.dart'; 9 import 'package:analyzer/error/error.dart';
10 import 'package:analyzer/error/listener.dart'; 10 import 'package:analyzer/error/listener.dart';
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 if (librarySource != null) { 324 if (librarySource != null) {
325 return new _NameOrSource(null, librarySource); 325 return new _NameOrSource(null, librarySource);
326 } 326 }
327 } 327 }
328 } 328 }
329 } 329 }
330 return null; 330 return null;
331 } 331 }
332 332
333 /** 333 /**
334 * Return `true` if the given [source] is a library.
335 */
336 bool _isLibrarySource(Source source) {
337 String uriStr = source.uri.toString();
338 return _store.unlinkedMap[uriStr]?.isPartOf == false;
339 }
340
341 /**
334 * Return a new parsed unresolved [CompilationUnit]. 342 * Return a new parsed unresolved [CompilationUnit].
335 */ 343 */
336 CompilationUnit _parse(FileState file) { 344 CompilationUnit _parse(FileState file) {
337 RecordingErrorListener errorListener = _getErrorListener(file); 345 RecordingErrorListener errorListener = _getErrorListener(file);
338 346
339 String content = file.content; 347 String content = file.content;
340 348
341 CharSequenceReader reader = new CharSequenceReader(content); 349 CharSequenceReader reader = new CharSequenceReader(content);
342 Scanner scanner = new Scanner(file.source, reader, errorListener); 350 Scanner scanner = new Scanner(file.source, reader, errorListener);
343 scanner.scanGenericMethodComments = _analysisOptions.strongMode; 351 scanner.scanGenericMethodComments = _analysisOptions.strongMode;
(...skipping 28 matching lines...) Expand all
372 380
373 ErrorReporter libraryErrorReporter = _getErrorReporter(_library); 381 ErrorReporter libraryErrorReporter = _getErrorReporter(_library);
374 LibraryIdentifier libraryNameNode = null; 382 LibraryIdentifier libraryNameNode = null;
375 bool hasPartDirective = false; 383 bool hasPartDirective = false;
376 var seenPartSources = new Set<Source>(); 384 var seenPartSources = new Set<Source>();
377 var directivesToResolve = <Directive>[]; 385 var directivesToResolve = <Directive>[];
378 for (Directive directive in definingCompilationUnit.directives) { 386 for (Directive directive in definingCompilationUnit.directives) {
379 if (directive is LibraryDirective) { 387 if (directive is LibraryDirective) {
380 libraryNameNode = directive.name; 388 libraryNameNode = directive.name;
381 directivesToResolve.add(directive); 389 directivesToResolve.add(directive);
390 } else if (directive is ImportDirective) {
391 for (ImportElement importElement in _libraryElement.imports) {
392 if (importElement.nameOffset == directive.offset) {
393 directive.element = importElement;
394 if (!_isLibrarySource(importElement.importedLibrary.source)) {
395 ErrorCode errorCode = importElement.isDeferred
396 ? StaticWarningCode.IMPORT_OF_NON_LIBRARY
397 : CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY;
398 libraryErrorReporter.reportErrorForNode(
399 errorCode, directive.uri, [directive.uri]);
400 }
401 }
402 }
403 } else if (directive is ExportDirective) {
404 for (ExportElement exportElement in _libraryElement.exports) {
405 if (exportElement.nameOffset == directive.offset) {
406 directive.element = exportElement;
407 if (!_isLibrarySource(exportElement.exportedLibrary.source)) {
408 libraryErrorReporter.reportErrorForNode(
409 CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY,
410 directive.uri,
411 [directive.uri]);
412 }
413 }
414 }
382 } else if (directive is PartDirective) { 415 } else if (directive is PartDirective) {
383 hasPartDirective = true; 416 hasPartDirective = true;
384 StringLiteral partUri = directive.uri; 417 StringLiteral partUri = directive.uri;
385 Source partSource = directive.uriSource; 418 Source partSource = directive.uriSource;
386 CompilationUnit partUnit = sourceToUnit[partSource]; 419 CompilationUnit partUnit = sourceToUnit[partSource];
387 if (partUnit != null) { 420 if (partUnit != null) {
388 directive.element = partUnit.element; 421 directive.element = partUnit.element;
389 // 422 //
390 // Validate that the part source is unique in the library. 423 // Validate that the part source is unique in the library.
391 // 424 //
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 ResolverErrorCode.MISSING_LIBRARY_DIRECTIVE_WITH_PART, 0, 0); 467 ResolverErrorCode.MISSING_LIBRARY_DIRECTIVE_WITH_PART, 0, 0);
435 } 468 }
436 469
437 // 470 //
438 // Resolve the relevant directives to the library element. 471 // Resolve the relevant directives to the library element.
439 // 472 //
440 for (Directive directive in directivesToResolve) { 473 for (Directive directive in directivesToResolve) {
441 directive.element = _libraryElement; 474 directive.element = _libraryElement;
442 } 475 }
443 476
444 { 477 // TODO(scheglov) remove DirectiveResolver class
445 // TODO(scheglov) fill these maps?
446 DirectiveResolver resolver = new DirectiveResolver({}, {}, {});
447 definingCompilationUnit.accept(resolver);
448 }
449 } 478 }
450 479
451 void _resolveFile(FileState file, CompilationUnit unit) { 480 void _resolveFile(FileState file, CompilationUnit unit) {
452 RecordingErrorListener errorListener = _getErrorListener(file); 481 RecordingErrorListener errorListener = _getErrorListener(file);
453 482
454 CompilationUnitElement unitElement = unit.element; 483 CompilationUnitElement unitElement = unit.element;
455 Source source = file.source; 484 Source source = file.source;
456 485
457 // TODO(scheglov) Hack: set types for top-level variables 486 // TODO(scheglov) Hack: set types for top-level variables
458 // Otherwise TypeResolverVisitor will set declared types, and because we 487 // Otherwise TypeResolverVisitor will set declared types, and because we
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 } 752 }
724 753
725 /** 754 /**
726 * Either the name or the source associated with a part-of directive. 755 * Either the name or the source associated with a part-of directive.
727 */ 756 */
728 class _NameOrSource { 757 class _NameOrSource {
729 final String name; 758 final String name;
730 final Source source; 759 final Source source;
731 _NameOrSource(this.name, this.source); 760 _NameOrSource(this.name, this.source);
732 } 761 }
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