Skip to content

Commit

Permalink
fix(jsii): unexpected deprecated warnings when a deprecated interface…
Browse files Browse the repository at this point in the history
… is extended

It is common in the AWS CDK for non-deprecated interfaces to extend
deprecated interfaces.
Currently, when this occurs and the non-deprecated subclass is
referenced, a warning is generated notifying the deprecation of its
superclass.

Modify the implementation such that the warning is produced only when
a property of the deprecated interface is referenced.

In the case when a deprecated interface with no required properties is
used without any of its properties set, a warning will not be generated.
This should occur rarely and the trade off is acceptable.

fixes #3111
  • Loading branch information
Niranjan Jayakar committed Nov 2, 2021
1 parent e1527ee commit 5d5dd12
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/jsii/lib/transforms/deprecation-warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export class DeprecationWarningsInjector {
),
);

if (
spec.isDeprecated(type) &&
(spec.isEnumType(type) || (spec.isInterfaceType(type) && type.datatype))
) {
if (spec.isDeprecated(type) && spec.isEnumType(type)) {
// The type is deprecated
statements.push(
createWarningFunctionCall(type.fqn, type.docs?.deprecated),
Expand Down Expand Up @@ -78,12 +75,14 @@ export class DeprecationWarningsInjector {
}
} else if (spec.isInterfaceType(type) && type.datatype) {
for (const prop of Object.values(type.properties ?? {})) {
if (spec.isDeprecated(prop)) {
// The property is deprecated
if (spec.isDeprecated(prop) || spec.isDeprecated(type)) {
// If the property individually is deprecated, or the entire type is deprecated
const deprecatedDocs =
prop.docs?.deprecated ?? type.docs?.deprecated;
statements.push(
createWarningFunctionCall(
`${type.fqn}#${prop.name}`,
prop.docs?.deprecated,
deprecatedDocs,
ts.createIdentifier(`"${prop.name}" in ${PARAMETER_NAME}`),
),
);
Expand Down
23 changes: 23 additions & 0 deletions packages/jsii/test/deprecation-warnings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ function testpkg_Baz(p) {
`);
});

test('generates calls for deprecated interface', async () => {
const result = await compileJsiiForTest(
`
/** @deprecated use Bar instead */
export interface Foo {
readonly bar: string;
}
`,
undefined /* callback */,
{ addDeprecationWarnings: true },
);

expect(jsFile(result, '.warnings.jsii')).toMatch(`function testpkg_Foo(p) {
if (p == null)
return;
visitedObjects.add(p);
if ("bar" in p)
print("testpkg.Foo#bar", "use Bar instead");
visitedObjects.delete(p);
}
`);
});

test('generates calls for types in other assemblies', async () => {
const calcBaseRoot = resolveModuleDir('@scope/jsii-calc-base');
const calcLibRoot = resolveModuleDir('@scope/jsii-calc-lib');
Expand Down

0 comments on commit 5d5dd12

Please sign in to comment.