-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Inline JavaScript in text block is not pretty #158
Comments
HTML parser in Prettier does something a bit more complex: https://github.com/prettier/prettier/blob/master/src/language-html/printer-html.js#L109 |
I think I know how I can solve this |
Ok, I looked in the token stream and see that I need to iterate over, bundle and format with JS everything between the If you want you can try creating a PR yourself 🙂 and I'll review it and help you. This could help get that improvement faster. |
@Shinigami92 - Sure, I'd love to help where I can. Sadly, I don't think I understand Prettier enough to know where to go from here. How do you switch the parser from "pug" to "babel" or something else? I'm sure I could study Prettier's built-in HTML parser a bit more... but it looks tricky... and very different from the pug parser here. What do you think? |
Oh, IMO, it's a lot easier than you think :) First, you should start reading the CONTRIBUTING.md Then you should create a folder Now you are interested in src/printer.ts Lines 185 to 189 in 9db4489
In this line, every token will be passed and called by it's related function Line 215 in 9db4489
You are specially interested in Lines 1065 to 1067 in 9db4489
and Lines 1046 to 1055 in 9db4489
So you will get a token-stream for e.g. script.
const x = 2 ; // comment
script.
if (usingPug)
console.log('you are awesome')
else
console.log('use pug') [
{"type":"tag","loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":7}},"val":"script"},
{"type":"dot","loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}}},
{"type":"start-pipeless-text","loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":8}}},
{"type":"text","loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":37}},"val":"const x = 2 ; // comment"},
{"type":"newline","loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":1}}},
{"type":"text","loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":1}},"val":""},
{"type":"end-pipeless-text","loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":1}}},
{"type":"newline","loc":{"start":{"line":4,"column":1},"end":{"line":4,"column":1}}},
{"type":"tag","loc":{"start":{"line":4,"column":1},"end":{"line":4,"column":7}},"val":"script"},
{"type":"dot","loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":8}}},
{"type":"start-pipeless-text","loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":8}}},
{"type":"text","loc":{"start":{"line":5,"column":3},"end":{"line":5,"column":16}},"val":"if (usingPug)"},
{"type":"newline","loc":{"start":{"line":6,"column":1},"end":{"line":6,"column":3}}},
{"type":"text","loc":{"start":{"line":6,"column":3},"end":{"line":6,"column":35}},"val":" console.log('you are awesome')"},
{"type":"newline","loc":{"start":{"line":7,"column":1},"end":{"line":7,"column":3}}},
{"type":"text","loc":{"start":{"line":7,"column":3},"end":{"line":7,"column":7}},"val":"else"},
{"type":"newline","loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":3}}},
{"type":"text","loc":{"start":{"line":8,"column":3},"end":{"line":8,"column":27}},"val":" console.log('use pug')"},
{"type":"end-pipeless-text","loc":{"start":{"line":8,"column":27},"end":{"line":8,"column":27}}},
{"type":"eos","loc":{"start":{"line":8,"column":27},"end":{"line":8,"column":27}}}
] You see the sections between |
@bminer Good news: I've completed the basic work on this enhancement |
@Shinigami92 - Looks great! Thanks! Inline scripts now run through prettier, but the inline script output does not respect the .prettierrc configuration file. Furthermore, the indentation within the pug file should probably be consistent with the inline script; that is, if the pug file is tab-indented, the inline JavaScript should also be tab-intented. |
Seems to work great if you merge |
The problem with the That's why it's always the opposite of Maybe we can improve it for some code parts like when it's not in an attribute
|
Ah yeah and another problem: |
I would suggest to checkout the repo and use If you found a solution, please create a PR |
It seems that Pug
script.
blocks aren't parsed / printed properly. It should make the JavaScript code prettier.Info
Prettier config
Just ignore what I'm doing with *.riot file extensions. This bug exists even when
--no-config
flag is passed to prettier.Input
Output or Error
cat test.pug | npx prettier --parser pug
Expected Output
Compare this to Prettier's HTML parser:
which outputs
Notice missing semicolon and proper spacing
The text was updated successfully, but these errors were encountered: