From ec29dce8e0cabe7a152a91d26981786d5eae87ce Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 23 Sep 2021 22:59:09 +0000 Subject: [PATCH] Remove CompilationUnitElementImpl.sourceContent I hoped to get hints on why we get out of sync with the number of classes, getters, etc in the AST and the element model. Unfortunately it don't not help, we might have a unit without any variables, the corresponding code, but AST has variables. So, this code does not help, and I want to remove it. It was also used for Move File refactoring, we would need some different solution later. For now I revert to the state it was not long ago. Change-Id: If7468aac33ce000f85e94f96bfa2d2966892e560 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214341 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../src/services/refactoring/move_file.dart | 16 ++-------------- .../lib/src/dart/analysis/driver.dart | 2 +- .../lib/src/dart/element/element.dart | 4 ---- .../src/generated/declaration_resolver.dart | 19 +++---------------- .../lib/src/summary2/bundle_reader.dart | 1 - .../lib/src/summary2/bundle_writer.dart | 1 - .../lib/src/summary2/library_builder.dart | 1 - 7 files changed, 6 insertions(+), 38 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/refactoring/move_file.dart b/pkg/analysis_server/lib/src/services/refactoring/move_file.dart index 456dc38af3b..6e9707235a2 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/move_file.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/move_file.dart @@ -8,10 +8,8 @@ import 'package:analysis_server/src/services/refactoring/refactoring.dart'; import 'package:analysis_server/src/services/refactoring/refactoring_internal.dart'; import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/dart/ast/ast.dart'; -import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/file_system/file_system.dart'; import 'package:analyzer/src/dart/analysis/driver.dart'; -import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/generated/source.dart'; import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; import 'package:analyzer_plugin/utilities/range_factory.dart'; @@ -179,11 +177,8 @@ class MoveFileRefactoringImpl extends RefactoringImpl } bool _isPackageReference(SourceReference reference) { - var unitSourceContent = reference.element.unitSourceContent; - if (unitSourceContent == null) { - return false; - } - var quotedImportUri = unitSourceContent.substring(reference.range.offset, + var source = reference.element.source!; + var quotedImportUri = source.contents.data.substring(reference.range.offset, reference.range.offset + reference.range.length); return packagePrefixedStringPattern.hasMatch(quotedImportUri); } @@ -220,10 +215,3 @@ class MoveFileRefactoringImpl extends RefactoringImpl } } } - -extension on Element { - String? get unitSourceContent { - var unitElement = thisOrAncestorOfType(); - return unitElement?.sourceContent; - } -} diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart index 670e73a340d..fcf5d4e5a4a 100644 --- a/pkg/analyzer/lib/src/dart/analysis/driver.dart +++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart @@ -80,7 +80,7 @@ import 'package:meta/meta.dart'; /// TODO(scheglov) Clean up the list of implicitly analyzed files. class AnalysisDriver implements AnalysisDriverGeneric { /// The version of data format, should be incremented on every format change. - static const int DATA_VERSION = 179; + static const int DATA_VERSION = 180; /// The number of exception contexts allowed to write. Once this field is /// zero, we stop writing any new exception contexts in this process. diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 9c3a24d48dd..766ace46203 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -974,10 +974,6 @@ class CompilationUnitElementImpl extends UriReferencedElementImpl @override late Source source; - /// The content of the [source] for which this element model was built. - /// Might be `null` if we don't have it (for example in google3 summaries). - String? sourceContent; - @override LineInfo? lineInfo; diff --git a/pkg/analyzer/lib/src/generated/declaration_resolver.dart b/pkg/analyzer/lib/src/generated/declaration_resolver.dart index 570ef8f6317..650fde49455 100644 --- a/pkg/analyzer/lib/src/generated/declaration_resolver.dart +++ b/pkg/analyzer/lib/src/generated/declaration_resolver.dart @@ -107,16 +107,6 @@ class ElementWalker { (element.aliasedElement as GenericFunctionTypeElement).parameters, _typeParameters = element.typeParameters; - String? get _unitSourceContent { - Element? element = this.element; - while (element != null) { - if (element is CompilationUnitElementImpl) { - return element.sourceContent; - } - element = element.enclosingElement; - } - } - void consumeLocalElements() { _functionIndex = _functions!.length; } @@ -138,8 +128,7 @@ class ElementWalker { '[accessors: $accessors]' '[element.source: ${element.source?.fullName}]' '[libraryFilePath: $libraryFilePath]' - '[unitFilePath: $unitFilePath]' - '[unitSourceContent: $_unitSourceContent]', + '[unitFilePath: $unitFilePath]', ); } return _accessors![_accessorIndex++] as PropertyAccessorElementImpl; @@ -158,8 +147,7 @@ class ElementWalker { '[classes: $classes]' '[element.source: ${element.source?.fullName}]' '[libraryFilePath: $libraryFilePath]' - '[unitFilePath: $unitFilePath]' - '[unitSourceContent: $_unitSourceContent]', + '[unitFilePath: $unitFilePath]', ); } return _classes![_classIndex++] as ClassElementImpl; @@ -215,8 +203,7 @@ class ElementWalker { '[variables: $variables]' '[element.source: ${element.source?.fullName}]' '[libraryFilePath: $libraryFilePath]' - '[unitFilePath: $unitFilePath]' - '[unitSourceContent: $_unitSourceContent]', + '[unitFilePath: $unitFilePath]', ); } return _variables![_variableIndex++] as VariableElementImpl; diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart index 8767463416d..2b75f9e4fa6 100644 --- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart +++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart @@ -1266,7 +1266,6 @@ class LibraryReader { unitElement.uri = _reader.readOptionalStringReference(); unitElement.isSynthetic = _reader.readBool(); - unitElement.sourceContent = _reader.readOptionalStringUtf8(); _readUnitMacroGenerationDataList(unitElement); _readClasses(unitElement, unitReference); diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart index 3103c3548af..33e913ebf06 100644 --- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart +++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart @@ -449,7 +449,6 @@ class BundleWriter { _sink._writeStringReference('${unitElement.source.uri}'); _sink._writeOptionalStringReference(unitElement.uri); _sink.writeBool(unitElement.isSynthetic); - _sink.writeOptionalStringUtf8(unitElement.sourceContent); _resolutionSink._writeAnnotationList(unitElement.metadata); _writeUnitElementMacroGenerationDataList(unitElement); _writeList(unitElement.classes, _writeClassElement); diff --git a/pkg/analyzer/lib/src/summary2/library_builder.dart b/pkg/analyzer/lib/src/summary2/library_builder.dart index 10620eb5b6e..fc66eed130f 100644 --- a/pkg/analyzer/lib/src/summary2/library_builder.dart +++ b/pkg/analyzer/lib/src/summary2/library_builder.dart @@ -415,7 +415,6 @@ class LibraryBuilder { unitElement.librarySource = inputLibrary.source; unitElement.lineInfo = unitNode.lineInfo; unitElement.source = inputUnit.source; - unitElement.sourceContent = inputUnit.sourceContent; unitElement.uri = inputUnit.partUriStr; unitElement.setCodeRange(0, unitNode.length);