Skip to content

Commit

Permalink
Elements. Migrate to Element2 (#3968)
Browse files Browse the repository at this point in the history
* Elements. Migrate to Element2

* Address comments
  • Loading branch information
keertip authored Jan 24, 2025
1 parent 08b2c10 commit 56934b7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 68 deletions.
7 changes: 4 additions & 3 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:analyzer/source/line_info.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
// ignore: implementation_imports
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/kind.dart';
import 'package:dartdoc/src/model/model.dart';
Expand All @@ -29,7 +30,7 @@ class Library extends ModelElement
final LibraryElement element;

@override
LibraryElement2 get element2 => element as LibraryElementImpl;
LibraryElement2 get element2 => element.asElement2;

/// The set of [Element]s declared directly in this library.
final Set<Element> _localElements;
Expand Down Expand Up @@ -315,7 +316,7 @@ class Library extends ModelElement

/// The real packageMeta, as opposed to the package we are documenting with.
late final PackageMeta? packageMeta =
packageGraph.packageMetaProvider.fromElement(element2, config.sdkDir);
packageGraph.packageMetaProvider.fromElement(element.asElement2, config.sdkDir);

late final List<Class> classesAndExceptions = [
..._localElementsOfType<ClassElement, Class>(),
Expand Down
10 changes: 10 additions & 0 deletions lib/src/warnings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import 'dart:io';
import 'dart:math' as math;

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/file_system/file_system.dart';
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:collection/collection.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/logging.dart';
Expand Down Expand Up @@ -498,6 +501,13 @@ class PackageWarningCounter {
UnmodifiableMapView<Element?, Map<PackageWarning, Set<String>>>
get countedWarnings => UnmodifiableMapView(_countedWarnings);

/// An unmodifiable map view of all counted warnings related by their element,
/// warning type, and message.
UnmodifiableMapView<Element2?, Map<PackageWarning, Set<String>>>
get countedWarnings2 => UnmodifiableMapView(_countedWarnings.map((key,
value) =>
MapEntry(key?.asElement2, value)));

PackageWarningCounter(this.packageGraph);

/// Logs [packageWarning].
Expand Down
6 changes: 2 additions & 4 deletions test/documentation_comment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// ignore_for_file: analyzer_use_new_elements

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/model/model.dart';
Expand All @@ -30,7 +28,7 @@ class DocumentationCommentTest extends DartdocTestBase {
late ModelElement libraryModel;

void expectNoWarnings() {
expect(packageGraph.packageWarningCounter.countedWarnings, isEmpty);
expect(packageGraph.packageWarningCounter.countedWarnings2, isEmpty);
expect(packageGraph.packageWarningCounter.hasWarnings, isFalse);
}

Expand Down Expand Up @@ -863,7 +861,7 @@ class _HasWarning extends Matcher {
Map<Object?, Object?> matchState, bool verbose) {
if (actual is ModelElement) {
var warnings = actual
.packageGraph.packageWarningCounter.countedWarnings[actual.element];
.packageGraph.packageWarningCounter.countedWarnings2[actual.element2];
if (warnings == null) {
return mismatchDescription.add('has no warnings');
}
Expand Down
4 changes: 1 addition & 3 deletions test/end2end/dartdoc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// ignore_for_file: analyzer_use_new_elements

import 'dart:async';

import 'package:analyzer/file_system/file_system.dart';
Expand Down Expand Up @@ -84,7 +82,7 @@ void main() {
await buildDartdoc(['--allow-tools'], testPackageToolError, tempDir);
var results = await dartdoc.generateDocsBase();
var p = results.packageGraph;
var unresolvedToolErrors = p.packageWarningCounter.countedWarnings.values
var unresolvedToolErrors = p.packageWarningCounter.countedWarnings2.values
.map((e) => e[PackageWarning.toolError] ?? {})
.expand((element) => element);

Expand Down
48 changes: 23 additions & 25 deletions test/mustachio/runtime_renderer_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
// 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.

// ignore_for_file: analyzer_use_new_elements

@TestOn('vm && !windows')
@Timeout.factor(2)
library;

import 'dart:io';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import 'builder_test_base.dart';

void main() {
group('builds a renderer class', () {
late final LibraryElement renderersLibrary;
late final LibraryElement2 renderersLibrary;
late final String generatedContent;

// Builders are fairly expensive (about 4 seconds per `testBuilder` call),
Expand All @@ -42,7 +40,7 @@ abstract class Foo extends FooBase with Mix<int> {
class Bar {}
class Baz {}
''');
renderersLibrary = await resolveGeneratedLibrary(runtimeRenderersPath);
renderersLibrary = await resolveGeneratedLibrary2(runtimeRenderersPath);
generatedContent = await File(runtimeRenderersPath).readAsString();
});

Expand All @@ -63,23 +61,23 @@ class Baz {}
test('for a class which is extended by a rendered class', () {
// No render function is necessary.
expect(renderersLibrary.getTopLevelFunction('_render_FooBase'), isNull);
expect(renderersLibrary.getClass('_Renderer_FooBase'), isNotNull);
expect(renderersLibrary.getClass2('_Renderer_FooBase'), isNotNull);
});

test('for a class which is mixed into a rendered class', () {
// No render function is necessary.
expect(renderersLibrary.getTopLevelFunction('_render_Mix'), isNull);
expect(renderersLibrary.getClass('_Renderer_Mix'), isNotNull);
expect(renderersLibrary.getClass2('_Renderer_Mix'), isNotNull);
});

test('for a type found in a getter', () {
expect(renderersLibrary.getTopLevelFunction('_render_Bar'), isNotNull);
expect(renderersLibrary.getClass('_Renderer_Bar'), isNotNull);
expect(renderersLibrary.getClass2('_Renderer_Bar'), isNotNull);
});

test('for a generic, bounded type found in a getter', () {
expect(renderersLibrary.getTopLevelFunction('_render_Baz'), isNotNull);
expect(renderersLibrary.getClass('_Renderer_Baz'), isNotNull);
expect(renderersLibrary.getClass2('_Renderer_Baz'), isNotNull);
});

test('with a property map', () {
Expand Down Expand Up @@ -190,12 +188,12 @@ class Baz {}
library foo;
import 'annotations.dart';
''');
var renderersLibrary = await resolveGeneratedLibrary(runtimeRenderersPath);
var renderersLibrary = await resolveGeneratedLibrary2(runtimeRenderersPath);

expect(renderersLibrary.getTopLevelFunction('renderFoo'), isNotNull);
expect(renderersLibrary.getTopLevelFunction('renderBar'), isNotNull);
expect(renderersLibrary.getClass('_Renderer_Foo'), isNotNull);
expect(renderersLibrary.getClass('_Renderer_Bar'), isNotNull);
expect(renderersLibrary.getClass2('_Renderer_Foo'), isNotNull);
expect(renderersLibrary.getClass2('_Renderer_Bar'), isNotNull);
});

group('builds a renderer class for a generic type', () {
Expand Down Expand Up @@ -267,21 +265,21 @@ class Foo<T extends num> {
class Bar {}
class Baz {}
''');
var renderersLibrary = await resolveGeneratedLibrary(runtimeRenderersPath);
var renderersLibrary = await resolveGeneratedLibrary2(runtimeRenderersPath);

var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!;
expect(fooRenderFunction.typeParameters, hasLength(1));
var fBound = fooRenderFunction.typeParameters.single.bound!;
expect(fooRenderFunction.typeParameters2, hasLength(1));
var fBound = fooRenderFunction.typeParameters2.single.bound!;
expect(fBound.getDisplayString(), equals('num'));

var fooRendererClass = renderersLibrary.getClass('_Renderer_Foo')!;
expect(fooRendererClass.typeParameters, hasLength(1));
var cBound = fooRenderFunction.typeParameters.single.bound!;
var fooRendererClass = renderersLibrary.getClass2('_Renderer_Foo')!;
expect(fooRendererClass.typeParameters2, hasLength(1));
var cBound = fooRenderFunction.typeParameters2.single.bound!;
expect(cBound.getDisplayString(), equals('num'));
});

group('does not generate a renderer', () {
late final LibraryElement renderersLibrary;
late final LibraryElement2 renderersLibrary;

setUpAll(() async {
await testMustachioBuilder('''
Expand All @@ -299,32 +297,32 @@ class Private {}
class Setter {}
class Method {}
''');
renderersLibrary = await resolveGeneratedLibrary(runtimeRenderersPath);
renderersLibrary = await resolveGeneratedLibrary2(runtimeRenderersPath);
});

test('found in a static getter', () {
expect(renderersLibrary.getTopLevelFunction('_render_Static'), isNull);
expect(renderersLibrary.getClass('_Renderer_Static'), isNull);
expect(renderersLibrary.getClass2('_Renderer_Static'), isNull);
});

test('found in a private getter', () {
expect(renderersLibrary.getTopLevelFunction('_render_Private'), isNull);
expect(renderersLibrary.getClass('_Renderer_Private'), isNull);
expect(renderersLibrary.getClass2('_Renderer_Private'), isNull);
});

test('found in a setter', () {
expect(renderersLibrary.getTopLevelFunction('_render_Setter'), isNull);
expect(renderersLibrary.getClass('_Renderer_Setter'), isNull);
expect(renderersLibrary.getClass2('_Renderer_Setter'), isNull);
});

test('found in a method', () {
expect(renderersLibrary.getTopLevelFunction('_render_Method'), isNull);
expect(renderersLibrary.getClass('_Renderer_Method'), isNull);
expect(renderersLibrary.getClass2('_Renderer_Method'), isNull);
});

test('for types not @visibleToMustache', () {
expect(renderersLibrary.getTopLevelFunction('_render_String'), isNull);
expect(renderersLibrary.getClass('_Renderer_String'), isNull);
expect(renderersLibrary.getClass2('_Renderer_String'), isNull);
});
});
}
Expand Down
2 changes: 0 additions & 2 deletions test/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// ignore_for_file: analyzer_use_new_elements

import 'dart:io';

import 'package:analyzer/file_system/file_system.dart';
Expand Down
Loading

0 comments on commit 56934b7

Please sign in to comment.