Skip to content

Commit

Permalink
Fix const declaration cosmetics and avoid use of computeNode for info…
Browse files Browse the repository at this point in the history
…rmation 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
  • Loading branch information
jcollins-g authored Jan 9, 2018
1 parent 7d3e01f commit 1358c8f
Show file tree
Hide file tree
Showing 573 changed files with 1,307 additions and 713 deletions.
46 changes: 32 additions & 14 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import 'package:analyzer/dart/ast/ast.dart'
show
AnnotatedNode,
Declaration,
Expression,
FieldDeclaration,
InstanceCreationExpression,
VariableDeclaration,
VariableDeclarationList;
import 'package:analyzer/dart/element/element.dart';
Expand All @@ -24,6 +26,7 @@ import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/source/sdk_ext.dart';
// TODO(jcollins-g): Stop using internal analyzer structures somehow.
import 'package:analyzer/src/context/builder.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/java_io.dart';
Expand Down Expand Up @@ -1449,9 +1452,9 @@ class Field extends ModelElement
bool get isInherited => _isInherited;

@override
String get kind => 'property';
String get kind => isConst ? 'constant' : 'property';

String get typeName => "property";
String get typeName => kind;

@override
List<String> get annotations {
Expand Down Expand Up @@ -1540,21 +1543,36 @@ abstract class GetterSetterCombo implements ModelElement {
ModelElement enclosingElement;
bool get isInherited;

String _constantValueBase() {
if (element.computeNode() != null) {
var v = element.computeNode().toSource();
if (v == null) return null;
var string = v.substring(v.indexOf('=') + 1, v.length).trim();
return const HtmlEscape(HtmlEscapeMode.UNKNOWN).convert(string);
Expression get constantInitializer =>
(element as ConstVariableElement).constantInitializer;

String linkifyConstantValue(String original) {
if (constantInitializer is! InstanceCreationExpression) return original;
String constructorName = (constantInitializer as InstanceCreationExpression)
.constructorName
.toString();
Element staticElement =
(constantInitializer as InstanceCreationExpression).staticElement;
Constructor target = new ModelElement.fromElement(staticElement, package);
Class targetClass = target.enclosingElement;
// TODO(jcollins-g): this logic really should be integrated into Constructor,
// but that's not trivial because of linkedName's usage.
if (targetClass.name == target.name) {
return original.replaceAll(constructorName, "${target.linkedName}");
}
return null;
return original.replaceAll(
"${targetClass.name}.${target.name}", "${targetClass.linkedName}.${target.linkedName}");
}

String get constantValueBase => _memoizer.memoized(_constantValueBase);

String get constantValue => constantValueBase;
String _constantValueBase() {
String result = constantInitializer?.toString() ?? '';
return const HtmlEscape(HtmlEscapeMode.UNKNOWN).convert(result);
}

String get constantValueTruncated => truncateString(constantValueBase, 200);
String get constantValue => linkifyConstantValue(constantValueBase);
String get constantValueTruncated =>
linkifyConstantValue(truncateString(constantValueBase, 200));
String get constantValueBase => _memoizer.memoized(_constantValueBase);

/// Returns true if both accessors are synthetic.
bool get hasSyntheticAccessors {
Expand Down Expand Up @@ -4632,7 +4650,7 @@ class TopLevelVariable extends ModelElement
}

@override
String get kind => 'top-level property';
String get kind => isConst ? 'top-level constant' : 'top-level property';

@override
Set<String> get features => super.features..addAll(comboFeatures);
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/_constant.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dt id="{{htmlId}}" class="constant">
<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{{ linkedName }}}</span>
<span class="signature">&#8594; {{{ linkedReturnType }}}</span>
<span class="signature">&#8594; const {{{ linkedReturnType }}}</span>
</dt>
<dd>
{{{ oneLineDoc }}}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/_name_summary.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{name}}</span>
{{#isConst}}const {{/isConst}}<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{name}}</span>
12 changes: 9 additions & 3 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -809,10 +809,12 @@ void main() {

test('get constants', () {
expect(Apple.publicConstants, hasLength(1));
expect(Apple.publicConstants.first.kind, equals('constant'));
});

test('get instance fields', () {
expect(Apple.publicInstanceProperties, hasLength(3));
expect(Apple.publicInstanceProperties.first.kind, equals('property'));
});

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

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

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

test('has the correct kind', () {
expect(greenConstant.kind, equals('top-level constant'));
});

test('has enclosing element', () {
expect(greenConstant.enclosingElement.name, equals(exLibrary.name));
});
Expand All @@ -1943,8 +1949,8 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
"const &lt;String&gt; [COLOR_GREEN, COLOR_ORANGE, &#39;blue&#39;]");
});

test('MY_CAT is not linked', () {
expect(cat.constantValue, 'const ConstantCat(&#39;tabby&#39;)');
test('MY_CAT is linked', () {
expect(cat.constantValue, 'const <a href="ex/ConstantCat/ConstantCat.html">ConstantCat</a>(&#39;tabby&#39;)');
});

test('exported property', () {
Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/anonymous_library/doesStuff.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ <h1>doesStuff function</h1>

<section class="multi-line-signature">
<span class="returntype">String</span>
<span class="name ">doesStuff</span>(<wbr>)
<span class="name ">doesStuff</span>
(<wbr>)
</section>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ <h1>greeting function</h1>

<section class="multi-line-signature">
<span class="returntype">String</span>
<span class="name ">greeting</span>(<wbr>)
<span class="name ">greeting</span>
(<wbr>)
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/css/theOnlyThingInTheLibrary.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ <h1>theOnlyThingInTheLibrary top-level property</h1>

<section class="multi-line-signature">
<span class="returntype">String</span>
<span class="name ">theOnlyThingInTheLibrary</span> <div class="features">read / write</div>
<span class="name ">theOnlyThingInTheLibrary</span>
<div class="features">read / write</div>
</section>


Expand Down
8 changes: 4 additions & 4 deletions testing/test_package_docs/ex/Animal-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ <h2>Constants</h2>
<dl class="properties">
<dt id="CAT" class="constant">
<span class="name ">CAT</span>
<span class="signature">&#8594; <a href="ex/Animal-class.html">Animal</a></span>
<span class="signature">&#8594; const <a href="ex/Animal-class.html">Animal</a></span>
</dt>
<dd>
<p>Single line docs.</p>
Expand All @@ -132,7 +132,7 @@ <h2>Constants</h2>
</dd>
<dt id="DOG" class="constant">
<span class="name ">DOG</span>
<span class="signature">&#8594; <a href="ex/Animal-class.html">Animal</a></span>
<span class="signature">&#8594; const <a href="ex/Animal-class.html">Animal</a></span>
</dt>
<dd>
<p>Multi line docs.</p>
Expand All @@ -144,7 +144,7 @@ <h2>Constants</h2>
</dd>
<dt id="HORSE" class="constant">
<span class="name ">HORSE</span>
<span class="signature">&#8594; <a href="ex/Animal-class.html">Animal</a></span>
<span class="signature">&#8594; const <a href="ex/Animal-class.html">Animal</a></span>
</dt>
<dd>

Expand All @@ -155,7 +155,7 @@ <h2>Constants</h2>
</dd>
<dt id="values" class="constant">
<span class="name ">values</span>
<span class="signature">&#8594; List<span class="signature">&lt;<a href="ex/Animal-class.html">Animal</a>&gt;</span></span>
<span class="signature">&#8594; const List<span class="signature">&lt;<a href="ex/Animal-class.html">Animal</a>&gt;</span></span>
</dt>
<dd>
<p>A constant List of the values in this enum, in order of their declaration.</p>
Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Animal/hashCode.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ <h1>hashCode property</h1>

<section class="multi-line-signature">
<span class="returntype">int</span>
<span class="name ">hashCode</span> <div class="features">inherited</div>
<span class="name ">hashCode</span>
<div class="features">inherited</div>
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Animal/noSuchMethod.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ <h1>noSuchMethod method</h1>

<section class="multi-line-signature">
<span class="returntype">dynamic</span>
<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>)
<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>)
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Animal/operator_equals.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ <h1>operator == method</h1>

<section class="multi-line-signature">
<span class="returntype">bool</span>
<span class="name ">operator ==</span>(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
<span class="name ">operator ==</span>
(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Animal/runtimeType.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ <h1>runtimeType property</h1>

<section class="multi-line-signature">
<span class="returntype">Type</span>
<span class="name ">runtimeType</span> <div class="features">inherited</div>
<span class="name ">runtimeType</span>
<div class="features">inherited</div>
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Animal/toString.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ <h1>toString method</h1>

<section class="multi-line-signature">
<span class="returntype">String</span>
<span class="name ">toString</span>(<wbr>)
<span class="name ">toString</span>
(<wbr>)
</section>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ <h1>hashCode property</h1>

<section class="multi-line-signature">
<span class="returntype">int</span>
<span class="name ">hashCode</span> <div class="features">inherited</div>
<span class="name ">hashCode</span>
<div class="features">inherited</div>
</section>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ <h1>noSuchMethod method</h1>

<section class="multi-line-signature">
<span class="returntype">dynamic</span>
<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>)
<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>)
</section>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ <h1>operator == method</h1>

<section class="multi-line-signature">
<span class="returntype">bool</span>
<span class="name ">operator ==</span>(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
<span class="name ">operator ==</span>
(<wbr><span class="parameter" id="==-param-other"><span class="parameter-name">other</span></span>)
</section>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ <h1>runtimeType property</h1>

<section class="multi-line-signature">
<span class="returntype">Type</span>
<span class="name ">runtimeType</span> <div class="features">inherited</div>
<span class="name ">runtimeType</span>
<div class="features">inherited</div>
</section>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ <h1>toString method</h1>

<section class="multi-line-signature">
<span class="returntype">String</span>
<span class="name ">toString</span>(<wbr>)
<span class="name ">toString</span>
(<wbr>)
</section>


Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/Apple-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ <h2>Constants</h2>
<dl class="properties">
<dt id="n" class="constant">
<span class="name "><a href="ex/Apple/n-constant.html">n</a></span>
<span class="signature">&#8594; int</span>
<span class="signature">&#8594; const int</span>
</dt>
<dd>

Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Apple/fieldWithTypedef.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ <h1>fieldWithTypedef property</h1>

<section class="multi-line-signature">
<span class="returntype"><a href="ex/ParameterizedTypedef.html">ParameterizedTypedef</a><span class="signature">&lt;bool&gt;</span></span>
<span class="name ">fieldWithTypedef</span> <div class="features">final</div>
<span class="name ">fieldWithTypedef</span>
<div class="features">final</div>
</section>
<section class="desc markdown">
<p>fieldWithTypedef docs here</p>
Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Apple/hashCode.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ <h1>hashCode property</h1>

<section class="multi-line-signature">
<span class="returntype">int</span>
<span class="name ">hashCode</span> <div class="features">inherited</div>
<span class="name ">hashCode</span>
<div class="features">inherited</div>
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Apple/isGreaterThan.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ <h1>isGreaterThan method</h1>

<section class="multi-line-signature">
<span class="returntype">bool</span>
<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> })
<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> })
</section>


Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Apple/m.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ <h1>m property</h1>

<section class="multi-line-signature">
<span class="returntype">int</span>
<span class="name ">m</span> <div class="features">read / write</div>
<span class="name ">m</span>
<div class="features">read / write</div>
</section>
<section class="desc markdown">
<p>The read-write field <code>m</code>.</p>
Expand Down
3 changes: 2 additions & 1 deletion testing/test_package_docs/ex/Apple/m1.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ <h1>m1 method</h1>

<section class="multi-line-signature">
<span class="returntype">void</span>
<span class="name ">m1</span>(<wbr>)
<span class="name ">m1</span>
(<wbr>)
</section>
<section class="desc markdown">
<p>This is a method.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ <h1>methodWithTypedefParam method</h1>

<section class="multi-line-signature">
<span class="returntype">void</span>
<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>)
<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>)
</section>


Expand Down
5 changes: 3 additions & 2 deletions testing/test_package_docs/ex/Apple/n-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ <h5>Apple class</h5>
</div><!--/.sidebar-offcanvas-left-->

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

<section class="multi-line-signature">
<span class="returntype">int</span>
<span class="name ">n</span> =
const <span class="name ">n</span>
=
<span class="constant-value">5</span>
</section>

Expand Down
Loading

0 comments on commit 1358c8f

Please sign in to comment.