@khanacademy/perseus-linter 7.0.2 → 8.0.1

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.
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,23 +42,24 @@
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?: LintTester;
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]) => undefined | string | string[];
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
65
  export type LinterIssue = {
@@ -146,12 +71,6 @@ export type LinterIssue = {
146
71
  severity: number;
147
72
  metadata?: Record<string, any>;
148
73
  };
149
- type Lint = string | {
150
- message: string;
151
- start: number;
152
- end: number;
153
- metadata?: Record<string, any>;
154
- };
155
74
  type LintRuleContextObject = {
156
75
  content: string;
157
76
  contentType: "article" | "exercise";
@@ -159,25 +78,24 @@ type LintRuleContextObject = {
159
78
  widgets: any[];
160
79
  } | null | undefined;
161
80
  type LintTester = (state: TraversalState, content: string, selectorMatch: ReadonlyArray<TreeNode>, patternMatch: PatternMatchType, context: LintRuleContextObject) => Lint | Lint[] | undefined;
162
- type AppliesTester = (context: LintRuleContextObject) => boolean;
163
81
  /**
164
82
  * A Rule object describes a Perseus lint rule. See the comment at the top of
165
83
  * this file for detailed description.
166
84
  */
167
85
  export default class Rule {
168
86
  name: string;
169
- severity: number;
170
87
  selector: Selector;
171
88
  pattern: RegExp | null | undefined;
172
89
  lint: LintTester;
173
- applies: AppliesTester;
174
- message: string | null | undefined;
175
90
  static DEFAULT_SELECTOR: Selector;
176
- 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);
177
92
  static makeRule(options: MakeRuleOptions): Rule;
178
93
  static makeWidgetRule<TWidgetType extends keyof PerseusWidgetTypes>(options: MakeWidgetRuleOptions<TWidgetType>): Rule;
179
94
  check(node: TreeNode, traversalState: TraversalState, content: string, context: LintRuleContextObject): LinterIssue[];
180
- _defaultLintFunction(state: TraversalState, content: string, selectorMatch: ReadonlyArray<TreeNode>, patternMatch: PatternMatchType, context: LintRuleContextObject): Lint;
95
+ /**
96
+ * Create a linter function from a message.
97
+ */
98
+ static makeLintFunction({ message, severity, }: MakeLintFunctionOptions): LintTester;
181
99
  static makePattern(pattern?: RegExp | string | null): RegExp | null | undefined;
182
100
  static FakePatternMatch(input: string, match: string | null | undefined, index: number): PatternMatchType;
183
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,4 +1,3 @@
1
- import type Rule from "./rule";
2
1
  import type { LinterIssue } from "./rule";
3
2
  /**
4
3
  * Run the Perseus linter over the specified markdown parse tree, with the
@@ -18,4 +17,4 @@ import type { LinterIssue } 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<LinterIssue>;
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.2",
6
+ "version": "8.0.1",
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.4",
28
- "@khanacademy/perseus-core": "42.1.1",
27
+ "@khanacademy/kmath": "3.1.6",
28
+ "@khanacademy/perseus-core": "42.3.0",
29
29
  "@khanacademy/perseus-utils": "3.0.2",
30
- "@khanacademy/pure-markdown": "3.1.2"
30
+ "@khanacademy/pure-markdown": "4.0.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@internal/build-settings": "0.9.2"