babel / babel-eslint

:tokyo_tower: A wrapper for Babel's parser used for ESLint (renamed to @babel/eslint-parser)
https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser
MIT License
2.96k stars 208 forks source link

Parse error on `import typeof` in `declare module` #775

Closed lydell closed 5 years ago

lydell commented 5 years ago

Repro repo: https://github.com/lydell/babel-eslint-flow-import-issue

package.json:

{
  "private": true,
  "scripts": {
    "test": "eslint test.js"
  },
  "dependencies": {
    "babel-eslint": "10.0.2",
    "eslint": "6.0.0"
  }
}

.eslintrc.json:

{
  "parser": "babel-eslint"
}

test.js:

// @flow

declare module "cross-spawn" {
  import typeof childProcess from "child_process";

  declare module.exports: {
    sync: $PropertyType<childProcess, "spawnSync">,
  };
}

Result:

$ npm test

> @ test /home/lydell/stuff/babel-eslint
> eslint test.js

/home/lydell/stuff/babel-eslint/test.js
  4:3  error  Parsing error: Imports within a `declare module` body must always be `import type` or `import typeof`

  2 | 
  3 | declare module "cross-spawn" {
> 4 |   import typeof childProcess from "child_process";
    |   ^
  5 | 
  6 |   declare module.exports: {
  7 |     sync: $PropertyType<childProcess, "spawnSync">,

✖ 1 problem (1 error, 0 warnings)

npm ERR! Test failed.  See above for more details.

Expected: No errors.

Flow itself accepts this input.

lydell commented 5 years ago

Just realized this is a bug in @babel/parser, so I filed a bug there: https://github.com/babel/babel/issues/10124

But I think the bug is most likely to be found by babel-eslint users.

lydell commented 5 years ago

Fixed by https://github.com/babel/babel/pull/10132.