Skip to content

Commit

Permalink
Privatize GetterSetterCombo.constantInitializer (#3898)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Oct 9, 2024
1 parent 5c114a9 commit fa3a351
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
29 changes: 2 additions & 27 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6331,19 +6331,6 @@ class _Renderer_GetterSetterCombo extends RendererBase<GetterSetterCombo> {
getters: _invisibleGetters['CharacterLocation']!);
},
),
'constantInitializer': Property(
getValue: (CT_ c) => c.constantInitializer,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(
c, remainingNames, 'Expression'),
isNullValue: (CT_ c) => c.constantInitializer == null,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
renderSimple(c.constantInitializer, ast, r.template, sink,
parent: r, getters: _invisibleGetters['Expression']!);
},
),
'constantValue': Property(
getValue: (CT_ c) => c.constantValue,
renderVariable:
Expand Down Expand Up @@ -12244,7 +12231,7 @@ class _Renderer_Package extends RendererBase<Package> {
}
}

String renderSearchPage(PackageTemplateData context, Template template) {
String renderIndex(PackageTemplateData context, Template template) {
var buffer = StringBuffer();
_render_PackageTemplateData(context, template.ast, template, buffer);
return buffer.toString();
Expand Down Expand Up @@ -12482,7 +12469,7 @@ class _Renderer_PackageTemplateData extends RendererBase<PackageTemplateData> {
}
}

String renderIndex(PackageTemplateData context, Template template) {
String renderSearchPage(PackageTemplateData context, Template template) {
var buffer = StringBuffer();
_render_PackageTemplateData(context, template.ast, template, buffer);
return buffer.toString();
Expand Down Expand Up @@ -16109,17 +16096,6 @@ const _invisibleGetters = {
'typeParameters',
'typeParameters2'
},
'Expression': {
'correspondingParameter',
'hashCode',
'inConstantContext',
'isAssignable',
'precedence',
'runtimeType',
'staticParameterElement',
'staticType',
'unParenthesized'
},
'ExtensionElement': {
'augmentation',
'augmentationTarget',
Expand Down Expand Up @@ -16195,7 +16171,6 @@ const _invisibleGetters = {
'arrow',
'characterLocation',
'comboAttributes',
'constantInitializer',
'constantValue',
'constantValueBase',
'constantValueTruncated',
Expand Down
23 changes: 12 additions & 11 deletions lib/src/model/getter_setter_combo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:convert';

import 'package:analyzer/dart/ast/ast.dart'
show Expression, InstanceCreationExpression;
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/source/line_info.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/element.dart'
Expand Down Expand Up @@ -67,18 +66,20 @@ mixin GetterSetterCombo on ModelElement {
/// Whether this has a constant value which should be displayed.
bool get hasConstantValueForDisplay => false;

Expression? get constantInitializer =>
late final Expression? _constantInitializer =
(element as ConstVariableElement).constantInitializer;

String linkifyConstantValue(String original) {
if (constantInitializer is! InstanceCreationExpression) return original;
var creationExpression = constantInitializer as InstanceCreationExpression;
var constructorName = creationExpression.constructorName.toString();
Element? staticElement = creationExpression.constructorName.staticElement;
if (_constantInitializer is! InstanceCreationExpression) return original;

var constructorName = _constantInitializer.constructorName.toString();
var staticElement = _constantInitializer.constructorName.staticElement;
if (staticElement == null) return original;

var target = getModelForElement(staticElement) as Constructor;
var enclosingElement = target.enclosingElement;
if (enclosingElement is! Class) return original;

// TODO(jcollins-g): this logic really should be integrated into
// `Constructor`, but that's not trivial because of `linkedName`'s usage.
if (enclosingElement.name == target.name) {
Expand Down Expand Up @@ -114,23 +115,23 @@ mixin GetterSetterCombo on ModelElement {
String get constantValueTruncated =>
linkifyConstantValue(truncateString(constantValueBase, 200));

/// The base text for this property's constant value as an unlinked String.
late final String constantValueBase = () {
final constantInitializer = this.constantInitializer;
if (constantInitializer == null) {
if (_constantInitializer == null) {
return '';
}

var initializerString = constantInitializer.toString();
var initializerString = _constantInitializer.toString();

final self = this;
if (self is! EnumField ||
constantInitializer is! InstanceCreationExpression) {
_constantInitializer is! InstanceCreationExpression) {
return _htmlEscape.convert(initializerString);
}

initializerString = 'const $initializerString';

var isImplicitConstructorCall = constantInitializer
var isImplicitConstructorCall = _constantInitializer
.constructorName.staticElement?.isDefaultConstructor ??
false;
if (isImplicitConstructorCall) {
Expand Down

0 comments on commit fa3a351

Please sign in to comment.