xojs / xo

❤️ JavaScript/TypeScript linter (ESLint wrapper) with great defaults
MIT License
7.61k stars 290 forks source link

Unexpected token with `import with type` assertion #727

Open remisture opened 11 months ago

remisture commented 11 months ago

Seems like xo struggle to parse this. What can I do?

const serviceAccount = await import('../serviceAccount.json', { assert: { type: 'json' } });

"Parsing error: Unexpected token , "

fregante commented 11 months ago

Not an XO issue:

Import attributes are not yet finalized so ESLint does not support them yet.

fregante commented 1 month ago

It turns out this is supported by TypeScript, and XO does ship with tsc and TS ESlint plugins. So what you can do is just setting this in your package.json so that XO/ESLint will use TypeScript's parser even on JS files:

    "xo": {
        "parser": "@typescript-eslint/parser"
    },

Seen in https://github.com/refined-github/shorten-repo-url/pull/48/files#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346

Should this be documented? Or maybe become XO's default? The problem might be performance though. cc @sindresorhus

sindresorhus commented 1 month ago

Not sure we could do it by default (for both JS and TS files) as it could have slightly different semantics than JS. I think the best would be to simply pass the TS parser into the config when the TS config uses flat config.