Skip to content

Commit 37cf3d8

Browse files
committed
Rule: no-sync to encourage async usage
1 parent 732daf1 commit 37cf3d8

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

conf/eslint.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"no-label-var": 1,
3131
"no-ternary": 0,
3232
"no-self-compare": 0,
33+
"no-sync": 0,
3334

3435
"smarter-eqeqeq": 0,
3536
"brace-style": 0,

lib/rules/no-sync.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @fileoverview Rule to check for properties whose identifier ends with the string Sync
3+
* @author Matt DuVall<http://mattduvall.com/>
4+
*/
5+
6+
/*jshint node:true*/
7+
8+
//------------------------------------------------------------------------------
9+
// Rule Definition
10+
//------------------------------------------------------------------------------
11+
12+
module.exports = function(context) {
13+
14+
"use strict";
15+
16+
return {
17+
18+
"MemberExpression": function(node) {
19+
var propertyName = node.property.name,
20+
syncRegex = /.*Sync$/;
21+
22+
if (syncRegex.exec(propertyName) !== null) {
23+
context.report(node, "Unexpected sync method: '" + propertyName + "'.");
24+
}
25+
}
26+
};
27+
28+
};

tests/lib/rules/no-sync.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @fileoverview Tests for no-sync.
3+
* @author Matt DuVall <http://www.mattduvall.com>
4+
*/
5+
6+
//------------------------------------------------------------------------------
7+
// Requirements
8+
//------------------------------------------------------------------------------
9+
10+
var vows = require("vows"),
11+
assert = require("assert"),
12+
eslint = require("../../../lib/eslint");
13+
14+
//------------------------------------------------------------------------------
15+
// Constants
16+
//------------------------------------------------------------------------------
17+
18+
var RULE_ID = "no-sync";
19+
20+
//------------------------------------------------------------------------------
21+
// Tests
22+
//------------------------------------------------------------------------------
23+
24+
vows.describe(RULE_ID).addBatch({
25+
26+
"when evaluating code that uses a *Sync method": {
27+
28+
topic: "var foo = fs.fooSync();",
29+
30+
"should report a violation": function(topic) {
31+
32+
var config = { rules: {} };
33+
config.rules[RULE_ID] = 1;
34+
35+
var messages = eslint.verify(topic, config);
36+
assert.equal(messages.length, 1);
37+
assert.equal(messages[0].ruleId, RULE_ID);
38+
assert.equal(messages[0].message, "Unexpected sync method: 'fooSync'.");
39+
assert.include(messages[0].node.type, "MemberExpression");
40+
}
41+
},
42+
43+
"when evaluating code that uses a *Sync property": {
44+
45+
topic: "var foo = fs.fooSync;",
46+
47+
"should report a violation": function(topic) {
48+
49+
var config = { rules: {} };
50+
config.rules[RULE_ID] = 1;
51+
52+
var messages = eslint.verify(topic, config);
53+
assert.equal(messages.length, 1);
54+
assert.equal(messages[0].ruleId, RULE_ID);
55+
assert.equal(messages[0].message, "Unexpected sync method: 'fooSync'.");
56+
assert.include(messages[0].node.type, "MemberExpression");
57+
}
58+
},
59+
60+
"when evaluating code that uses a non *Sync method": {
61+
62+
topic: "var foo = fs.foo.foo();",
63+
64+
"should not report a violation": function(topic) {
65+
66+
var config = { rules: {} };
67+
config.rules[RULE_ID] = 1;
68+
69+
var messages = eslint.verify(topic, config);
70+
assert.equal(messages.length, 0);
71+
}
72+
}
73+
74+
}).export(module);

0 commit comments

Comments
 (0)