Skip to content

Commit

Permalink
ESLint: Require await inside async functions
Browse files Browse the repository at this point in the history
If a function is marked as async, V8 will create a few extra promises
behind the scenes when awaiting it. If the function doesn't itself
contain any awaits, these extra promises just adds overhead that isn't
needed.

The `require-await` rule isn't enabled in tests, where the extra created
promises doesn't harm anyone.
  • Loading branch information
watson committed Feb 18, 2025
1 parent 052a833 commit 25835a5
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion benchmark/sirun/get-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function getResults (gitCommit) {
async function main () {
const ref = process.argv.length > 2 ? process.argv[2] : 'HEAD'
const gitCommit = execSync(`git rev-parse ${ref}`).toString().trim()
console.log(JSON.stringify(getResults(gitCommit), null, 4))
console.log(JSON.stringify(await getResults(gitCommit), null, 4))
}

module.exports = {
Expand Down
6 changes: 4 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export default [
'no-console': 'error',
'no-prototype-builtins': 'off', // Override (turned on by @eslint/js/recommnded)
'no-unused-expressions': 'off', // Override (turned on by standard)
'no-var': 'error' // Override (set to warn in standard)
'no-var': 'error', // Override (set to warn in standard)
'require-await': 'error'
}
},
{
Expand Down Expand Up @@ -133,7 +134,8 @@ export default [
'mocha/no-sibling-hooks': 'off',
'mocha/no-skipped-tests': 'off',
'mocha/no-top-level-hooks': 'off',
'n/handle-callback-err': 'off'
'n/handle-callback-err': 'off',
'require-await': 'off'
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ addHook({ name: 'apollo-server-core', file: 'dist/runHttpQuery.js', versions: ['
const HttpQueryError = runHttpQueryModule.HttpQueryError

shimmer.wrap(runHttpQueryModule, 'runHttpQuery', function wrapRunHttpQuery (originalRunHttpQuery) {
return async function runHttpQuery () {
return function runHttpQuery () {
if (!requestChannel.start.hasSubscribers) {
return originalRunHttpQuery.apply(this, arguments)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/apollo-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const requestChannel = dc.tracingChannel('datadog:apollo:request')
let HeaderMap

function wrapExecuteHTTPGraphQLRequest (originalExecuteHTTPGraphQLRequest) {
return async function executeHTTPGraphQLRequest () {
return function executeHTTPGraphQLRequest () {
if (!HeaderMap || !requestChannel.start.hasSubscribers) {
return originalExecuteHTTPGraphQLRequest.apply(this, arguments)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/datadog-instrumentations/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ addHook({
}, getTestEnvironment)

function getWrappedScheduleTests (scheduleTests, frameworkVersion) {
return async function (tests) {
return function (tests) {
if (!isSuitesSkippingEnabled || hasFilteredSkippableSuites) {
return scheduleTests.apply(this, arguments)
}
Expand Down Expand Up @@ -741,7 +741,7 @@ function coverageReporterWrapper (coverageReporter) {
* This calculation adds no value, so we'll skip it, as long as the user has not manually opted in to code coverage,
* in which case we'll leave it.
*/
shimmer.wrap(CoverageReporter.prototype, '_addUntestedFiles', addUntestedFiles => async function () {
shimmer.wrap(CoverageReporter.prototype, '_addUntestedFiles', addUntestedFiles => function () {
// If the user has added coverage manually, they're willing to pay the price of this execution, so
// we will not skip it.
if (isSuitesSkippingEnabled && !isUserCodeCoverageEnabled) {
Expand Down Expand Up @@ -898,7 +898,7 @@ addHook({
}, transformPackage => {
const originalCreateScriptTransformer = transformPackage.createScriptTransformer

transformPackage.createScriptTransformer = async function (config) {
transformPackage.createScriptTransformer = function (config) {
const { testEnvironmentOptions, ...restOfConfig } = config
const {
_ddTestModuleId,
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/mocha/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ addHook({
versions: ['>=5.2.0'],
file: 'lib/cli/run-helpers.js'
}, (run) => {
shimmer.wrap(run, 'runMocha', runMocha => async function () {
shimmer.wrap(run, 'runMocha', runMocha => function () {
if (!testStartCh.hasSubscribers) {
return runMocha.apply(this, arguments)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ addHook({
name: 'nyc',
versions: ['>=17']
}, (nycPackage) => {
shimmer.wrap(nycPackage.prototype, 'wrap', wrap => async function () {
shimmer.wrap(nycPackage.prototype, 'wrap', wrap => function () {
// Only relevant if the config `all` is set to true
try {
if (JSON.parse(process.env.NYC_CONFIG).all) {
Expand Down
4 changes: 2 additions & 2 deletions packages/datadog-instrumentations/src/vitest.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ addHook({
const { VitestTestRunner } = vitestPackage

// `onBeforeRunTask` is run before any repetition or attempt is run
shimmer.wrap(VitestTestRunner.prototype, 'onBeforeRunTask', onBeforeRunTask => async function (task) {
shimmer.wrap(VitestTestRunner.prototype, 'onBeforeRunTask', onBeforeRunTask => function (task) {
const testName = getTestName(task)

const {
Expand Down Expand Up @@ -361,7 +361,7 @@ addHook({
})

// `onAfterRunTask` is run after all repetitions or attempts are run
shimmer.wrap(VitestTestRunner.prototype, 'onAfterRunTask', onAfterRunTask => async function (task) {
shimmer.wrap(VitestTestRunner.prototype, 'onAfterRunTask', onAfterRunTask => function (task) {
const { isEarlyFlakeDetectionEnabled, isQuarantinedTestsEnabled } = getProvidedContext()

if (isEarlyFlakeDetectionEnabled && taskToStatuses.has(task)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/src/debugger/devtools_client/breakpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ async function removeBreakpoint ({ id }) {
if (breakpoints.size === 0) return stop() // return instead of await to reduce number of promises created
}

async function start () {
function start () {
sessionStarted = true
return session.post('Debugger.enable') // return instead of await to reduce number of promises created
}

async function stop () {
function stop () {
sessionStarted = false
return session.post('Debugger.disable') // return instead of await to reduce number of promises created
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function traverseGetPropertiesResult (props, opts, depth) {
return props
}

async function getObjectProperties (subtype, objectId, opts, depth) {
function getObjectProperties (subtype, objectId, opts, depth) {
if (ITERABLE_SUBTYPES.has(subtype)) {
return getIterable(objectId, opts, depth)
} else if (subtype === 'promise') {
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/profiling/profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Profiler extends EventEmitter {
this._timeoutInterval = this._config.flushInterval
}

async stop () {
stop () {
if (!this._enabled) return

// collect and export current profiles
Expand Down

0 comments on commit 25835a5

Please sign in to comment.