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

Add linter to check Expression button sets #1531

Merged
merged 3 commits into from
Aug 22, 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
5 changes: 5 additions & 0 deletions .changeset/fluffy-otters-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus-linter": minor
---

Add expression-widget lint rule
115 changes: 115 additions & 0 deletions packages/perseus-linter/src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import absoluteUrlRule from "../rules/absolute-url";
import blockquotedMathRule from "../rules/blockquoted-math";
import blockquotedWidgetRule from "../rules/blockquoted-widget";
import doubleSpacingAfterTerminalRule from "../rules/double-spacing-after-terminal";
import expressionWidgetRule from "../rules/expression-widget";
import extraContentSpacingRule from "../rules/extra-content-spacing";
import headingLevel1Rule from "../rules/heading-level-1";
import headingLevelSkipRule from "../rules/heading-level-skip";
Expand Down Expand Up @@ -507,6 +508,120 @@ describe("Individual lint rules tests", () => {
},
});

expectWarning(expressionWidgetRule, "[[☃ expression 1]]", {
widgets: {
"expression 1": {
options: {
answerForms: [
{
value: "\\sqrt{42}",
form: true,
simplify: true,
considered: "correct",
key: "0",
},
],
buttonSets: ["basic"],
},
},
},
});

expectPass(expressionWidgetRule, "[[☃ expression 1]]", {
widgets: {
"expression 1": {
options: {
answerForms: [
{
value: "\\sqrt{42}",
form: true,
simplify: true,
considered: "correct",
key: "0",
},
],
buttonSets: ["basic", "prealgebra"],
},
},
},
});

expectWarning(expressionWidgetRule, "[[☃ expression 1]]", {
widgets: {
"expression 1": {
options: {
answerForms: [
{
value: "\\sin\\left(42\\right)",
form: true,
simplify: true,
considered: "correct",
key: "0",
},
],
buttonSets: ["basic"],
},
},
},
});

expectPass(expressionWidgetRule, "[[☃ expression 1]]", {
widgets: {
"expression 1": {
options: {
answerForms: [
{
value: "\\sin\\left(42\\right)",
form: true,
simplify: true,
considered: "correct",
key: "0",
},
],
buttonSets: ["basic", "trig"],
},
},
},
});

expectWarning(expressionWidgetRule, "[[☃ expression 1]]", {
widgets: {
"expression 1": {
options: {
answerForms: [
{
value: "\\log\\left(5\\right)",
form: true,
simplify: true,
considered: "correct",
key: "0",
},
],
buttonSets: ["basic"],
},
},
},
});

expectPass(expressionWidgetRule, "[[☃ expression 1]]", {
widgets: {
"expression 1": {
options: {
answerForms: [
{
value: "\\log\\left(5\\right)",
form: true,
simplify: true,
considered: "correct",
key: "0",
},
],
buttonSets: ["basic", "logarithms"],
},
},
},
});

// @ts-expect-error - TS2554 - Expected 3 arguments, but got 2.
expectWarning(doubleSpacingAfterTerminalRule, [
"Good times. Great oldies.",
Expand Down
2 changes: 2 additions & 0 deletions packages/perseus-linter/src/rules/all-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AbsoluteUrl from "./absolute-url";
import BlockquotedMath from "./blockquoted-math";
import BlockquotedWidget from "./blockquoted-widget";
import DoubleSpacingAfterTerminal from "./double-spacing-after-terminal";
import ExpressionWidget from "./expression-widget";
import ExtraContentSpacing from "./extra-content-spacing";
import HeadingLevel1 from "./heading-level-1";
import HeadingLevelSkip from "./heading-level-skip";
Expand Down Expand Up @@ -40,6 +41,7 @@ export default [
BlockquotedMath,
BlockquotedWidget,
DoubleSpacingAfterTerminal,
ExpressionWidget,
ExtraContentSpacing,
HeadingLevel1,
HeadingLevelSkip,
Expand Down
48 changes: 48 additions & 0 deletions packages/perseus-linter/src/rules/expression-widget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Rule from "../rule";

function buttonNotInButtonSet(name: string, set: string): string {
return `Answer requires a button not found in the button sets: ${name} (in ${set})`;
}

const stringToButtonSet = {
"\\sqrt": "prealgebra",
"\\sin": "trig",
"\\cos": "trig",
"\\tan": "trig",
"\\log": "logarithms",
"\\ln": "logarithms",
};

/**
* Rule to make sure that Expression questions that require
* a specific math symbol to answer have that math symbol
* available in the keypad (desktop learners can use a keyboard,
* but mobile learners must use the MathInput keypad)
*/
export default Rule.makeRule({
name: "expression-widget",
severity: Rule.Severity.WARNING,
selector: "widget",
lint: function (state, content, nodes, match, context) {
// This rule only looks at image widgets
if (state.currentNode().widgetType !== "expression") {
return;
}

// If it can't find a definition for the widget it does nothing
const widget = context?.widgets?.[state.currentNode().id];
if (!widget) {
return;
}

Check warning on line 36 in packages/perseus-linter/src/rules/expression-widget.ts

View check run for this annotation

Codecov / codecov/patch

packages/perseus-linter/src/rules/expression-widget.ts#L35-L36

Added lines #L35 - L36 were not covered by tests

const answers = widget.options.answerForms;
const buttons = widget.options.buttonSets;
for (const answer of answers) {
for (const [str, set] of Object.entries(stringToButtonSet)) {
if (answer.value.includes(str) && !buttons.includes(set)) {
return buttonNotInButtonSet(str, set);
}
}
}
},
}) as Rule;