Skip to content

Commit

Permalink
fix(pacmak): examples with multi-line strings produce invalid python …
Browse files Browse the repository at this point in the history
…docstrings

Since docstrings are now rendered inside single-quoted multiline strings to allow the docstring itself to include example code with double-quoted multiline string literals. Those are the ones produced by rosetta for python.

Fixes #2569
  • Loading branch information
Elad Ben-Israel committed Feb 14, 2021
1 parent 0368d42 commit b854f9b
Show file tree
Hide file tree
Showing 6 changed files with 826 additions and 782 deletions.
7 changes: 7 additions & 0 deletions packages/jsii-calc/lib/documented.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
*
* Multiple paragraphs are separated by an empty line.
*
* @example
* const x = 12 + 44;
* const s1 = "string";
* const s2 = "string \nwith new newlines"; // see https://github.com/aws/jsii/issues/2569
* const s3 = `string
* with
* new lines`;
* @stable
*/
export class DocumentedClass {
Expand Down
14 changes: 9 additions & 5 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const requirementsFile = path.resolve(
'requirements-dev.txt',
);

// we use single-quotes for multi-line strings to allow examples within the
// docstrings themselves to include double-quotes (see https://github.com/aws/jsii/issues/2569)
const DOCSTRING_QUOTES = "'''";

export default class Python extends Target {
protected readonly generator: PythonGenerator;

Expand Down Expand Up @@ -1571,9 +1575,9 @@ class PythonModule implements PythonType {
this.fqn === this.assembly.name &&
this.package.convertedReadme.trim().length > 0
) {
code.line('"""');
code.line(DOCSTRING_QUOTES);
code.line(this.package.convertedReadme);
code.line('"""');
code.line(DOCSTRING_QUOTES);
}
}

Expand Down Expand Up @@ -2189,16 +2193,16 @@ class PythonGenerator extends Generator {
}

if (lines.length === 1) {
code.line(`"""${lines[0]}"""`);
code.line(`${DOCSTRING_QUOTES}${lines[0]}${DOCSTRING_QUOTES}`);
} else {
code.line(`"""${lines[0]}`);
code.line(`${DOCSTRING_QUOTES}${lines[0]}`);
lines.splice(0, 1);

for (const line of lines) {
code.line(line);
}

code.line('"""');
code.line(DOCSTRING_QUOTES);
}
if (options.trailingNewLine) {
code.line();
Expand Down

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

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

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

Loading

0 comments on commit b854f9b

Please sign in to comment.