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

Parsing Error: Unexpected token typescript inside Vue files #659

Closed
masterofnothing11 opened this issue Oct 21, 2019 · 8 comments
Closed

Comments

@masterofnothing11
Copy link

Hello, first of all I would like to state I know this is not the right place to make such question but I have tried other means but got no response. This leaves me no choice other than to come to the repository itself.

I'm getting the following error when watching for files rebuild from ESLint

/var/www/assets/js/app/pages/ErpFlex.vue
  140:10  error  Parsing error: Unexpected token

   9 |   @Getter getTotal: number;
  10 | 
> 11 |   private resendOrder: any = null;
     |           ^
  12 |   private loadingOrders: boolean = true;
  13 |   private callout: object = {
  14 |     show: false,

In my webpack configuration I have added typeScriptLoader:

.enableTypeScriptLoader(function (typeScriptConfigOptions) {
        typeScriptConfigOptions.transpileOnly = true;
        typeScriptConfigOptions.configFile = path.resolve(__dirname, 'tsconfig.json');
    })

My tsconfig.json file looks like:

{
  "exclude": [
    "vendor",
    "/node_modules/"
  ],
  "include": [
    "**/*.ts",
    "**/*.vue"
  ],
  "compilerOptions": {
    "target": "es6",
    "allowJs": true,                       /* Allow javascript files to be compiled. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

Probably I'm looking at something pretty stupid from my part here. Any help would be much appreciated

@Lyrkan
Copy link
Collaborator

Lyrkan commented Oct 21, 2019

Hi @masterofnothing11,

You'll need to configure ESLint to use typescript-eslint: https://github.com/typescript-eslint/typescript-eslint#how-do-i-configure-my-project-to-use-typescript-eslint

This can be done by either using the options callback of Encore.enableEslintLoader() or through an .eslintrc.* file (note that in the latter case you'll currently have to delete options.parser in the enableEslintLoader's callback to be able to override the parser from an external file)

@masterofnothing11
Copy link
Author

@Lyrkan Thank you for the answer.
I have already done that with the following:

.enableEslintLoader(eslLinterLoaderOptions => {
        eslLinterLoaderOptions.configPath = path.resolve(__dirname, '.eslintrc.js');
        eslLinterLoaderOptions.ignorePath = path.resolve(__dirname, '.eslintignore');
        eslLinterLoaderOptions.cache = false;

        // Encore enforce parser option to babel-eslint and its not compatible with eslint-plugin-vue
        // Check the following link: https://github.com/symfony/webpack-encore/issues/656
        delete eslLinterLoaderOptions.parser;
    })

    .configureLoaderRule('eslint', loader => {
        loader.test = /\.(jsx?|vue)$/;
    })

with my .eslintrc file:

module.exports = {
    "parser": "vue-eslint-parser",
    "parserOptions": {
        "parser": "babel-eslint",
        "allowImportExportEverywhere": true,
        "ecmaFeatures": {
            "legacyDecorators": true,
            "modules": true,
            "experimentalObjectRestSpread": true
        },
        "ecmaVersion": 6,
        "sourceType": "module",
    },
    "extends": [
        "plugin:vue/base",
        "airbnb-base"
    ],
    "rules": {
        "indent": [2, 4],
        "vue/html-indent": [2, 4],
        "camelcase": [2, {properties: "always"}],
        "new-cap": [2, {"newIsCap": true}],
        "padding-line-between-statements": [
            "error",
            {blankLine: "always", prev: ["const", "let", "var"], next: "*"},
            {blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}
        ],
        "space-infix-ops": "error",
        "no-var": "error",
        "one-var": [2, "always"],
        "vars-on-top": "error",
        "quotes": ["error", "single"],
        "prefer-template": "error",
        "eqeqeq": "error",
        "default-param-last": ["error"],
        "prefer-arrow-callback": "error",
        "no-param-reassign": [
            "error",
            {
                "props": true,
                // ignore vuex files
                "ignorePropertyModificationsFor": [
                    "state"
                ]
            }
        ],
        "import/no-cycle": "off",
        "import/no-unresolved": "off",
        "@typescript-eslint/explicit-member-accessibility": "off"
    },
    "env": {
        browser: true,
        es6: true,
        node: true
    },
};

I think everything is well configured but I still can't figure out on why I'm getting such error.

@Lyrkan
Copy link
Collaborator

Lyrkan commented Oct 21, 2019

You're not using typescript-eslint in your .eslintrc file, so everything is going through babel-eslint which does not support TypeScript.

Replace babel-eslint by @typescript-eslint/parser and add the @typescript-eslint plugin to your conf (see https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#usage).

@masterofnothing11
Copy link
Author

@Lyrkan Thank you for your answer and helping me out.

I have installed typescrpit loader: yarn add @typescript-eslint/eslint-plugin --save-dev and added to the .eslintrc.js file:

 "parserOptions": {
        "parser": "@typescript-eslint/parser"
},
"plugins": ["@typescript-eslint"]

and now I'm getting a error Parsing error: Cannot find module '@typescript-eslint/parser' any idea on why?

@Lyrkan
Copy link
Collaborator

Lyrkan commented Oct 21, 2019

You only added the @typescript-eslint/eslint-plugin to your dependencies, the parser is in another package: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser

@masterofnothing11
Copy link
Author

@Lyrkan Solved my problem. Thank you very much. Have a wonderful week 👍

weaverryan added a commit that referenced this issue Mar 20, 2020
This PR was merged into the master branch.

Discussion
----------

Remove ESLint user-related config

Hi ✋

This PR is a proposal for #657 implementation and should fix issues encountered in #473, #574, #656, #659, and #685.

As discussed in #657, it would be better if Encore didn't configure ESLint itself. It should be done by the end user in an configuration file (e.g.: `.eslintrc.js`)

ESLint's `parser` option is not configured by default anymore, and `babel-eslint` requirement has been removed. We can considering this as a breaking change, end users should now configure `parser: 'babel-eslint` themselves.

Method `Encore.enableEslintLoader('extends-name')` has been deprecated and undocumented, in order to prevent end users to configure ESLint through Encore.

A nice message is also displayed when no ESLint configuration is found:
![Capture d’écran de 2020-01-12 11-15-09](https://user-images.githubusercontent.com/2103975/72217430-dfa2a480-352d-11ea-96e3-0e77236127d6.png)
I couldn't use `FriendlyErrorsPlugin` for this because error is not coming from Webpack. If someone has a better idea... 😕

**Pros:**
- end users have now full control hover ESLint configuration **by default**
- no need to `delete options.parser` when trying to use [`eslint-plugin-vue`](https://github.com/vuejs/eslint-plugin-vue) or [`@typescript-eslint/parser`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser)
- IDEs will be able to integrate ESLint (if they support it)

**Cons:**
- end users should now configure `parser` option to `babel-eslint` themselves
- end users will have to move their config to external config file, but it's ok

What do you think?
Thanks.

**EDIT:** tests failures are unrelated I think.

Commits
-------

9d3b02f tweaking wording and order
d23982a Display message if no ESLint configuration is found
c828b32 Deprecate `Encore.enableEslintLoader('extends-name')`
9f31d95 Add test for ESLint with external configuration (and babel-eslint usage)
3ba6cf2 Remove babel-eslint from ESLint configuration
@Uhelliton
Copy link

@Lyrkan Solved my problem.

@my-name-is-nheo
Copy link

@Lyrkan thank you very much

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

4 participants