Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

declare global causing parse errors #570

Closed
milesj opened this issue Nov 28, 2018 · 19 comments
Closed

declare global causing parse errors #570

milesj opened this issue Nov 28, 2018 · 19 comments
Labels

Comments

@milesj
Copy link

milesj commented Nov 28, 2018

What version of TypeScript are you using?

3.0

What version of typescript-eslint-parser are you using?

21.0.1

What code were you trying to parse?

declare global {
  namespace NodeJS {
    interface Process {
      beemo: {
        context: any;
        tool: BeemoTool;
      };
    }
  }
}

What did you expect to happen?

It parse.

What happened?

It fails with this error.

/Users/Miles/Sites/beemo/packages/core/src/index.ts
  17:15  error  Parse errors in imported module './types': Cannot read property '0' of undefined (undefined:undefined)  import/export

This has happened on 2 projects now, both because of declare global.

@milesj
Copy link
Author

milesj commented Nov 28, 2018

Probably more related to export * coupled with declare global.

Here's the index file: https://github.com/milesj/beemo/blob/master/packages/core/src/index.ts#L17

And types file: https://github.com/milesj/beemo/blob/master/packages/core/src/types.ts#L62

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

@milesj
Copy link
Author

milesj commented Nov 29, 2018

This was working before a recent parser upgrade though. Do you know exactly what changed that may have caused this?

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

@milesj hmm i did some testing with plain eslint configuration, without your wrappers, on core package from your repository. And i was not able to reproduce this issue.

{
  "parser": "typescript-eslint-parser",
  "parserOptions": {
    "ecmaVersion": 8,
    "sourceType": "module",
    "jsx": true,
    "useJSXTextNode": true
  },
  "env": {
      "browser": true,
      "node": true
  },
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [
          ".js",
          ".jsx",
          ".ts",
          ".tsx"
        ]
      }
    }
  },
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings"
  ],
  "plugins": [
    "import",
    "typescript"
  ],
  "rules": {
    "no-unused-vars": "off",
    "no-undef": "off",
    "import/export": "error"
  }
}
$ eslint --ext .ts src/
Done in 1.43s.

@milesj
Copy link
Author

milesj commented Nov 29, 2018

This is the eslint config I'm using: https://github.com/milesj/build-tool-config/blob/master/packages/config/configs/eslint.js

Nothing atm stands out, except for.

'import/parsers': {
      'typescript-eslint-parser': ['.ts', '.tsx'],
},

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

yea i found it, and looks like its issue in AST after all xd

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

TypeError: Cannot read property '0' of undefined
    at isGlobalAugmentation (******\typescript-eslint-parser\analyze-scope.js:32:26)
    at Referencer.TSModuleDeclaration (******\typescript-eslint-parser\analyze-scope.js:578:13)
    at Referencer.Visitor.visit (******\typescript-eslint-parser\node_modules\esrecurse\esrecurse.js:104:34)
    at Referencer.Visitor.visitChildren (******\typescript-eslint-parser\node_modules\esrecurse\esrecurse.js:83:38)
    at Referencer.Program (******\typescript-eslint-parser\node_modules\eslint-scope\lib\referencer.js:423:14)
    at Referencer.Visitor.visit (******\typescript-eslint-parser\node_modules\esrecurse\esrecurse.js:104:34)
    at module.exports (******\typescript-eslint-parser\analyze-scope.js:685:16)
    at Object.parseForESLint (******\typescript-eslint-parser\parser.js:74:26)
    at Object.exports.parse (******\typescript-eslint-parser\parser.js:79:17)
    at parse (******\test\node_modules\eslint-module-utils\parse.js:36:17)

@milesj
Copy link
Author

milesj commented Nov 29, 2018

Oh nice! Luckily we found it.

@mysticatea mysticatea added bug and removed triage labels Nov 29, 2018
@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

this is related to JamesHenry/typescript-estree#27

@mysticatea mysticatea added triage and removed bug labels Nov 29, 2018
@mysticatea
Copy link
Member

mysticatea commented Nov 29, 2018

The cause seems that eslint-plugin-import doesn't give implicit tokens option to the parser.

@mysticatea mysticatea added bug and removed triage labels Nov 29, 2018
@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

yes, and no

issue is most likely somewhere in parser

when i have export and declare in one file tokens are empty

export const foo = "";
declare global {
}

but when only one of them tokens are present

@mysticatea
Copy link
Member

ESLint gives some implicit options: https://github.com/eslint/eslint/blob/5d451c510c15abc41b5bb14b4955a7db96aeb100/lib/linter.js#L436

  {
        loc: true,
        range: true,
        raw: true,
        tokens: true,
        comment: true,
        eslintVisitorKeys: true,
        eslintScopeManager: true,
        filePath
    }

eslint-plugin-import has some lacking implicit options: https://github.com/benmosher/eslint-plugin-import/blob/798eed7e559adab2eac07bf1b3e3518d4b7a4296/utils/parse.js#L9

  // always include and attach comments
  parserOptions.comment = true
  parserOptions.attachComment = true

  // attach node locations
  parserOptions.loc = true

  // provide the `filePath` like eslint itself does, in `parserOptions`
  // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
  parserOptions.filePath = path

This difference is the cause.

However, probably we should not assume those options.
We can have the required options in this parser itself.

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

ok, i tested it, options.tokens = true; in parser.js#L35, is solving issue.

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

@mysticatea why do check it there anyway?

we can do

if (node.declare && node.id && node.id.type === 'Identifier' && node.id.name === 'global') {
}

@mysticatea
Copy link
Member

See JamesHenry/typescript-estree#27

@armano2
Copy link
Contributor

armano2 commented Nov 29, 2018

@milesj for now you can patch it by adding tokens: true, to parserOptions: https://github.com/milesj/build-tool-config/blob/master/packages/config/configs/eslint.js#L31

@milesj
Copy link
Author

milesj commented Nov 30, 2018

Thanks both for the deep debugging! Will give that patch a try.

@armano2
Copy link
Contributor

armano2 commented Dec 3, 2018

@platinumazure this issue got fixed in last release

@platinumazure
Copy link
Member

Thanks @armano2, next time you can ensure this happens yourself by including "fixes ####" in the commit summary, commit message, or PR body 😄

Closing as this is resolved.

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

No branches or pull requests

4 participants