Skip to content

Commit 318799f

Browse files
committed
fix(apidom-ls): fix partial keys identification
1 parent 9181926 commit 318799f

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

packages/apidom-ls/src/config/asyncapi/config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@ import httpMessageBindingMeta from './http-message-binding/meta';
2323
import kafkaMessageBindingMeta from './kafka-message-binding/meta';
2424
import messageMeta from './message/meta';
2525
import componentsMeta from './components/meta';
26+
import ApilintCodes from '../codes';
2627

2728
export default {
2829
'*': {
29-
lint: [],
30+
lint: [
31+
{
32+
code: ApilintCodes.DUPLICATE_KEYS,
33+
source: 'apilint',
34+
message: 'an object cannot contain duplicate keys',
35+
severity: 1,
36+
linterFunction: 'apilintNoDuplicateKeys',
37+
marker: 'key',
38+
},
39+
],
3040
},
3141
info: infoMeta,
3242
contact: contactMeta,

packages/apidom-ls/src/config/codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ const ApilintCodes = {
218218
COMPONENTS_CHANNELBINDINGS: (code += 1),
219219
COMPONENTS_OPERATIONBINDINGS: (code += 1),
220220
COMPONENTS_MESSAGEBINDINGS: (code += 1),
221+
DUPLICATE_KEYS: (code += 1),
221222
};
222223

223224
export default ApilintCodes;

packages/apidom-ls/src/services/completion/completion-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ export class DefaultCompletionService implements CompletionService {
260260
}
261261

262262
const offset = textDocument.offsetAt(position);
263+
debug('doCompletion - position and offset', position, offset);
264+
trace('doCompletion - text', text);
263265

264266
// if no spec version has been set, provide completion for it anyway
265267
// TODO handle also JSON, must identify offset
@@ -319,6 +321,7 @@ export class DefaultCompletionService implements CompletionService {
319321
textModified = true;
320322
}
321323
}
324+
trace('doCompletion - processedText first', processedText);
322325
perfStart(PerfLabels.PARSE_FIRST);
323326
let result = await this.settings?.documentCache?.get(
324327
textDocument,
@@ -331,6 +334,7 @@ export class DefaultCompletionService implements CompletionService {
331334
perfStart(PerfLabels.CORRECT_PARTIAL);
332335
debug('doCompletion - correctPartialKeys');
333336
processedText = correctPartialKeys(result, textDocument, isJson);
337+
trace('doCompletion - processedText second', processedText);
334338
perfEnd(PerfLabels.CORRECT_PARTIAL);
335339
if (processedText) {
336340
debug('doCompletion - parsing processedText');

packages/apidom-ls/src/services/validation/linter-functions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,16 @@ export const standardLinterfunctions: FunctionItem[] = [
725725
return true;
726726
},
727727
},
728+
{
729+
functionName: 'apilintNoDuplicateKeys',
730+
function: (element: Element): boolean => {
731+
if (element && isObject(element)) {
732+
const keys = element.keys() as string[];
733+
if (keys.length !== new Set(keys).size) {
734+
return false;
735+
}
736+
}
737+
return true;
738+
},
739+
},
728740
];

packages/apidom-ls/src/utils/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ export function isPartialKey(textDocument: TextDocument, offset: number): number
613613
lineNonEmptyContent.length > 0 &&
614614
!lineNonEmptyContent.startsWith('-') &&
615615
!lineNonEmptyContent.endsWith(':') &&
616+
!(lineNonEmptyContent.indexOf(':') > -1 && !lineNonEmptyContent.endsWith(':')) && // must not be a correct key/value
616617
(prevIndent < lineIndent || nextIndent < prevIndent)
617618
) {
618619
return lineContentRange ? textDocument.offsetAt(lineContentRange.end) : undefined;

0 commit comments

Comments
 (0)