question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Linting a Vue SFC containing JSX throws a parsing error

See original GitHub issue

The following Vue SFC causes a lint error:

<script setup lang="jsx">
const JsxElement = (
	<div>
		Rendered from JSX!
	</div>
)
</script>

<template>
	<JsxElement />
</template>

image

Repro: https://github.com/leonzalion/vue-eslint-plugin-jsx-bug

I did some debugging, and I think the problem has to do with the .vue file extension when the file is passed to @typescript-eslint/parser.

This is the parserOptions configuration passed to @typescript-eslint/parser at this line: https://github.com/vuejs/vue-eslint-parser/blob/c476e7c25ab161b336339448765085e6219d0e04/src/script/index.ts#L555

 {
  comment: true,
  loc: true,
  range: true,
  tokens: true,
  ecmaVersion: undefined,
  sourceType: 'module',
  parser: '@typescript-eslint/parser',
  project: '/Users/leonzalion/projects/jsx-test/tsconfig.json',
  extraFileExtensions: [ '.vue' ],
  ecmaFeatures: { jsx: true, globalReturn: false },
  raw: true,
  eslintVisitorKeys: true,
  eslintScopeManager: true,
  filePath: '/Users/leonzalion/projects/jsx-test/src/app.vue'
}

When I modified the JavaScript code and only changed the above parserOptions.filePath parameter from app.vue to app.jsx, the linter didn’t throw any error.

I then traced down the bug through the @typescript-eslint/parser code and I think it has to do with the following line: https://github.com/typescript-eslint/typescript-eslint/blob/5e794512bf124c39de76d4e2cf8a3d6cfb08f1a8/packages/typescript-estree/src/create-program/createWatchProgram.ts#L351

I think this line causes TypeScript to try and infer the type of the file based on the file extension, and because the file extension in .vue (which TypeScript doesn’t recognize), TypeScript treats the file as a .ts file by default, causing the parsing to fail since the file is actually a JSX file.

(Sidenote: when I tried changing that line to ts.ScriptKind.TSX, it gave me the following error:) Screen Shot 2022-04-15 at 3 58 37 PM

When I modified the TypeScript compiler code and added a case ".vue": under this line, the lint then started working again (you can test this by running npx patch-package && npm lint in the repro).

As somebody who isn’t really experienced with the internals of ESLint, I’m not exactly sure what the best way to fix this is, so please let me know what you think!

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ota-meshicommented, May 25, 2022

You may be able to patch it personally by putting the following script in .eslintrc.js.

        const ts = require('typescript')
        const { ensureScriptKind } = ts
        ts.ensureScriptKind = function (fileName, ...args) {
            if (fileName.endsWith(".vue")) {
                return ts.ScriptKind.TSX
            }
            return ensureScriptKind.call(this, fileName, ...args)
        }
1reaction
ota-meshicommented, May 24, 2022

Thank you for posting this issue.

I think it’s a typescript-eslint/parser spec. It is documented in the following link. https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/parser#parseroptionsecmafeaturesjsx

I think you need to stop using JSX in .vue file, empty parserOptions.project, or use another parser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Eslint Vue 3 Parsing error: '>' expected.eslint - Stack Overflow
I'm using a Vue 3 with the "vue-ts" Vite template. VSCode is my editor and i'm obviously the ESlint plugin :) Here is...
Read more >
Sonar Scanner throws parsing errors when scanning .vue files
One parsing error refers to a line with the script tag of a vue file and says: Adjacent JSX elements must be wrapped...
Read more >
parsing error: this experimental syntax requires enabling one ...
How to Resolve "Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript'" in Vue SFC.
Read more >
Vue JavaScript Tutorial in Visual Studio Code
Vue JavaScript tutorial showing IntelliSense, debugging, and code ... to browser compatible ES5 and install the ESLint linter to detect coding errors.
Read more >
How does one go about adding Vue Components in sage10 ...
There was a key thing I missed after running yarn bud install , which was running yarn install . That threw an error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found