rejects-requires-await.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @fileoverview Ensure Assert.rejects is preceded by await.
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. "use strict";
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. url:
  13. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-requires-await.html",
  14. },
  15. messages: {
  16. rejectRequiresAwait: "Assert.rejects needs to be preceded by await.",
  17. },
  18. type: "problem",
  19. },
  20. create(context) {
  21. return {
  22. CallExpression(node) {
  23. if (node.callee.type === "MemberExpression") {
  24. let memexp = node.callee;
  25. if (
  26. memexp.object.type === "Identifier" &&
  27. memexp.object.name === "Assert" &&
  28. memexp.property.type === "Identifier" &&
  29. memexp.property.name === "rejects"
  30. ) {
  31. // We have ourselves an Assert.rejects.
  32. if (node.parent.type !== "AwaitExpression") {
  33. context.report({
  34. node,
  35. messageId: "rejectRequiresAwait",
  36. });
  37. }
  38. }
  39. }
  40. },
  41. };
  42. },
  43. };