Skip to content

Commit

Permalink
Add positivity validation for line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-oq committed Mar 23, 2023
1 parent 1bc5d7f commit bff6d25
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions libs/extensions/std/lang/src/text-range-selector-meta-inf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { strict as assert } from 'assert';

import {
BlockMetaInformation,
IOType,
PropertyAssignment,
PropertyValuetype,
isNumericLiteral,
} from '@jvalue/language-server';
import { ValidationAcceptor } from 'langium';

export class TextRangeSelectorMetaInformation extends BlockMetaInformation {
constructor() {
Expand All @@ -12,10 +17,12 @@ export class TextRangeSelectorMetaInformation extends BlockMetaInformation {
lineFrom: {
type: PropertyValuetype.INTEGER,
defaultValue: 1,
validation: greaterThanZeroValidation,
},
lineTo: {
type: PropertyValuetype.INTEGER,
defaultValue: Number.POSITIVE_INFINITY,
validation: greaterThanZeroValidation,
},
},
// Input type:
Expand All @@ -27,3 +34,17 @@ export class TextRangeSelectorMetaInformation extends BlockMetaInformation {
this.docs.description = 'Selects a range of lines from a `TextFile`.';
}
}

function greaterThanZeroValidation(
property: PropertyAssignment,
accept: ValidationAcceptor,
) {
const propertyValue = property.value;
assert(isNumericLiteral(propertyValue));

if (propertyValue.value <= 0) {
accept('error', `Line numbers need to be greater than zero`, {
node: propertyValue,
});
}
}

0 comments on commit bff6d25

Please sign in to comment.