Skip to content

Commit

Permalink
fix(dotnet): fix doc comment model parsing in .NET generator (#455)
Browse files Browse the repository at this point in the history
Mirror the changes to assembly docs model in the separate model
that the .NET generator maintains. This fixes the crash that
happens when parsing a model with custom doc attributes.

Add compliance test to prevent this from happening again.
  • Loading branch information
rix0rrr authored Apr 15, 2019
1 parent 4d84e0b commit ae85aa5
Show file tree
Hide file tree
Showing 38 changed files with 476 additions and 190 deletions.
19 changes: 19 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,3 +1617,22 @@ export class OptionalStructConsumer {
export interface OptionalStruct {
readonly field?: string;
}


/**
* This class has docs.
*
* The docs are great. They're a bunch of tags.
*
* @example
*
* function anExample() {
* }
*
* @see https://aws.amazon.com/
* @customAttribute hasAValue
* @deprecated Use something else please
* @stable
*/
export class ClassWithDocs {
}
24 changes: 23 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,28 @@
}
]
},
"jsii-calc.ClassWithDocs": {
"assembly": "jsii-calc",
"docs": {
"custom": {
"customAttribute": "hasAValue"
},
"deprecated": "Use something else please",
"example": "function anExample() {\n}",
"remarks": "The docs are great. They're a bunch of tags.",
"see": "https://aws.amazon.com/",
"stability": "stable",
"summary": "This class has docs."
},
"fqn": "jsii-calc.ClassWithDocs",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1637
},
"name": "ClassWithDocs"
},
"jsii-calc.ClassWithMutableObjectLiteralProperty": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.ClassWithMutableObjectLiteralProperty",
Expand Down Expand Up @@ -6637,5 +6659,5 @@
}
},
"version": "0.9.0",
"fingerprint": "Lg8wDjQSsRLa2sk8EriArt/u5aibYilCuYFRZHh02Eg="
"fingerprint": "F319lrdZvWrVwdgKTX3nSjnYs1YghHSnYqGopWs2N+0="
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Amazon.JSII.Generator.Class;
using Amazon.JSII.JsonModel.Spec;
using Xunit;
using System.Collections.Generic;

namespace Amazon.JSII.Generator.UnitTests.Class
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public void IncludesDocs()
name: "myClass",
isAbstract: true,
initializer: new Initializer(),
docs: new Docs {{"foo", "bar"}}
docs: new Docs(custom: new Dictionary<string, string>{{"foo", "bar"}})
);

string actual = Render(classType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis;
using Xunit;
using TypeKind = Amazon.JSII.JsonModel.Spec.TypeKind;
using System.Collections.Generic;

namespace Amazon.JSII.Generator.UnitTests.Class
{
Expand Down Expand Up @@ -133,7 +134,7 @@ public void IncludesDocs()
name: "myClass",
isAbstract: false,
initializer: new Initializer(),
docs: new Docs {{"foo", "bar"}}
docs: new Docs(custom: new Dictionary<string, string>{{"foo", "bar"}})
);

string actual = Render(classType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit;
using System.Collections.Generic;

namespace Amazon.JSII.Generator.UnitTests.Class
{
Expand Down Expand Up @@ -114,7 +115,7 @@ public void DoesNotIncludeDocs()
Method method = new Method
(
name: "myMethod",
docs: new Docs { { "foo", "bar" } }
docs: new Docs(custom: new Dictionary<string, string>{{"foo", "bar"}})
);

Symbols.MapMethodName("myClassFqn", "myMethod", "MyMethod");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit;
using System.Collections.Generic;

namespace Amazon.JSII.Generator.UnitTests.Class
{
Expand Down Expand Up @@ -198,7 +199,7 @@ public void DoesNotIncludeDocs()
isImmutable: true,
isAbstract: false,
isProtected: false,
docs: new Docs { { "foo", "bar" } }
docs: new Docs(custom: new Dictionary<string, string>{{"foo", "bar"}})
);

Symbols.MapPropertyName("myClassFqn", "myProperty", "MyProperty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis;
using Xunit;
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using System.Collections.Generic;

namespace Amazon.JSII.Generator.UnitTests.DocComment
{
Expand All @@ -29,9 +30,9 @@ string Render(Docs docs)
public void IncludesSingleLineSummary()
{
Docs docs = new Docs
{
{ "summary", "my comment" }
};
(
summary: "my comment"
);

string actual = Render(docs);
string expected =
Expand All @@ -47,9 +48,9 @@ public void IncludesSingleLineSummary()
public void IncludesMultiLineSummary(string summary)
{
Docs docs = new Docs
{
{ "summary", summary }
};
(
summary: summary
);

string actual = Render(docs);
string expected =
Expand All @@ -66,9 +67,9 @@ public void IncludesMultiLineSummary(string summary)
public void IncludesSingleLineRemarks()
{
Docs docs = new Docs
{
{ "myKey", "my comment" }
};
(
custom: new Dictionary<string, string>{{ "myKey", "my comment" }}
);

string actual = Render(docs);
string expected =
Expand All @@ -82,10 +83,12 @@ public void IncludesSingleLineRemarks()
public void IncludesMultiLineRemarks()
{
Docs docs = new Docs
{
{ "myKey1", "my\ncomment" },
{ "myKey2", "my\r\ncomment" }
};
(
custom: new Dictionary<string, string>{
{ "myKey1", "my\ncomment" },
{ "myKey2", "my\r\ncomment" }
}
);

string actual = Render(docs);
string expected =
Expand All @@ -104,9 +107,9 @@ public void IncludesMultiLineRemarks()
public void SeparatesSingleLineLink()
{
Docs docs = new Docs
{
{ "link", "www.example.com" }
};
(
custom: new Dictionary<string, string>{{ "link", "www.example.com" }}
);

string actual = Render(docs);
string expected =
Expand All @@ -120,9 +123,9 @@ public void SeparatesSingleLineLink()
public void IgnoresParam()
{
Docs docs = new Docs
{
{ "param", "my comment" }
};
(
custom: new Dictionary<string, string>{{ "param", "my comment" }}
);

string actual = Render(docs);
string expected = @"Member";
Expand All @@ -134,9 +137,9 @@ public void IgnoresParam()
public void IgnoresReturns()
{
Docs docs = new Docs
{
{ "returns", "my comment" }
};
(
returns: "my comment"
);

string actual = Render(docs);
string expected = @"Member";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis;
using Xunit;
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using System.Collections.Generic;

namespace Amazon.JSII.Generator.UnitTests.DocComment
{
Expand Down Expand Up @@ -34,9 +35,9 @@ string Render(Docs docs, params Parameter[] parameters)
public void IncludesSingleLineSummary()
{
Docs docs = new Docs
{
{ "summary", "my comment" }
};
(
summary: "my comment"
);

string actual = Render(docs);
string expected =
Expand All @@ -52,9 +53,9 @@ public void IncludesSingleLineSummary()
public void IncludesMultiLineSummary(string summary)
{
Docs docs = new Docs
{
{ "summary", summary }
};
(
summary: summary
);

string actual = Render(docs);
string expected =
Expand All @@ -71,9 +72,9 @@ public void IncludesMultiLineSummary(string summary)
public void IncludesSingleLineRemarks()
{
Docs docs = new Docs
{
{ "myKey", "my comment" }
};
(
custom: new Dictionary<string, string>{{ "myKey", "my comment" }}
);

string actual = Render(docs);
string expected =
Expand All @@ -87,10 +88,12 @@ public void IncludesSingleLineRemarks()
public void IncludesMultiLineRemarks()
{
Docs docs = new Docs
{
{ "myKey1", "my\ncomment" },
{ "myKey2", "my\r\ncomment" }
};
(
custom: new Dictionary<string, string>{
{ "myKey1", "my\ncomment" },
{ "myKey2", "my\r\ncomment" }
}
);

string actual = Render(docs);
string expected =
Expand All @@ -109,9 +112,9 @@ public void IncludesMultiLineRemarks()
public void SeparatesSingleLineLink()
{
Docs docs = new Docs
{
{ "link", "www.example.com" }
};
(
custom: new Dictionary<string, string>{ { "link", "www.example.com" } }
);

string actual = Render(docs);
string expected =
Expand All @@ -125,9 +128,9 @@ public void SeparatesSingleLineLink()
public void IgnoresParam()
{
Docs docs = new Docs
{
{ "param", "my comment" }
};
(
custom: new Dictionary<string, string>{ { "param", "my comment" } }
);

string actual = Render(docs);
string expected = @"void Method()";
Expand All @@ -139,9 +142,9 @@ public void IgnoresParam()
public void IncludesReturns()
{
Docs docs = new Docs
{
{ "returns", "my comment" }
};
(
returns: "my comment"
);

string actual = Render(docs);
string expected =
Expand All @@ -158,9 +161,9 @@ public void IgnoresParameterParam()
name: "myParam",
type: new TypeReference(primitive: PrimitiveType.String),
docs: new Docs
{
{ "param", "my comment" }
}
(
custom: new Dictionary<string, string>{ { "param", "my comment" } }
)
);

Symbols.MapParameterName("myParam", "myParam");
Expand All @@ -178,9 +181,9 @@ public void TrimsParameterSummary()
name: "myParam",
type: new TypeReference(primitive: PrimitiveType.String),
docs: new Docs
{
{ "summary", "my comment" }
}
(
summary: "my comment"
)
);

Symbols.MapParameterName("myParam", "myParam");
Expand All @@ -193,27 +196,23 @@ public void TrimsParameterSummary()
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesParameterRemarks))]
public void IncludesParameterRemarks()
[Fact(DisplayName = Prefix + nameof(IncludesParameterSummary))]
public void IncludesParameterSummary()
{
Parameter parameter = new Parameter(
name: "myParam",
type: new TypeReference(primitive: PrimitiveType.String),
docs: new Docs
{
{ "myKey1", "my comment" },
{ "myKey2", "my comment" }
}
(
summary: "This parameter is swell"
)
);

Symbols.MapParameterName("myParam", "myParam");

string actual = Render(null, parameter);
string expected =
@"/// <param name = ""myParam"">
/// myKey1: my comment
/// myKey2: my comment
/// </param>
@"/// <param name = ""myParam"">This parameter is swell</param>
void Method()";

Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
Expand Down
Loading

0 comments on commit ae85aa5

Please sign in to comment.