Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fat arrow method definitions #935

Closed
dadamssg opened this issue Jun 26, 2017 · 52 comments

Comments

@dadamssg
Copy link

commented Jun 26, 2017

Is disallowing defining methods via fat arrows a rule or a bug? This is valid syntax with babel and I'm using babel-eslint but the rule is no-undef which I believe is wrong.

image

This is really helpful because it auto-binds this to the component instance. If you were to use the non fat arrow syntax, you would need to manually bind this to that method.

This is enabled by the transform-class-properties babel plugin which i have enabled in my .babelrc

{
  "presets": [
    "stage-0",
  	["es2015", {"modules": false}],
  	"react"
  ],
  "plugins": [
    "transform-class-properties"
  ]
}
@eddiemonge

This comment has been minimized.

Copy link

commented Jun 28, 2017

That plugin also enables

static PropTypes = {
  ...
}

which Standard is complaining about

@Talor-A

This comment has been minimized.

Copy link

commented Jun 28, 2017

another example that I believe is related, standard doesn't like it when writing your react state this way:

class MyComponent extends Component {
  state = {}
  ...

this is convenient because it's much more readable than the alternative:

class MyComponent extends Component {
  constructor (props) {
    super(props)
    this.state = {}
  }
  ...
@bcomnes

This comment has been minimized.

Copy link
Member

commented Jun 28, 2017

@dadamssg

This comment has been minimized.

Copy link
Author

commented Jun 28, 2017

I think it has something to do with babel-eslint but i have this in my package.json and it still isn't working.

image

@eddiemonge

This comment has been minimized.

Copy link

commented Jun 28, 2017

If I use babel-eslint then that complains about double quotes in JSX.

@dadamssg

This comment has been minimized.

Copy link
Author

commented Jun 28, 2017

That's just a normal rule. Second bullet point here

@eddiemonge

This comment has been minimized.

Copy link

commented Jun 28, 2017

Except its not. If I don't use babel-eslint as the parser, then Standard does not complain about it.

@dadamssg

This comment has been minimized.

Copy link
Author

commented Jun 28, 2017

It might not have been linting your jsx properly if you weren't using the babel-eslint parser with the react preset.

¯\_(ツ)_/¯

@bcomnes

This comment has been minimized.

Copy link
Member

commented Jun 28, 2017

I think if you are not using babel-eslint with JSX, the standard linter won't parse the JSX blocks, thus missing any lint errors that may be in those sections of code. Not 100% on that.

@bcomnes

This comment has been minimized.

Copy link
Member

commented Jun 28, 2017

The other issue is that class properties assigned to arrow functions is still in babel land iirc: http://babeljs.io/docs/plugins/transform-class-properties/

If you are using babel transforms to access future JS features, you are definately going to want to turn on the babel parser.

@bcomnes

This comment has been minimized.

Copy link
Member

commented Jun 28, 2017

Yeah class properties are still in the ES Next zone: http://kangax.github.io/compat-table/esnext/#test-class_fields_class_properties

If you are using these features, you must have the babel-eslint parser enabled.

@feross

This comment has been minimized.

Copy link
Member

commented Jun 28, 2017

If babel-eslint is set up correctly then using experimental syntax should work. Can you share the output of:

$ standard --version
$ npm ls standard
$ npm ls babel-eslint
@dadamssg

This comment has been minimized.

Copy link
Author

commented Jun 28, 2017

standard --version  = 10.0.2
npm ls standard     = standard@10.0.2
npm ls babel-eslint = babel-eslint@7.2.3
@dadamssg

This comment has been minimized.

Copy link
Author

commented Jun 28, 2017

I think I figured it out. I was attempting to convert my project from using my own set of eslint rules to standard. Incidentally, I had "eslint": "^4.0.0", in my package.json file.

After noticing the unmet peer dependency that standard has on eslint@3.19.0, I....

  • removed the eslint and eslint-plugin-react dependencies from my package.json file
  • removed my own(leftover) .eslintrc file
  • did a rm -rf node_modules/
  • deleted my yarn.lock file
  • did a fresh yarn install

and now everything is working as expected.

@feross I'll let you close this issue if you think it's resolved or wait on the others to see if what I did works for them as well.

@joke2k

This comment has been minimized.

Copy link

commented Nov 30, 2017

Hi @dadamssg, your solution doesn't work for me.
My project was started with create-react-app script then I yarn add standard and in my package.json:

"standard": {
    "parser": "babel-eslint"
  },
class Please extends Component {
  work = () => { console.log('cmon') }
  render () { ... }
}

returns: error 'work' is not defined

npm ls standard
└── standard@10.0.3

npm ls babel-eslint
├── babel-eslint@8.0.2 
└─┬ react-scripts@1.0.16
  └── babel-eslint@7.2.3

npm ls eslint
├─┬ react-scripts@1.0.16
│ └── eslint@4.10.0 
└─┬ standard@10.0.3
  └── eslint@3.19.0
@dadamssg

This comment has been minimized.

Copy link
Author

commented Nov 30, 2017

@joke2k I can only guess that it's because you have eslint 4.10.0 installed alongside eslint 3.19.0 because of the requirement from react-scripts. I bet if you removed react-scripts, and thus eslint 4.10.0, and only had 3.19.0 it would work.

@joke2k

This comment has been minimized.

Copy link

commented Nov 30, 2017

Yeah! @dadamssg you were right!
But uninstalling react-script... I cannot execute start/build/test 😆
This is a mess

@dadamssg

This comment has been minimized.

Copy link
Author

commented Nov 30, 2017

@joke2k haha sorry to hear that! Unfortunately, I don't have any experience with create-react-app/react-scripts so I can't help you there :/

@joke2k

This comment has been minimized.

Copy link

commented Dec 2, 2017

It seems that we have to wait standard 11

@IlyasAbdighni

This comment has been minimized.

Copy link

commented Dec 12, 2017

Did someone find any solution?

@GBarkhatov

This comment has been minimized.

Copy link

commented Jan 3, 2018

Any solutions?

@ematipico

This comment has been minimized.

Copy link
Contributor

commented Jan 3, 2018

Have you tried with standard@beta?

@GBarkhatov

This comment has been minimized.

Copy link

commented Jan 3, 2018

Yep, not working

@Tommos0

This comment has been minimized.

Copy link

commented Jan 3, 2018

I'm having the same issue. Using parser: babel-eslint doesn't help.

└─ eslint@3.19.0
└─ babel-eslint@8.1.2
└─ standard@10.0.3
@Flet

This comment has been minimized.

Copy link
Member

commented Jan 3, 2018

@Tommos0 does npm install standard@beta work better? It uses eslint 4 instead which should help avoid some conflicts.

@Dean177

This comment has been minimized.

Copy link

commented Jan 4, 2018

@Flet using the beta version of standard resolved the issues I was having with ‘babel -eslint’

@KITSMattStyles

This comment has been minimized.

Copy link

commented Jan 9, 2018

Upgrading to beta helped me also. Despite npm ls eslint showing only version 3.x under standard, something else must have installed version 4.x. Updating standard to use the same version fixed it. Pretty sure this isn't actually an issue with standard.

@franky47

This comment has been minimized.

Copy link

commented Jan 19, 2018

I also have this issue with the following setup:

$ npm -ls -g --depth=0
├── babel-eslint@8.2.1
├── eslint@4.15.0
├── standard@11.0.0-beta.0
...

Example code :

// test.js
export default class Foo {
  // Static class fields - https://github.com/tc39/proposal-static-class-features/
  static propTypes = {}

  // Class fields - https://github.com/tc39/proposal-class-fields
  state = {
    foo: 'foo'
  }

  // Fat arrow method
  foo = () => this.state.foo
}

Result:

$ standard test.js
test.js:3:20: Parsing error: Unexpected token =

$ standard --parser babel-eslint test.js
test.js:3:10: 'propTypes' is not defined.
test.js:6:3: 'state' is not defined.
test.js:11:3: 'foo' is not defined.
@ip413

This comment has been minimized.

Copy link

commented Jan 25, 2018

Doesn't work with beta version 11.0.0-beta.0

Still have Parsing error: Unexpected token = for xx = () => {...

@franky47

This comment has been minimized.

Copy link

commented Jan 26, 2018

@bcomnes yep, tried that initially, no luck with either local or global standard, babel-eslint and eslint.

Edit: alright I've got a combination working:

standard@11.0.0-beta.0
babel-eslint@8.0.0-alpha.17
eslint@4.15.0

The confusing thing seems to come from babel-eslint's apparent misuse of semver: 8.0.0-alpha.17 is tagged as next, but the latest stable is 8.2.1, go figure.

@bcomnes

This comment has been minimized.

Copy link
Member

commented Jan 26, 2018

iirc you also need to ensure the correct babel transforms, I forget if env ships class fields or not.

@azizhk

This comment has been minimized.

Copy link

commented Feb 5, 2018

babel-eslint's version is what is causing the issue, downgraded babel-eslint to ^7.0.0 and the error stopped appearing.

@jnreynoso

This comment has been minimized.

Copy link

commented Feb 9, 2018

Any solutions?

@azizhk

This comment has been minimized.

Copy link

commented Feb 9, 2018

@jnreynoso Are you using prettier-standard?
You will have to downgrade that as well.
Otherwise only downgrading babel-eslint will solve this problem.

@fedorinoGore

This comment has been minimized.

Copy link

commented Feb 9, 2018

Any solutions? None of the listed works for me...
still having error while using class member field Parsing error: Unexpected token = for
class XX { xx = {...} }
"babel-eslint": "^7.2.3"
"eslint": "^4.17.0"
"standard": "^11.0.0-beta.0"

@jnreynoso

This comment has been minimized.

Copy link

commented Feb 9, 2018

@azizhk prettier-standard solved this problem?

@azizhk

This comment has been minimized.

Copy link

commented Feb 9, 2018

@fedorinoGore my setup is:
"babel-eslint": "^7.0.0"
"standard": "^10.0.3"
"eslint": "^3.19.0"

@jnreynoso
I removed prettier-standard for now. You can use prettier-eslint with standard config, that might give you better control. sheerun/prettier-standard#30 (comment)

@fedorinoGore

This comment has been minimized.

Copy link

commented Feb 10, 2018

@jnreynoso @azizhk I'm not using Prettier for now... But the problem persist.

@azizhk

This comment has been minimized.

Copy link

commented Feb 10, 2018

Run these three commands and write the results here.
yarn list babel-eslint
yarn list standard
yarn list eslint
or if you are using npm
npm ls babel-eslint
npm ls standard
npm ls eslint

@fedorinoGore

This comment has been minimized.

Copy link

commented Feb 12, 2018

npm ls babel-eslint
└── babel-eslint@7.2.3
npm ls standard
└── standard@11.0.0-beta.0
npm ls eslint
├── eslint@4.17.0
└─┬ standard@11.0.0-beta.0
  └── eslint@4.13.1

I made several upgrades recently trying to escape from other issue with eslint and babel-styled-components plugin
So now my configuration differ from initial posted

@azizhk

This comment has been minimized.

Copy link

commented Feb 12, 2018

Ok so based on these issues:
babel/babel-eslint#566
babel/babel-eslint#564
babel/babel-eslint#589
I thought this was an issue with babel-eslint and downgraded it

But it might also be of having mixed dependency versions of babel (babel/babel-eslint#585)
standard@11 depends on babel@7 which must be causing multiple babel versions.
Can you try either of these:

  • Downgrade standard to 10 or
  • Upgrade all babel dependencies to 7?
@fedorinoGore

This comment has been minimized.

Copy link

commented Feb 12, 2018

I really do afraid to upgrade babel and all of its dependencies :)
so for the starter tried to downgrade standard.
And things getting worse. Not standard claims that arrow function class member is undefined
2018-02-12 18 17 30
2018-02-12 18 17 19

@Rachomir

This comment has been minimized.

Copy link

commented Feb 15, 2018

I am having same problems with fat arrow functions. :(

@will-hart

This comment has been minimized.

Copy link

commented Feb 15, 2018

I found a combination that works for me with a create-react-app project. In my package.json (Windows 10) I have:

  "scripts": { 
    "lint": "./node_modules/.bin/standard --parser=babel-eslint | ./node_modules/.bin/snazzy"
  },
  "standard": {
    "parser": "babel-eslint"
  },
  "devDependencies": {
    "babel-eslint": "8.2.1",
    "eslint": "4.17.0",
    "snazzy": "^7.0.0",
    "standard": "^11.0.0-beta.0"
  }

At this stage yarn lint was passing, but class arrow functions and static properties were flagged as errors in VS Code by the chenxsan.vscode-standardjs plugin. I had to add the following line to my .vscode/settings.json file for the workspace:

  "standard.usePackageJson": true

Hopefully that helps somebody else!

@tlvince

This comment has been minimized.

Copy link

commented Feb 21, 2018

This is fixed for me in v11.0.0 (and using babel-eslint). Thanks!

@Kikobeats

This comment has been minimized.

Copy link

commented Feb 21, 2018

Me too!

@feross

This comment has been minimized.

Copy link
Member

commented Feb 22, 2018

Sorry for all the trouble with this issue. If you install the latest version of standard (version 11.0.0) then this issue is resolved.

If you're not sure what version of standard you're using, run:

npx standard --version

Ensure that you're on at least 11.0.0 and you'll be good to go!

@feross feross closed this Feb 22, 2018

@jligeza

This comment has been minimized.

Copy link

commented Feb 28, 2018

I have version 11 and it still doesn't work.

@feross

This comment has been minimized.

Copy link
Member

commented Feb 28, 2018

@jligeza Without any additional information, it's impossible to help you. Open a new issue and share a full bug report.

@jligeza

This comment has been minimized.

Copy link

commented Mar 3, 2018

Sorry, it works now. I just didn't use babel-eslint as written in the documentation.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 1, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.