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

Possible unreachable code in ValueSelector component #476

Open
nateplusplus opened this issue Jul 29, 2023 · 0 comments
Open

Possible unreachable code in ValueSelector component #476

nateplusplus opened this issue Jul 29, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@nateplusplus
Copy link
Contributor

While refactoring to TypeScript (#456), I noticed the property "array" is added in the following code snippet (Ln 153):

{
{
string: (
<Input
value={val || ''}
placeholder='String value'
type='string'
onChange={(ev: any) => {
const newVal = ev.target.value;
const oldCombo = possibility.value.enum[index];
onChange({
...possibility,
value: {
enum: [
...enumArr.slice(0, index),
{ ...oldCombo, [key]: newVal },
...enumArr.slice(index + 1),
],
},
});
}}
className='card-modal-text'
/>
),
number: (
<Input
value={val || ''}
placeholder='Number value'
type='number'
onChange={(ev: any) => {
const newVal = Number.parseFloat(ev.target.value);
const oldCombo = possibility.value.enum[index];
onChange({
...possibility,
value: {
enum: [
...enumArr.slice(0, index),
{ ...oldCombo, [key]: newVal },
...enumArr.slice(index + 1),
],
},
});
}}
className='card-modal-number'
/>
),
array: (
<Input
value={JSON.stringify(val) || ''}
placeholder='Array in JSON'
type='string'
onChange={(ev: any) => {
let newVal = val;
try {
newVal = JSON.parse(ev.target.value);
} catch (error) {
// eslint-disable-next-line no-console
console.error('invalid JSON array input');
}
const oldCombo = possibility.value.enum[index];
onChange({
...possibility,
value: {
enum: [
...enumArr.slice(0, index),
{ ...oldCombo, [key]: newVal },
...enumArr.slice(index + 1),
],
},
});
}}
className='card-modal-text'
/>
),
object: (
<Input
value={JSON.stringify(val) || ''}
placeholder='Object in JSON'
type='string'
onChange={(ev: any) => {
let newVal = val;
try {
newVal = JSON.parse(ev.target.value);
} catch (error) {
// eslint-disable-next-line no-console
console.error('invalid JSON object input');
}
const oldCombo = possibility.value.enum[index];
onChange({
...possibility,
value: {
enum: [
...enumArr.slice(0, index),
{ ...oldCombo, [key]: newVal },
...enumArr.slice(index + 1),
],
},
});
}}
className='card-modal-text'
/>
),
}[typeof val]

As I understand it, in this code, square brackets after the object are being used to dynamically compute a property name based on the type of data that the val variable holds. So if val is a string, it would return the value associated with the string property in the object.

However, if the val variable holds an array, JavaScript's typeof would return 'object', not 'array'.

For this reason, the code inside the array property of this object may not be reachable. It might be worth further testing to ensure there is no missing functionality here.

@raymond-lam raymond-lam added the bug Something isn't working label Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants