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): examples with multi-line strings produce invalid python docstrings #2571

Merged
merged 3 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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
17 changes: 9 additions & 8 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -4144,6 +4144,7 @@
"jsii-calc.DocumentedClass": {
"assembly": "jsii-calc",
"docs": {
"example": "const x = 12 + 44;\nconst s1 = \"string\";\nconst s2 = \"string \\nwith new newlines\"; // see https://github.com/aws/jsii/issues/2569\nconst s3 = `string\n with\n new lines`;",
"remarks": "This is the meat of the TSDoc comment. It may contain\nmultiple lines and multiple paragraphs.\n\nMultiple paragraphs are separated by an empty line.",
"stability": "stable",
"summary": "Here's the first line of the TSDoc comment."
Expand All @@ -4157,7 +4158,7 @@
"kind": "class",
"locationInModule": {
"filename": "lib/documented.ts",
"line": 11
"line": 18
},
"methods": [
{
Expand All @@ -4169,7 +4170,7 @@
},
"locationInModule": {
"filename": "lib/documented.ts",
"line": 21
"line": 28
},
"name": "greet",
"parameters": [
Expand Down Expand Up @@ -4197,7 +4198,7 @@
},
"locationInModule": {
"filename": "lib/documented.ts",
"line": 31
"line": 38
},
"name": "hola"
}
Expand Down Expand Up @@ -5239,7 +5240,7 @@
"kind": "interface",
"locationInModule": {
"filename": "lib/documented.ts",
"line": 39
"line": 46
},
"name": "Greetee",
"properties": [
Expand All @@ -5253,7 +5254,7 @@
"immutable": true,
"locationInModule": {
"filename": "lib/documented.ts",
"line": 45
"line": 52
},
"name": "name",
"optional": true,
Expand Down Expand Up @@ -9391,7 +9392,7 @@
"kind": "class",
"locationInModule": {
"filename": "lib/documented.ts",
"line": 54
"line": 61
},
"methods": [
{
Expand All @@ -9401,7 +9402,7 @@
},
"locationInModule": {
"filename": "lib/documented.ts",
"line": 58
"line": 65
},
"name": "doAThing"
}
Expand Down Expand Up @@ -14440,5 +14441,5 @@
}
},
"version": "3.20.120",
"fingerprint": "Qv5CJVZ6t7cfQmCSHOLrbefAuLKX9JUbhUCSh+mD+po="
"fingerprint": "DELAKIDgUwCEVXhZflQBdllcDRnscledlYW0UXrjzbM="
}
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)
eladb marked this conversation as resolved.
Show resolved Hide resolved
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