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(jsii): deprecation warnings erroneously warn for duplicate enum values #3105

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions packages/jsii/lib/transforms/deprecation-warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class DeprecationWarningsInjector {
statements.push(
createEnumRequireStatement(type.locationInModule?.filename),
);
statements.push(createDuplicateEnumValuesCheck(type));

for (const member of Object.values(type.members ?? [])) {
if (spec.isDeprecated(member)) {
Expand Down Expand Up @@ -600,6 +601,56 @@ function createTypeHandlerCall(
);
}

/**
* There is a chance an enum contains duplicates values with distinct keys,
* with one of those keys being deprecated. This is a potential pattern to "rename" an enum.
* In this case, we can't concretely determine if the deprecated member was used or not,
* so in those cases we skip the warnings altogether, rather than erroneously warning for valid usage.
* This create a statement to check if the enum value is a duplicate:
*
* if (Object.values(Foo).filter(x => x === p).length > 1) { return; }
*
* Note that we can't just check the assembly for these duplicates, due to:
* https://github.com/aws/jsii/issues/2782
*/
function createDuplicateEnumValuesCheck(
type: spec.TypeBase & spec.EnumType,
): ts.Statement {
return ts.createIf(
ts.createBinary(
ts.createPropertyAccess(
ts.createCall(
ts.createPropertyAccess(
ts.createCall(ts.createIdentifier('Object.values'), undefined, [
ts.createIdentifier(`${LOCAL_ENUM_NAMESPACE}.${type.name}`),
]),
ts.createIdentifier('filter'),
),
undefined,
[
ts.createArrowFunction(
undefined,
undefined,
[ts.createParameter(undefined, undefined, undefined, 'x')],
undefined,
ts.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.createBinary(
ts.createIdentifier('x'),
ts.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken),
ts.createIdentifier(PARAMETER_NAME),
),
),
],
),
ts.createIdentifier('length'),
),
ts.createToken(ts.SyntaxKind.GreaterThanToken),
ts.createNumericLiteral('1'),
),
ts.createReturn(),
);
}

/**
* Force a path to be UNIXy (use `/` as a separator)
*
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii/test/deprecation-warnings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ function testpkg_Baz(p) {
return;
visitedObjects.add(p);
const ns = require("./index.js");
if (Object.values(ns.State).filter(x => x === p).length > 1)
return;
if (p === ns.State.OFF)
print("testpkg.State#OFF", "Use something else");
visitedObjects.delete(p);
Expand Down