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

Namespace exported function return causing later code to be marked unreachable #127

Closed
blemoine opened this issue Dec 14, 2016 · 8 comments
Labels

Comments

@blemoine
Copy link

What version of TypeScript are you using?
TypeScript 2.1.4

What version of typescript-eslint-parser are you using?
[email protected]
[email protected]

What code were you trying to parse?

export namespace query {
    export interface Query {
        from: string;
    }

    export function isQuery(query: any): query is Query {
        return (query as Query).from !== undefined;
    }
}

export type SearchQuery = query.Query;

What did you expect to happen?

With

"rules": {
      'no-unreachable': 2
    }

in my config, I would expect no error (the last line is reachable, because it's exported)

What happened?

I get "Unreachable code no-unreachable" error.
When I remove the type guard function, the problem disappear.

@nzakas
Copy link
Member

nzakas commented Dec 31, 2016

Can you please paste in the complete output from ESLint?

@nzakas nzakas added bug and removed triage labels Dec 31, 2016
@blemoine
Copy link
Author

blemoine commented Jan 3, 2017

I provide more information here :

  • the code above is a in file test.ts
  • my full config is :
    {
       "parser": "typescript-eslint-parser",
       "rules": {
          "no-unreachable": 2
       }
    }
    

And the full output from eslint is:

/Users/******/tslint-bug/test.ts
  11:5  error  Unreachable code  no-unreachable

✖ 1 problem (1 error, 0 warnings)

Do you need any other informations?

@JamesHenry
Copy link
Member

I can reproduce the issue with your example, thanks @blemoine. I will look into this!

@JamesHenry JamesHenry self-assigned this Jan 4, 2017
@JamesHenry
Copy link
Member

The type guard is actually not relevant... It is the return statement inside an exported function from the namespace that is the key.

Here is the minimal example for reproducing the issue:

export namespace foo {
    export function bar() {
        return;
    }
}
export type Qux = true;

export type Qux = true; will currently be flagged as unreachable.

@JamesHenry JamesHenry changed the title code marked as unreachable when using type guard Namespace exported function return causing later code to be marked unreachable Jan 4, 2017
@soda0289
Copy link
Member

soda0289 commented Feb 7, 2017

Here is another example:

/* eslint-disable no-undef */
namespace foo {

   function bar() {
        return;
   }

   //Unreachable code.
   function baz() {
        return;
   }
}

I think the problem is that the code analyzer is not creating a new code path when the function ends. Will look into this more tonight, might need to patch eslint or create a new rule in the plugin.

@soda0289
Copy link
Member

I spent some more time on this.
The issue is that functions inside namespaces are given node type: TSNamespaceFunctionDeclaration instead of standard FunctionDeclaration.
https://github.com/eslint/typescript-eslint-parser/blob/master/lib/ast-converter.js#L882-L887

Not sure why this is the case but if you keep the functions type to FunctionDeclartion the rule works as expected.

@soda0289
Copy link
Member

Looks like it was to fix a bug with empty body function in namespaces:
PR: #82
Issue: #78

Maybe we can have it so if the there is a body to the function we keep the type to FunctionDeclaration.

What are your thoughts @JamesHenry

@mightyiam
Copy link

Makes sense to me that a function declaration remains of node type FunctionDeclaration regardless of the context in which it is in.

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

6 participants