Skip to content

Commit

Permalink
Improve argument count linting
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
valtzu committed Feb 2, 2025
1 parent 165c46b commit d34abd6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

0.10
---

* Exclude optional arguments when linting argument count
* Lint minimum argument count

0.9
---

Expand Down
14 changes: 11 additions & 3 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ export const expressionLanguageLinterSource = (state: EditorState) => {
if (!args) {
return;
}
const argCountMin = args.reduce((count, arg) => count + Number(!arg.optional), 0);
const argCountMax = args.length;
const argumentCountHintFn = () => `<code>${fn.name}</code> takes ${argCountMin == argCountMax ? `exactly ${argCountMax}` : `${argCountMin}${argCountMax}`} argument${argCountMax == 1 ? '' : 's'}`;
let i = 0;

for (let n = node.node.firstChild, i = 0; n != null; n = n.nextSibling) {
for (let n = node.node.firstChild; n != null; n = n.nextSibling) {
if (n.type.is(BlockComment)) {
continue;
}

if (i > args.length - 1) {
diagnostics.push({ from: n.from, to: n.to, severity: 'warning', message: `Unexpected argument – <code>${fn.name}</code> takes ${args.length} argument${args.length == 1 ? '' : 's'}` });
if (i > argCountMax - 1) {
diagnostics.push({ from: n.from, to: n.to, severity: 'warning', message: `Unexpected argument – ${argumentCountHintFn()}` });
continue;
}

Expand All @@ -58,6 +62,10 @@ export const expressionLanguageLinterSource = (state: EditorState) => {
i++;
}

if (i < argCountMin) {
diagnostics.push({ from: node.from, to: node.to, severity: 'error', message: `Too few arguments – ${argumentCountHintFn()}` });
}

break;
case Property:
case Method:
Expand Down
29 changes: 27 additions & 2 deletions test/test-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const config = {
],
functions: [
{name: "smh", args: [], returnType: ["string"]},
{name: "any_fn", args: [{name: "anything", type: ["any"]}], returnType: ["any"]},
{name: "any_fn", args: [{name: "anything", type: ["any"]}, {name: "optional", optional: true}], returnType: ["any"]},
{name: "smash_my_head", args: [{name: "object", type: ["object"]}]},
{name: "getObject", returnType: ["custom44"]},
],
Expand Down Expand Up @@ -92,7 +92,32 @@ describe("Expression language linting", () => {
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 18);
ist(diagnostics[0].to, 19);
ist(diagnostics[0].message, "Unexpected argument – <code>smash_my_head</code> takes 1 argument");
ist(diagnostics[0].message, "Unexpected argument – <code>smash_my_head</code> takes exactly 1 argument");
});

it("complains about too many arguments when there are optional arguments", () => {
const diagnostics = get("any_fn(1, 2, 3)");

ist(diagnostics.length, 1);
ist(diagnostics[0].from, 13);
ist(diagnostics[0].to, 14);
ist(diagnostics[0].message, "Unexpected argument – <code>any_fn</code> takes 1–2 arguments");
});

it("complains about too few arguments", () => {
const diagnostics = get("smash_my_head()");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 13);
ist(diagnostics[0].to, 15);
ist(diagnostics[0].message, "Too few arguments – <code>smash_my_head</code> takes exactly 1 argument");
});

it("complains about too few arguments when there are optional arguments", () => {
const diagnostics = get("any_fn()");
ist(diagnostics.length, 1);
ist(diagnostics[0].from, 6);
ist(diagnostics[0].to, 8);
ist(diagnostics[0].message, "Too few arguments – <code>any_fn</code> takes 1–2 arguments");
});

it("complains about wrong argument type using constant", () => {
Expand Down

0 comments on commit d34abd6

Please sign in to comment.