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
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
37 changes: 36 additions & 1 deletion src/utilities/codemirror/codemirror-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { describe, expect, it } from 'vitest';
import { isQuoted, quoteEscape, removeEscapedQuotes, unquoteUnescape } from './codemirror-utils';
import {
isHexValue,
isQuoted,
parseNumericArg,
quoteEscape,
removeEscapedQuotes,
unquoteUnescape,
} from './codemirror-utils';

describe(`'Escaped quotes' from input`, () => {
it('Should remove escaped quotes surrounding a string', () => {
Expand Down Expand Up @@ -82,3 +89,31 @@ describe('quoteEscape', () => {
});
});
});

describe('parseNumericArg', function () {
it("should parse 'float' and 'numeric' args as floats", function () {
expect(parseNumericArg('1.23', 'float')).toEqual(1.23);
expect(parseNumericArg('2.34', 'numeric')).toEqual(2.34);
expect(parseNumericArg('bad', 'float')).toEqual(NaN);
// can't parse hex numbers as float
expect(parseNumericArg('0xabc', 'float')).toEqual(0);
});
it("should parse 'integer' and 'unsigned' args as integers", function () {
expect(parseNumericArg('123', 'integer')).toEqual(123);
expect(parseNumericArg('234', 'unsigned')).toEqual(234);
expect(parseNumericArg('234.567', 'integer')).toEqual(234);
// can parse hex integers
expect(parseNumericArg('0xff', 'integer')).toEqual(255);
expect(parseNumericArg('0x1f', 'unsigned')).toEqual(31);
});
});
describe('isHexValue', function () {
it('should correctly identify a hex number string', function () {
expect(isHexValue('12')).toBe(false);
expect(isHexValue('ff')).toBe(false);
expect(isHexValue('0x99')).toBe(true);
expect(isHexValue('0xdeadBEEF')).toBe(true);
expect(isHexValue('0x12ab')).toBe(true);
expect(isHexValue('0x12xx')).toBe(false);
});
});
13 changes: 13 additions & 0 deletions src/utilities/codemirror/codemirror-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,16 @@ export function removeEscapedQuotes(text: string | number | boolean): string | n
}
return text;
}

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

export function isHexValue(argText: string) {
return /^0x[\da-f]+$/i.test(argText);
}
11 changes: 6 additions & 5 deletions src/utilities/sequence-editor/sequence-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { TOKEN_COMMAND, TOKEN_ERROR, TOKEN_REPEAT_ARG, TOKEN_REQUEST } from '../
import { TimeTypes } from '../../enums/time';
import { getGlobals, sequenceAdaptation } from '../../stores/sequence-adaptation';
import { CustomErrorCodes } from '../../workers/customCodes';
import { addDefaultArgs, quoteEscape } from '../codemirror/codemirror-utils';
import { addDefaultArgs, quoteEscape, parseNumericArg, isHexValue } from '../codemirror/codemirror-utils';
import {
getBalancedDuration,
getDoyTime,
Expand Down Expand Up @@ -1164,13 +1164,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