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

Update: allow sync operation at root level (fixes: #7985) #8859

Merged
merged 5 commits into from Jul 4, 2017

Conversation

VictorHom
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X] Changes an existing rule (template)

What rule do you want to change?
This address issues #7985
This update lets the no-sync rule take an option parameter allowAtRootLevel: boolean that will allow sync operations at the root level.

Does this change cause the rule to produce more or fewer warnings?
less errors since it is making the rule more lenient.

How will the change be implemented? (New option, new default behavior, etc.)?
New option

Please provide some example code that this change will affect:

<!-- example code here -->
//no-sync: [1, { allowAtRootLevel: true }]

// good
const contents = fs.readFileSync('/super/important.txt')

// bad
function someFunction() {
    fs.readFileSync('file.txt'); // very clearly not root of module
}

What does the rule currently do for this code?
If sync operations are found, it reports

What will the rule do after it's changed?
if allowAtRootLevel:true is used, no errors will be reported for sync operations at the root level. If functions are at in function declaration/ function expressions and sync operations are inside, it will be reported

@mention-bot
Copy link

@VictorHom, thanks for your PR! By analyzing the history of the files in this pull request, we identified @mduvall, @vitorbal and @btmills to be potential reviewers.

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Jul 2, 2017
}

return {
"MemberExpression[property.name=/.*Sync$/]"(node) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of manually keeping track of function scope, I think it would be simpler to use a descendant AST selector:

module.exports = {
    meta: {
        // ...
    },
    create(context) {
        const selector = context.options[0] && context.options[0].allowAtRootLevel
            ? ":function MemberExpression[property.name=/.*Sync$/]"
            : "MemberExpression[property.name=/.*Sync$/]"

        return {
            [selector](node) {
                context.report({
                    node,
                    message: "Unexpected sync method: '{{propertyName}}'.",
                    data: {
                        propertyName: node.property.name
                    }
                });
            }
        }
    }
};

That way you won't have to do any of the function scope tracking yourself.

Copy link
Member Author

Choose a reason for hiding this comment

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

that's what I wanted to do, but didn't know how to. Will know better next time!
@not-an-aardvark I fixed with your changes

@eslintbot
Copy link

LGTM

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

Looks great to me, thanks!

Unrelated to your PR, I'm wondering if we should add tests for chained calls off of a .*Sync function call (e.g. foo.somethingSync(). transform()). I'll try to hold onto that thought and maybe make a PR later.

Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants