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

unicorn/prefer-ternary: auto-fix removes comment #1797

Closed
FloEdelmann opened this issue Apr 19, 2022 · 1 comment
Closed

unicorn/prefer-ternary: auto-fix removes comment #1797

FloEdelmann opened this issue Apr 19, 2022 · 1 comment

Comments

@FloEdelmann
Copy link
Contributor

This code:

// If the component name is PascalCase or camelCase
// it can be used in various of ways inside template,
// like "theComponent", "The-component" etc.
// but except snake_case
if (casing.isPascalCase(name) || casing.isCamelCase(name)) {
  return ![...usedComponents].some((n) => {
    return (
      n.indexOf('_') === -1 &&
      (name === casing.pascalCase(n) ||
        casing.camelCase(n) === name)
    )
  })
} else {
  // In any other case the used component name must exactly match
  // the registered name
  return !usedComponents.has(name)
}

gets auto-fixed by unicorn/prefer-ternary to:

// If the component name is PascalCase or camelCase
// it can be used in various of ways inside template,
// like "theComponent", "The-component" etc.
// but except snake_case
return casing.isPascalCase(name) || casing.isCamelCase(name) ? ![...usedComponents].some((n) => {
    return (
      n.indexOf('_') === -1 &&
      (name === casing.pascalCase(n) ||
        casing.camelCase(n) === name)
    )
  }) : !usedComponents.has(name);

So the comment in the else branch is lost. I suggest to either disable the auto-fix in this case, or don't report this at all (since a comment indicates more complicated code where a ternary likely decreases readability).

Repo where this can be reproduced: https://github.com/vuejs/eslint-plugin-vue/blob/a473a0d543655addf28b8bbc3c1e1393e2b30854/lib/rules/no-unused-components.js#L111-L127

@fisker
Copy link
Collaborator

fisker commented Apr 22, 2022

You are using an old version https://github.com/vuejs/eslint-plugin-vue/blob/cdcf75e4ecf3ddab806ef33da77510022777d0da/package.json#L81, this should already fix in #1763

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants