Skip to content

Commit

Permalink
Update: add no-sync option to allow at root level (fixes #7985) (#8859)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom authored and not-an-aardvark committed Jul 4, 2017
1 parent 1ce553d commit 3767cda
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 15 additions & 3 deletions lib/rules/no-sync.js
Expand Up @@ -19,14 +19,26 @@ module.exports = {
recommended: false
},

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

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

return {

"MemberExpression[property.name=/.*Sync$/]"(node) {
[selector](node) {
context.report({
node,
message: "Unexpected sync method: '{{propertyName}}'.",
Expand Down
12 changes: 10 additions & 2 deletions tests/lib/rules/no-sync.js
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: "MemberExpression" }] },
{ code: "var a = function someFunction() {fs.fooSync();}", options: [{ allowAtRootLevel: true }], errors: [{ message: "Unexpected sync method: 'fooSync'.", type: "MemberExpression" }] }

]
});

0 comments on commit 3767cda

Please sign in to comment.