This repository was archived by the owner on Apr 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
More work towards feature parity of 1x #116
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // 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 'specs/directive.dart'; | ||
| import 'specs/reference.dart'; | ||
|
|
||
| /// Collects references and automatically allocates prefixes and imports. | ||
| /// | ||
| /// `Allocator` takes out the manual work of deciding whether a symbol will | ||
| /// clash with other imports in your generated code, or what imports are needed | ||
| /// to resolve all symbols in your generated code. | ||
| abstract class Allocator { | ||
| /// An allocator that does not prefix symbols nor collects imports. | ||
| static const Allocator none = const _NullAllocator(); | ||
|
|
||
| /// Creates a new default allocator that applies no prefixing. | ||
| factory Allocator() = _Allocator; | ||
|
|
||
| /// Creates a new allocator that applies naive prefixing to avoid conflicts. | ||
| /// | ||
| /// This implementation is not optimized for any particular code generation | ||
| /// style and instead takes a conservative approach of prefixing _every_ | ||
| /// import except references to `dart:core` (which are considered always | ||
| /// imported). | ||
| factory Allocator.simplePrefixing() = _PrefixedAllocator; | ||
|
|
||
| /// Returns a reference string given a [reference] object. | ||
| /// | ||
| /// For example, a no-op implementation: | ||
| /// ```dart | ||
| /// allocate(const Reference('List', 'dart:core')); // Returns 'List'. | ||
| /// ``` | ||
| /// | ||
| /// Where-as an implementation that prefixes imports might output: | ||
| /// ```dart | ||
| /// allocate(const Reference('Foo', 'package:foo')); // Returns '_i1.Foo'. | ||
| /// ``` | ||
| String allocate(Reference reference); | ||
|
|
||
| /// All imports that have so far been added implicitly via [allocate]. | ||
| Iterable<Directive> get imports; | ||
| } | ||
|
|
||
| class _Allocator implements Allocator { | ||
| final _imports = new Set<String>(); | ||
|
|
||
| @override | ||
| String allocate(Reference reference) { | ||
| if (reference.url != null) { | ||
| _imports.add(reference.url); | ||
| } | ||
| return reference.symbol; | ||
| } | ||
|
|
||
| @override | ||
| Iterable<Directive> get imports { | ||
| return _imports.map((u) => new Directive.import(u)); | ||
| } | ||
| } | ||
|
|
||
| class _NullAllocator implements Allocator { | ||
| const _NullAllocator(); | ||
|
|
||
| @override | ||
| String allocate(Reference reference) => reference.symbol; | ||
|
|
||
| @override | ||
| Iterable<Directive> get imports => const []; | ||
| } | ||
|
|
||
| class _PrefixedAllocator implements Allocator { | ||
| static const _doNotPrefix = const ['dart:core']; | ||
|
|
||
| final _imports = <String, int>{}; | ||
| var _keys = 1; | ||
|
|
||
| @override | ||
| String allocate(Reference reference) { | ||
| final symbol = reference.symbol; | ||
| if (reference.url == null || _doNotPrefix.contains(reference.url)) { | ||
| return symbol; | ||
| } | ||
| return '_${_imports.putIfAbsent(reference.url, _nextKey)}.$symbol'; | ||
| } | ||
|
|
||
| int _nextKey() => _keys++; | ||
|
|
||
| @override | ||
| Iterable<Directive> get imports { | ||
| return _imports.keys.map( | ||
| (u) => new Directive.import(u, as: '_${_imports[u]}'), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // 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. | ||
|
|
||
| library code_builder.src.specs.directive; | ||
|
|
||
| import 'package:built_value/built_value.dart'; | ||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import '../base.dart'; | ||
| import '../visitors.dart'; | ||
|
|
||
| part 'directive.g.dart'; | ||
|
|
||
| @immutable | ||
| abstract class Directive implements Built<Directive, DirectiveBuilder>, Spec { | ||
| factory Directive([void updates(DirectiveBuilder b)]) = _$Directive; | ||
|
|
||
| factory Directive.import( | ||
| String url, { | ||
| String as, | ||
| }) => | ||
| new Directive((builder) => builder | ||
| ..as = as | ||
| ..type = DirectiveType.import | ||
| ..url = url); | ||
|
|
||
| factory Directive.export(String url) => new Directive((builder) => builder | ||
| ..type = DirectiveType.export | ||
| ..url = url); | ||
|
|
||
| Directive._(); | ||
|
|
||
| @nullable | ||
| String get as; | ||
|
|
||
| String get url; | ||
|
|
||
| DirectiveType get type; | ||
|
|
||
| @override | ||
| R accept<R>(SpecVisitor<R> visitor) => visitor.visitDirective(this); | ||
| } | ||
|
|
||
| abstract class DirectiveBuilder | ||
| implements Builder<Directive, DirectiveBuilder> { | ||
| factory DirectiveBuilder() = _$DirectiveBuilder; | ||
|
|
||
| DirectiveBuilder._(); | ||
|
|
||
| String as; | ||
|
|
||
| String url; | ||
|
|
||
| DirectiveType type; | ||
| } | ||
|
|
||
| enum DirectiveType { | ||
| import, | ||
| export, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think _allocator.imports is ever called. So how would you actually print out the imports in a file?
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.
Thanks, good catch. Added to
visitFileand updatedfile_test.