Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const messageIdUniqueLint: LinterMeta = {
message: "messageID' must be unique among all messages",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintPropertyUniqueValue',
linterParams: [['message', 'messageTrait'], 'messageId'],
linterParams: [['message', 'messageTrait'], 'messageId', 'propertyValues'],
marker: 'key',
markerTarget: 'messageId',
target: 'messageId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const messageIdUniqueLint: LinterMeta = {
message: "messageID' must be unique among all messages",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintPropertyUniqueValue',
linterParams: [['message', 'messageTrait'], 'messageId'],
linterParams: [['message', 'messageTrait'], 'messageId', 'propertyValues'],
marker: 'key',
markerTarget: 'messageId',
target: 'messageId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const operationIdUniqueLint: LinterMeta = {
message: "operationId' must be unique among all operations",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintPropertyUniqueValue',
linterParams: [['operation', 'operationTrait'], 'operationId'],
linterParams: [['operation', 'operationTrait'], 'operationId', 'propertyValues'],
marker: 'key',
markerTarget: 'operationId',
target: 'operationId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const operationIdUniqueLint: LinterMeta = {
message: "operationId' must be unique among all operations",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintPropertyUniqueValue',
linterParams: [['operation', 'operationTrait'], 'operationId'],
linterParams: [['operation', 'operationTrait'], 'operationId', 'propertyValues'],
marker: 'key',
markerTarget: 'operationId',
target: 'operationId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const $refNotUsedLint: LinterMeta = {
message: 'Definition was declared but never used in document',
severity: DiagnosticSeverity.Warning,
linterFunction: 'apilintReferenceNotUsed',
linterParams: ['string'],
linterParams: ['referenceNames'],
marker: 'key',
data: {},
targetSpecs: [...OpenAPI2, ...OpenAPI30],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const operationIdUniqueLint: LinterMeta = {
message: "operationId' must be unique among all operations",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintPropertyUniqueValue',
linterParams: [['operation'], 'operationId'],
linterParams: [['operation'], 'operationId', 'propertyValues'],
marker: 'key',
markerTarget: 'operationId',
target: 'operationId',
Expand Down
82 changes: 58 additions & 24 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
ObjectElement,
isArrayElement,
includesClasses,
isObjectElement,
traverse,
} from '@swagger-api/apidom-core';
import { URIFragmentIdentifier } from '@swagger-api/apidom-json-pointer/modern';
import { CompletionItem } from 'vscode-languageserver-types';
Expand Down Expand Up @@ -956,22 +958,42 @@ export const standardLinterfunctions: FunctionItem[] = [
},
{
functionName: 'apilintPropertyUniqueValue',
function: (element: Element, elementOrClasses: string[], key: string): boolean => {
function: (
element: Element,
elementOrClasses: string[],
key: string,
propertyValues: Map<string, string[]>,
): boolean => {
const api = root(element);
const value = toValue(element);
const elements: ArraySlice = filter((el: Element) => {
const classes: string[] = toValue(el.getMetaProperty('classes', []));
return (
(elementOrClasses.includes(el.element) ||
classes.every((v) => elementOrClasses.includes(v))) &&
isObject(el) &&
el.hasKey(key) &&
toValue(el.get(key)) === value
);
}, api);
if (elements.length > 1) {
const cacheKey = elementOrClasses.join(',');

if (!propertyValues.has(cacheKey)) {
traverse((el: Element) => {
const classes: ArrayElement = el.getMetaProperty('classes', []);
if (
(elementOrClasses.includes(el.element) ||
classes.filter((classElement: Element) =>
elementOrClasses.includes(toValue(classElement)),
).length === classes.length) &&
isObject(el) &&
el.hasKey(key)
) {
const elValue = toValue(el.get(key));
const cachedValues = propertyValues.get(cacheKey) ?? [];

cachedValues.push(elValue);
propertyValues.set(cacheKey, cachedValues);
}
}, api);
}

const cachedValues = propertyValues.get(cacheKey) ?? [];

if (cachedValues.filter((cachedValue) => cachedValue === value).length > 1) {
return false;
}

return true;
},
},
Expand Down Expand Up @@ -1327,7 +1349,7 @@ export const standardLinterfunctions: FunctionItem[] = [
},
{
functionName: 'apilintReferenceNotUsed',
function: (element: Element & { content?: { key?: string } }) => {
function: (element: Element & { content?: { key?: string } }, referenceNames: string[]) => {
const elParent: Element = element.parent?.parent?.parent?.parent;
if (
(typeof elParent?.hasKey !== 'function' || !elParent.hasKey('schemas')) &&
Expand All @@ -1336,17 +1358,29 @@ export const standardLinterfunctions: FunctionItem[] = [
return true;
}

const api = root(element);
const isReferenceElement = (el: Element & { content?: { key?: string } }) =>
toValue(el.content.key) === '$ref';
const referenceElements = filter((el) => {
return isReferenceElement(el);
}, api);
const referenceNames = referenceElements.map((refElement: Element) =>
// @ts-expect-error
toValue(refElement.content.value).split('/').at(-1),
);
// @ts-expect-error
if (referenceNames.length === 0) {
const api = root(element);
const isReferenceElement = (el: Element & { content?: { key?: string } }) => {
if (!isObjectElement(el) || !el.hasKey('$ref')) {
return false;
}

const $ref = el.get('$ref');

return isStringElement($ref) && toValue($ref).startsWith('#');
};

const referenceElements = filter((el) => {
return isReferenceElement(el);
}, api);

referenceNames.push(
...referenceElements.map((refElement: ObjectElement) =>
toValue(refElement.get('$ref')).split('/').at(-1),
),
);
}

return referenceNames.includes(toValue(element.parent.key));
},
},
Expand Down
21 changes: 20 additions & 1 deletion packages/apidom-ls/src/services/validation/validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class DefaultValidationService implements ValidationService {

private lintingRulesSemanticCache: Map<string, LinterMeta[]> = new Map();

private referenceNamesCache: string[] = [];

private propertyValuesCache: Map<string, string[]> = new Map();

public constructor() {
this.validationEnabled = true;
this.commentSeverity = undefined;
Expand Down Expand Up @@ -838,6 +842,9 @@ export class DefaultValidationService implements ValidationService {
}
}

this.referenceNamesCache = [];
this.propertyValuesCache.clear();

return diagnostics;
}

Expand Down Expand Up @@ -894,7 +901,19 @@ export class DefaultValidationService implements ValidationService {
Array.isArray(meta.linterParams) &&
meta.linterParams.length > 0
) {
const params = [targetElement].concat(meta.linterParams);
const params = [targetElement].concat(
meta.linterParams.map((param) => {
if (param === 'referenceNames') {
return this.referenceNamesCache;
}

if (param === 'propertyValues') {
return this.propertyValuesCache;
}

return param;
}),
);
lintRes = lintFunc(...params) as boolean;
} else {
lintRes = lintFunc(targetElement) as boolean;
Expand Down