Skip to content

Code blocks in markdown list items with two or more empty lines may crash parser #4306

@kachkaev

Description

@kachkaev

Looks like the following code crashes the parser (at least in the latest Playground):

1.  Two or more empty lines with no spaces:

    ```js
    const a = 1;


    const b = 2;
    ```

(note no spaces in lines between const)

Chrome:

TypeError: Cannot read property 'split' of null
    at Object.embed$2 [as embed] (https://prettier.io/lib/index.js:1380:20)
    at Object.printSubtree (https://prettier.io/lib/index.js:1646:1643)
    at genericPrint$6 (https://prettier.io/lib/index.js:1652:21)
    at https://prettier.io/lib/index.js:1648:206
    at Object.printComments (https://prettier.io/lib/index.js:562:873)
    at printGenerically (https://prettier.io/lib/index.js:1648:165)
    at FastPath.call (https://prettier.io/lib/index.js:1540:261)
    at processor (https://prettier.io/lib/index.js:1398:535)
    at https://prettier.io/lib/index.js:1398:905
    at FastPath.map (https://prettier.io/lib/index.js:1549:380)

FF:

embed$2@https://prettier.io/lib/index.js:1380:5
printSubtree@https://prettier.io/lib/index.js:1646:1627
genericPrint$6@https://prettier.io/lib/index.js:1652:9
printGenerically/res<@https://prettier.io/lib/index.js:1648:206
printComments@https://prettier.io/lib/index.js:562:873
printGenerically@https://prettier.io/lib/index.js:1648:156
call@https://prettier.io/lib/index.js:1540:261
processor@https://prettier.io/lib/index.js:1398:525
printChildren/<@https://prettier.io/lib/index.js:1398:905
map@https://prettier.io/lib/index.js:1549:380
printChildren@https://prettier.io/lib/index.js:1398:821
printRoot@https://prettier.io/lib/index.js:1398:18
genericPrint$3@https://prettier.io/lib/index.js:1381:1739
genericPrint$6@https://prettier.io/lib/index.js:1653:10
printGenerically/res<@https://prettier.io/lib/index.js:1648:206
printComments@https://prettier.io/lib/index.js:562:873
printGenerically@https://prettier.io/lib/index.js:1648:156
printAstToDoc@https://prettier.io/lib/index.js:1648:347
_formatWithCursor@https://prettier.io/lib/index.js:2366:1221
_format@https://prettier.io/lib/index.js:2367:239
format@https://prettier.io/lib/index.js:2386:277
formatCode@https://prettier.io/worker.js:130:12
self.onmessage@https://prettier.io/worker.js:80:19

Playground link

# Options (if any):
--parser=markdown

Expected behavior:

I'm not quite sure what the expected behaviour should be here. As an author of prettier-plugin-elm, I'd say it'd be great if the code block remained as is despite more than one empty line of code. That's because two empty lines are common in Elm. E.g.:

module Main exposing (..)
+
import Html exposing (text)
+
+
main =
    text "Hello, World!"

In 1.11.0 the output was the following (see playground for #4035):

1. Two or more empty lines with no spaces:

   ```js
   const a = 1;
   ```


    const b = 2;
    ```

Still broken, but at least no crash.

Would it be feasible to leave two or more empty lines in code blocks (even without spaces) given that a plugin that formats that block may want so? If not, I'd have to get rid of double empty lines in Elm blocks, which some users might not like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:markdownIssues affecting Markdownlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.priority:highCode is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent!scope:dependencyIssues that cannot be solved inside Prettier itself, and must be fixed in a dependency

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions