|
| 1 | +'use strict'; |
| 2 | +const {findVariable, getFunctionHeadLocation} = require('eslint-utils'); |
| 3 | +const getDocumentationUrl = require('./utils/get-documentation-url'); |
| 4 | +const {matches, memberExpressionSelector} = require('./selectors'); |
| 5 | + |
| 6 | +const ERROR_PROMISE = 'promise'; |
| 7 | +const ERROR_IIFE = 'iife'; |
| 8 | +const ERROR_IDENTIFIER = 'identifier'; |
| 9 | +const SUGGESTION_ADD_AWAIT = 'add-await'; |
| 10 | +const messages = { |
| 11 | + [ERROR_PROMISE]: 'Prefer top-level await over using a promise chain.', |
| 12 | + [ERROR_IIFE]: 'Prefer top-level await over an async IIFE.', |
| 13 | + [ERROR_IDENTIFIER]: 'Prefer top-level await over an async function `{{name}}` call.', |
| 14 | + [SUGGESTION_ADD_AWAIT]: 'Insert `await`.' |
| 15 | +}; |
| 16 | + |
| 17 | +const topLevelCallExpression = 'Program > ExpressionStatement > CallExpression[optional!=true].expression'; |
| 18 | +const iife = [ |
| 19 | + topLevelCallExpression, |
| 20 | + matches([ |
| 21 | + '[callee.type="FunctionExpression"]', |
| 22 | + '[callee.type="ArrowFunctionExpression"]' |
| 23 | + ]), |
| 24 | + '[callee.async!=false]', |
| 25 | + '[callee.generator!=true]' |
| 26 | +].join(''); |
| 27 | +const promise = [ |
| 28 | + topLevelCallExpression, |
| 29 | + memberExpressionSelector({ |
| 30 | + path: 'callee', |
| 31 | + names: ['then', 'catch', 'finally'] |
| 32 | + }) |
| 33 | +].join(''); |
| 34 | +const identifier = [ |
| 35 | + topLevelCallExpression, |
| 36 | + '[callee.type="Identifier"]' |
| 37 | +].join(''); |
| 38 | + |
| 39 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 40 | +function create(context) { |
| 41 | + return { |
| 42 | + [promise](node) { |
| 43 | + context.report({ |
| 44 | + node: node.callee.property, |
| 45 | + messageId: ERROR_PROMISE |
| 46 | + }); |
| 47 | + }, |
| 48 | + [iife](node) { |
| 49 | + context.report({ |
| 50 | + node, |
| 51 | + loc: getFunctionHeadLocation(node.callee, context.getSourceCode()), |
| 52 | + messageId: ERROR_IIFE |
| 53 | + }); |
| 54 | + }, |
| 55 | + [identifier](node) { |
| 56 | + const variable = findVariable(context.getScope(), node.callee); |
| 57 | + if (!variable || variable.defs.length !== 1) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const [definition] = variable.defs; |
| 62 | + const value = definition.type === 'Variable' && definition.kind === 'const' ? |
| 63 | + definition.node.init : |
| 64 | + definition.node; |
| 65 | + if ( |
| 66 | + !( |
| 67 | + ( |
| 68 | + value.type === 'ArrowFunctionExpression' || |
| 69 | + value.type === 'FunctionExpression' || |
| 70 | + value.type === 'FunctionDeclaration' |
| 71 | + ) && !value.generator && value.async |
| 72 | + ) |
| 73 | + ) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + context.report({ |
| 78 | + node, |
| 79 | + messageId: ERROR_IDENTIFIER, |
| 80 | + data: {name: node.callee.name}, |
| 81 | + suggest: [ |
| 82 | + { |
| 83 | + messageId: SUGGESTION_ADD_AWAIT, |
| 84 | + fix: fixer => fixer.insertTextBefore(node, 'await ') |
| 85 | + } |
| 86 | + ] |
| 87 | + }); |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = { |
| 93 | + create, |
| 94 | + meta: { |
| 95 | + type: 'suggestion', |
| 96 | + docs: { |
| 97 | + description: 'Prefer top-level await over top-level promises and async function calls.', |
| 98 | + url: getDocumentationUrl(__filename), |
| 99 | + suggestion: true |
| 100 | + }, |
| 101 | + schema: [], |
| 102 | + messages |
| 103 | + } |
| 104 | +}; |
0 commit comments