Skip to content

Commit 1358c8f

Browse files
authored
Fix const declaration cosmetics and avoid use of computeNode for information analyzer already has. (#1585)
* First version of adding const * Serve test package docs through grinder * Almost done, finish test for STUFF * pubspec update * Fix label for test package server * Fix up const-declaration * dartfmt * Fix inconsistencies in #1581 * WIP * rebuild test package docs
1 parent 7d3e01f commit 1358c8f

File tree

573 files changed

+1307
-713
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

573 files changed

+1307
-713
lines changed

lib/src/model.dart

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import 'package:analyzer/dart/ast/ast.dart'
1313
show
1414
AnnotatedNode,
1515
Declaration,
16+
Expression,
1617
FieldDeclaration,
18+
InstanceCreationExpression,
1719
VariableDeclaration,
1820
VariableDeclarationList;
1921
import 'package:analyzer/dart/element/element.dart';
@@ -24,6 +26,7 @@ import 'package:analyzer/source/package_map_resolver.dart';
2426
import 'package:analyzer/source/sdk_ext.dart';
2527
// TODO(jcollins-g): Stop using internal analyzer structures somehow.
2628
import 'package:analyzer/src/context/builder.dart';
29+
import 'package:analyzer/src/dart/element/element.dart';
2730
import 'package:analyzer/src/dart/sdk/sdk.dart';
2831
import 'package:analyzer/src/generated/engine.dart';
2932
import 'package:analyzer/src/generated/java_io.dart';
@@ -1449,9 +1452,9 @@ class Field extends ModelElement
14491452
bool get isInherited => _isInherited;
14501453

14511454
@override
1452-
String get kind => 'property';
1455+
String get kind => isConst ? 'constant' : 'property';
14531456

1454-
String get typeName => "property";
1457+
String get typeName => kind;
14551458

14561459
@override
14571460
List<String> get annotations {
@@ -1540,21 +1543,36 @@ abstract class GetterSetterCombo implements ModelElement {
15401543
ModelElement enclosingElement;
15411544
bool get isInherited;
15421545

1543-
String _constantValueBase() {
1544-
if (element.computeNode() != null) {
1545-
var v = element.computeNode().toSource();
1546-
if (v == null) return null;
1547-
var string = v.substring(v.indexOf('=') + 1, v.length).trim();
1548-
return const HtmlEscape(HtmlEscapeMode.UNKNOWN).convert(string);
1546+
Expression get constantInitializer =>
1547+
(element as ConstVariableElement).constantInitializer;
1548+
1549+
String linkifyConstantValue(String original) {
1550+
if (constantInitializer is! InstanceCreationExpression) return original;
1551+
String constructorName = (constantInitializer as InstanceCreationExpression)
1552+
.constructorName
1553+
.toString();
1554+
Element staticElement =
1555+
(constantInitializer as InstanceCreationExpression).staticElement;
1556+
Constructor target = new ModelElement.fromElement(staticElement, package);
1557+
Class targetClass = target.enclosingElement;
1558+
// TODO(jcollins-g): this logic really should be integrated into Constructor,
1559+
// but that's not trivial because of linkedName's usage.
1560+
if (targetClass.name == target.name) {
1561+
return original.replaceAll(constructorName, "${target.linkedName}");
15491562
}
1550-
return null;
1563+
return original.replaceAll(
1564+
"${targetClass.name}.${target.name}", "${targetClass.linkedName}.${target.linkedName}");
15511565
}
15521566

1553-
String get constantValueBase => _memoizer.memoized(_constantValueBase);
1554-
1555-
String get constantValue => constantValueBase;
1567+
String _constantValueBase() {
1568+
String result = constantInitializer?.toString() ?? '';
1569+
return const HtmlEscape(HtmlEscapeMode.UNKNOWN).convert(result);
1570+
}
15561571

1557-
String get constantValueTruncated => truncateString(constantValueBase, 200);
1572+
String get constantValue => linkifyConstantValue(constantValueBase);
1573+
String get constantValueTruncated =>
1574+
linkifyConstantValue(truncateString(constantValueBase, 200));
1575+
String get constantValueBase => _memoizer.memoized(_constantValueBase);
15581576

15591577
/// Returns true if both accessors are synthetic.
15601578
bool get hasSyntheticAccessors {
@@ -4632,7 +4650,7 @@ class TopLevelVariable extends ModelElement
46324650
}
46334651

46344652
@override
4635-
String get kind => 'top-level property';
4653+
String get kind => isConst ? 'top-level constant' : 'top-level property';
46364654

46374655
@override
46384656
Set<String> get features => super.features..addAll(comboFeatures);

lib/templates/_constant.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<dt id="{{htmlId}}" class="constant">
22
<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{{ linkedName }}}</span>
3-
<span class="signature">&#8594; {{{ linkedReturnType }}}</span>
3+
<span class="signature">&#8594; const {{{ linkedReturnType }}}</span>
44
</dt>
55
<dd>
66
{{{ oneLineDoc }}}

lib/templates/_name_summary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{name}}</span>
1+
{{#isConst}}const {{/isConst}}<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{name}}</span>

test/model_test.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,10 +809,12 @@ void main() {
809809

810810
test('get constants', () {
811811
expect(Apple.publicConstants, hasLength(1));
812+
expect(Apple.publicConstants.first.kind, equals('constant'));
812813
});
813814

814815
test('get instance fields', () {
815816
expect(Apple.publicInstanceProperties, hasLength(3));
817+
expect(Apple.publicInstanceProperties.first.kind, equals('property'));
816818
});
817819

818820
test('get inherited properties, including properties of Object', () {
@@ -1911,7 +1913,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
19111913

19121914
test('substrings of the constant values type are not linked (#1535)', () {
19131915
expect(aName.constantValue,
1914-
'const ExtendedShortName(&quot;hello there&quot;)');
1916+
'const <a href="ex/ExtendedShortName/ExtendedShortName.html">ExtendedShortName</a>(&quot;hello there&quot;)');
19151917
});
19161918

19171919
test('constant field values are escaped', () {
@@ -1922,6 +1924,10 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
19221924
expect(greenConstant.fullyQualifiedName, 'ex.COLOR_GREEN');
19231925
});
19241926

1927+
test('has the correct kind', () {
1928+
expect(greenConstant.kind, equals('top-level constant'));
1929+
});
1930+
19251931
test('has enclosing element', () {
19261932
expect(greenConstant.enclosingElement.name, equals(exLibrary.name));
19271933
});
@@ -1943,8 +1949,8 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
19431949
"const &lt;String&gt; [COLOR_GREEN, COLOR_ORANGE, &#39;blue&#39;]");
19441950
});
19451951

1946-
test('MY_CAT is not linked', () {
1947-
expect(cat.constantValue, 'const ConstantCat(&#39;tabby&#39;)');
1952+
test('MY_CAT is linked', () {
1953+
expect(cat.constantValue, 'const <a href="ex/ConstantCat/ConstantCat.html">ConstantCat</a>(&#39;tabby&#39;)');
19481954
});
19491955

19501956
test('exported property', () {

testing/test_package_docs/anonymous_library/doesStuff.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ <h1>doesStuff function</h1>
5454

5555
<section class="multi-line-signature">
5656
<span class="returntype">String</span>
57-
<span class="name ">doesStuff</span>(<wbr>)
57+
<span class="name ">doesStuff</span>
58+
(<wbr>)
5859
</section>
5960

6061

testing/test_package_docs/another_anonymous_lib/greeting.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ <h1>greeting function</h1>
5454

5555
<section class="multi-line-signature">
5656
<span class="returntype">String</span>
57-
<span class="name ">greeting</span>(<wbr>)
57+
<span class="name ">greeting</span>
58+
(<wbr>)
5859
</section>
5960

6061

testing/test_package_docs/css/theOnlyThingInTheLibrary.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ <h1>theOnlyThingInTheLibrary top-level property</h1>
5454

5555
<section class="multi-line-signature">
5656
<span class="returntype">String</span>
57-
<span class="name ">theOnlyThingInTheLibrary</span> <div class="features">read / write</div>
57+
<span class="name ">theOnlyThingInTheLibrary</span>
58+
<div class="features">read / write</div>
5859
</section>
5960

6061

testing/test_package_docs/ex/Animal-class.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ <h2>Constants</h2>
121121
<dl class="properties">
122122
<dt id="CAT" class="constant">
123123
<span class="name ">CAT</span>
124-
<span class="signature">&#8594; <a href="ex/Animal-class.html">Animal</a></span>
124+
<span class="signature">&#8594; const <a href="ex/Animal-class.html">Animal</a></span>
125125
</dt>
126126
<dd>
127127
<p>Single line docs.</p>
@@ -132,7 +132,7 @@ <h2>Constants</h2>
132132
</dd>
133133
<dt id="DOG" class="constant">
134134
<span class="name ">DOG</span>
135-
<span class="signature">&#8594; <a href="ex/Animal-class.html">Animal</a></span>
135+
<span class="signature">&#8594; const <a href="ex/Animal-class.html">Animal</a></span>
136136
</dt>
137137
<dd>
138138
<p>Multi line docs.</p>
@@ -144,7 +144,7 @@ <h2>Constants</h2>
144144
</dd>
145145
<dt id="HORSE" class="constant">
146146
<span class="name ">HORSE</span>
147-
<span class="signature">&#8594; <a href="ex/Animal-class.html">Animal</a></span>
147+
<span class="signature">&#8594; const <a href="ex/Animal-class.html">Animal</a></span>
148148
</dt>
149149
<dd>
150150

@@ -155,7 +155,7 @@ <h2>Constants</h2>
155155
</dd>
156156
<dt id="values" class="constant">
157157
<span class="name ">values</span>
158-
<span class="signature">&#8594; List<span class="signature">&lt;<a href="ex/Animal-class.html">Animal</a>&gt;</span></span>
158+
<span class="signature">&#8594; const List<span class="signature">&lt;<a href="ex/Animal-class.html">Animal</a>&gt;</span></span>
159159
</dt>
160160
<dd>
161161
<p>A constant List of the values in this enum, in order of their declaration.</p>

testing/test_package_docs/ex/Animal/hashCode.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ <h1>hashCode property</h1>
7272

7373
<section class="multi-line-signature">
7474
<span class="returntype">int</span>
75-
<span class="name ">hashCode</span> <div class="features">inherited</div>
75+
<span class="name ">hashCode</span>
76+
<div class="features">inherited</div>
7677
</section>
7778

7879

testing/test_package_docs/ex/Animal/noSuchMethod.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ <h1>noSuchMethod method</h1>
6969

7070
<section class="multi-line-signature">
7171
<span class="returntype">dynamic</span>
72-
<span class="name ">noSuchMethod</span>(<wbr><span class="parameter" id="noSuchMethod-param-invocation"><span class="type-annotation">Invocation</span> <span class="parameter-name">invocation</span></span>)
72+
<span class="name ">noSuchMethod</span>
73+
(<wbr><span class="parameter" id="noSuchMethod-param-invocation"><span class="type-annotation">Invocation</span> <span class="parameter-name">invocation</span></span>)
7374
</section>
7475

7576

testing/test_package_docs/ex/Animal/operator_equals.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ <h1>operator == method</h1>
6969

7070
<section class="multi-line-signature">
7171
<span class="returntype">bool</span>
72-
<span class="name ">operator ==</span>(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
72+
<span class="name ">operator ==</span>
73+
(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
7374
</section>
7475

7576

testing/test_package_docs/ex/Animal/runtimeType.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ <h1>runtimeType property</h1>
7272

7373
<section class="multi-line-signature">
7474
<span class="returntype">Type</span>
75-
<span class="name ">runtimeType</span> <div class="features">inherited</div>
75+
<span class="name ">runtimeType</span>
76+
<div class="features">inherited</div>
7677
</section>
7778

7879

testing/test_package_docs/ex/Animal/toString.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ <h1>toString method</h1>
6969

7070
<section class="multi-line-signature">
7171
<span class="returntype">String</span>
72-
<span class="name ">toString</span>(<wbr>)
72+
<span class="name ">toString</span>
73+
(<wbr>)
7374
</section>
7475

7576

testing/test_package_docs/ex/AnotherParameterizedClass/hashCode.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ <h1>hashCode property</h1>
6868

6969
<section class="multi-line-signature">
7070
<span class="returntype">int</span>
71-
<span class="name ">hashCode</span> <div class="features">inherited</div>
71+
<span class="name ">hashCode</span>
72+
<div class="features">inherited</div>
7273
</section>
7374

7475

testing/test_package_docs/ex/AnotherParameterizedClass/noSuchMethod.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ <h1>noSuchMethod method</h1>
6565

6666
<section class="multi-line-signature">
6767
<span class="returntype">dynamic</span>
68-
<span class="name ">noSuchMethod</span>(<wbr><span class="parameter" id="noSuchMethod-param-invocation"><span class="type-annotation">Invocation</span> <span class="parameter-name">invocation</span></span>)
68+
<span class="name ">noSuchMethod</span>
69+
(<wbr><span class="parameter" id="noSuchMethod-param-invocation"><span class="type-annotation">Invocation</span> <span class="parameter-name">invocation</span></span>)
6970
</section>
7071

7172

testing/test_package_docs/ex/AnotherParameterizedClass/operator_equals.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ <h1>operator == method</h1>
6565

6666
<section class="multi-line-signature">
6767
<span class="returntype">bool</span>
68-
<span class="name ">operator ==</span>(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
68+
<span class="name ">operator ==</span>
69+
(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
6970
</section>
7071

7172

testing/test_package_docs/ex/AnotherParameterizedClass/runtimeType.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ <h1>runtimeType property</h1>
6868

6969
<section class="multi-line-signature">
7070
<span class="returntype">Type</span>
71-
<span class="name ">runtimeType</span> <div class="features">inherited</div>
71+
<span class="name ">runtimeType</span>
72+
<div class="features">inherited</div>
7273
</section>
7374

7475

testing/test_package_docs/ex/AnotherParameterizedClass/toString.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ <h1>toString method</h1>
6565

6666
<section class="multi-line-signature">
6767
<span class="returntype">String</span>
68-
<span class="name ">toString</span>(<wbr>)
68+
<span class="name ">toString</span>
69+
(<wbr>)
6970
</section>
7071

7172

testing/test_package_docs/ex/Apple-class.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ <h2>Constants</h2>
319319
<dl class="properties">
320320
<dt id="n" class="constant">
321321
<span class="name "><a href="ex/Apple/n-constant.html">n</a></span>
322-
<span class="signature">&#8594; int</span>
322+
<span class="signature">&#8594; const int</span>
323323
</dt>
324324
<dd>
325325

testing/test_package_docs/ex/Apple/fieldWithTypedef.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ <h1>fieldWithTypedef property</h1>
8080

8181
<section class="multi-line-signature">
8282
<span class="returntype"><a href="ex/ParameterizedTypedef.html">ParameterizedTypedef</a><span class="signature">&lt;bool&gt;</span></span>
83-
<span class="name ">fieldWithTypedef</span> <div class="features">final</div>
83+
<span class="name ">fieldWithTypedef</span>
84+
<div class="features">final</div>
8485
</section>
8586
<section class="desc markdown">
8687
<p>fieldWithTypedef docs here</p>

testing/test_package_docs/ex/Apple/hashCode.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ <h1>hashCode property</h1>
8383

8484
<section class="multi-line-signature">
8585
<span class="returntype">int</span>
86-
<span class="name ">hashCode</span> <div class="features">inherited</div>
86+
<span class="name ">hashCode</span>
87+
<div class="features">inherited</div>
8788
</section>
8889

8990

testing/test_package_docs/ex/Apple/isGreaterThan.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ <h1>isGreaterThan method</h1>
8080

8181
<section class="multi-line-signature">
8282
<span class="returntype">bool</span>
83-
<span class="name ">isGreaterThan</span>(<wbr><span class="parameter" id="isGreaterThan-param-number"><span class="type-annotation">int</span> <span class="parameter-name">number</span>, {</span> <span class="parameter" id="isGreaterThan-param-check"><span class="type-annotation">int</span> <span class="parameter-name">check</span>: <span class="default-value">5</span></span> })
83+
<span class="name ">isGreaterThan</span>
84+
(<wbr><span class="parameter" id="isGreaterThan-param-number"><span class="type-annotation">int</span> <span class="parameter-name">number</span>, {</span> <span class="parameter" id="isGreaterThan-param-check"><span class="type-annotation">int</span> <span class="parameter-name">check</span>: <span class="default-value">5</span></span> })
8485
</section>
8586

8687

testing/test_package_docs/ex/Apple/m.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ <h1>m property</h1>
8080

8181
<section class="multi-line-signature">
8282
<span class="returntype">int</span>
83-
<span class="name ">m</span> <div class="features">read / write</div>
83+
<span class="name ">m</span>
84+
<div class="features">read / write</div>
8485
</section>
8586
<section class="desc markdown">
8687
<p>The read-write field <code>m</code>.</p>

testing/test_package_docs/ex/Apple/m1.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ <h1>m1 method</h1>
8080

8181
<section class="multi-line-signature">
8282
<span class="returntype">void</span>
83-
<span class="name ">m1</span>(<wbr>)
83+
<span class="name ">m1</span>
84+
(<wbr>)
8485
</section>
8586
<section class="desc markdown">
8687
<p>This is a method.</p>

testing/test_package_docs/ex/Apple/methodWithTypedefParam.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ <h1>methodWithTypedefParam method</h1>
8080

8181
<section class="multi-line-signature">
8282
<span class="returntype">void</span>
83-
<span class="name ">methodWithTypedefParam</span>(<wbr><span class="parameter" id="methodWithTypedefParam-param-p"><span class="type-annotation"><a href="ex/processMessage.html">processMessage</a></span> <span class="parameter-name">p</span></span>)
83+
<span class="name ">methodWithTypedefParam</span>
84+
(<wbr><span class="parameter" id="methodWithTypedefParam-param-p"><span class="type-annotation"><a href="ex/processMessage.html">processMessage</a></span> <span class="parameter-name">p</span></span>)
8485
</section>
8586

8687

testing/test_package_docs/ex/Apple/n-constant.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ <h5>Apple class</h5>
7676
</div><!--/.sidebar-offcanvas-left-->
7777

7878
<div class="col-xs-12 col-sm-9 col-md-8 main-content">
79-
<h1>n property</h1>
79+
<h1>n constant</h1>
8080

8181
<section class="multi-line-signature">
8282
<span class="returntype">int</span>
83-
<span class="name ">n</span> =
83+
const <span class="name ">n</span>
84+
=
8485
<span class="constant-value">5</span>
8586
</section>
8687

0 commit comments

Comments
 (0)