Improve null checks and object handling in config.html

Replaces optional chaining with explicit null checks and more robust object property access throughout the file. Refactors destructuring and object spreading to use Object.assign for better compatibility. These changes improve code reliability and compatibility with older browsers or environments lacking support for newer JavaScript features.
This commit is contained in:
Sergey Krashevich
2025-12-27 09:09:32 +03:00
parent 4c50a2c00c
commit 3bd433c950
+18 -14
View File
@@ -204,7 +204,7 @@
if (indentIndex >= line.length) return null; if (indentIndex >= line.length) return null;
let i = indentIndex; const i = indentIndex;
let key = ''; let key = '';
let rawKey = ''; let rawKey = '';
let isQuoted = false; let isQuoted = false;
@@ -275,7 +275,7 @@
keyEndIndex = i + rawKey.length; keyEndIndex = i + rawKey.length;
} }
const after = line.slice(colonIndex + 1) ?? ''; const after = line.slice(colonIndex + 1);
const isContainer = after.trim() === '' || after.trim().startsWith('#'); const isContainer = after.trim() === '' || after.trim().startsWith('#');
const valueStartIndex = colonIndex + 1; const valueStartIndex = colonIndex + 1;
@@ -301,11 +301,12 @@
if (seen.has(ref)) return schema; if (seen.has(ref)) return schema;
seen.add(ref); seen.add(ref);
const name = ref.slice('#/definitions/'.length); const name = ref.slice('#/definitions/'.length);
const def = schemaRoot.definitions?.[name]; const def = schemaRoot.definitions && schemaRoot.definitions[name];
if (!def) return schema; if (!def) return schema;
const resolved = resolveRef(def, seen); const resolved = resolveRef(def, seen);
const {$ref, ...rest} = schema; const rest = Object.assign({}, schema);
return {...resolved, ...rest}; delete rest.$ref;
return Object.assign({}, resolved, rest);
} }
} }
return schema; return schema;
@@ -466,7 +467,8 @@
let blockScalarParentIndent = null; let blockScalarParentIndent = null;
const isBlockScalarHeader = (text) => { const isBlockScalarHeader = (text) => {
const t = (text ?? '').trimStart(); const rawText = (text == null) ? '' : text;
const t = rawText.trimStart ? rawText.trimStart() : rawText.replace(/^\s+/, '');
return t.startsWith('|') || t.startsWith('>'); return t.startsWith('|') || t.startsWith('>');
}; };
@@ -495,14 +497,16 @@
}); });
}; };
if (window.jsyaml?.load) { if (window.jsyaml && window.jsyaml.load) {
try { try {
window.jsyaml.load(model.getValue()); window.jsyaml.load(model.getValue());
} catch (e) { } catch (e) {
const mark = e?.mark || {}; const mark = (e && e.mark) || {};
const line = typeof mark.line === 'number' ? mark.line + 1 : 1; const line = typeof mark.line === 'number' ? mark.line + 1 : 1;
const column = typeof mark.column === 'number' ? mark.column + 1 : 1; const column = typeof mark.column === 'number' ? mark.column + 1 : 1;
markLineError(line, e?.reason ? `YAML: ${e.reason}` : `YAML: ${e?.message || 'Invalid YAML'}`, column); const reason = e && e.reason;
const messageText = e && e.message;
markLineError(line, reason ? `YAML: ${reason}` : `YAML: ${messageText || 'Invalid YAML'}`, column);
} }
} }
@@ -520,7 +524,7 @@
const stack = [{ const stack = [{
indent: -1, indent: -1,
schema: schemaTools?.schemaRoot || null, schema: (schemaTools && schemaTools.schemaRoot) || null,
expected: 'object', expected: 'object',
actual: null, actual: null,
keys: new Map(), keys: new Map(),
@@ -856,8 +860,8 @@
const lineNoCommentTrimmedEnd = lineNoComment.trimEnd(); const lineNoCommentTrimmedEnd = lineNoComment.trimEnd();
const listItem = parseListItem(lineNoCommentTrimmedEnd); const listItem = parseListItem(lineNoCommentTrimmedEnd);
const {word, startColumn, endColumn} = model.getWordUntilPosition(position); const wordUntil = model.getWordUntilPosition(position);
const range = new monaco.Range(position.lineNumber, startColumn, position.lineNumber, endColumn); const range = new monaco.Range(position.lineNumber, wordUntil.startColumn, position.lineNumber, wordUntil.endColumn);
const cursorIndex = position.column - 1; const cursorIndex = position.column - 1;
let contentStartIndex = 0; let contentStartIndex = 0;
@@ -917,7 +921,7 @@
kind: monaco.languages.CompletionItemKind.Property, kind: monaco.languages.CompletionItemKind.Property,
insertText, insertText,
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet, insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: s?.description, documentation: s && s.description,
range, range,
}; };
}); });
@@ -989,7 +993,7 @@
if (!propSchema) return null; if (!propSchema) return null;
const resolved = resolveRef(propSchema); const resolved = resolveRef(propSchema);
const description = resolved?.description; const description = resolved && resolved.description;
if (!description) return null; if (!description) return null;
return { return {