Skip to content

Commit 31cb269

Browse files
committed
Add linter error preventing use of async outside the test suite
We want to keep the runtime dependency footprint small, so that means avoiding use of `async` at runtime (which creates a dependency on the Regenerator runtime). There is no built-in eslint rule for this, so we make a custom one. Test plan: Add an `async` function to a file, run `yarn run lint` and see: graphql-js/src/subscription/subscribe.js 288:1 error async functions are not allowed outside of the test suite no-async ✖ 1 problem (1 error, 0 warnings) Note that no errors are issued for the `async` functions in the test suite. Closes: #1008
1 parent 8ef2d04 commit 31cb269

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@
219219
"vars-on-top": 0,
220220
"wrap-iife": 2,
221221
"wrap-regex": 0,
222-
"yoda": [2, "never", {"exceptRange": true}]
222+
"yoda": [2, "never", {"exceptRange": true}],
223+
224+
"no-async": 2
223225
}
224226
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"test": "npm run lint && npm run check && npm run testonly",
2525
"testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha",
2626
"t": "babel-node ./node_modules/.bin/_mocha --require ./resources/mocha-bootload",
27-
"lint": "eslint src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)",
27+
"lint": "eslint --rulesdir ./resources/lint src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)",
2828
"check": "flow check",
2929
"check-cover": "for file in {src/*.js,src/**/*.js}; do echo $file; flow coverage $file; done",
3030
"build": "babel src --optional runtime --ignore __tests__ --out-dir dist/ && cp package.json dist/ && npm run build-dot-flow",

resources/lint/no-async.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2017, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
module.exports = function(context) {
11+
if (context.getFilename().match(/\b__tests__\b/)) {
12+
return {};
13+
} else {
14+
return {
15+
FunctionDeclaration: function(node) {
16+
if (node.async) {
17+
context.report(
18+
node,
19+
'async functions are not allowed outside of the test suite'
20+
);
21+
}
22+
},
23+
};
24+
}
25+
};

0 commit comments

Comments
 (0)