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

Fix no-this-in-sfc for class properties #1995

Merged
merged 4 commits into from Sep 29, 2018

Conversation

sergei-startsev
Copy link
Contributor

@sergei-startsev sergei-startsev commented Sep 24, 2018

The PR fixes #1960 - no-this-in-sfc rule for class properties.

Class properties example:

class Foo {
  bar = () => {
    this.something();
    return null;
  };
}

The corresponding AST: link.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall

scope = scope.upper;
while (scope) {
const type = scope.block.type;
if (type === 'FunctionExpression' || type === 'FunctionDeclaration') {
if (type === 'FunctionExpression' || type === 'FunctionDeclaration'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we have a utility function for "is a function"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find isFunction, only isFunctionLikeExpression in ast.js, so extracted some util functions for reusing in Components.js

if (isArrowFunction) {
functionScope = utils.getParentFunctionScope(scope);
enclosingScope = utils.getArrowFunctionScope(scope);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like it could be const enclosingScope = isArrowFunction ? utils.getArrowFunctionScope(scope) : scope;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing, fixed

@@ -16,6 +16,8 @@ const ERROR_MESSAGE = 'Stateless functional components should not use this';
const rule = require('../../../lib/rules/no-this-in-sfc');
const RuleTester = require('eslint').RuleTester;

require('babel-eslint');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb do you know if this is really necessary? Not all of the test files that use this parser actually have the require.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you run those test files in isolation, do they pass?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, they do - just checked with ESLint 3, 4, and 5. could be an artifact from an even earlier time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any differences after removing require('babel-eslint'). See the proof #2004 . Also removed it here.

Copy link
Collaborator

@EvHaus EvHaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as well

@ljharb ljharb merged commit b77be96 into jsx-eslint:master Sep 29, 2018
This was referenced Jan 4, 2019
@ghost ghost mentioned this pull request Jan 12, 2019
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Getting unrelated (react/no-this-in-sfc) violation error
4 participants