Skip to content

feat(eslint): add no-object-keys-length rule with auto-fix#7691

Closed
bm1549 wants to merge 3 commits intomasterfrom
brian.marks/eslint-no-object-keys-length
Closed

feat(eslint): add no-object-keys-length rule with auto-fix#7691
bm1549 wants to merge 3 commits intomasterfrom
brian.marks/eslint-no-object-keys-length

Conversation

@bm1549
Copy link
Copy Markdown
Contributor

@bm1549 bm1549 commented Mar 5, 2026

Summary

  • Adds a shared isEmptyObject() utility in packages/datadog-core/src/utils/src/is-empty-object.js that checks emptiness without allocating an intermediate array (uses for...in with Object.hasOwn guard)
  • Adds an ESLint rule (eslint-no-object-keys-length) that flags Object.keys(x).length emptiness checks and auto-fixes them to use isEmptyObject()
  • Auto-fix computes correct relative require paths, handles 'use strict' placement, and only inserts at module-level scope
  • Fixes all existing violations across 23 source files in packages/

Motivation

Object.keys(obj).length === 0 allocates an intermediate array just to check if an object is empty. In a tracer that runs in application hot paths, this is unnecessary overhead. The isEmptyObject() utility performs the same check with zero allocations.

ESLint rule features

  • Detects: === 0, !== 0, == 0, != 0, > 0, < 1, !Object.keys(x).length, and truthy checks in if/ternary/logical expressions
  • Also catches Object.values() and Object.entries() patterns
  • Does not flag threshold checks like Object.keys(x).length >= 10 or === 3
  • Auto-fix inserts require('..../is-empty-object') at the correct module-level position

Test plan

  • ESLint rule unit tests pass (22 test cases — valid + invalid)
  • Full npm run lint passes (only pre-existing oxc-parser error remains)
  • Verified auto-fix produces correct require paths at all directory depths
  • Verified 'use strict' ordering is preserved after auto-fix
  • Verified require insertion stays at module level (not inside functions)

🤖 Generated with Claude Code

Add an ESLint rule that flags `Object.keys(x).length` emptiness checks
and auto-fixes them to use a shared `isEmptyObject()` utility that
avoids allocating an intermediate array.

- Add `isEmptyObject` utility in `datadog-core/src/utils/src/`
- ESLint rule with `fixable: 'code'` auto-fix support
- Auto-fix computes correct relative require paths
- Auto-fix handles 'use strict' and top-level require placement
- Fix all existing violations across packages

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@bm1549 bm1549 added semver-minor AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos labels Mar 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 5, 2026

Overall package size

Self size: 4.93 MB
Deduped: 5.77 MB
No deduping: 5.77 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.0 | 81.15 kB | 815.98 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

Extend computeRequirePath to handle files outside the packages/
directory by falling back to context.cwd for the repo root. Remove
all eslint-disable comments and auto-fix the remaining violations
in integration-tests/, test/, and scripts/.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Mar 5, 2026

Benchmarks

Benchmark execution time: 2026-03-06 00:41:50

Comparing candidate commit 9deba25 in PR branch brian.marks/eslint-no-object-keys-length with baseline commit b6f7a69 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 233 metrics, 27 unstable metrics.

bm1549 added a commit that referenced this pull request Mar 5, 2026
Add ESLint rules and a CI check based on recurring PR review themes:

1. eslint-no-structured-clone: bans structuredClone() in production
   code (not available in all supported Node.js versions)

2. eslint-prefer-assert-object-contains: detects 3+ consecutive
   assert.strictEqual calls on the same object and suggests using
   assertObjectContains instead (currently off, 143 violations)

3. check-config-completeness: validates that all DD_/OTEL_ env vars
   in config/index.js are registered in supported-configurations.json

Note: eslint-no-object-keys-length and eslint-no-does-not-throw have
been split into separate PRs (#7691 and #7692) with auto-fix support.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@bm1549 bm1549 requested a review from a team March 5, 2026 23:08
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 5, 2026

Codecov Report

❌ Patch coverage is 89.28571% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.32%. Comparing base (b6f7a69) to head (9deba25).

Files with missing lines Patch % Lines
packages/datadog-plugin-jest/src/util.js 33.33% 2 Missing ⚠️
...ges/datadog-plugin-aws-sdk/src/services/kinesis.js 50.00% 1 Missing ⚠️
...ackages/datadog-plugin-aws-sdk/src/services/sns.js 50.00% 1 Missing ⚠️
...ackages/datadog-plugin-aws-sdk/src/services/sqs.js 50.00% 1 Missing ⚠️
...ckages/datadog-plugin-azure-functions/src/index.js 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7691      +/-   ##
==========================================
+ Coverage   80.31%   80.32%   +0.01%     
==========================================
  Files         739      740       +1     
  Lines       31946    31969      +23     
==========================================
+ Hits        25657    25680      +23     
  Misses       6289     6289              
Flag Coverage Δ
aiguard-macos 38.93% <57.14%> (-0.07%) ⬇️
aiguard-ubuntu 39.05% <57.14%> (-0.07%) ⬇️
aiguard-windows 38.79% <57.14%> (-0.07%) ⬇️
apm-capabilities-tracing-macos 48.81% <63.82%> (+0.09%) ⬆️
apm-capabilities-tracing-ubuntu 48.85% <63.82%> (+0.05%) ⬆️
apm-capabilities-tracing-windows 48.59% <63.82%> (+0.05%) ⬆️
apm-integrations-child-process 38.52% <57.14%> (-0.06%) ⬇️
apm-integrations-couchbase-18 37.45% <57.14%> (+0.08%) ⬆️
apm-integrations-couchbase-eol 37.78% <57.14%> (-0.06%) ⬇️
apm-integrations-oracledb 37.75% <57.14%> (-0.06%) ⬇️
appsec-express 55.34% <88.46%> (-0.04%) ⬇️
appsec-fastify 51.66% <80.76%> (-0.04%) ⬇️
appsec-graphql 51.85% <74.19%> (-0.03%) ⬇️
appsec-kafka 44.34% <52.94%> (-0.05%) ⬇️
appsec-ldapjs 44.04% <70.58%> (-0.04%) ⬇️
appsec-lodash 43.69% <70.58%> (-0.04%) ⬇️
appsec-macos 58.33% <75.86%> (-0.04%) ⬇️
appsec-mongodb-core 48.85% <65.00%> (-0.04%) ⬇️
appsec-mongoose 49.52% <70.00%> (-0.03%) ⬇️
appsec-mysql 50.88% <69.23%> (-0.04%) ⬇️
appsec-node-serialize 43.21% <70.58%> (-0.04%) ⬇️
appsec-passport 47.65% <65.21%> (-0.05%) ⬇️
appsec-postgres 50.64% <69.23%> (-0.04%) ⬇️
appsec-sourcing 42.61% <70.58%> (-0.04%) ⬇️
appsec-template 43.38% <70.58%> (-0.04%) ⬇️
appsec-ubuntu 58.41% <75.86%> (-0.04%) ⬇️
appsec-windows 58.20% <75.86%> (-0.04%) ⬇️
instrumentations-instrumentation-bluebird 32.33% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-body-parser 40.49% <52.94%> (-0.06%) ⬇️
instrumentations-instrumentation-child_process 37.84% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-cookie-parser 34.33% <52.94%> (-0.05%) ⬇️
instrumentations-instrumentation-express 34.66% <58.82%> (-0.05%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.46% <52.94%> (-0.05%) ⬇️
instrumentations-instrumentation-express-session 40.12% <58.82%> (-0.06%) ⬇️
instrumentations-instrumentation-fs 31.95% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-generic-pool 29.75% <33.33%> (+0.04%) ⬆️
instrumentations-instrumentation-http 39.84% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-knex 32.34% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-mongoose 33.47% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-multer 40.24% <52.94%> (-0.06%) ⬇️
instrumentations-instrumentation-mysql2 38.31% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-passport 44.02% <65.21%> (-0.05%) ⬇️
instrumentations-instrumentation-passport-http 43.69% <65.21%> (-0.05%) ⬇️
instrumentations-instrumentation-passport-local 44.23% <65.21%> (-0.05%) ⬇️
instrumentations-instrumentation-pg 37.74% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-promise 32.26% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-promise-js 32.27% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-q 32.31% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-url 32.23% <57.14%> (-0.06%) ⬇️
instrumentations-instrumentation-when 32.28% <57.14%> (-0.06%) ⬇️
llmobs-ai 42.14% <70.58%> (-0.05%) ⬇️
llmobs-anthropic 40.30% <57.14%> (-0.06%) ⬇️
llmobs-bedrock 39.27% <55.00%> (-0.04%) ⬇️
llmobs-google-genai 39.83% <57.14%> (-0.06%) ⬇️
llmobs-langchain 39.91% <54.54%> (-0.03%) ⬇️
llmobs-openai 44.14% <68.75%> (-0.05%) ⬇️
llmobs-vertex-ai 40.09% <57.14%> (-0.06%) ⬇️
platform-core 31.62% <33.33%> (+0.08%) ⬆️
platform-esbuild 34.53% <33.33%> (+0.04%) ⬆️
platform-instrumentations-misc 45.50% <33.33%> (-0.03%) ⬇️
platform-shimmer 37.65% <33.33%> (+0.02%) ⬆️
platform-unit-guardrails 33.02% <33.33%> (+0.06%) ⬆️
plugins-azure-event-hubs 25.94% <33.33%> (+0.11%) ⬆️
plugins-azure-service-bus 25.30% <33.33%> (+0.11%) ⬆️
plugins-bullmq 43.93% <57.14%> (-0.07%) ⬇️
plugins-cassandra 37.79% <57.14%> (-0.06%) ⬇️
plugins-cookie 26.99% <33.33%> (+0.10%) ⬆️
plugins-cookie-parser 26.78% <33.33%> (+0.10%) ⬆️
plugins-crypto 26.93% <33.33%> (+0.13%) ⬆️
plugins-dd-trace-api 38.38% <57.14%> (-0.07%) ⬇️
plugins-express-mongo-sanitize 26.92% <33.33%> (+0.10%) ⬆️
plugins-express-session 26.74% <33.33%> (+0.10%) ⬆️
plugins-fastify 42.24% <70.58%> (-0.05%) ⬇️
plugins-fetch 38.34% <52.94%> (-0.05%) ⬇️
plugins-fs 38.62% <57.14%> (-0.07%) ⬇️
plugins-generic-pool 25.98% <33.33%> (+0.11%) ⬆️
plugins-google-cloud-pubsub 45.36% <64.28%> (-0.09%) ⬇️
plugins-grpc 40.94% <57.14%> (-0.06%) ⬇️
plugins-handlebars 26.97% <33.33%> (+0.10%) ⬆️
plugins-hapi 40.13% <57.14%> (-0.06%) ⬇️
plugins-hono 40.39% <57.14%> (-0.06%) ⬇️
plugins-ioredis 38.43% <57.14%> (-0.06%) ⬇️
plugins-knex 26.60% <33.33%> (+0.10%) ⬆️
plugins-ldapjs 24.47% <33.33%> (+0.11%) ⬆️
plugins-light-my-request 26.33% <33.33%> (+0.10%) ⬆️
plugins-limitd-client 32.62% <57.14%> (-0.06%) ⬇️
plugins-lodash 26.07% <33.33%> (+0.11%) ⬆️
plugins-mariadb 39.49% <57.14%> (-0.06%) ⬇️
plugins-memcached 38.17% <57.14%> (-0.06%) ⬇️
plugins-microgateway-core 39.17% <57.14%> (-0.06%) ⬇️
plugins-moleculer 40.50% <57.14%> (-0.06%) ⬇️
plugins-mongodb 39.20% <57.14%> (-0.06%) ⬇️
plugins-mongodb-core 39.03% <57.14%> (-0.06%) ⬇️
plugins-mongoose 38.86% <57.14%> (-0.06%) ⬇️
plugins-multer 26.74% <33.33%> (+0.10%) ⬆️
plugins-mysql 39.17% <57.14%> (-0.06%) ⬇️
plugins-mysql2 39.27% <57.14%> (-0.04%) ⬇️
plugins-node-serialize 27.04% <33.33%> (+0.10%) ⬆️
plugins-opensearch 37.63% <57.14%> (-0.06%) ⬇️
plugins-passport-http 26.79% <33.33%> (+0.10%) ⬆️
plugins-postgres 35.74% <50.00%> (-0.04%) ⬇️
plugins-process 26.93% <33.33%> (+0.13%) ⬆️
plugins-pug 26.99% <33.33%> (+0.10%) ⬆️
plugins-redis 38.89% <57.14%> (-0.06%) ⬇️
plugins-router 42.96% <58.82%> (-0.20%) ⬇️
plugins-sequelize 25.58% <33.33%> (+0.11%) ⬆️
plugins-test-and-upstream-amqp10 38.50% <57.14%> (-0.06%) ⬇️
plugins-test-and-upstream-amqplib 43.81% <57.14%> (-0.07%) ⬇️
plugins-test-and-upstream-apollo 39.04% <52.63%> (-0.05%) ⬇️
plugins-test-and-upstream-avsc 38.71% <57.14%> (-0.07%) ⬇️
plugins-test-and-upstream-bunyan 33.90% <57.14%> (-0.06%) ⬇️
plugins-test-and-upstream-connect 40.78% <57.14%> (-0.06%) ⬇️
plugins-test-and-upstream-graphql 40.15% <68.75%> (-0.05%) ⬇️
plugins-test-and-upstream-koa 40.37% <57.14%> (-0.06%) ⬇️
plugins-test-and-upstream-protobufjs 38.93% <57.14%> (-0.07%) ⬇️
plugins-test-and-upstream-rhea 44.03% <57.14%> (-0.04%) ⬇️
plugins-undici 39.12% <52.94%> (-0.05%) ⬇️
plugins-url 26.93% <33.33%> (+0.13%) ⬆️
plugins-valkey 38.09% <57.14%> (-0.06%) ⬇️
plugins-vm 26.93% <33.33%> (+0.13%) ⬆️
plugins-winston 34.09% <57.14%> (-0.06%) ⬇️
plugins-ws 41.88% <71.42%> (-0.05%) ⬇️
profiling-macos 39.85% <68.75%> (-0.05%) ⬇️
profiling-ubuntu 39.97% <68.75%> (-0.05%) ⬇️
profiling-windows 41.18% <72.22%> (-0.04%) ⬇️
serverless-azure-functions-client 25.66% <33.33%> (+0.11%) ⬆️
serverless-azure-functions-eventhubs 25.66% <33.33%> (+0.11%) ⬆️
serverless-azure-functions-servicebus 25.66% <33.33%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sadly does not really address the problem in a way that I would expect.

In most cases, we can prevent using Object.keys(). That would be a good goal, it is just not possible to create a lint rule for that.

This just replaces these with another notation that is slower in my local benchmarks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos semver-patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants