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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 67 additions & 10 deletions lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,79 @@ module.exports = {
recommended: false
},

schema: []
schema: [
{
type: "object",
properties: {
allowAtRootLevel: {
type: "boolean"
}
},
additionalProperties: false
}
]
},

create(context) {
const allowAtRootLevel = context.options[0] ? context.options[0].allowAtRootLevel : false;
const stackOfSyncExpressions = [];
let setOfSyncExpressions = new Set();

return {
/**
* push Sync Expression sets and track Sync Expressions in a new set
*
* @returns {void}
*/
function trackSyncExpression() {
if (allowAtRootLevel) {
stackOfSyncExpressions.push(setOfSyncExpressions);
setOfSyncExpressions = new Set();
}
}

"MemberExpression[property.name=/.*Sync$/]"(node) {
context.report({
node,
message: "Unexpected sync method: '{{propertyName}}'.",
data: {
propertyName: node.property.name
}
});
/**
* Report if the Sync Expression is not in the root level
*
* @param {ASTNode} node the FunctionDeclaration/Expression node
* @returns {void}
*/
function reportIfSyncInFunction(node) {
if (allowAtRootLevel) {
if (setOfSyncExpressions.size > 0) {
setOfSyncExpressions.forEach(syncExpression => {
context.report({
node,
message: "Unexpected sync method: '{{propertyName}}'.",
data: {
propertyName: syncExpression.property.name
}
});
});

}
setOfSyncExpressions.clear();
setOfSyncExpressions = stackOfSyncExpressions.pop();
}
}

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

if (allowAtRootLevel) {
setOfSyncExpressions.add(node);
} else {
context.report({
node,
message: "Unexpected sync method: '{{propertyName}}'.",
data: {
propertyName: node.property.name
}
});
}
},
FunctionDeclaration: trackSyncExpression,
"FunctionDeclaration:exit": reportIfSyncInFunction,
FunctionExpression: trackSyncExpression,
"FunctionExpression:exit": reportIfSyncInFunction
};

}
Expand Down
12 changes: 10 additions & 2 deletions tests/lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ const ruleTester = new RuleTester();

ruleTester.run("no-sync", rule, {
valid: [
"var foo = fs.foo.foo();"
"var foo = fs.foo.foo();",
{ code: "var foo = fs.fooSync;", options: [{ allowAtRootLevel: true }] },
{ code: "if (true) {fs.fooSync();}", options: [{ allowAtRootLevel: true }] }
],
invalid: [
{ code: "var foo = fs.fooSync();", errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] },
{ code: "var foo = fs.fooSync;", errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] }
{ code: "var foo = fs.fooSync();", options: [{ allowAtRootLevel: false }], errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] },
{ code: "if (true) {fs.fooSync();}", errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] },
{ code: "var foo = fs.fooSync;", errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] },
{ code: "function someFunction() {fs.fooSync();}", errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] },
{ code: "function someFunction() {fs.fooSync();}", options: [{ allowAtRootLevel: true }], errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "FunctionDeclaration" }] },
{ code: "var a = function someFunction() {fs.fooSync();}", options: [{ allowAtRootLevel: true }], errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "FunctionExpression" }] }

]
});