-
Notifications
You must be signed in to change notification settings - Fork 110
Add findType, and fix a bug in TypeChecker. #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:analyzer/dart/element/element.dart'; | ||
| import 'package:analyzer/src/dart/resolver/scope.dart'; | ||
|
|
||
| final _namespaceCache = new Expando<Namespace>(); | ||
|
|
||
| /// Returns a top-level [ClassElement] publicly visible in [library] by [name]. | ||
| /// | ||
| /// Unlike [LibraryElement.getType], this also correctly traverses identifiers | ||
| /// that are accessible via one or more `export` directives. | ||
| ClassElement findType(LibraryElement library, String name) => | ||
| library.getType(name) ?? | ||
| (_namespaceCache[library] ??= | ||
| new NamespaceBuilder().createExportNamespaceForLibrary(library)) | ||
| .get(name) as ClassElement; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:analyzer/dart/element/element.dart'; | ||
| import 'package:build_test/build_test.dart'; | ||
| import 'package:source_gen/source_gen.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| LibraryElement library; | ||
|
|
||
| setUpAll(() async { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, I notice you use this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The body of this function (doing analysis) is expensive, and doesn't change between tests, so this makes the test take ~20s instead of minutes.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I talked with @kevmoo and I guess you get better errors if the setup fails at least so there is that advantage. Also if all tests end up skipped then it wouldn't execute this code.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, also it makes the use of |
||
| final resolver = await resolveSource(r''' | ||
| library test_lib; | ||
| export 'dart:collection' show LinkedHashMap; | ||
| export 'package:source_gen/source_gen.dart' show Generator; | ||
| import 'dart:async' show Stream; | ||
| class Example {} | ||
| '''); | ||
| library = resolver.getLibraryByName('test_lib'); | ||
| }); | ||
|
|
||
| final isClassElement = const isInstanceOf<ClassElement>(); | ||
|
|
||
| test('should return a type not exported', () { | ||
| expect(findType(library, 'Example'), isClassElement); | ||
| }); | ||
|
|
||
| test('should return a type exported from dart:', () { | ||
| expect(findType(library, 'LinkedHashMap'), isClassElement); | ||
| }); | ||
|
|
||
| test('should return a type exported from package:', () { | ||
| expect(findType(library, 'Generator'), isClassElement); | ||
| }); | ||
|
|
||
| test('should not return a type imported', () { | ||
| expect(findType(library, 'Stream'), isNull); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "stable package or the Dart SDK via
dart:URLs.