Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pacmak): '*/' not escaped in JavaDocs #3104

Merged
merged 3 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/jsii-pacmak/lib/targets/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2336,10 +2336,22 @@ class JavaGenerator extends Generator {

if (docs.example) {
paras.push('Example:');

const convertedExample = this.convertExample(docs.example);

// We used to use the slightly nicer `<pre>{@code ...}</pre>`, which doesn't
// require (and therefore also doesn't allow) escaping special characters.
//
// However, code samples may contain block quotes of their own ('*/') that
// would terminate the block comment from the PoV of the Java tokenizer, and
// since `{@code ... }` doesn't allow any escaping, there's also no way to encode
// '*/' in a way that would (a) hide it from the tokenizer and (b) give '*/' back
// after processing JavaDocs.
//
// Hence, we just resort to HTML-encoding everything (same as we do for code
// examples that have been translated from MarkDown).
paras.push(
`<blockquote><pre>{@code\n${this.convertExample(
docs.example,
)}}</pre></blockquote>`,
markDownToJavaDoc(['```', convertedExample, '```'].join('\n')),
);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion packages/jsii-rosetta/lib/markdown/javadoc-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class JavaDocRenderer extends MarkdownRenderer {
}

public document(_node: cm.Node, context: RendererContext) {
return stripTrailingWhitespace(collapseParaJava(context.content()));
return stripTrailingWhitespace(specialDocCommentEscape(collapseParaJava(context.content())));
}

public heading(node: cm.Node, context: RendererContext) {
Expand Down Expand Up @@ -80,3 +80,18 @@ export class JavaDocRenderer extends MarkdownRenderer {
function collapseParaJava(x: string) {
return collapsePara(x, '\n<p>\n');
}

/**
* A final single-pass escape of '* /' which might otherwise end a doc comment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A final single-pass escape of '* /' which might otherwise end a doc comment.
* A final single-pass escape of '*/' which might otherwise end a doc comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't, right, because that would end THIS doc comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh. true!

*
* We have to do this in one final pass because I've observed that in running
* next, the MarkDown parser will parse the two symbols to:
*
* [..., text('*'), text('/'), ...]
*
* which means we have no other ability to observe the two-character combination
* properly.
*/
function specialDocCommentEscape(x: string) {
return x.replace(new RegExp('\\*\\/', 'g'), '*&#47;');
}
13 changes: 13 additions & 0 deletions packages/jsii-rosetta/test/markdown/javadoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ if (x &lt; 3) {
);
});

test('doc comment closing chars are escaped', () => {
expectOutput(
['Escape this */', '```', 'And this */', '```'].join('\n'),
`
Escape this *&#47;
<p>
<blockquote><pre>
And this *&#47;
</pre></blockquote>
`,
);
});

test('quotes are escaped inside attributes', () => {
expectOutput(
`
Expand Down