@khanacademy/simple-markdown 0.8.2 → 0.8.4

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Javascript markdown parsing, made simple",
4
4
  "author": "Khan Academy",
5
5
  "license": "MIT",
6
- "version": "0.8.2",
6
+ "version": "0.8.4",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/react-dom": ">=16.0.0",
28
- "perseus-build-settings": "^0.0.2",
28
+ "perseus-build-settings": "^0.0.3",
29
29
  "size-limit": "^0.21.1",
30
30
  "typescript": "^3.6.4",
31
31
  "uglify-js": "^3.6.7"
@@ -4943,4 +4943,31 @@ describe("simple markdown", function () {
4943
4943
  );
4944
4944
  });
4945
4945
  });
4946
+
4947
+ describe("custom rules", () => {
4948
+ it("should throw if `parse` returns invalid result", () => {
4949
+ const parse = jest
4950
+ .fn()
4951
+ // Flow correctly catches that the `parse` function returns and
4952
+ // incorrect type, but I want to keep this test here for now
4953
+ // because in call sites that use this, we've seen Flow not
4954
+ // catch this. So for now, we hard-fail!
4955
+ // $FlowFixMe[incompatible-call]
4956
+ .mockImplementation(() => "invalid parse result");
4957
+
4958
+ const invalidRules = {
4959
+ parseDoesntReturnCorrectType: {
4960
+ order: 1,
4961
+ match: (source, state, prevCapture) => true,
4962
+ parse,
4963
+ },
4964
+ };
4965
+
4966
+ expect(() =>
4967
+ // $FlowFixMe[incompatible-call]
4968
+ SimpleMarkdown.parserFor(invalidRules)("some input"),
4969
+ ).toThrow();
4970
+ expect(parse).toHaveBeenCalled();
4971
+ });
4972
+ });
4946
4973
  });
package/src/index.js CHANGED
@@ -320,7 +320,10 @@ var parserFor = function (rules: ParserRules, defaultState: ?State): Parser {
320
320
  });
321
321
 
322
322
  var latestState: State;
323
- var nestedParse = function (source: string, state: ?State): Parser {
323
+ var nestedParse: Parser = function (
324
+ source: string,
325
+ state: ?State,
326
+ ): Array<SingleASTNode> {
324
327
  var result: Array<SingleASTNode> = [];
325
328
  state = state || latestState;
326
329
  latestState = state;
@@ -400,7 +403,6 @@ var parserFor = function (rules: ParserRules, defaultState: ?State): Parser {
400
403
  );
401
404
  }
402
405
 
403
- // $FlowFixMe
404
406
  var parsed = rule.parse(capture, nestedParse, state);
405
407
  // We maintain the same object here so that rules can
406
408
  // store references to the objects they return and
@@ -410,6 +412,12 @@ var parserFor = function (rules: ParserRules, defaultState: ?State): Parser {
410
412
  // $FlowFixMe
411
413
  Array.prototype.push.apply(result, parsed);
412
414
  } else {
415
+ if (parsed == null || typeof parsed !== "object") {
416
+ throw new Error(
417
+ `parse() function returned invalid parse result: '${parsed}'`,
418
+ );
419
+ }
420
+
413
421
  // We also let rules override the default type of
414
422
  // their parsed node if they would like to, so that
415
423
  // there can be a single output function for all links,
@@ -429,7 +437,10 @@ var parserFor = function (rules: ParserRules, defaultState: ?State): Parser {
429
437
  return result;
430
438
  };
431
439
 
432
- var outerParse = function (source: string, state: ?State): Parser {
440
+ var outerParse: Parser = function (
441
+ source: string,
442
+ state: ?State,
443
+ ): Array<SingleASTNode> {
433
444
  latestState = populateInitialState(state, defaultState);
434
445
  if (!latestState.inline && !latestState.disableAutoBlockNewlines) {
435
446
  source = source + "\n\n";