@khanacademy/perseus-linter 1.2.18 → 1.3.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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright 2022 Khan Academy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to use,
6
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ Software, and to permit persons to whom the Software is furnished to do so, subject
8
+ to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17
+ AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/es/index.js CHANGED
@@ -1,16 +1,7 @@
1
- import { PerseusError, Errors, addLibraryVersionToPerseusDebug } from '@khanacademy/perseus-core';
1
+ import _extends from '@babel/runtime/helpers/extends';
2
+ import { PerseusError, Errors } from '@khanacademy/perseus-core';
2
3
  import PropTypes from 'prop-types';
3
4
 
4
- function _extends() {
5
- return _extends = Object.assign ? Object.assign.bind() : function (n) {
6
- for (var e = 1; e < arguments.length; e++) {
7
- var t = arguments[e];
8
- for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
9
- }
10
- return n;
11
- }, _extends.apply(null, arguments);
12
- }
13
-
14
5
  /* eslint-disable no-useless-escape */
15
6
  /**
16
7
  * This is the base class for all Selector types. The key method that all
@@ -530,22 +521,27 @@ class SiblingCombinator extends SelectorCombinator {
530
521
  // This represents the type returned by String.match(). It is an
531
522
  // array of strings, but also has index:number and input:string properties.
532
523
  // TypeScript doesn't handle it well, so we punt and just use any.
524
+
533
525
  // This is the return type of the check() method of a Rule object
526
+
534
527
  // This is the return type of the lint detection function passed as the 4th
535
528
  // argument to the Rule() constructor. It can return null or a string or an
536
529
  // object containing a string and two numbers.
537
530
  // prettier-ignore
538
531
  // (prettier formats this in a way that ka-lint does not like)
532
+
539
533
  // This is the type of the lint detection function that the Rule() constructor
540
534
  // expects as its fourth argument. It is passed the TraversalState object and
541
535
  // content string that were passed to check(), and is also passed the array of
542
536
  // nodes returned by the selector match and the array of strings returned by
543
537
  // the pattern match. It should return null if no lint is detected or an
544
538
  // error message or an object contining an error message.
539
+
545
540
  // An optional check to verify whether or not a particular rule should
546
541
  // be checked by context. For example, some rules only apply in exercises,
547
542
  // and should never be applied to articles. Defaults to true, so if we
548
543
  // omit the applies function in a rule, it'll be tested everywhere.
544
+
549
545
  /**
550
546
  * A Rule object describes a Perseus lint rule. See the comment at the top of
551
547
  * this file for detailed description.
@@ -634,7 +630,6 @@ class Rule {
634
630
  if (!error) {
635
631
  return null; // No lint; we're done
636
632
  }
637
-
638
633
  if (typeof error === "string") {
639
634
  // If the lint function returned a string we assume it
640
635
  // applies to the entire content of the node and return it.
@@ -1019,6 +1014,28 @@ Whitespace in image URLs causes translation difficulties.`;
1019
1014
  }
1020
1015
  });
1021
1016
 
1017
+ var ImageUrlEmpty = Rule.makeRule({
1018
+ name: "image-url-empty",
1019
+ severity: Rule.Severity.ERROR,
1020
+ selector: "image",
1021
+ lint: function (state, content, nodes) {
1022
+ const image = nodes[0];
1023
+ const url = image.target;
1024
+
1025
+ // If no URL is provided, an infinite spinner will be shown in articles
1026
+ // overlaying the page where the image should be. This prevents the page
1027
+ // from fully loading. As a result, we check for URLS with all images.
1028
+ if (!url || !url.trim()) {
1029
+ return "Images should have a URL";
1030
+ }
1031
+
1032
+ // NOTE(TB): Ideally there would be a check to confirm the URL works
1033
+ // and leads to a valid resource, but fetching the URL would require
1034
+ // linting to be able to handle async functions, which it currently
1035
+ // cannot do.
1036
+ }
1037
+ });
1038
+
1022
1039
  // Normally we have one rule per file. But since our selector class
1023
1040
  // can't match specific widget types directly, this rule implements
1024
1041
  // a number of image widget related rules in one place. This should
@@ -1284,7 +1301,7 @@ do not put widgets inside of tables.`
1284
1301
  });
1285
1302
 
1286
1303
  // TODO(davidflanagan):
1287
- var AllRules = [AbsoluteUrl, BlockquotedMath, BlockquotedWidget, DoubleSpacingAfterTerminal, ExpressionWidget, ExtraContentSpacing, HeadingLevel1, HeadingLevelSkip, HeadingSentenceCase, HeadingTitleCase, ImageAltText, ImageInTable, LinkClickHere, LongParagraph, MathAdjacent, MathAlignExtraBreak, MathAlignLinebreaks, MathEmpty, MathFrac, MathNested, MathStartsWithSpace, MathTextEmpty, NestedLists, StaticWidgetInQuestionStem, TableMissingCells, UnescapedDollar, WidgetInTable, MathWithoutDollars, UnbalancedCodeDelimiters, ImageSpacesAroundUrls, ImageWidget];
1304
+ var AllRules = [AbsoluteUrl, BlockquotedMath, BlockquotedWidget, DoubleSpacingAfterTerminal, ImageUrlEmpty, ExpressionWidget, ExtraContentSpacing, HeadingLevel1, HeadingLevelSkip, HeadingSentenceCase, HeadingTitleCase, ImageAltText, ImageInTable, LinkClickHere, LongParagraph, MathAdjacent, MathAlignExtraBreak, MathAlignLinebreaks, MathEmpty, MathFrac, MathNested, MathStartsWithSpace, MathTextEmpty, NestedLists, StaticWidgetInQuestionStem, TableMissingCells, UnescapedDollar, WidgetInTable, MathWithoutDollars, UnbalancedCodeDelimiters, ImageSpacesAroundUrls, ImageWidget];
1288
1305
 
1289
1306
  /**
1290
1307
  * TreeTransformer is a class for traversing and transforming trees. Create a
@@ -1346,9 +1363,11 @@ var AllRules = [AbsoluteUrl, BlockquotedMath, BlockquotedWidget, DoubleSpacingAf
1346
1363
 
1347
1364
  // TreeNode is the type of a node in a parse tree. The only real requirement is
1348
1365
  // that every node has a string-valued `type` property
1366
+
1349
1367
  // TraversalCallback is the type of the callback function passed to the
1350
1368
  // traverse() method. It is invoked with node, state, and content arguments
1351
1369
  // and is expected to return nothing.
1370
+
1352
1371
  // This is the TreeTransformer class described in detail at the
1353
1372
  // top of this file.
1354
1373
  class TreeTransformer {
@@ -1823,9 +1842,51 @@ class Stack {
1823
1842
  }
1824
1843
  }
1825
1844
 
1845
+ /**
1846
+ * Adds the given perseus library version information to the __perseus_debug__
1847
+ * object and ensures that the object is attached to `globalThis` (`window` in
1848
+ * browser environments).
1849
+ *
1850
+ * This allows each library to provide runtime version information to assist in
1851
+ * debugging in production environments.
1852
+ */
1853
+ const addLibraryVersionToPerseusDebug = (libraryName, libraryVersion) => {
1854
+ // If the library version is the default value, then we don't want to
1855
+ // prefix it with a "v" to indicate that it is a version number.
1856
+ let prefix = "v";
1857
+ if (libraryVersion === "__lib_version__") {
1858
+ prefix = "";
1859
+ }
1860
+ const formattedVersion = `${prefix}${libraryVersion}`;
1861
+ if (typeof globalThis !== "undefined") {
1862
+ var _globalThis$__perseus;
1863
+ globalThis.__perseus_debug__ = (_globalThis$__perseus = globalThis.__perseus_debug__) != null ? _globalThis$__perseus : {};
1864
+ const existingVersionEntry = globalThis.__perseus_debug__[libraryName];
1865
+ if (existingVersionEntry) {
1866
+ // If we already have an entry and it doesn't match the registered
1867
+ // version, we morph the entry into an array and log a warning.
1868
+ if (existingVersionEntry !== formattedVersion) {
1869
+ // Existing entry might be an array already (oops, at least 2
1870
+ // versions of the library already loaded!).
1871
+ const allVersions = Array.isArray(existingVersionEntry) ? existingVersionEntry : [existingVersionEntry];
1872
+ allVersions.push(formattedVersion);
1873
+ globalThis.__perseus_debug__[libraryName] = allVersions;
1874
+
1875
+ // eslint-disable-next-line no-console
1876
+ console.warn(`Multiple versions of ${libraryName} loaded on this page: ${allVersions.sort().join(", ")}`);
1877
+ }
1878
+ } else {
1879
+ globalThis.__perseus_debug__[libraryName] = formattedVersion;
1880
+ }
1881
+ } else {
1882
+ // eslint-disable-next-line no-console
1883
+ console.warn(`globalThis not found found (${formattedVersion})`);
1884
+ }
1885
+ };
1886
+
1826
1887
  // This file is processed by a Rollup plugin (replace) to inject the production
1827
1888
  const libName = "@khanacademy/perseus-linter";
1828
- const libVersion = "1.2.18";
1889
+ const libVersion = "1.3.1";
1829
1890
  addLibraryVersionToPerseusDebug(libName, libVersion);
1830
1891
 
1831
1892
  // Define the shape of the linter context object that is passed through the