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: 'import' and 'export' may appear only with 'sourceType: module' #163

Closed
johanbook opened this issue Oct 28, 2020 · 8 comments
Labels

Comments

@johanbook
Copy link

johanbook commented Oct 28, 2020

This seems like a really cool eslint config! I gave it a try on a typescript project with eslint config

{
  "extends": ["hardcore", "hardcore/fp", "hardcore/node"],
  "overrides": [
    {
      "files": ["*.js"],
      "extends": ["hardcore/ts-for-js"],
      "parserOptions": { "project": "./tsconfig.json" }
    }
  ]
}

and get the error

/home/pong/utils/src/cookies/cookies.ts
  3:1  error  Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

I tried with a ts config of {} as suggested in readme.

I am using E6 syntax and want to use the import/export syntax. Do you know what has gone wrong here?

@EvgenyOrekhov
Copy link
Owner

The default value for sourceType is script (see https://eslint.org/docs/user-guide/configuring#specifying-parser-options). If you want to use ES6 modules, you have to set "sourceType": "module" in parserOptions.

Regarding TypeScript, I have not tried using eslint-config-hardcore with TypeScript, and I have no idea whether it would work or not. And the hardcore/ts-for-js config is meant for linting JavaScript projects, not TypeScript projects (it contains only a small subset of rules, specific to JavaScript).

@EvgenyOrekhov
Copy link
Owner

Please let me know if "sourceType": "module" fixed your issue.

@EvgenyOrekhov
Copy link
Owner

BTW, I can see that Node.js 14 was promoted to LTS two days ago, which means now we should be able to use ES6 modules both on client side and on server side. I guess I should set "sourceType": "module" as default in eslint-config-hardcore then. And also discourage the use of commonjs. Created #164.

@johanbook
Copy link
Author

In which cases would you want to use eslint-typscript for regular javascript?

I tried adding some variations of parserOptions but seems to get

> eslint `src/**/*.ts`


Oops! Something went wrong! :(

ESLint: 7.12.1

No files matching the pattern "/home/pong//utils" were found.
Please check for typing mistakes in the pattern.

Bit unfamiliar to Eslint, do you know why this might be?

@EvgenyOrekhov
Copy link
Owner

In which cases would you want to use eslint-typscript for regular javascript?

  • when you use new ES features, like class properties or optional chaining, and want to run additional checks on those
  • when you want to run additional type-aware checks

Here are some useful typescript-eslint rules that work with JavaScript (they all are included in hardcore/ts-for-js):


No files matching the pattern "/home/pong//utils" were found.

Looks like you are facing a known issue with eslint-plugin-import and its import/no-unused-modules rule, see import-js/eslint-plugin-import#1645. I guess you should just disable this rule.

Let me know if that helps.

@EvgenyOrekhov
Copy link
Owner

BTW, I noticed you have "files": ["*.js"] in your overrides section. If you want to lint *.ts files, you should change it to "files": ["*.ts", "*.js"].

@johanbook
Copy link
Author

Aha, I see. With the config

{
  "extends": ["hardcore", "hardcore/fp", "hardcore/node"],
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "script"
  },
  "overrides": [
    {
      "files": ["*.ts"],
      "extends": ["hardcore/ts-for-js"],
      "parserOptions": { "project": "./tsconfig.json" },
      "rules": {"import/no-unused-modules": "off"}
    }
  ]
}
> eslint 'src/**/*.ts'


Oops! Something went wrong! :(

ESLint: 7.12.1

TypeError: Cannot read property 'node' of undefined
Occurred while linting /home/pong/test-hardcore/src/cookies/index.ts:1
    at /home/pong/test-hardcore/node_modules/eslint-plugin-import/lib/rules/no-namespace.js:28:26
    at Array.find (<anonymous>)
    at ImportNamespaceSpecifier (/home/book/@book/utils-hardcore/node_modules/eslint-plugin-import/lib/rules/no-namespace.js:27:50)
    at /home/book/@book/utils-hardcore/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/home/book/@book/utils-hardcore/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/book/@book/utils-hardcore/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
    at NodeEventGenerator.applySelectors (/home/book/@book/utils-hardcore/node_modules/eslint/lib/linter/node-event-generator.js:283:22)
    at NodeEventGenerator.enterNode (/home/book/@book/utils-hardcore/node_modules/eslint/lib/linter/node-event-generator.js:297:14)
    at CodePathAnalyzer.enterNode (/home/book/@book/utils-hardcore/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:711:23)
npm ERR! code ELIFECYCLE

But then I'll look for another config for typescript ☺️

@EvgenyOrekhov
Copy link
Owner

TypeError: Cannot read property 'node' of undefined

That's unfortunate :( I can see in the stack trace that it's related to the import/no-namespace rule. Feel free to report it to eslint-plugin-import's repo.

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

No branches or pull requests

2 participants