Skip to content

script-indent does not work properly with the ternary operator #625

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

Closed
korya opened this issue Oct 31, 2018 · 4 comments · Fixed by #675
Closed

script-indent does not work properly with the ternary operator #625

korya opened this issue Oct 31, 2018 · 4 comments · Fixed by #675
Assignees

Comments

@korya
Copy link

korya commented Oct 31, 2018

Tell us about your environment

  • ESLint Version: 5.8.0
  • eslint-plugin-vue Version: 5.0.0-beta3
  • Node Version: 11

Please show your full configuration:

module.exports = {
  extends: [
    'plugin:vue/recommended',
  ],

  parserOptions: {
    parser: 'babel-eslint',
  },

  overrides: [
    // Disable the default `indent` rule for `.vue` files to avoid conflicts
    // with `vue/script-indent`.
    { files: ['*.vue'], rules: { indent: 'off' } },
  ],
};

What did you do? Please include the actual source code causing the issue.

<template>
  <img :src="imgUrl">
</template>

<script>
export default {
  data() {
    const hours = new Date().getHours();

    return {
      imgUrl: hours > 21 && hours < 6 ? 'night.png' :
        'day.png',
    };
  },
}
</script>

What did you expect to happen?

I expect the code to be successfully linted.

What actually happened? Please include the actual, raw output from ESLint.

  40:1  error  Expected indentation of 6 spaces but found 8 spaces  vue/script-indent

Additional notes

I was able to reproduce the issue in the playground: follow this link

Additionally, if I change the JS code from:

imgUrl: hours > 21 && hours < 6 ? 'night.png' :
  'day.png',

to

imgUrl: hours > 21 && hours < 6 ?
  'night.png' : 'day.png',

The the lint validation succeeds. Here is a playground link for a fixed case

@michalsnik
Copy link
Member

I confirmed it.

Btw. why would you style your code like this? Wouldn't this be a little bit more readable?

{
  imgUrl: hours > 21 || hours < 6
    ? 'night.png'
    : 'day.png',
}

Or even better:

export default {
  data() {
    const hours = new Date().getHours();
    const isNight = hours > 21 || hours < 6;

    return {
      imgUrl: isNight ? 'night.png' : 'day.png',
    };
  },
}

@michalsnik
Copy link
Member

@mysticatea would you give it a look? You're most proficient with indent-related matter.

@mysticatea
Copy link
Member

I will do. Sorry for my delay!

@michalsnik
Copy link
Member

No worries @mysticatea :) For your convenience I assigned you to issues where your help would be the most vital: https://github.com/vuejs/eslint-plugin-vue/issues/assigned/mysticatea

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

Successfully merging a pull request may close this issue.

3 participants