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

vue/script-indent: ignoring nested arrays/objects causes incorrect formatting for object #833

Open
tony19 opened this issue Feb 26, 2019 · 3 comments

Comments

@tony19
Copy link

tony19 commented Feb 26, 2019

Tell us about your environment

  • ESLint version: 5.14.1
  • eslint-plugin-vue version: 5.2.2
  • Node version: 10.11.0

Please show your full configuration:

{
  "root": true,
  "env": {
    "node": true
  },
  "extends": [
    "plugin:vue/essential",
    "eslint:recommended"
  ],
  "rules": {
    "vue/script-indent": [
      "error",
      2,
      {
        "ignores": [
          "[value.type='ObjectExpression']:not(:matches(ExportDefaultDeclaration, [left.property.name='exports']) > * > [value.type='ObjectExpression'])",
          "[value.type='ArrayExpression']"
        ]
      }
    ]
  },
  "parserOptions": {
    "parser": "babel-eslint"
  }
}

What did you do?

  1. Generate default project with vue-cli.
  2. Add ESLint rule above to package.json.
  3. In src/App.vue, add variable of object containing nested arrays/objects and literals.
  4. Run yarn lint.

See reproduction repo

src/App.vue:

<script>
import HelloWorld from './components/HelloWorld.vue'

const x = {
a: [],
b: 1,
c: {},
d: 2
}
console.log(x)

export default {
  name: 'app',
  components: {
    HelloWorld
  },
  methods: {
foo() {
const x = {
a: [],
b: 1,
c: {},
d: 2
}
console.log(x)
}
  }
}
</script>

What did you expect to happen?

<script>
import HelloWorld from './components/HelloWorld.vue'

const x = {
a: [],
  b: 1,
c: {},
  d: 2
}
console.log(x)

export default {
  name: 'app',
  components: {
    HelloWorld
  },
  methods: {
    foo() {
      const x = {
a: [],
        b: 1,
c: {},
        d: 2
      }
      console.log(x)
    }
  }
}
</script>

What actually happened?

<script>
import HelloWorld from './components/HelloWorld.vue'

const x = {
a: [],
b: 1, ❌
c: {},
d: 2
}
console.log(x)

export default {
  name: 'app',
  components: {
    HelloWorld
  },
  methods: {
    foo() {
      const x = {
a: [],
b: 1, ❌
c: {},
d: 2
      }
      console.log(x)
    }
  }
}
</script>
@tony19
Copy link
Author

tony19 commented Feb 26, 2019

Workaround: Move the literal field to be first in the object, which somehow allows the linter to format the object correctly (in addition to ignoring nested arrays/objects):

const x = {
b: 1,  👈
a: [],
c: {},
d: 2
}

@tony19
Copy link
Author

tony19 commented Feb 26, 2019

A similar issue exists in eslint/eslint#11445 with two differences:

  1. Inserting a literal as the first value has no effect.
  2. All fields that follow a nested array/object are ignored.

@mysticatea
Copy link
Member

mysticatea commented Feb 27, 2019

Thank you for the report.

Our indent rule aligns other properties to the same column of the first property. In your case, the first property is ignored by your configuration, so other properties are aligned to the first property, then those are ignored as well.

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

No branches or pull requests

2 participants