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: generated examples fail when scope is not of type Construct #262

Merged
merged 2 commits into from
Jun 1, 2023
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
6 changes: 5 additions & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ function generateStaticFactoryPropertyExample(staticFactoryProperty: reflect.Pro
* Generate an example value of the given parameter.
*/
function exampleValueForParameter(context: ExampleContext, param: reflect.Parameter, position: number, level: number): Code {
if (param.name === 'scope' && position === 0) {
if (
param.name === 'scope' &&
['constructs.IConstruct', 'constructs.Construct'].includes(param.type.fqn ?? '') &&
position === 0
) {
return new Code('this');
}

Expand Down
6 changes: 3 additions & 3 deletions test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('generateClassAssignment ', () => {
'declare const prop5: any;',
'declare const property: my_assembly.Property;',
'',
'const classA = new my_assembly.ClassA(this, \'MyClassA\', {',
'const classA = new my_assembly.ClassA(\'scope\', \'MyClassA\', {',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this has to happen. i could not find a good way of mocking the constructs.Construct fqn and spent far too much time trying to figure it out.

' prop1: 123,',
' prop2: property,',
' prop3: [\'prop3\'],',
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('generateClassAssignment ', () => {
expected: [
'import * as my_assembly from \'my_assembly\';',
'',
'const classA = new my_assembly.ClassA(this, \'MyClassA\', {',
'const classA = new my_assembly.ClassA(\'scope\', \'MyClassA\', {',
' prop1: 123,',
'',
' // the properties below are optional',
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('generateClassAssignment ', () => {
expected: [
'import * as my_assembly from \'my_assembly\';',
'',
'const classA = new my_assembly.ClassA(this, \'MyClassA\', /* all optional props */ {',
'const classA = new my_assembly.ClassA(\'scope\', \'MyClassA\', /* all optional props */ {',
' prop1: 123,',
' prop2: 123,',
'});',
Expand Down