-
-
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: Apostrophes are destroyed inside of angulars interpolation #9
Comments
I fixed it in attributes, but I did not find a way to handle that in text 🤔 The thing is that the correct format needs to be .with-apostrophe(foo="{{ 'it\'s me' | uppercase }}") and not .with-apostrophe(foo='{{ "it's me" | uppercase }}')
~ The end of the string reached with no closing bracket ) found Therefore, we cant quote the text in a way without to escape the single quote .with-apostrophe(foo='{{ "it\'s me" | uppercase }}') So I have written code to procude .with-apostrophe(foo="{{ 'it\'s me' | uppercase }}") and it is working. // Quotes › should format single to double quotes
// and
// Quotes › should use double quotes by default
- Expected
+ Received
- .grey--text {{ $t('my-language-key.title') }}
+ .grey--text {{ $t("my-language-key.title") }} I don't know how to pass them Hope someone can help me here: https://github.com/prettier/plugin-pug/blob/0817c64/src/index.ts#L391-L407 |
@j-f1 Do you know if we have any regex/JS experts in @prettier/core who can help me here? |
Hehe, yeah, I like regexp, but I'm not quite sure what you are trying to do here? People write Angular using Pug and therefore embed pieces of Angular template code inside attributes, and you're trying to do regex replacements on them (instead of parsing)? |
@lydell Yea... something like that. One problem is that I cant use prettier to format js code in example mustache brackets because the prettier will use linebreaks and semicolons and stuff and breaks the formatting completely. Therefore I want to handle quotes on this place: Lines 401 to 410 in 8890d4c
But it has sideeffects and breaks my other tests in
formats to: .grey--text {{ $t("my-language-key.title") }}
// ~ ~ and I don't know why 🤔 See also Build-62 and click on Test |
It's not possible to do this safely with regex. Consider this example: `"${ `Jean ${ '"John"' } d'Arc`.toUpperCase() }"` |
Besides this example hurts my brain, do you know another strategy? I will build a test for you example 😁 |
I think you'll have to parse it as JS and then print is somehow. If it is JS. But btw, how can you know that the attribute contains Angular code? |
If text starts and ends with mustache brackets, I assume js code. Maybe I should check if there are different quotes in the string and if so then dont touch it? Edit: also in most cases it is not directly valid js code because a prepending |
FYI, there is a |
@ikatyang Thank you, you have definitely put me on the right track! At first I tested So I tried Then I tried I needed to remove the last I think I can merge this to release a next alpha, but keep in mind that it will break code that uses more then one statement in a text. But in my opinion, something like this should be done on the |
Why didn’t you want to use the |
Um ... I thought it's used internally And I do not really know how to |
You should be able to specify |
Ah, sadly it throws an error: code = format(code, { parser: '__ng_interpolation' as any, singleQuote: !singleQuote, printWidth: 9000 }); So nevermind 🤷♂️ Details:
|
What’s in the |
`"${ `Jean ${ '"John"' } d'Arc`.toUpperCase() }"` |
I think you’re supposed to pass in the whole interpolation, i.e. |
No it's not working 😕 code = format(
`{{ ${`"${`Jean ${'"John"'} d'Arc`.toUpperCase()}"`} }}`,
{
parser: '__ng_interpolation' as any,
singleQuote: !singleQuote,
printWidth: 9000
}
); SyntaxError: Unexpected token {, expected identifier, keyword, or string
at o (node_modules/prettier/parser-angular.js:1:43202)
at t.parseNgInterpolation (node_modules/prettier/parser-angular.js:1:44326)
at n (node_modules/prettier/parser-angular.js:1:54823)
at Object.t.parseInterpolation (node_modules/prettier/parser-angular.js:1:55161)
at node_modules/prettier/parser-angular.js:1:55798
at Object.parse (node_modules/prettier/parser-angular.js:1:55454)
at Object.parse$2 [as parse] (node_modules/prettier/index.js:10629:19)
at coreFormat (node_modules/prettier/index.js:13888:23)
at format (node_modules/prettier/index.js:14146:73)
at formatWithCursor (node_modules/prettier/index.js:14162:12) |
It looks like the Angular parser doesn’t support template strings :( Prettier 1.18.2 --parser __ng_interpolation Input: foo(`hello`) Output:
|
Yea, checked it at stackblitz.com. |
Their grammars are different, you should use |
@ikatyang Nice to know that there are so many internal things I didn't noticed ^^' So I will try that. I also don't know how to assume whether it is using vue or angular. Do you have an example for testing? So a vue and an anguar snippet that are formatted differently? |
More precisely, the babel expression parser. Though they use the same parser, their
IIRC, they are formatted in the same way, the only difference between them is that they support different grammars. I guess you could try to guess which parser to use based on attribute names next to it (vue: |
Uh... That would work for attributes. But currently I'm working on I only have something like: span {{ my.angular.value }} {{ someInputFromComponent | uppercase }} |
You can simply choose either parser to use, and use another one if the previous parsing failed. The attribute names are just hints for better guessing, it does not really matter if it's wrong since one of the two parsers must be the correct one and their formatting results are the same. |
Used configuration:
Input file:
Output file:
Expected behavior:
It should not change a text which will be displayed (it's me was changed to it"s me).
Additional the handling of spaces after
{{
and before}}
should be consistently. The added spaces at the second{{ ... }}
is the expected behavior.The text was updated successfully, but these errors were encountered: