Skip to content

Commit 9bc5d34

Browse files
authored
ESLint: Require await inside async functions (#5263)
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.
1 parent 991e77a commit 9bc5d34

File tree

11 files changed

+18
-16
lines changed

11 files changed

+18
-16
lines changed

benchmark/sirun/get-results.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async function getResults (gitCommit) {
134134
async function main () {
135135
const ref = process.argv.length > 2 ? process.argv[2] : 'HEAD'
136136
const gitCommit = execSync(`git rev-parse ${ref}`).toString().trim()
137-
console.log(JSON.stringify(getResults(gitCommit), null, 4))
137+
console.log(JSON.stringify(await getResults(gitCommit), null, 4))
138138
}
139139

140140
module.exports = {

eslint.config.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ export default [
8787
'no-console': 'error',
8888
'no-prototype-builtins': 'off', // Override (turned on by @eslint/js/recommnded)
8989
'no-unused-expressions': 'off', // Override (turned on by standard)
90-
'no-var': 'error' // Override (set to warn in standard)
90+
'no-var': 'error', // Override (set to warn in standard)
91+
'require-await': 'error'
9192
}
9293
},
9394
{
@@ -212,7 +213,8 @@ export default [
212213
'mocha/no-sibling-hooks': 'off',
213214
'mocha/no-skipped-tests': 'off',
214215
'mocha/no-top-level-hooks': 'off',
215-
'n/handle-callback-err': 'off'
216+
'n/handle-callback-err': 'off',
217+
'require-await': 'off'
216218
}
217219
},
218220
{

packages/datadog-instrumentations/src/apollo-server-core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ addHook({ name: 'apollo-server-core', file: 'dist/runHttpQuery.js', versions: ['
1010
const HttpQueryError = runHttpQueryModule.HttpQueryError
1111

1212
shimmer.wrap(runHttpQueryModule, 'runHttpQuery', function wrapRunHttpQuery (originalRunHttpQuery) {
13-
return async function runHttpQuery () {
13+
return function runHttpQuery () {
1414
if (!requestChannel.start.hasSubscribers) {
1515
return originalRunHttpQuery.apply(this, arguments)
1616
}

packages/datadog-instrumentations/src/apollo-server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const requestChannel = dc.tracingChannel('datadog:apollo:request')
1212
let HeaderMap
1313

1414
function wrapExecuteHTTPGraphQLRequest (originalExecuteHTTPGraphQLRequest) {
15-
return async function executeHTTPGraphQLRequest () {
15+
return function executeHTTPGraphQLRequest () {
1616
if (!HeaderMap || !requestChannel.start.hasSubscribers) {
1717
return originalExecuteHTTPGraphQLRequest.apply(this, arguments)
1818
}

packages/datadog-instrumentations/src/jest.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ addHook({
442442
}, getTestEnvironment)
443443

444444
function getWrappedScheduleTests (scheduleTests, frameworkVersion) {
445-
return async function (tests) {
445+
return function (tests) {
446446
if (!isSuitesSkippingEnabled || hasFilteredSkippableSuites) {
447447
return scheduleTests.apply(this, arguments)
448448
}
@@ -744,7 +744,7 @@ function coverageReporterWrapper (coverageReporter) {
744744
* This calculation adds no value, so we'll skip it, as long as the user has not manually opted in to code coverage,
745745
* in which case we'll leave it.
746746
*/
747-
shimmer.wrap(CoverageReporter.prototype, '_addUntestedFiles', addUntestedFiles => async function () {
747+
shimmer.wrap(CoverageReporter.prototype, '_addUntestedFiles', addUntestedFiles => function () {
748748
// If the user has added coverage manually, they're willing to pay the price of this execution, so
749749
// we will not skip it.
750750
if (isSuitesSkippingEnabled && !isUserCodeCoverageEnabled) {
@@ -901,7 +901,7 @@ addHook({
901901
}, transformPackage => {
902902
const originalCreateScriptTransformer = transformPackage.createScriptTransformer
903903

904-
transformPackage.createScriptTransformer = async function (config) {
904+
transformPackage.createScriptTransformer = function (config) {
905905
const { testEnvironmentOptions, ...restOfConfig } = config
906906
const {
907907
_ddTestModuleId,

packages/datadog-instrumentations/src/mocha/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ addHook({
349349
versions: ['>=5.2.0'],
350350
file: 'lib/cli/run-helpers.js'
351351
}, (run) => {
352-
shimmer.wrap(run, 'runMocha', runMocha => async function () {
352+
shimmer.wrap(run, 'runMocha', runMocha => function () {
353353
if (!testStartCh.hasSubscribers) {
354354
return runMocha.apply(this, arguments)
355355
}

packages/datadog-instrumentations/src/nyc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ addHook({
77
name: 'nyc',
88
versions: ['>=17']
99
}, (nycPackage) => {
10-
shimmer.wrap(nycPackage.prototype, 'wrap', wrap => async function () {
10+
shimmer.wrap(nycPackage.prototype, 'wrap', wrap => function () {
1111
// Only relevant if the config `all` is set to true
1212
try {
1313
if (JSON.parse(process.env.NYC_CONFIG).all) {

packages/datadog-instrumentations/src/vitest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ addHook({
329329
const { VitestTestRunner } = vitestPackage
330330

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

335335
const {
@@ -361,7 +361,7 @@ addHook({
361361
})
362362

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

367367
if (isEarlyFlakeDetectionEnabled && taskToStatuses.has(task)) {

packages/dd-trace/src/debugger/devtools_client/breakpoints.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ async function removeBreakpoint ({ id }) {
7676
if (breakpoints.size === 0) return stop() // return instead of await to reduce number of promises created
7777
}
7878

79-
async function start () {
79+
function start () {
8080
sessionStarted = true
8181
return session.post('Debugger.enable') // return instead of await to reduce number of promises created
8282
}
8383

84-
async function stop () {
84+
function stop () {
8585
sessionStarted = false
8686
return session.post('Debugger.disable') // return instead of await to reduce number of promises created
8787
}

packages/dd-trace/src/debugger/devtools_client/snapshot/collector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async function traverseGetPropertiesResult (props, opts, depth) {
6363
return props
6464
}
6565

66-
async function getObjectProperties (subtype, objectId, opts, depth) {
66+
function getObjectProperties (subtype, objectId, opts, depth) {
6767
if (ITERABLE_SUBTYPES.has(subtype)) {
6868
return getIterable(objectId, opts, depth)
6969
} else if (subtype === 'promise') {

packages/dd-trace/src/profiling/profiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Profiler extends EventEmitter {
126126
this._timeoutInterval = this._config.flushInterval
127127
}
128128

129-
async stop () {
129+
stop () {
130130
if (!this._enabled) return
131131

132132
// collect and export current profiles

0 commit comments

Comments
 (0)