@khanacademy/perseus-linter 7.0.1 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,17 @@
1
- import type { LinterWarning } from "./rule";
1
+ import type { LinterIssue } from "./rule";
2
2
  import type { PerseusItem, PerseusArticle, PerseusRenderer } from "@khanacademy/perseus-core";
3
- export type LinterWarningItem = {
4
- question: LinterWarning[];
5
- hints: LinterWarning[][];
3
+ export type ItemLinterIssues = {
4
+ question: LinterIssue[];
5
+ hints: LinterIssue[][];
6
6
  };
7
7
  /**
8
8
  * Lint a full Perseus item, including the question and all hints.
9
9
  *
10
10
  * @param parsedPerseusItem - A parsed PerseusItem, typically the output of
11
11
  * `parseAndMigratePerseusItem`.
12
- * @returns Question warnings plus per-hint warning arrays.
12
+ * @returns Question issues plus per-hint issue arrays.
13
13
  */
14
- export declare function lintPerseusItem(parsedPerseusItem: PerseusItem): LinterWarningItem;
14
+ export declare function lintPerseusItem(parsedPerseusItem: PerseusItem): ItemLinterIssues;
15
15
  /**
16
16
  * Lint a Perseus article.
17
17
  *
@@ -19,15 +19,15 @@ export declare function lintPerseusItem(parsedPerseusItem: PerseusItem): LinterW
19
19
  * has length 1.
20
20
  * @param parsedPerseusArticle - A parsed PerseusArticle, typically the output
21
21
  * of `parseAndMigratePerseusArticle`.
22
- * @returns A nested array with one array of lint warnings per article section
22
+ * @returns A nested array with one array of linter issues per article section
23
23
  */
24
- export declare function lintPerseusArticle(parsedPerseusArticle: PerseusArticle): LinterWarning[][];
24
+ export declare function lintPerseusArticle(parsedPerseusArticle: PerseusArticle): LinterIssue[][];
25
25
  /**
26
26
  * Lint a single Perseus renderer block by parsing its markdown content and
27
27
  * running the standard lint rules against it.
28
28
  *
29
29
  * @param parsedPerseusRenderer - Parsed renderer (content and widgets).
30
30
  * @param contentType - Lint context: article, exercise, or renderer-only.
31
- * @returns An array of lint warnings found in the content.
31
+ * @returns An array of lint issues found in the content.
32
32
  */
33
- export declare function lintPerseusRenderer(parsedPerseusRenderer: PerseusRenderer, contentType: "article" | "exercise" | "renderer"): LinterWarning[];
33
+ export declare function lintPerseusRenderer(parsedPerseusRenderer: PerseusRenderer, contentType: "article" | "exercise" | "renderer"): LinterIssue[];
package/dist/lint.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ export interface LintOptions {
2
+ /** Indices into the content string where the issue occurs */
3
+ location?: {
4
+ start: number;
5
+ end: number;
6
+ };
7
+ metadata?: Record<string, any>;
8
+ }
9
+ export declare const Severity: {
10
+ ERROR: number;
11
+ WARNING: number;
12
+ GUIDELINE: number;
13
+ };
14
+ /**
15
+ * The type returned by the lint detection function passed to the Rule
16
+ * constructor.
17
+ */
18
+ export interface Lint extends LintOptions {
19
+ message: string;
20
+ severity: number;
21
+ }
22
+ export declare function linterWarning(message: string, options?: LintOptions): Lint;
23
+ export declare function linterError(message: string, options?: LintOptions): Lint;
24
+ export declare function linterGuideline(message: string, options?: LintOptions): Lint;
package/dist/rule.d.ts CHANGED
@@ -3,95 +3,19 @@
3
3
  * method that takes the same (node, state, content) arguments that a
4
4
  * TreeTransformer traversal callback function does. Call the check() method
5
5
  * during a tree traversal to determine whether the current node of the tree
6
- * violates the rule. If there is no violation, then check() returns
7
- * null. Otherwise, it returns an object that includes the name of the rule,
8
- * an error message, and the start and end positions within the node's content
9
- * string of the lint.
6
+ * violates the rule. If there is a violation, check() returns an array of
7
+ * issues. Each issue is an object that includes the name of the rule,
8
+ * an error message, and the start and end positions of the lint within the
9
+ * node's content string. If there are no violations of the rule, check()
10
+ * returns an empty array.
10
11
  *
11
- * A Perseus lint rule consists of a name, a severity, a selector, a pattern
12
- * (RegExp) and two functions. The check() method uses the selector, pattern,
13
- * and functions as follows:
12
+ * A Rule is typically defined in terms of a name, a selector, a pattern
13
+ * (RegExp) and a linting function. Use the `Rule.makeRule()` or
14
+ * `Rule.makeWidgetRule()` static methods to create rules.
14
15
  *
15
- * - First, when determining which rules to apply to a particular piece of
16
- * content, each rule can specify an optional function provided in the fifth
17
- * parameter to evaluate whether or not we should be applying this rule.
18
- * If the function returns false, we don't use the rule on this content.
19
- *
20
- * - Next, check() tests whether the node currently being traversed matches
21
- * the selector. If it does not, then the rule does not apply at this node
22
- * and there is no lint and check() returns null.
23
- *
24
- * - If the selector matched, then check() tests the text content of the node
25
- * (and its children) against the pattern. If the pattern does not match,
26
- * then there is no lint, and check() returns null.
27
- *
28
- * - If both the selector and pattern match, then check() calls the function
29
- * passing the TraversalState object, the content string for the node, the
30
- * array of nodes returned by the selector match, and the array of strings
31
- * returned by the pattern match. This function can use these arguments to
32
- * implement any kind of lint detection logic it wants. If it determines
33
- * that there is no lint, then it should return null. Otherwise, it should
34
- * return an error message as a string, or an object with `message`, `start`
35
- * and `end` properties. The start and end properties are numbers that mark
36
- * the beginning and end of the problematic content. Note that these numbers
37
- * are relative to the content string passed to the traversal callback, not
38
- * to the entire string that was used to generate the parse tree in the
39
- * first place.
40
- *
41
- * - If the function returns null, then check() returns null. Otherwise,
42
- * check() returns an object with `rule`, `message`, `start` and `end`
43
- * properties. The value of the `rule` property is the name of the rule,
44
- * which is useful for error reporting purposes.
45
- *
46
- * The name, severity, selector, pattern and function arguments to the Rule()
47
- * constructor are optional, but you may not omit both the selector and the
48
- * pattern. If you do not specify a selector, a default selector that matches
49
- * any node of type "text" will be used. If you do not specify a pattern, then
50
- * any node that matches the selector will be assumed to match the pattern as
51
- * well. If you don't pass a function as the fourth argument to the Rule()
52
- * constructor, then you must pass an error message string instead. If you do
53
- * this, you'll get a default function that unconditionally returns an object
54
- * that includes the error message and the start and end indexes of the
55
- * portion of the content string that matched the pattern. If you don't pass a
56
- * function in the fifth parameter, the rule will be applied in any context.
57
- *
58
- * One of the design goals of this Rule class is to allow simple lint rules to
59
- * be described in JSON files without any JavaScript code. So in addition to
60
- * the Rule() constructor, the class also defines a Rule.makeRule() factory
61
- * method. This method takes a single object as its argument and expects the
62
- * object to have four string properties. The `name` property is passed as the
63
- * first argument to the Rule() construtctor. The optional `selector`
64
- * property, if specified, is passed to Selector.parse() and the resulting
65
- * Selector object is used as the second argument to Rule(). The optional
66
- * `pattern` property is converted to a RegExp before being passed as the
67
- * third argument to Rule(). (See Rule.makePattern() for details on the string
68
- * to RegExp conversion). Finally, the `message` property specifies an error
69
- * message that is passed as the final argument to Rule(). You can also use a
70
- * real RegExp as the value of the `pattern` property or define a custom lint
71
- * function on the `lint` property instead of setting the `message`
72
- * property. Doing either of these things means that your rule description can
73
- * no longer be saved in a JSON file, however.
74
- *
75
- * For example, here are two lint rules defined with Rule.makeRule():
76
- *
77
- * let nestedLists = Rule.makeRule({
78
- * name: "nested-lists",
79
- * selector: "list list",
80
- * message: `Nested lists:
81
- * nested lists are hard to read on mobile devices;
82
- * do not use additional indentation.`,
83
- * });
84
- *
85
- * let longParagraph = Rule.makeRule({
86
- * name: "long-paragraph",
87
- * selector: "paragraph",
88
- * pattern: /^.{501,}/,
89
- * lint: function(state, content, nodes, match) {
90
- * return `Paragraph too long:
91
- * This paragraph is ${content.length} characters long.
92
- * Shorten it to 500 characters or fewer.`;
93
- * },
94
- * });
16
+ * Rules only apply to nodes matching both the selector and the RegExp pattern.
17
+ * If check() is called with a non-matching node, then the linting function is
18
+ * not called, and check() returns an empty array.
95
19
  *
96
20
  * Certain advanced lint rules need additional information about the content
97
21
  * being linted in order to detect lint. For example, a rule to check for
@@ -118,26 +42,27 @@
118
42
  * the Perseus article or exercise that is being linted.
119
43
  */
120
44
  import Selector from "./selector";
45
+ import type { Lint } from "./lint";
121
46
  import type { TraversalState, TreeNode } from "./tree-transformer";
122
47
  import type { PerseusWidgetTypes } from "@khanacademy/perseus-core";
123
48
  export type MakeRuleOptions = {
124
49
  name: string;
125
50
  pattern?: RegExp;
126
- severity: number;
127
51
  selector?: string;
128
52
  locale?: string;
129
- message?: string;
130
- lint?: (state: TraversalState, content: string, nodes: any, match: any, context: LintRuleContextObject) => LintTesterReturnType;
131
- applies?: AppliesTester;
53
+ lint: LintTester;
132
54
  };
133
55
  export type MakeWidgetRuleOptions<TWidgetType extends keyof PerseusWidgetTypes> = {
134
56
  name: string;
135
- severity: number;
136
57
  widgetType: TWidgetType;
137
- lint: (widgetInfo: PerseusWidgetTypes[TWidgetType]) => string | undefined;
58
+ lint: (widgetInfo: PerseusWidgetTypes[TWidgetType], context: any) => undefined | Lint | Lint[];
59
+ };
60
+ export type MakeLintFunctionOptions = {
61
+ message: string;
62
+ severity: number;
138
63
  };
139
64
  type PatternMatchType = any;
140
- export type LinterWarning = {
65
+ export type LinterIssue = {
141
66
  rule: string;
142
67
  message: string;
143
68
  start: number;
@@ -146,43 +71,31 @@ export type LinterWarning = {
146
71
  severity: number;
147
72
  metadata?: Record<string, any>;
148
73
  };
149
- export type RuleCheckReturnType = LinterWarning | null | undefined;
150
- type LintTesterReturnType = string | {
151
- message: string;
152
- start: number;
153
- end: number;
154
- metadata?: Record<string, any>;
155
- } | null | undefined;
156
74
  type LintRuleContextObject = {
157
75
  content: string;
158
76
  contentType: "article" | "exercise";
159
77
  stack: string[];
160
78
  widgets: any[];
161
79
  } | null | undefined;
162
- type LintTester = (state: TraversalState, content: string, selectorMatch: ReadonlyArray<TreeNode>, patternMatch: PatternMatchType, context: LintRuleContextObject) => LintTesterReturnType;
163
- type AppliesTester = (context: LintRuleContextObject) => boolean;
80
+ type LintTester = (state: TraversalState, content: string, selectorMatch: ReadonlyArray<TreeNode>, patternMatch: PatternMatchType, context: LintRuleContextObject) => Lint | Lint[] | undefined;
164
81
  /**
165
82
  * A Rule object describes a Perseus lint rule. See the comment at the top of
166
83
  * this file for detailed description.
167
84
  */
168
85
  export default class Rule {
169
86
  name: string;
170
- severity: number;
171
87
  selector: Selector;
172
88
  pattern: RegExp | null | undefined;
173
89
  lint: LintTester;
174
- applies: AppliesTester;
175
- message: string | null | undefined;
176
90
  static DEFAULT_SELECTOR: Selector;
177
- constructor(name: string | null | undefined, severity: number, selector: Selector | null | undefined, pattern: RegExp | null | undefined, lint: LintTester | string | undefined, applies: AppliesTester | undefined);
91
+ constructor(name: string | null | undefined, selector: Selector | null | undefined, pattern: RegExp | null | undefined, lint: LintTester);
178
92
  static makeRule(options: MakeRuleOptions): Rule;
179
93
  static makeWidgetRule<TWidgetType extends keyof PerseusWidgetTypes>(options: MakeWidgetRuleOptions<TWidgetType>): Rule;
180
- check(node: TreeNode, traversalState: TraversalState, content: string, context: LintRuleContextObject): RuleCheckReturnType;
181
- _defaultLintFunction(state: TraversalState, content: string, selectorMatch: ReadonlyArray<TreeNode>, patternMatch: PatternMatchType, context: LintRuleContextObject): {
182
- end: number;
183
- message: string;
184
- start: number;
185
- };
94
+ check(node: TreeNode, traversalState: TraversalState, content: string, context: LintRuleContextObject): LinterIssue[];
95
+ /**
96
+ * Create a linter function from a message.
97
+ */
98
+ static makeLintFunction({ message, severity, }: MakeLintFunctionOptions): LintTester;
186
99
  static makePattern(pattern?: RegExp | string | null): RegExp | null | undefined;
187
100
  static FakePatternMatch(input: string, match: string | null | undefined, index: number): PatternMatchType;
188
101
  static Severity: {
@@ -1 +1,2 @@
1
- export declare const allRules: import("..").Rule[];
1
+ import type Rule from "../rule";
2
+ export declare function allRules(): Rule[];
@@ -0,0 +1,2 @@
1
+ import Rule from "../rule";
2
+ export declare const ExplanationWidgetRule: Rule;
@@ -0,0 +1,2 @@
1
+ import Rule from "../rule";
2
+ export declare const FillInTheBlankRule: Rule;
@@ -0,0 +1,2 @@
1
+ import Rule from "../rule";
2
+ export declare const GradedGroupSetWidgetRule: Rule;
@@ -0,0 +1,2 @@
1
+ import Rule from "../rule";
2
+ export declare const GradedGroupWidgetRule: Rule;
@@ -0,0 +1,2 @@
1
+ import Rule from "../rule";
2
+ export declare const GroupWidgetRule: Rule;
@@ -0,0 +1,7 @@
1
+ import type { Lint } from "../lint";
2
+ import type { PerseusGradedGroupWidgetOptions } from "@khanacademy/perseus-core";
3
+ interface Context {
4
+ contentType: "exercise" | "article" | "renderer";
5
+ }
6
+ export declare function lintGradedGroup(options: PerseusGradedGroupWidgetOptions, { contentType }: Context): Lint[];
7
+ export {};
@@ -1,5 +1,4 @@
1
- import type Rule from "./rule";
2
- import type { LinterWarning } from "./rule";
1
+ import type { LinterIssue } from "./rule";
3
2
  /**
4
3
  * Run the Perseus linter over the specified markdown parse tree, with the
5
4
  * specified context object, and return a (possibly empty) array of lint
@@ -18,4 +17,4 @@ import type { LinterWarning } from "./rule";
18
17
  * context.widgets is the widgets object associated
19
18
  * with the content string
20
19
  */
21
- export declare function runLinter(tree: any, context: any, highlight: boolean, rules?: ReadonlyArray<Rule>): ReadonlyArray<LinterWarning>;
20
+ export declare function runLinter(tree: any, context: any, highlight: boolean): ReadonlyArray<LinterIssue>;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Linter engine for Perseus",
4
4
  "author": "Khan Academy",
5
5
  "license": "MIT",
6
- "version": "7.0.1",
6
+ "version": "8.0.0",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -24,10 +24,10 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "@khanacademy/kas": "3.1.1",
27
- "@khanacademy/kmath": "3.1.3",
28
- "@khanacademy/perseus-core": "42.1.0",
27
+ "@khanacademy/kmath": "3.1.5",
28
+ "@khanacademy/perseus-core": "42.2.0",
29
29
  "@khanacademy/perseus-utils": "3.0.2",
30
- "@khanacademy/pure-markdown": "3.1.1"
30
+ "@khanacademy/pure-markdown": "3.1.2"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@internal/build-settings": "0.9.2"