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


##########
superset-frontend/plugins/plugin-chart-ag-grid-table/src/renderers/parseJsonCellValue.ts:
##########
@@ -0,0 +1,210 @@
+/**
+ * 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.
+ */
+
+export type JsonContainer = Record<string, unknown> | unknown[];
+
+// Visible cells are parsed on the render path. Above this size the cell stays
+// plain text. Parsed results are reused for the same string.
+const MAX_JSON_CELL_LENGTH = 100_000;
+const PARSE_CACHE_LIMIT = 200;
+
+const parsedJsonCache = new Map<string, JsonContainer | null>();
+
+function isJsonContainer(value: unknown): value is JsonContainer {
+  if (value === null || typeof value !== 'object') {
+    return false;
+  }
+  if (Array.isArray(value)) {
+    return true;
+  }
+  if (value instanceof Date) {
+    return false;
+  }
+  const prototype = Object.getPrototypeOf(value);
+  return prototype === Object.prototype || prototype === null;
+}
+
+function isPlainJsonObject(value: unknown): value is Record<string, unknown> {
+  return isJsonContainer(value) && !Array.isArray(value);
+}
+
+function rememberParse(
+  text: string,
+  parsed: JsonContainer | null,
+): JsonContainer | null {
+  if (parsedJsonCache.size >= PARSE_CACHE_LIMIT) {
+    const oldest = parsedJsonCache.keys().next().value;
+    if (oldest !== undefined) {
+      parsedJsonCache.delete(oldest);
+    }
+  }
+  parsedJsonCache.set(text, parsed);
+  return parsed;
+}
+
+/**
+ * Accept a JSON object or array, either already parsed or as text.
+ * Scalars, invalid JSON, and non-JSON text return null so the cell stays
+ * on the plain-text renderer.
+ */
+export function parseJsonCellValue(value: unknown): JsonContainer | null {
+  if (isJsonContainer(value)) {
+    return value;
+  }
+  if (typeof value !== 'string' || value.length > MAX_JSON_CELL_LENGTH) {
+    return null;
+  }
+  const trimmed = value.trim();
+  if (trimmed.length === 0 || (trimmed[0] !== '{' && trimmed[0] !== '[')) {
+    return null;
+  }
+  const cached = parsedJsonCache.get(trimmed);
+  if (cached !== undefined) {
+    return cached;
+  }
+  try {
+    const parsed: unknown = JSON.parse(trimmed);
+    return rememberParse(trimmed, isJsonContainer(parsed) ? parsed : null);
+  } catch {
+    return rememberParse(trimmed, null);
+  }
+}
+
+function isFormattingWhitespace(char: string): boolean {
+  return char === ' ' || char === '\n' || char === '\r' || char === '\t';
+}
+
+/**
+ * Collapse formatting whitespace onto one line. Spaces inside JSON strings
+ * stay as written.
+ */
+function collapseFormattingWhitespace(source: string): string {
+  let collapsed = '';
+  let inString = false;
+  let escaped = false;
+  let pendingSpace = false;
+
+  for (let index = 0; index < source.length; index += 1) {
+    const char = source[index];
+    if (inString) {
+      collapsed += char;
+      if (escaped) {
+        escaped = false;
+      } else if (char === '\\') {
+        escaped = true;
+      } else if (char === '"') {
+        inString = false;
+      }
+      continue;
+    }
+    if (isFormattingWhitespace(char)) {
+      pendingSpace = collapsed.length > 0;
+      continue;
+    }
+    if (char === '"') {
+      inString = true;
+    }
+    if (pendingSpace) {
+      collapsed += ' ';
+      pendingSpace = false;
+    }
+    collapsed += char;
+  }
+  return collapsed.trim();
+}
+
+// Deep enough to cover real payloads, and shallow enough that the later
+// JSON.stringify cannot overflow the call stack.
+const PREVIEW_MAX_DEPTH = 1_000;
+
+function exceedsPreviewBudget(value: JsonContainer): boolean {
+  let left = MAX_JSON_CELL_LENGTH;
+  const seen = new Set<object>();
+  const stack: Array<{ node: unknown; depth: number }> = [
+    { node: value, depth: 0 },
+  ];
+
+  while (stack.length > 0) {
+    const current = stack.pop();
+    if (!current) {
+      break;
+    }
+    const { node, depth } = current;
+    if (depth > PREVIEW_MAX_DEPTH) {
+      return true;
+    }
+    if (typeof node === 'string') {
+      left -= node.length;
+      if (left < 0) {
+        return true;
+      }
+      continue;
+    }
+    if (node === null || typeof node !== 'object') {
+      continue;
+    }
+    if (seen.has(node)) {
+      return true;
+    }
+    seen.add(node);
+    if (Array.isArray(node)) {
+      left -= node.length;
+      if (left < 0) {
+        return true;
+      }
+      for (let index = node.length - 1; index >= 0; index -= 1) {
+        stack.push({ node: node[index], depth: depth + 1 });
+      }
+      continue;
+    }
+    if (!isPlainJsonObject(node)) {
+      continue;
+    }
+    const keys = Object.keys(node);
+    left -= keys.length;
+    if (left < 0) {
+      return true;
+    }
+    for (let index = keys.length - 1; index >= 0; index -= 1) {
+      const key = keys[index];
+      if (key !== undefined) {
+        stack.push({ node: node[key], depth: depth + 1 });
+      }

Review Comment:
   <!-- Bito Reply -->
   The suggestion is correct. In the provided code, `keys[index]` is guaranteed 
to be a string because `Object.keys()` returns an array of strings and 
`noUncheckedIndexedAccess` is not enabled. Therefore, the `key !== undefined` 
check is redundant and can be safely removed to simplify the code.
   
   
**superset-frontend/plugins/plugin-chart-ag-grid-table/src/renderers/parseJsonCellValue.ts**
   ```
   for (let index = keys.length - 1; index >= 0; index -= 1) {
         const key = keys[index];
         stack.push({ node: node[key], depth: depth + 1 });
       }
   ```



-- 
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