Skip to content

Instantly share code, notes, and snippets.

@slonoed
Created March 6, 2019 11:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slonoed/36fa14347cc04850266ae29da2091209 to your computer and use it in GitHub Desktop.
Save slonoed/36fa14347cc04850266ae29da2091209 to your computer and use it in GitHub Desktop.
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