From 49839b28410f476646e2b60fe9fc5a570a540905 Mon Sep 17 00:00:00 2001 From: Kaizen Conroy <36202692+kaizencc@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:50:29 -0500 Subject: [PATCH] chore(cli): cli import code never reaches "??" operator (#28113) We're seeing this linter error flag: `Warning: G] The "??" operator here will always return the left operand [suspicious-nullish-coalescing]` in the yarn upgrade [task](https://github.com/aws/aws-cdk/actions/runs/6958118087/job/18935111755). It is because `const defaultValue = typeof resourceProps[idProp] ?? '';` never reaches the `??` since `typeof` returns a string (returns `"undefined"` when the type its looking at is `undefined`). The code in question is buggy, but also ambiguous in meaning. It could mean: `const defaultValue = typeof (resourceProps[idProp] ?? '');`, which means that we want `defaultValue === 'string'` when `resourceProps[idProp] === undefined`. or `const defaultValue = resourceProps[idProp] ? typeof resourceProps[idProp] : '';`, which means that we want `defaultValue === ''` when `resourceProps[idProp] === undefined`. Later on we use `defaultValue` as the condition in a ternary operator. This tells me that the latter is the correct way to interpret the code's intention, since `''` evaluates to `false`. All other options, including `'string'` and `'undefined'`, evaluate to `true`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk/lib/import.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk/lib/import.ts b/packages/aws-cdk/lib/import.ts index f90e9853fb8a7..0b181247a44c5 100644 --- a/packages/aws-cdk/lib/import.ts +++ b/packages/aws-cdk/lib/import.ts @@ -290,7 +290,7 @@ export class ResourceImporter { for (const idProp of idProps) { // If we have a value from the template, use it as default. This will only be a partial // identifier if present, otherwise we would have done the import already above. - const defaultValue = typeof resourceProps[idProp] ?? ''; + const defaultValue = resourceProps[idProp] ?? ''; const prompt = [ promptPattern.replace(/%/g, chalk.blue(idProp)),