prettier / plugin-python

Prettier Python Plugin
MIT License
517 stars 41 forks source link

Fatal error with comment-only file #69

Closed ryanashcraft closed 6 years ago

ryanashcraft commented 6 years ago

Running prettier on a python file with just comments errors because the last comment doesn't get printed.

Example file:

# 1
# 2

Here's the error:

yarn run v1.2.1
$ prettier --plugin=. --parser=python tests/python_comments/just_comments.py

[error] tests/python_comments/comments.py: Error: Comment "# 2" was not printed. Please report this error!
[error]     at astComments.forEach.comment (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/index.js:61:13)
[error]     at Array.forEach (<anonymous>)
[error]     at ensureAllCommentsPrinted (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/index.js:59:15)
[error]     at formatWithCursor (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/index.js:129:3)
[error]     at Object.formatWithCursor (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/index.js:402:12)
[error]     at format (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/src/cli/util.js:157:19)
[error]     at eachFilename (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/src/cli/util.js:380:16)
[error]     at filePaths.forEach.filePath (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/src/cli/util.js:322:7)
[error]     at Array.forEach (<anonymous>)
[error]     at eachFilename (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/src/cli/util.js:321:15)
[error]     at Object.formatFiles (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/src/cli/util.js:338:3)
[error]     at Object.run (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/src/cli/index.js:60:12)
[error]     at Object.<anonymous> (/Users/ryanashcraft/Projects/plugin-python/node_modules/prettier/bin/prettier.js:5:23)
[error]     at Module._compile (module.js:624:30)
[error]     at Object.Module._extensions..js (module.js:635:10)
[error]     at Module.load (module.js:545:32)
error Command failed with exit code 2.

I can repro the same issue for JavaScript by removing these lines from printer-estree.js.

Conversely, I can hack a fix for python by importing the comments module from prettier/src/main/comments.js and concating to the return value of comments.printDanglingComments(path, options, /* sameIndent */ true) to the "Module" AST type return value.

Not sure what it would take to fix this properly.