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

Weird errors and crashes with v-for in ES5 mode (ecmaVersion: 5) #1089

Closed
catrope opened this issue Mar 31, 2020 · 2 comments
Closed

Weird errors and crashes with v-for in ES5 mode (ecmaVersion: 5) #1089

catrope opened this issue Mar 31, 2020 · 2 comments

Comments

@catrope
Copy link

catrope commented Mar 31, 2020

I've been working on setting up Vue support for eslint in a repo where we use ES5, and the Vue plugin doesn't work well with eslint's ES5 mode.

Installing the Vue plugin silently overwrites certain settings: ecmaVersion is set to 2018, sourceType to module, and JSX is enabled. This caused us to accidentally disable enforcement of our "no ES6" rule for about a month until I found out.

I tried to remedy this by changing these settings back in our .eslintrc.json, but that causes strange errors and even crashes to happen when linting templates that use v-for.

Tell us about your environment

  • ESLint version: 6.8.0
  • eslint-plugin-vue version: 6.1.2
  • Node version: 10.15.2

Please show your full configuration:

{
	"extends": [
		"plugin:vue/recommended"
	],
	"parserOptions": {
		"ecmaVersion": 6,
		"sourceType": "script",
		"ecmaFeatures": {
			"jsx": false
		}
	}
}

What did you do?
First file (simple v-for):

<template>
  <div class="foo">
    <p
      v-for="x in xs"
      :key="x"
    >
      {{ x }}
    </p>
  </div>
</template>

Second file (v-for using parens to get both the value and the index):

<template>
  <div class="foo">
    <p
      v-for="(x, index) in xs"
      :key="index"
    >
      {{ x }}
    </p>
  </div>
</template>

What did you expect to happen?
Both of these files should lint without errors, even in ES5 mode (ecmaVersion: 5 in the eslint config).

What actually happened?
For the first file, I get confusing errors about the v-for line:

  4:7   error  'v-for' directives require that attribute value                                                  vue/valid-v-for
  4:14  error  Parsing error: Unexpected token x                                                                vue/no-parsing-error
  5:7   error  Expected 'v-bind:key' directive to use the variables which are defined by the 'v-for' directive  vue/valid-v-for

For the second file, I get an error message that exposes a JS error / crash in the plugin code, without reference to a line number:

  0:0  error  Parsing error: Cannot read property 'block' of undefined

Both of these errors only happen if ecmaVersion: 5 is set. If I set ecmaVersion: 6, the errors go away.

@catrope
Copy link
Author

catrope commented Mar 31, 2020

As a workaround, I'm enabling the Vue plugin only for *.vue files, leaving the ecmaVersion setting unchanged, then using es/no-2015 to prohibit ES6 features (eslint config here). But it would be great if the Vue plugin didn't break with ecmaVersion: 5, so that I can use eslint's "real" ES5 mode instead.

@ota-meshi
Copy link
Member

ota-meshi commented Apr 3, 2020

Thank you for posting this issue.

I have debugged and checked this. This is a problem with vue-eslint-parser.

v-for="(x, index) in xs" directive are parsed by converting them to for(let [x, index] in xs); code, which is almost invalid syntax in ES5.
Here, it analyzes the invalid AST and fails to analyze the variables.

Please post your issue to vue-eslint-parser to solve this issue.

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