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

Handle for...of in vue v-for #132

Merged
merged 13 commits into from
Nov 20, 2020
Merged

Handle for...of in vue v-for #132

merged 13 commits into from
Nov 20, 2020

Conversation

Shinigami92
Copy link
Member

closes #129

@Shinigami92 Shinigami92 added the type: bug Functionality that does not work as intended/expected label Oct 4, 2020
@Shinigami92 Shinigami92 self-assigned this Oct 4, 2020
@Shinigami92 Shinigami92 added the framework: Vue Related to the framework Vue label Oct 4, 2020
@Shinigami92
Copy link
Member Author

val = format(val, { parser: parser as any, ...this.codeInterpolationOptions });

@fisker Hi 🙂 (You are my person of trust in prettier's team ^^ so I write you)
I'm passing the following data to prettier's format function

{
  val: 'item of items', // not 'item in items'!
  parser: '__vue_expression',
  codeInterpolationOptions: {
    singleQuote: true,
    bracketSpacing: true,
    arrowParens: 'always',
    printWidth: 9000,
    endOfLine: 'lf'
  }
}

Vue seems to allow v-for with for of syntax:


Is this a prettier issue? Or do I need to call another (internal) parser for this specific case?

src/printer.ts Outdated Show resolved Hide resolved
src/printer.ts Outdated Show resolved Hide resolved
@fisker
Copy link
Member

fisker commented Oct 4, 2020

The v-for is similar to for-of but not the same, v-for="(a, b) of c" is valid, for ((a, b) of c){} is not valid js, here is how prettier handle v-for https://github.com/prettier/prettier/blob/fbb212ab59a5951f062caf01e94eba89759f24b0/src/language-html/syntax-vue.js#L7

FYI, we just improved some v-for cases few days ago prettier/prettier#9225

@Shinigami92
Copy link
Member Author

Shinigami92 commented Oct 5, 2020

@fisker So I did not dig deep into it but the only call to this function is here

https://github.com/prettier/prettier/blob/64cb7abed85566ec5d0eaee13e9d4fc58198f001/src/language-html/printer-html.js#L1081-L1083

  if (options.parser === "vue") {
    if (node.fullName === "v-for") {
      return printVueFor(getValue(), textToDoc);

But I'm calling directly with parser: '__vue_expression', so maybe I never reach this code 🤔
Maybe I need to call with parser: 'vue' directly here in this case
But until now every thing worked with all the internal parsers

plugin-pug/src/printer.ts

Lines 368 to 413 in 8106cda

private formatVueEventBinding(val: string): string {
val = val.trim();
val = val.slice(1, -1); // Remove quotes
val = format(val, { parser: '__vue_event_binding' as any, ...this.codeInterpolationOptions });
val = unwrapLineFeeds(val);
if (val[val.length - 1] === ';') {
val = val.slice(0, -1);
}
return this.quoteString(val);
}
private formatVueExpression(val: string): string {
return this.formatDelegatePrettier(val, '__vue_expression');
}
private formatAngularBinding(val: string): string {
return this.formatDelegatePrettier(val, '__ng_binding');
}
private formatAngularAction(val: string): string {
return this.formatDelegatePrettier(val, '__ng_action');
}
private formatAngularDirective(val: string): string {
return this.formatDelegatePrettier(val, '__ng_directive');
}
private formatAngularInterpolation(val: string): string {
val = val.slice(1, -1); // Remove quotes
val = val.slice(2, -2); // Remove braces
val = val.trim();
if (val.includes(`\\${this.otherQuotes}`)) {
logger.warn(
'The following expression could not be formatted correctly. Please try to fix it yourself and if there is a problem, please open a bug issue:',
val
);
} else {
val = format(val, {
parser: '__ng_interpolation' as any,
...this.codeInterpolationOptions
});
val = unwrapLineFeeds(val);
}
val = handleBracketSpacing(this.options.pugBracketSpacing, val);
return this.quoteString(val);
}

@Shinigami92
Copy link
Member Author

@fisker So indeed if calling

return this.formatDelegatePrettier(val, '__vue_expression');

with only 'vue' instead of '__vue_expression', the one specific v-for="x of quux" test passes!

But why '__vue_expression' don't handle this case?
Should we handle it in prettier itself? Or should I add a condition if v-for and contains ' of ' then use parser: 'vue' 🤔

@Shinigami92
Copy link
Member Author

Awaiting release of prettier: prettier/prettier#9225 (comment)

@fisker
Copy link
Member

fisker commented Oct 17, 2020

But why '__vue_expression' don't handle this case?

Because it's not legal js code.

@Shinigami92
Copy link
Member Author

Oh you mean cause using of in js is different to in
But in Vue: of is just an alias to in

@fisker
Copy link
Member

fisker commented Oct 17, 2020

No, of and in the same.

Check #132 (comment)

@Shinigami92 Shinigami92 marked this pull request as ready for review November 20, 2020 11:58
@Shinigami92 Shinigami92 merged commit 77e8b5b into master Nov 20, 2020
@Shinigami92 Shinigami92 deleted the issue-129 branch November 20, 2020 11:59
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: Does not work when source contains for...of statement (Vue)
3 participants