From d0104495956ae02458530926b296fcc508a0622a Mon Sep 17 00:00:00 2001 From: Nick Lynch Date: Wed, 27 Oct 2021 10:32:51 +0100 Subject: [PATCH 1/2] fix: deprecation-warnings includes TS-only import The --deprecation-warnings flag produces code which compiles to valid JS in some scenarios (namely, when something is exported from the same file), but not in others. In the latter case, the `import =` syntax causes the compiler to choke. --- .../lib/transforms/deprecation-warnings.ts | 34 +++++++++++++------ .../jsii/test/deprecation-warnings.test.ts | 23 ++++++------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/jsii/lib/transforms/deprecation-warnings.ts b/packages/jsii/lib/transforms/deprecation-warnings.ts index 24311654bf..1e2b465ac0 100644 --- a/packages/jsii/lib/transforms/deprecation-warnings.ts +++ b/packages/jsii/lib/transforms/deprecation-warnings.ts @@ -55,7 +55,7 @@ export class DeprecationWarningsInjector { if (spec.isEnumType(type) && type.locationInModule?.filename) { statements.push( - createRequireStatement(type.locationInModule?.filename), + createEnumRequireStatement(type.locationInModule?.filename), ); for (const member of Object.values(type.members ?? [])) { @@ -338,12 +338,7 @@ class Transformer { : `./${FILE_NAME}`; return ts.updateSourceFileNode(result, [ - ts.createImportEqualsDeclaration( - undefined, - undefined, - NAMESPACE, - ts.createExternalModuleReference(ts.createLiteral(importPath)), - ), + createRequireStatement(NAMESPACE, importPath), ...result.statements, ]) as any; } @@ -525,13 +520,30 @@ function insertStatements(block: ts.Block, newStatements: ts.Statement[]) { return ts.createNodeArray(result); } -function createRequireStatement(typeLocation: string): ts.ExpressionStatement { +function createEnumRequireStatement(typeLocation: string): ts.Statement { const { ext } = path.parse(typeLocation); const jsFileName = typeLocation.replace(ext, '.js'); - return ts.createExpressionStatement( - ts.createIdentifier( - `const ${LOCAL_ENUM_NAMESPACE} = require("./${jsFileName}")`, + return createRequireStatement(LOCAL_ENUM_NAMESPACE, `./${jsFileName}`); +} + +function createRequireStatement( + name: string, + importPath: string, +): ts.Statement { + return ts.createVariableStatement( + undefined, + ts.createVariableDeclarationList( + [ + ts.createVariableDeclaration( + name, + undefined, + ts.createCall(ts.createIdentifier('require'), undefined, [ + ts.createLiteral(importPath), + ]), + ), + ], + ts.NodeFlags.Const, ), ); } diff --git a/packages/jsii/test/deprecation-warnings.test.ts b/packages/jsii/test/deprecation-warnings.test.ts index f5dc5d69a5..94f9cea258 100644 --- a/packages/jsii/test/deprecation-warnings.test.ts +++ b/packages/jsii/test/deprecation-warnings.test.ts @@ -78,8 +78,8 @@ function testpkg_Baz(p) { export interface Foo {} export interface Bar {} export interface Baz { - readonly foo: Foo; - readonly bar: Bar; + readonly foo: Foo; + readonly bar: Bar; readonly x: string; } `, @@ -190,8 +190,8 @@ function testpkg_Baz(p) { ` export enum State { ON, - - ${DEPRECATED} + + ${DEPRECATED} OFF } `, @@ -239,10 +239,10 @@ function testpkg_Baz(p) { export interface Bar { readonly x: string; } - + export interface Foo { readonly y: string; - + /** @deprecated kkkkkkkk */ readonly bar: Bar; } @@ -317,14 +317,11 @@ describe('Call injections', () => { ); const expectedPath = ['..', '..', '.warnings.jsii.js'].join(path.sep); - const requireRegex = /const jsiiDeprecationWarnings = require\("(.+)"\);/g; - const content = jsFile(result, 'some/folder/source'); - const match = requireRegex.exec(content); - expect(match).toBeDefined(); - const actualPath = match![1]; - expect(path.normalize(actualPath)).toEqual(expectedPath); + expect(content).toContain( + `const jsiiDeprecationWarnings = require("${expectedPath}");`, + ); }); test('deprecated methods', async () => { @@ -385,7 +382,7 @@ describe('Call injections', () => { export class Foo { private _x = 0; public get x(){return this._x} - + ${DEPRECATED} public set x(_x: number) {this._x = _x;} } From c0ac57c8ae97c1f68f673aad3dc3df07bce4877e Mon Sep 17 00:00:00 2001 From: Nick Lynch Date: Wed, 27 Oct 2021 10:55:50 +0100 Subject: [PATCH 2/2] revert changes to test file --- .../jsii/test/deprecation-warnings.test.ts | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/jsii/test/deprecation-warnings.test.ts b/packages/jsii/test/deprecation-warnings.test.ts index 94f9cea258..f5dc5d69a5 100644 --- a/packages/jsii/test/deprecation-warnings.test.ts +++ b/packages/jsii/test/deprecation-warnings.test.ts @@ -78,8 +78,8 @@ function testpkg_Baz(p) { export interface Foo {} export interface Bar {} export interface Baz { - readonly foo: Foo; - readonly bar: Bar; + readonly foo: Foo; + readonly bar: Bar; readonly x: string; } `, @@ -190,8 +190,8 @@ function testpkg_Baz(p) { ` export enum State { ON, - - ${DEPRECATED} + + ${DEPRECATED} OFF } `, @@ -239,10 +239,10 @@ function testpkg_Baz(p) { export interface Bar { readonly x: string; } - + export interface Foo { readonly y: string; - + /** @deprecated kkkkkkkk */ readonly bar: Bar; } @@ -317,11 +317,14 @@ describe('Call injections', () => { ); const expectedPath = ['..', '..', '.warnings.jsii.js'].join(path.sep); + const requireRegex = /const jsiiDeprecationWarnings = require\("(.+)"\);/g; + const content = jsFile(result, 'some/folder/source'); + const match = requireRegex.exec(content); + expect(match).toBeDefined(); - expect(content).toContain( - `const jsiiDeprecationWarnings = require("${expectedPath}");`, - ); + const actualPath = match![1]; + expect(path.normalize(actualPath)).toEqual(expectedPath); }); test('deprecated methods', async () => { @@ -382,7 +385,7 @@ describe('Call injections', () => { export class Foo { private _x = 0; public get x(){return this._x} - + ${DEPRECATED} public set x(_x: number) {this._x = _x;} }