Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
eslint rule to prevent await in expressions
/*
Should be used with "eslint-plugin-local-rules" plugin
*/
'use strict'
module.exports = {
'no-await-in-expressions': {
meta: {
type: 'problem',
docs: {
description: 'No await in expressions',
category: 'Stylistic Issues',
recommended: false,
},
schema: [],
},
create: context => {
// This rule doesn't check await before return.
// Use "no-return-await" rule to prevent it.
const allowedAncestors = [
'ReturnStatement',
'VariableDeclarator',
'ExpressionStatement',
'AssignmentExpression',
]
return {
AwaitExpression: node => {
const ancestors = context.getAncestors(node)
const last = ancestors[ancestors.length - 1]
if (!allowedAncestors.includes(last.type)) {
context.report({
node,
message: 'Using await in expressions is forbidden. Assign await result to a variable',
})
}
},
}
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment