Skip to content

Commit

Permalink
Add diagnostics for public properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vetlek committed Aug 10, 2023
1 parent 6f84042 commit bc49cba
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 22 deletions.
112 changes: 90 additions & 22 deletions server/src/language-service/diagnosticsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
SepticAttributeDocumentation,
SepticTokenType,
SepticObjectInfo,
SepticObjectHierarchy,
} from "../septic";
import { SettingsManager } from "../settings";
import { isPureJinja } from "../util";
Expand All @@ -49,6 +48,8 @@ export enum DiagnosticCode {
invalidNumberOfParams = "E204",
missingValueNonOptionalParam = "E205",
algMaxLength = "E206",
missingPublicProperty = "E207",
unknownPublicProperty = "E208",
missingListLengthValue = "E301",
mismatchLengthList = "E302",
missingAttributeValue = "E304",
Expand Down Expand Up @@ -211,32 +212,99 @@ export function validateAlgs(
);
});

for (let variable of visitor.variables) {
if (isPureJinja(variable.value)) {
continue;
}
if (
!refProvider.validateRef(
variable.value.split(".")[0],
defaultRefValidationFunction
visitor.variables.forEach((variable) => {
diagnostics.push(
...validateAlgVariable(
variable,
doc,
refProvider,
offsetStartAlg
)
) {
const diagnostic = createDiagnostic(
DiagnosticSeverity.Warning,
{
start: doc.positionAt(offsetStartAlg + variable.start),
end: doc.positionAt(offsetStartAlg + variable.end),
},
`Undefined Xvr '${variable.value}'`,
DiagnosticCode.missingReference
);
diagnostics.push(diagnostic);
}
}
);
});
}
return diagnostics;
}

export function validateAlgVariable(
variable: AlgLiteral,
doc: ITextDocument,
refProvider: SepticReferenceProvider,
offsetStartAlg: number
): Diagnostic[] {
if (isPureJinja(variable.value)) {
return [];
}
let variableParts = variable.value.split(".");
if (
!refProvider.validateRef(variableParts[0], defaultRefValidationFunction)
) {
return [
createDiagnostic(
DiagnosticSeverity.Warning,
{
start: doc.positionAt(offsetStartAlg + variable.start),
end: doc.positionAt(offsetStartAlg + variable.end),
},
`Undefined Xvr '${variable.value}'`,
DiagnosticCode.missingReference
),
];
}
if (variableParts.length === 1) {
return [];
}
if (variableParts[1] === "") {
return [
createDiagnostic(
DiagnosticSeverity.Error,
{
start: doc.positionAt(offsetStartAlg + variable.end - 1),
end: doc.positionAt(offsetStartAlg + variable.end),
},
`Missing public property for variable`,
DiagnosticCode.missingPublicProperty
),
];
}
const metaInfoProvider = SepticMetaInfoProvider.getInstance();
let referencedObjects = refProvider.getObjectsByIdentifier(
variableParts[0]
);
referencedObjects = referencedObjects.filter((obj) => obj.isXvr());
if (!referencedObjects.length) {
return [];
}
const publicAttributes = metaInfoProvider.getObjectDocumentation(
referencedObjects[0].type
)?.publicAttributes;

if (!publicAttributes) {
return [];
}

if (!publicAttributes.includes(variableParts[1])) {
return [
createDiagnostic(
DiagnosticSeverity.Error,
{
start: doc.positionAt(
offsetStartAlg +
variable.start +
variableParts[0].length +
1
),
end: doc.positionAt(offsetStartAlg + variable.end),
},
`Unknown public property ${variableParts[1]} for ${referencedObjects[0].type}'`,
DiagnosticCode.unknownPublicProperty
),
];
}

return [];
}

export function validateCalc(
calc: AlgCalc,
doc: ITextDocument,
Expand Down
44 changes: 44 additions & 0 deletions server/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,50 @@ describe("Test diagnostics for references in algs", () => {
let diag = validateAlgs(cnfg, doc, cnfg);
expect(diag.length).to.equal(0);
});
it("Expect no diagnostics for correctly used public property", () => {
const text = `
Mvr: TestMvr
CalcPvr: TestCalcPvr
Text1= "Test"
Alg= "TestMvr.SSVal"
`;

const doc = new MockDocument(text);

const cnfg = parseSeptic(doc.getText());
let diag = validateAlgs(cnfg, doc, cnfg);
expect(diag.length).to.equal(0);
});
it("Expect diagnostics for missing property after .", () => {
const text = `
Mvr: TestMvr
CalcPvr: TestCalcPvr
Text1= "Test"
Alg= "TestMvr."
`;

const doc = new MockDocument(text);

const cnfg = parseSeptic(doc.getText());
let diag = validateAlgs(cnfg, doc, cnfg);
expect(diag.length).to.equal(1);
expect(diag[0].code).to.equal(DiagnosticCode.missingPublicProperty);
});
it("Expect diagnostics for unknown property", () => {
const text = `
Mvr: TestMvr
CalcPvr: TestCalcPvr
Text1= "Test"
Alg= "TestMvr.BS"
`;

const doc = new MockDocument(text);

const cnfg = parseSeptic(doc.getText());
let diag = validateAlgs(cnfg, doc, cnfg);
expect(diag.length).to.equal(1);
expect(diag[0].code).to.equal(DiagnosticCode.unknownPublicProperty);
});
});

describe("Test parameter diagnostics in alg", () => {
Expand Down

0 comments on commit bc49cba

Please sign in to comment.