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

feat: detect non-member expressions in n/no-sync #127

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 23 additions & 6 deletions lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
*/
"use strict"

const allowedAtRootLevelSelector = [
// fs.readFileSync()
":function MemberExpression > Identifier[name=/Sync$/]",
// readFileSync.call(null, 'path')
":function MemberExpression > Identifier[name=/Sync$/]",
// readFileSync()
":function :not(MemberExpression) > Identifier[name=/Sync$/]",
]

const disallowedAtRootLevelSelector = [
// fs.readFileSync()
"MemberExpression > Identifier[name=/Sync$/]",
// readFileSync.call(null, 'path')
"MemberExpression > Identifier[name=/Sync$/]",
// readFileSync()
":not(MemberExpression) > Identifier[name=/Sync$/]",
]

module.exports = {
meta: {
type: "suggestion",
Expand Down Expand Up @@ -32,18 +50,17 @@ module.exports = {
},

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

return {
[selector](node) {
context.report({
node,
node: node.parent,
messageId: "noSync",
data: {
propertyName: node.property.name,
propertyName: node.name,
},
})
},
Expand Down
38 changes: 38 additions & 0 deletions tests/lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ new RuleTester().run("no-sync", rule, {
code: "var foo = fs.fooSync;",
options: [{ allowAtRootLevel: true }],
},
{
code: "var foo = fooSync;",
options: [{ allowAtRootLevel: true }],
},
{
code: "if (true) {fs.fooSync();}",
options: [{ allowAtRootLevel: true }],
},
{
code: "if (true) {fooSync();}",
options: [{ allowAtRootLevel: true }],
},
],
invalid: [
{
Expand All @@ -30,6 +38,36 @@ new RuleTester().run("no-sync", rule, {
},
],
},
{
code: "var foo = fs.fooSync.apply();",
errors: [
{
messageId: "noSync",
data: { propertyName: "fooSync" },
type: "MemberExpression",
},
],
},
{
code: "var foo = fooSync();",
errors: [
{
messageId: "noSync",
data: { propertyName: "fooSync" },
type: "CallExpression",
},
],
},
{
code: "var foo = fooSync.apply();",
errors: [
{
messageId: "noSync",
data: { propertyName: "fooSync" },
type: "MemberExpression",
},
],
},
{
code: "var foo = fs.fooSync();",
options: [{ allowAtRootLevel: false }],
Expand Down