diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cb3a108d9..74346e7a8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [1.44.2](https://github.com/aws/jsii/compare/v1.44.1...v1.44.2) (2021-11-18) + + +### Bug Fixes + +* **jsii:** require statement for the warning file is generated when it's not used ([#3184](https://github.com/aws/jsii/issues/3184)) ([8f53f89](https://github.com/aws/jsii/commit/8f53f897ebc03e3b3f9e5837fea988e7af592571)) + ## [1.44.1](https://github.com/aws/jsii/compare/v1.44.0...v1.44.1) (2021-11-16) * revert "fix: dependency submodules may not be discovered" ([#3170](https://github.com/aws/jsii/pull/3170)) ([0449dd9](https://github.com/aws/jsii/commit/0449dd92ce3297b065c171efafc28d1f877432cc)) diff --git a/lerna.json b/lerna.json index 56890a91fe..4333be0c9b 100644 --- a/lerna.json +++ b/lerna.json @@ -10,5 +10,5 @@ "rejectCycles": true } }, - "version": "1.44.1" + "version": "1.44.2" } diff --git a/packages/jsii/lib/transforms/deprecation-warnings.ts b/packages/jsii/lib/transforms/deprecation-warnings.ts index 253419d433..bee6bbc3a6 100644 --- a/packages/jsii/lib/transforms/deprecation-warnings.ts +++ b/packages/jsii/lib/transforms/deprecation-warnings.ts @@ -333,6 +333,8 @@ module.exports.DeprecationError = DeprecationError; } class Transformer { + private warningCallsWereInjected = false; + public constructor( private readonly typeChecker: ts.TypeChecker, private readonly context: ts.TransformationContext, @@ -342,9 +344,11 @@ class Transformer { ) {} public transform(node: T): T { + this.warningCallsWereInjected = false; + const result = this.visitEachChild(node); - if (ts.isSourceFile(result)) { + if (ts.isSourceFile(result) && this.warningCallsWereInjected) { const importDir = path.relative( path.dirname(result.fileName), this.projectRoot, @@ -368,6 +372,8 @@ class Transformer { private visitor(node: T): ts.VisitResult { if (ts.isMethodDeclaration(node) && node.body != null) { const statements = this.getStatementsForDeclaration(node); + this.warningCallsWereInjected = + this.warningCallsWereInjected || statements.length > 0; return ts.updateMethod( node, node.decorators, @@ -385,6 +391,8 @@ class Transformer { ) as any; } else if (ts.isGetAccessorDeclaration(node) && node.body != null) { const statements = this.getStatementsForDeclaration(node); + this.warningCallsWereInjected = + this.warningCallsWereInjected || statements.length > 0; return ts.updateGetAccessor( node, node.decorators, @@ -399,6 +407,8 @@ class Transformer { ) as any; } else if (ts.isSetAccessorDeclaration(node) && node.body != null) { const statements = this.getStatementsForDeclaration(node); + this.warningCallsWereInjected = + this.warningCallsWereInjected || statements.length > 0; return ts.updateSetAccessor( node, node.decorators, @@ -412,6 +422,8 @@ class Transformer { ) as any; } else if (ts.isConstructorDeclaration(node) && node.body != null) { const statements = this.getStatementsForDeclaration(node); + this.warningCallsWereInjected = + this.warningCallsWereInjected || statements.length > 0; return ts.updateConstructor( node, node.decorators, diff --git a/packages/jsii/test/deprecation-warnings.test.ts b/packages/jsii/test/deprecation-warnings.test.ts index ded6bfc61b..44fc829e6d 100644 --- a/packages/jsii/test/deprecation-warnings.test.ts +++ b/packages/jsii/test/deprecation-warnings.test.ts @@ -388,6 +388,26 @@ describe('Call injections', () => { ); }); + test('does not generate a require statement when no calls were injected', async () => { + const result = await compileJsiiForTest( + { + 'index.ts': `export * from './some/folder/handler'`, + 'some/folder/handler.ts': ` + export function handler(event: any) { return event; } + `, + }, + undefined /* callback */, + { addDeprecationWarnings: true }, + ); + + const expectedPath = ['..', '..', '.warnings.jsii.js'].join('/'); + + const content = jsFile(result, 'some/folder/handler'); + expect(content).not.toContain( + `const jsiiDeprecationWarnings = require("${expectedPath}")`, + ); + }); + test('deprecated methods', async () => { const result = await compileJsiiForTest( `