Skip to content

Commit

Permalink
Add linter error preventing use of async outside the test suite
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wincent committed Aug 26, 2017
1 parent 8ef2d04 commit 31cb269
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@
"vars-on-top": 0,
"wrap-iife": 2,
"wrap-regex": 0,
"yoda": [2, "never", {"exceptRange": true}]
"yoda": [2, "never", {"exceptRange": true}],

"no-async": 2
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test": "npm run lint && npm run check && npm run testonly",
"testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha",
"t": "babel-node ./node_modules/.bin/_mocha --require ./resources/mocha-bootload",
"lint": "eslint src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)",
"lint": "eslint --rulesdir ./resources/lint src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)",
"check": "flow check",
"check-cover": "for file in {src/*.js,src/**/*.js}; do echo $file; flow coverage $file; done",
"build": "babel src --optional runtime --ignore __tests__ --out-dir dist/ && cp package.json dist/ && npm run build-dot-flow",
Expand Down
25 changes: 25 additions & 0 deletions resources/lint/no-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2017, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

module.exports = function(context) {
if (context.getFilename().match(/\b__tests__\b/)) {
return {};
} else {
return {
FunctionDeclaration: function(node) {
if (node.async) {
context.report(
node,
'async functions are not allowed outside of the test suite'
);
}
},
};
}
};

0 comments on commit 31cb269

Please sign in to comment.