Created
March 6, 2019 11:13
-
-
Save slonoed/36fa14347cc04850266ae29da2091209 to your computer and use it in GitHub Desktop.
eslint rule to prevent await in expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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