Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Circular flow type definitions trigger no-use-before-define #485

Closed
qm3ster opened this issue Jun 8, 2017 · 5 comments
Closed

Circular flow type definitions trigger no-use-before-define #485

qm3ster opened this issue Jun 8, 2017 · 5 comments

Comments

@qm3ster
Copy link

qm3ster commented Jun 8, 2017

Brought here from eslint/eslint#8703, by @soda0289 request:
When using core rule no-use-before-define, default for example in standard, it is applied to type definitions.
This could be a good thing, but the current implementation gives an unavoidable error on circular definitions:

/* @flow */
type File = {chunks: Array<Chunk>}
type Chunk = {file: File}

produces

.../src/commands.js
  2:21  error  'File' was used before it was defined  no-use-before-define

✖ 1 problem (1 error, 0 warnings)

OR

/* @flow */
type Chunk = {file: File}
type File = {chunks: Array<Chunk>}

produces

.../src/commands.js
  2:28  error  'Chunk' was used before it was defined  no-use-before-define

✖ 1 problem (1 error, 0 warnings)

respectively.

Currently, disabling no-use-before-define in the files that have flow enabled (all for me) is a fix when actual flow is checked, since that catches undefined.
However, it is still probably a bug, since:

// This is an actual error
const a = {b}
const b = {a}
// While this works and is often necessary
type A = {b:B}
type B = {a:A}
@brendanzab
Copy link

I worked around this by disabling the check inline:

/* eslint-disable no-use-before-define */

export type GetState = () => State;
export type ThunkAction = (dispatch: Dispatch, getState: GetState) => any;
export type PromiseAction = Promise<Action>;
export type Dispatch = (action: Action | ThunkAction | PromiseAction) => any;

/* eslint-enable no-use-before-define */

joa added a commit to joa/babel-eslint that referenced this issue Jan 21, 2018
A type alias shouldn't trigger a no-use-before-define warning just
like a function declaration.

Cyclic type dependencies are common when using flow.
For instance: type Node<T> = { head: T; tail: Node<T> }

Fixes babel#485
mwiencek added a commit to metabrainz/musicbrainz-server that referenced this issue Feb 10, 2018
It currently conflicts with Flow, see
babel/babel-eslint#485
@hzoo hzoo closed this as completed in #584 Sep 25, 2018
hzoo pushed a commit that referenced this issue Sep 25, 2018
A type alias shouldn't trigger a no-use-before-define warning just
like a function declaration.

Cyclic type dependencies are common when using flow.
For instance: type Node<T> = { head: T; tail: Node<T> }

Fixes #485
@hzoo hzoo mentioned this issue Sep 27, 2018
@hzoo hzoo reopened this Sep 27, 2018
@hzoo
Copy link
Member

hzoo commented Sep 27, 2018

Had to revert this because this was causing many issues in other rules due to the AST change in #697, https://github.com/babel/babel-eslint/releases/tag/v10.0.1

@techieshark
Copy link

Tip for folks putting types in a types/ (etc) specific directory, you might add an eslint config file to disable the rule for that directory:

// .eslintrc.js
module.exports = {
  'rules': {
    'no-use-before-define': 'off',
  },
};

@linonetwo
Copy link

Maybe add another option in config to allow type be ignored by this rule? Both flow and typescript.

@kaicataldo
Copy link
Member

Thank you for the issue. Now that @babel/eslint-parser has been released, we are making this repository read-only. If this is a change you would still like to advocate for, please reopen this in the babel/babel monorepo.

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

No branches or pull requests

7 participants