Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint range of hex args #1465

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Changes from 1 commit
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
22 changes: 18 additions & 4 deletions src/utilities/sequence-editor/sequence-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ type VariableMap = {
[name: string]: VariableDeclaration;
};

function parseNumericArg(argText: string, dictArgType: 'float' | 'integer' | 'numeric' | 'unsigned') {
switch (dictArgType) {
case 'float':
case 'numeric':
return parseFloat(argText);
}
return parseInt(argText);
}

function isHexValue(argText: string) {
return /^0x[\da-f]+$/i.test(argText);
}

/**
* Linter function that returns a Code Mirror extension function.
* Can be optionally called with a command dictionary so it's available during linting.
Expand Down Expand Up @@ -1164,13 +1177,14 @@ export function sequenceLinter(
break;
}
const { max, min } = dictArg.range;
const nodeTextAsNumber = parseFloat(argText);

const nodeTextAsNumber = parseNumericArg(argText, dictArgType);
const isHex = isHexValue(argText);
if (nodeTextAsNumber < min || nodeTextAsNumber > max) {
const numFormat = (n: number) => (isHex ? `0x${n.toString(16).toUpperCase()}` : n);
const message =
max !== min
? `Number out of range. Range is between ${min} and ${max} inclusive.`
: `Number out of range. Range is ${min}.`;
? `Number out of range. Range is between ${numFormat(min)} and ${numFormat(max)} inclusive.`
: `Number out of range. Range is ${numFormat(min)}.`;
diagnostics.push({
actions:
max === min
Expand Down
Loading