From 8f53f897ebc03e3b3f9e5837fea988e7af592571 Mon Sep 17 00:00:00 2001 From: Otavio Macedo Date: Thu, 18 Nov 2021 16:13:09 +0000 Subject: [PATCH] fix(jsii): require statement for the warning file is generated when it's not used (#3184) When `add-deprecation-warnings` is `true` but no calls were injected in a given file, the `require` statement for `.warnings.jsii.js` should not be generated. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0 --- .../lib/transforms/deprecation-warnings.ts | 14 ++++++++++++- .../jsii/test/deprecation-warnings.test.ts | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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( `