coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

React error: Import in body of module; reorder to top

Open ChristerNilsson opened this issue 9 years ago • 16 comments

I have to manually move one line of code after every compilation.

// Generated by CoffeeScript 2.0.3
var App, code, div, fix, h1, header, img, p;

import React, {
  Component
} from 'react';

import logo from './logo.svg';

import './App.css';

must be changed to

// Generated by CoffeeScript 2.0.3
import React, {
  Component
} from 'react';

import logo from './logo.svg';

import './App.css';

var App, code, div, fix, h1, header, img, p;

to avoid this error message from React:

capture

App.txt

ChristerNilsson avatar Dec 01 '17 21:12 ChristerNilsson

Hi!

The messages in your screen shot are not from React. They are from eslint-plugin-import, specifically the import/first rule.

Both pieces of code are actually equivalent! If you run them, you'll see that both work exactly the same. However, your tooling seems to prevent you from running if the import/first rule is not satisfied. It does not make much sense to run ESLint on the compiled JS from a CoffeeScript program, so I'd recommend not doing that.

lydell avatar Dec 01 '17 21:12 lydell

Really hard to tell eslint to stop checking this. This is the only way I could make it work:

###
eslint-disable 
###

I tried # eslint-disable but that does not work.

ChristerNilsson avatar Dec 02 '17 00:12 ChristerNilsson

Ran into this with create-react-coffee-app

Another workaround would be to add backticks so the above code would be:

`
import React, { Component } from 'react'
`

class App extends Component
  render: ->
    <div>Hello React</div>

quangv avatar Apr 16 '18 14:04 quangv

I recently got this problem too. Import statements must ahead of declaration statements. This line code below should not in there . var App, code, div, fix, h1, header, img, p;

JaylyLiu avatar May 30 '18 03:05 JaylyLiu

While ### eslint-disable ### fixes this specific issue, there are other linter rules that cause problems. Only feasable workaround for CRA without ejecting i found was to replace their linter config (with an npm task) before building.

Inve1951 avatar Jun 01 '18 20:06 Inve1951

For the record:

/* eslint-disable import/first */

bichotll avatar Feb 28 '19 17:02 bichotll

I recently got this problem . Import statements must declare before the variable. //example import React, { Component } from "react"; import { withRouter } from "react-router-dom";

import { Container, Grid, Card, Button, Form, Icon, Table } from "tabler-react"; var user = xyz.

bojegowda avatar Aug 01 '19 08:08 bojegowda

how do I disable eslint in a Create-React-App? I tried add an .eslintignore file, did nothing

ORESoftware avatar Feb 13 '20 00:02 ORESoftware

To clarify, you can put the following at the top of .coffee files (worked for me):

###
eslint-disable import/first
###

Only block level comments work and this will be compiled to a js block level comment:

/* eslint-disable import/first */

rbabayoff avatar Feb 19 '20 19:02 rbabayoff

I'm a bit new to this JS library, where should one mention '/* eslint-disable import/first */' ?

Grv-Singh avatar Sep 19 '20 12:09 Grv-Singh

Is there a way to disable this lint in a project-wide setting? Unfortunately .eslintignore doesn't help:-(

kayoch1n avatar Mar 05 '21 08:03 kayoch1n

@kayoch1n This is not a CoffeeScript question, but rather an eslint question. See Configuring ESLint. For example, you can make .eslintrc.yaml contain the following:

rules:
  import/first: off

edemaine avatar Mar 05 '21 14:03 edemaine

in react you get this error when you declare a variable in between the imports So its better to write the code in the last after the final import is done

babul101 avatar Aug 30 '21 16:08 babul101

this really affects my error

/* eslint-disable import/first */

PassyahRaffi avatar Jun 14 '22 04:06 PassyahRaffi

Hi!

The messages in your screen shot are not from React. They are from eslint-plugin-import, specifically the import/first rule.

Both pieces of code are actually equivalent! If you run them, you'll see that both work exactly the same. However, your tooling seems to prevent you from running if the import/first rule is not satisfied. It does not make much sense to run ESLint on the compiled JS from a CoffeeScript program, so I'd recommend not doing that.

With respect, I cannot generate code where the imports are always at the top. I wish there were a compile option to the coffee binary for this. ESLint is commonly used, and saying not to run it - where in many projects it's a given - doesn't seem like a solution. I've looked lower on this thread, and disabling the warning for it doesn't feel like a fix either.

I understand your normally would not run a linter on the produced JS, but I am thinking of large projects that are a mix of both Coffeescript & JS, where it would be tedious to disable the warning per-file.

Would love more attention on this. :-)

blitmap avatar Jan 09 '23 00:01 blitmap

Would love more attention on this. :-)

The var line that’s added at the top scope exists because the code in question has variables declared at the top scope. Another solution is to simply not declare any variables at the top scope. For example, instead of this:

import React, { Component } from 'react'

class App # Will cause a `var` line at the top of the file
  constructor: ->

Do this:

import React, { Component } from 'react'

do ->
  class App # Will cause a `var` line at the top of the inline function created by `do ->`
    constructor: -> # Do something

Or prepend export:

import React, { Component } from 'react'

export class App # Will not cause a `var` line
  constructor: -> # Do something

There are many ways to avoid the top-scope var line; you use the playground to investigate.

GeoffreyBooth avatar Jan 09 '23 00:01 GeoffreyBooth