Skip to content

Commit

Permalink
Separate AugmentationImportDirective from ImportDirective.
Browse files Browse the repository at this point in the history
Change-Id: I202320513390dbb218ec86fed18fc2943b168dd8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250413
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Bot committed Jul 2, 2022
1 parent 1ed8748 commit 33d6a34
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 55 deletions.
25 changes: 25 additions & 0 deletions pkg/analyzer/lib/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ abstract class AstVisitor<R> {

R? visitAssignmentExpression(AssignmentExpression node);

R? visitAugmentationImportDirective(AugmentationImportDirective node);

R? visitAwaitExpression(AwaitExpression node);

R? visitBinaryExpression(BinaryExpression node);
Expand Down Expand Up @@ -617,6 +619,29 @@ abstract class AstVisitor<R> {
R? visitYieldStatement(YieldStatement node);
}

/// An augmentation import directive.
///
/// importDirective ::=
/// [Annotation] 'import' 'augment' [StringLiteral] ';'
///
/// Clients may not extend, implement or mix-in this class.
@experimental
abstract class AugmentationImportDirective implements UriBasedDirective {
/// The token representing the 'augment' keyword.
Token get augmentKeyword;

/// Return the element associated with this directive, or `null` if the AST
/// structure has not been resolved.
@override
AugmentationImportElement? get element;

/// The token representing the 'import' keyword.
Token get importKeyword;

/// Return the semicolon terminating the directive.
Token get semicolon;
}

/// An await expression.
///
/// awaitExpression ::=
Expand Down
29 changes: 29 additions & 0 deletions pkg/analyzer/lib/dart/ast/visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class GeneralizingAstVisitor<R> implements AstVisitor<R> {
R? visitAssignmentExpression(AssignmentExpression node) =>
visitExpression(node);

@override
R? visitAugmentationImportDirective(AugmentationImportDirective node) =>
visitUriBasedDirective(node);

@override
R? visitAwaitExpression(AwaitExpression node) => visitExpression(node);

Expand Down Expand Up @@ -697,6 +701,12 @@ class RecursiveAstVisitor<R> implements AstVisitor<R> {
return null;
}

@override
R? visitAugmentationImportDirective(AugmentationImportDirective node) {
node.visitChildren(this);
return null;
}

@override
R? visitAwaitExpression(AwaitExpression node) {
node.visitChildren(this);
Expand Down Expand Up @@ -1480,6 +1490,9 @@ class SimpleAstVisitor<R> implements AstVisitor<R> {
@override
R? visitAssignmentExpression(AssignmentExpression node) => null;

@override
R? visitAugmentationImportDirective(AugmentationImportDirective node) => null;

@override
R? visitAwaitExpression(AwaitExpression node) => null;

Expand Down Expand Up @@ -1896,6 +1909,10 @@ class ThrowingAstVisitor<R> implements AstVisitor<R> {
@override
R? visitAssignmentExpression(AssignmentExpression node) => _throw(node);

@override
R? visitAugmentationImportDirective(AugmentationImportDirective node) =>
_throw(node);

@override
R? visitAwaitExpression(AwaitExpression node) => _throw(node);

Expand Down Expand Up @@ -2362,6 +2379,14 @@ class TimedAstVisitor<T> implements AstVisitor<T> {
return result;
}

@override
T? visitAugmentationImportDirective(AugmentationImportDirective node) {
stopwatch.start();
T? result = _baseVisitor.visitAugmentationImportDirective(node);
stopwatch.stop();
return result;
}

@override
T? visitAwaitExpression(AwaitExpression node) {
stopwatch.start();
Expand Down Expand Up @@ -3400,6 +3425,10 @@ class UnifyingAstVisitor<R> implements AstVisitor<R> {
@override
R? visitAssignmentExpression(AssignmentExpression node) => visitNode(node);

@override
R? visitAugmentationImportDirective(AugmentationImportDirective node) =>
visitNode(node);

@override
R? visitAwaitExpression(AwaitExpression node) => visitNode(node);

Expand Down
22 changes: 10 additions & 12 deletions pkg/analyzer/lib/src/dart/analysis/file_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -931,19 +931,17 @@ class FileState {
if (directive is ExportDirective) {
var builder = _serializeNamespaceDirective(directive);
exports.add(builder);
} else if (directive is AugmentationImportDirectiveImpl) {
augmentations.add(
UnlinkedImportAugmentationDirective(
uri: directive.uri.stringValue,
),
);
} else if (directive is ImportDirectiveImpl) {
if (directive.augmentKeyword != null) {
augmentations.add(
UnlinkedImportAugmentationDirective(
uri: directive.uri.stringValue,
),
);
} else {
var builder = _serializeNamespaceDirective(directive);
imports.add(builder);
if (builder.uri == 'dart:core') {
hasDartCoreImport = true;
}
var builder = _serializeNamespaceDirective(directive);
imports.add(builder);
if (builder.uri == 'dart:core') {
hasDartCoreImport = true;
}
} else if (directive is LibraryAugmentationDirective) {
final uri = directive.uri;
Expand Down
22 changes: 10 additions & 12 deletions pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,17 @@ class LibraryAnalyzer {
if (directive is LibraryDirectiveImpl) {
libraryNameNode = directive.name;
directivesToResolve.add(directive);
} else if (directive is AugmentationImportDirective) {
// TODO(scheglov) implement
throw UnimplementedError();
} else if (directive is ImportDirectiveImpl) {
if (directive.augmentKeyword != null) {
// TODO(scheglov) implement
throw UnimplementedError();
} else {
_resolveImportDirective(
directive: directive,
importElement: _libraryElement.imports[importIndex],
importState: _library.imports[importIndex],
libraryErrorReporter: libraryErrorReporter,
);
importIndex++;
}
_resolveImportDirective(
directive: directive,
importElement: _libraryElement.imports[importIndex],
importState: _library.imports[importIndex],
libraryErrorReporter: libraryErrorReporter,
);
importIndex++;
} else if (directive is ExportDirectiveImpl) {
_resolveExportDirective(
directive: directive,
Expand Down
64 changes: 58 additions & 6 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,64 @@ abstract class AstNodeImpl implements AstNode {
}
}

/// An augmentation import directive.
///
/// importDirective ::=
/// [Annotation] 'import' 'augment' [StringLiteral] ';'
class AugmentationImportDirectiveImpl extends UriBasedDirectiveImpl
implements AugmentationImportDirective {
@override
Token importKeyword;

@override
Token augmentKeyword;

@override
Token semicolon;

AugmentationImportDirectiveImpl({
required CommentImpl? comment,
required List<Annotation>? metadata,
required this.importKeyword,
required this.augmentKeyword,
required this.semicolon,
required StringLiteralImpl uri,
}) : super(comment, metadata, uri) {
_becomeParentOf(_uri);
}

@override
AugmentationImportElement? get element {
return super.element as AugmentationImportElement?;
}

@override
Token get endToken => semicolon;

@override
Token get firstTokenAfterCommentAndMetadata => keyword;

@override
Token get keyword => importKeyword;

@override
LibraryAugmentationElement? get uriElement {
return element?.augmentation;
}

@override
ChildEntities get _childEntities => super._childEntities
..addToken('keyword', keyword)
..addToken('augmentKeyword', augmentKeyword)
..addNode('uri', uri)
..addToken('semicolon', semicolon);

@override
E? accept<E>(AstVisitor<E> visitor) {
return visitor.visitAugmentationImportDirective(this);
}
}

/// An await expression.
///
/// awaitExpression ::=
Expand Down Expand Up @@ -6311,10 +6369,6 @@ class ImplicitCallReferenceImpl extends ExpressionImpl
// [Combinator]* ';'
class ImportDirectiveImpl extends NamespaceDirectiveImpl
implements ImportDirective {
/// The token representing the 'augment' keyword, or `null` if the import is
/// not a library augmentation import.
Token? augmentKeyword;

/// The token representing the 'deferred' keyword, or `null` if the imported
/// is not deferred.
@override
Expand All @@ -6339,7 +6393,6 @@ class ImportDirectiveImpl extends NamespaceDirectiveImpl
CommentImpl? comment,
List<Annotation>? metadata,
Token keyword,
this.augmentKeyword,
StringLiteralImpl libraryUri,
List<Configuration>? configurations,
this.deferredKeyword,
Expand Down Expand Up @@ -6370,7 +6423,6 @@ class ImportDirectiveImpl extends NamespaceDirectiveImpl
@override
ChildEntities get _childEntities => super._childEntities
..addToken('keyword', keyword)
..addToken('augmentKeyword', augmentKeyword)
..addNode('uri', uri)
..addToken('deferredKeyword', deferredKeyword)
..addToken('asKeyword', asKeyword)
Expand Down
23 changes: 11 additions & 12 deletions pkg/analyzer/lib/src/dart/ast/ast_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -735,22 +735,21 @@ class AstFactoryImpl {
typeArgumentTypes: typeArgumentTypes);

ImportDirectiveImpl importDirective(
Comment? comment,
List<Annotation>? metadata,
Token keyword,
StringLiteral libraryUri,
List<Configuration>? configurations,
Token? deferredKeyword,
Token? asKeyword,
SimpleIdentifier? prefix,
List<Combinator>? combinators,
Token semicolon,
{Token? augmentKeyword}) =>
Comment? comment,
List<Annotation>? metadata,
Token keyword,
StringLiteral libraryUri,
List<Configuration>? configurations,
Token? deferredKeyword,
Token? asKeyword,
SimpleIdentifier? prefix,
List<Combinator>? combinators,
Token semicolon,
) =>
ImportDirectiveImpl(
comment as CommentImpl?,
metadata,
keyword,
augmentKeyword,
libraryUri as StringLiteralImpl,
configurations,
deferredKeyword,
Expand Down
8 changes: 8 additions & 0 deletions pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class ToSourceVisitor implements AstVisitor<void> {
_visitNode(node.rightHandSide);
}

@override
void visitAugmentationImportDirective(AugmentationImportDirective node) {
_visitNodeList(node.metadata, separator: ' ', suffix: ' ');
sink.write('import augment ');
_visitNode(node.uri);
sink.write(';');
}

@override
void visitAwaitExpression(AwaitExpression node) {
sink.write('await ');
Expand Down
18 changes: 18 additions & 0 deletions pkg/analyzer/lib/src/dart/ast/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ class AstComparator implements AstVisitor<bool> {
isEqualNodes(node.rightHandSide, other.rightHandSide);
}

@override
bool visitAugmentationImportDirective(AugmentationImportDirective node) {
final other = _other as AugmentationImportDirective;
return isEqualNodes(
node.documentationComment, other.documentationComment) &&
_isEqualNodeLists(node.metadata, other.metadata) &&
isEqualTokens(node.keyword, other.keyword) &&
isEqualNodes(node.uri, other.uri) &&
isEqualTokens(node.semicolon, other.semicolon);
}

@override
bool visitAwaitExpression(AwaitExpression node) {
AwaitExpression other = _other as AwaitExpression;
Expand Down Expand Up @@ -1697,6 +1708,13 @@ class NodeReplacer implements AstVisitor<bool> {
return visitNode(node);
}

@override
bool visitAugmentationImportDirective(
covariant AugmentationImportDirectiveImpl node,
) {
return visitUriBasedDirective(node);
}

@override
bool visitAwaitExpression(covariant AwaitExpressionImpl node) {
if (identical(node.expression, _oldNode)) {
Expand Down
41 changes: 28 additions & 13 deletions pkg/analyzer/lib/src/fasta/ast_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ class AstBuilder extends StackListener {
var asKeyword = pop(NullValue.As) as Token?;
var prefix = pop(NullValue.Prefix) as SimpleIdentifier?;
var configurations = pop() as List<Configuration>?;
var uri = pop() as StringLiteral;
var uri = pop() as StringLiteralImpl;
var metadata = pop() as List<Annotation>?;
var comment = _findComment(metadata, importKeyword);

Expand All @@ -1839,18 +1839,33 @@ class AstBuilder extends StackListener {
}
}

directives.add(ast.importDirective(
comment,
metadata,
importKeyword,
uri,
configurations,
deferredKeyword,
asKeyword,
prefix,
combinators,
semicolon ?? Tokens.semicolon(),
augmentKeyword: augmentToken));
if (augmentToken != null) {
directives.add(
AugmentationImportDirectiveImpl(
comment: comment,
uri: uri,
importKeyword: importKeyword,
augmentKeyword: augmentToken,
metadata: metadata,
semicolon: semicolon ?? Tokens.semicolon(),
),
);
} else {
directives.add(
ast.importDirective(
comment,
metadata,
importKeyword,
uri,
configurations,
deferredKeyword,
asKeyword,
prefix,
combinators,
semicolon ?? Tokens.semicolon(),
),
);
}
}

@override
Expand Down
Loading

0 comments on commit 33d6a34

Please sign in to comment.