Skip to content

Commit 2699d7f

Browse files
authored
fix: wrong diagnostics for non valid public properties (#877)
* Fix bugg in alg variable diagnostics * Update pnpm script to new package name * Add tests to alg visitor * Fix minor unused code
1 parent b90a081 commit 2699d7f

5 files changed

Lines changed: 55 additions & 70 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
"packages/*"
2929
],
3030
"scripts": {
31-
"dev": "pnpm --filter septic watch"
31+
"dev": "pnpm --filter septic-config-lib watch"
3232
}
3333
}

packages/septic/src/alg.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,8 @@ export interface IAlgVisitor {
407407
export class AlgVisitor implements IAlgVisitor {
408408
calcs: AlgCalc[] = [];
409409
variables: AlgLiteral[] = [];
410-
private ignoreIdentifierCalcParams;
411410

412-
constructor(ignoreIdentifierCalcParams: boolean = false) {
413-
this.ignoreIdentifierCalcParams = ignoreIdentifierCalcParams;
414-
}
411+
constructor() {}
415412

416413
visit(expr: AlgExpr) {
417414
expr.accept(this);
@@ -435,13 +432,6 @@ export class AlgVisitor implements IAlgVisitor {
435432
visitCalc(expr: AlgCalc): any {
436433
this.calcs.push(expr);
437434
for (const param of expr.params) {
438-
if (
439-
this.ignoreIdentifierCalcParams &&
440-
param instanceof AlgLiteral &&
441-
param.type === AlgTokenType.identifier
442-
) {
443-
continue;
444-
}
445435
param.accept(this);
446436
}
447437
}

packages/septic/src/diagnostics.ts

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export function validateAlg(
278278
diagnostics.push(diagnostic);
279279
return diagnostics;
280280
}
281-
const visitor = new AlgVisitor(true);
281+
const visitor = new AlgVisitor();
282282
visitor.visit(expr);
283283
visitor.calcs.forEach((calc) => {
284284
diagnostics.push(
@@ -289,9 +289,8 @@ export function validateAlg(
289289
diagnostics.push(
290290
...validateAlgVariable(
291291
variable,
292-
doc,
293292
contextProvider,
294-
offsetStartAlg,
293+
algPositionTransformer,
295294
),
296295
);
297296
});
@@ -300,9 +299,8 @@ export function validateAlg(
300299

301300
export function validateAlgVariable(
302301
variable: AlgLiteral,
303-
doc: TextDocument,
304302
contextProvider: SepticContext,
305-
offsetStartAlg: number,
303+
algPositionTransformer: AlgPositionTransformer,
306304
): SepticDiagnostic[] {
307305
if (isPureJinja(variable.value)) {
308306
return [];
@@ -317,10 +315,7 @@ export function validateAlgVariable(
317315
return [
318316
createDiagnostic(
319317
SepticDiagnosticLevel.warning,
320-
{
321-
start: doc.positionAt(offsetStartAlg + variable.start),
322-
end: doc.positionAt(offsetStartAlg + variable.end),
323-
},
318+
algPositionTransformer(variable.start, variable.end),
324319
`Reference to undefined variable: ${variable.value}`,
325320
SepticDiagnosticCode.missingReference,
326321
),
@@ -333,10 +328,7 @@ export function validateAlgVariable(
333328
return [
334329
createDiagnostic(
335330
SepticDiagnosticLevel.error,
336-
{
337-
start: doc.positionAt(offsetStartAlg + variable.end - 1),
338-
end: doc.positionAt(offsetStartAlg + variable.end),
339-
},
331+
algPositionTransformer(variable.end - 1, variable.end),
340332
`Missing public property for variable`,
341333
SepticDiagnosticCode.missingPublicProperty,
342334
),
@@ -358,21 +350,15 @@ export function validateAlgVariable(
358350
return [
359351
createDiagnostic(
360352
SepticDiagnosticLevel.error,
361-
{
362-
start: doc.positionAt(
363-
offsetStartAlg +
364-
variable.start +
365-
variableParts[0]!.length +
366-
1,
367-
),
368-
end: doc.positionAt(offsetStartAlg + variable.end),
369-
},
353+
algPositionTransformer(
354+
variable.end - variableParts[1]!.length,
355+
variable.end,
356+
),
370357
`Unknown public property ${variableParts[1]} for ${referencedObjects[0]!.type}'`,
371358
SepticDiagnosticCode.unknownPublicProperty,
372359
),
373360
];
374361
}
375-
376362
return [];
377363
}
378364

@@ -593,11 +579,7 @@ function validateParamType(
593579
algPositionTransformer: AlgPositionTransformer,
594580
): SepticDiagnostic[] {
595581
if (types[0]!.startsWith("value")) {
596-
return validateValueParamType(
597-
expr,
598-
contextProvider,
599-
algPositionTransformer,
600-
);
582+
return [];
601583
}
602584
return validateObjectParamType(
603585
expr,
@@ -642,36 +624,6 @@ function validateObjectParamType(
642624
];
643625
}
644626

645-
function validateValueParamType(
646-
expr: AlgExpr,
647-
contextProvider: SepticContext,
648-
algPositionTransformer: AlgPositionTransformer,
649-
): SepticDiagnostic[] {
650-
if (!isAlgExprObjectReference(expr)) {
651-
return [];
652-
}
653-
const exprLiteral = expr as AlgLiteral;
654-
if (isPureJinja(exprLiteral.value)) {
655-
return [];
656-
}
657-
if (
658-
contextProvider.validateReferences(
659-
exprLiteral.value.split(".")[0]!,
660-
defaultRefValidationFunction,
661-
)
662-
) {
663-
return [];
664-
}
665-
return [
666-
createDiagnostic(
667-
SepticDiagnosticLevel.warning,
668-
algPositionTransformer(expr.start, expr.end),
669-
`Reference to undefined variable: ${exprLiteral.value}`,
670-
SepticDiagnosticCode.missingReference,
671-
),
672-
];
673-
}
674-
675627
function isAlgExprObjectReference(expr: AlgExpr) {
676628
return expr instanceof AlgLiteral && expr.type === AlgTokenType.identifier;
677629
}

packages/septic/src/test/algParser.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
AlgLiteral,
99
parseAlg,
1010
AlgUnary,
11+
AlgVisitor,
1112
} from "../alg";
1213

1314
describe("Test scanning of operators", () => {
@@ -462,3 +463,30 @@ describe("Test parsing of invalid algs", () => {
462463
expect(parse).to.not.throw();
463464
});
464465
});
466+
467+
describe("Test algVisitor", () => {
468+
it("Test extraction of variables in alg", () => {
469+
const input =
470+
"setmaxdn({{ Wellname }}ZpcNew, -abs({{ Wellname }}ZpcYWCD.Tests)/SPM)";
471+
const expr = parseAlg(input);
472+
const visitor = new AlgVisitor();
473+
visitor.visit(expr);
474+
expect(visitor.variables.length).to.equal(3);
475+
const variableNames = visitor.variables.map((v) => v.value);
476+
expect(variableNames).to.include("{{Wellname}}ZpcNew");
477+
expect(variableNames).to.include("{{Wellname}}ZpcYWCD.Tests");
478+
expect(variableNames).to.include("SPM");
479+
});
480+
it("Test extraction of variables in alg", () => {
481+
const input =
482+
"setmaxdn({{ Wellname }}ZpcNew, -abs({{ Wellname }}ZpcYWCD.Tests)/SPM) + min(10, -10)";
483+
const expr = parseAlg(input);
484+
const visitor = new AlgVisitor();
485+
visitor.visit(expr);
486+
expect(visitor.calcs.length).to.equal(3);
487+
const calcNames = visitor.calcs.map((v) => v.identifier);
488+
expect(calcNames).to.include("setmaxdn");
489+
expect(calcNames).to.include("abs");
490+
expect(calcNames).to.include("min");
491+
});
492+
});

packages/septic/src/test/diagnostics.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ describe("Test diagnostics for references in algs", () => {
197197
SepticDiagnosticCode.unknownPublicProperty,
198198
);
199199
});
200+
it("Expect diagnostics for unknown property when variable is first param in calc", () => {
201+
const text = `
202+
Mvr: TestMvr
203+
CalcPvr: TestCalcPvr
204+
Text1= "Test"
205+
Alg= "-abs(TestMvr.Tests)"
206+
`;
207+
208+
const cnfg = parseSepticForTest(text);
209+
const diag = validateAlgs(cnfg, cnfg);
210+
expect(diag.length).to.equal(1);
211+
expect(diag[0]!.code).to.equal(
212+
SepticDiagnosticCode.unknownPublicProperty,
213+
);
214+
});
200215
});
201216

202217
describe("Test datatype diagnostics in algs", () => {

0 commit comments

Comments
 (0)