bito-code-review[bot] commented on code in PR #42910:
URL: https://github.com/apache/superset/pull/42910#discussion_r4152913223


##########
superset-frontend/plugins/plugin-chart-echarts/test/Bullet/utils.test.ts:
##########
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+  isRangesInputComplete,
+  tokenizeToNumericArray,
+} from '../../src/Bullet/utils';
+
+test('tokenizeToNumericArray drops blank tokens, including an embedded one', 
() => {
+  expect(tokenizeToNumericArray('20,40,60')).toEqual([20, 40, 60]);
+  expect(tokenizeToNumericArray('20,,60')).toEqual([20, 60]);
+  expect(tokenizeToNumericArray('')).toBeNull();
+  expect(tokenizeToNumericArray(undefined)).toBeNull();
+});

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Untested non-finite drop path</b></div>
   <div id="fix">
   
   These assertions cover the happy path and blank-token drop, but 
`tokenizeToNumericArray` in `src/Bullet/utils.ts` also silently drops 
non-numeric and non-finite tokens (`Number.isFinite(Number(token))`) and trims 
whitespace. None of that behavior is pinned here, so a regression in the 
`.filter(n => Number.isFinite(n))` or `.map(token => token.trim())` steps would 
pass this suite unnoticed.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #05c8db</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/src/explore/components/controls/IntervalColorsControl/index.tsx:
##########
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { t } from '@apache-superset/core/translation';
+import { getCategoricalSchemeRegistry } from '@superset-ui/core';
+import ControlHeader from '../../ControlHeader';
+import ColorPickerControl from '../ColorPickerControl';
+import type { ColorPickerValue } from '../ColorPickerControl';
+import {
+  RangeRow as IntervalRow,
+  RangeLabel as BoundLabel,
+  replaceColorAtIndex,
+} from '../shared/RangeColorRow';
+import { IntervalColorsControlProps } from './types';
+import {
+  parseIntervalBounds as parseBounds,
+  resolveLegacyIntervalColors as resolveLegacyColors,
+} from './legacyColors';
+
+/**
+ * Per-interval color editor for the Gauge chart. Row *count* is driven by
+ * the sibling `intervals` control (one row per parsed upper bound) so bound
+ * values keep a single source of truth; this control only owns colors,
+ * stored as an array of hex strings positionally matched to those bounds.
+ */
+export default function IntervalColorsControl({
+  value,
+  onChange,
+  intervals,
+  legacyIntervalColorIndices,
+  colorScheme,
+  ...headerProps
+}: IntervalColorsControlProps) {
+  const bounds = parseBounds(intervals);
+  const legacyColors = resolveLegacyColors(
+    bounds,
+    legacyIntervalColorIndices,
+    colorScheme,
+  );
+  const schemeColors =
+    getCategoricalSchemeRegistry().get(colorScheme)?.colors ?? [];
+
+  const colorAt = (index: number): string =>
+    value?.[index] ||
+    legacyColors[index] ||
+    schemeColors[index % (schemeColors.length || 1)] ||
+    '';
+
+  const handleColorChange = (index: number) => (color: ColorPickerValue) => {
+    if (typeof color !== 'string') return;
+    onChange?.(replaceColorAtIndex(bounds.length, index, color, colorAt));
+  };

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing mid-edit guard</b></div>
   <div id="fix">
   
   `BulletRangeColorsControl` (same PR) guards `handleColorChange` with 
`isRangesInputComplete` and disables its picker because 
`tokenizeToNumericArray`/`parseIntervalBounds` leniently compact a mid-edit 
list like "20,,60", shifting colors to wrong positions. `IntervalColorsControl` 
uses the identical lenient `parseIntervalBounds` but has no such guard and 
never disables its picker, so a color edit during a transient edit can be 
positionally misaligned. Add the same completeness guard + `disabled`.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #05c8db</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/plugins/plugin-chart-echarts/test/Bullet/utils.test.ts:
##########
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+  isRangesInputComplete,
+  tokenizeToNumericArray,
+} from '../../src/Bullet/utils';
+
+test('tokenizeToNumericArray drops blank tokens, including an embedded one', 
() => {
+  expect(tokenizeToNumericArray('20,40,60')).toEqual([20, 40, 60]);
+  expect(tokenizeToNumericArray('20,,60')).toEqual([20, 60]);
+  expect(tokenizeToNumericArray('')).toBeNull();
+  expect(tokenizeToNumericArray(undefined)).toBeNull();
+});
+
+test('isRangesInputComplete is true for an empty or fully numeric list', () => 
{
+  expect(isRangesInputComplete(undefined)).toBe(true);
+  expect(isRangesInputComplete('')).toBe(true);
+  expect(isRangesInputComplete('20')).toBe(true);
+  expect(isRangesInputComplete('20,40,60')).toBe(true);
+});
+
+test('isRangesInputComplete tolerates a single trailing blank token while 
typing', () => {
+  expect(isRangesInputComplete('20,40,')).toBe(true);
+});

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Untested trailing-blank edge cases</b></div>
   <div id="fix">
   
   The single-trailing-blank tolerance is tested only for `'20,40,'`. The 
implementation in `isRangesInputComplete` slices off exactly one trailing empty 
token, so inputs like `','` (all tokens blank after slicing, leaving 
`tokensToValidate.length === 0`) or `'20,,'` (blank token remains before the 
trailing one) must return false. These boundaries are untested, so a regression 
in the `tokensToValidate.length > 0` guard or the slice logic would go 
undetected.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #05c8db</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/src/explore/components/controls/IntervalColorsControl/legacyColors.ts:
##########
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { getCategoricalSchemeRegistry } from '@superset-ui/core';
+
+export const parseIntervalBounds = (intervals?: string): number[] =>
+  (intervals ?? '')
+    .split(',')
+    .map(part => part.trim())
+    .filter(part => part !== '')
+    .map(Number)
+    .filter(bound => Number.isFinite(bound));
+
+/**
+ * Resolves the legacy `interval_color_indices` control (comma-separated,
+ * 1-indexed positions into the chosen categorical color scheme) into real
+ * hex colors, positionally matched to `bounds`. Shared by the control panel
+ * (display-only fallback while a legacy chart is being edited) and by
+ * `handleDeprecatedControls` (which migrates the value into `interval_colors`
+ * on load, since `interval_color_indices` is no longer a registered control
+ * and would otherwise be silently dropped on the next save).
+ */
+export const resolveLegacyIntervalColors = (
+  bounds: number[],
+  legacyIntervalColorIndices: string | undefined,
+  colorScheme: string | undefined,
+): string[] => {
+  const schemeColors =
+    getCategoricalSchemeRegistry().get(colorScheme)?.colors ?? [];
+  const indices = (legacyIntervalColorIndices ?? '')
+    .split(',')
+    .map(part => part.trim())
+    .map(part => (part === '' ? NaN : Number(part)));
+  return bounds.map((_, index) => {
+    const legacyIndex = indices[index];
+    if (schemeColors.length === 0) return '';
+    if (Number.isFinite(legacyIndex)) {
+      return schemeColors[
+        (((legacyIndex - 1) % schemeColors.length) + schemeColors.length) %
+          schemeColors.length
+      ];
+    }
+    return schemeColors[index % schemeColors.length];

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Legacy color fallback mismatch</b></div>
   <div id="fix">
   
   In `resolveLegacyIntervalColors`, the fallback `schemeColors[index % 
schemeColors.length]` for bounds beyond the legacy indices disagrees with the 
migration test. With bounds [20,40,60] and indices '2,3' on testScheme, index 2 
resolves to '#2ca02c' but the test expects '#1f77b4', so the 
`handleDeprecatedControls` migration test fails and legacy Gauge charts get 
misaligned colors.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #05c8db</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to