Skip to content
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

Fix corrupt attribute values on dynamically bound Vue props without quotes #252

Merged
merged 5 commits into from
Jul 6, 2021

Conversation

bitencode
Copy link
Contributor

Changes:

Note that I changed isQuoted and added the back-tic quote to the test with the other two quote styles (was "', now is "`'). All the tests pass, running this on my code based produced correct results, but I only have plain and Vue-based code (no angular, react, svelte, etc..). I looked at all the calling sites of isQuoted and it looks okay to me, but someone more familiar with the code base should probably review that carefully.

bitencode added 2 commits July 5, 2021 17:33
This change allows for dynamically bound Vue props that are missing their quotes to be properly quoted.  Performing the change in `src/printer.ts` fixed the bug, but caused other tests to fail.  Adding the additional back-tic quote in `isQuoted` allows all unit tests to pass.  I looked at all of the calling sites of `isQuoted` and it looks okay to me, but it should probably be double checked.
@bitencode bitencode requested a review from Shinigami92 as a code owner July 6, 2021 00:48
@Shinigami92 Shinigami92 linked an issue Jul 6, 2021 that may be closed by this pull request
7 tasks
@Shinigami92 Shinigami92 added framework: Vue Related to the framework Vue type: bug Functionality that does not work as intended/expected labels Jul 6, 2021
@@ -165,7 +165,7 @@ export function isWrappedWith(val: string, start: string, end: string, offset: n
* @returns Whether the value is quoted or not.
*/
export function isQuoted(val: string): boolean {
if (/^(["'])(.*)\1$/.test(val)) {
if (/^(["'`])(.*)\1$/.test(val)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, backtick quoted strings are pre processed by pug as JS. Therefore it is handled a bit differently than normal quoted strings.
But beside that, if this is the case, I should find a test case to cover that 🤔
Will have a deeper look later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bitencode Do you have a case for this? Then add it to the tests
If not, I would like to revert this line and just change it when it is absolutely necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shinigami92 I do not have a specific test case for this. However, with the changes to avoid quoting pug variables assigned to Vue props to other test cases break (issue-61 - 'should not wrap chained data-bindings' and issue-80 - 'should not reformat multiline interpolation strings' because they are then not unquoted before being passed to the prettier format function.

It's possible that if isQuoted need to not check for back-tic quotes that there should be another function that does the check needed in formatDelegatePrettier. Or possibly an additional parameter that indicates which quotes to check for with the default being "' and the new call can pass in all 3 types of quotes. I don't know this code base well, or your coding preferences, so I'm open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx 🙂 I will look into the branch and discover it
Will send feedback later back to you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AHA! The thing is, that 61 and 80 have backtick quoted strings, but can be normal strings cause they don't use code interpolation! So no ${ in it.
Then my plugin converts these template literal strings to normal strings (by design)

But I currently don't understand why the new change is needed then 🤔

And we should create a test to check that it doesn't convert the backticks if it contains interpolated code in one single line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bitencode So I added commit: a491e08

Seems that it still works with your change and not work when reverting...
So thx I'm now less afraid and confident using your change 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Awesome - Thanks!

tests/issues/issue-250/formatted.pug Outdated Show resolved Hide resolved
tests/issues/issue-250/issue-250.test.ts Outdated Show resolved Hide resolved
bitencode and others added 3 commits July 6, 2021 08:20
- fix copy/paste error on test description
- fix formatted/unformatted test files:
- unquoted pug variable assignment to Vue prop should not be quoted
@Shinigami92 Shinigami92 changed the title fix: corrupt attribute values on dynamically bound Vue props without quotes Fix corrupt attribute values on dynamically bound Vue props without quotes Jul 6, 2021
@Shinigami92 Shinigami92 merged commit ff0715c into prettier:main Jul 6, 2021
@bitencode bitencode deleted the fix/corrupt-quotes branch July 6, 2021 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework: Vue Related to the framework Vue type: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: corrupts pug variable names that are assigned to bound Vue props
2 participants