Skip to content

Commit 323fd0d

Browse files
authored
chore(eslint): enable rules globally and fix violations (#7548)
* chore(eslint): enable rules globally and fix violations The following rules now apply to all files (enabled in global config): - `eslint-rules/eslint-safe-typeof-object` - `no-implicit-coercion` - `no-useless-assignment` - `no-void` - `operator-assignment` - `prefer-exponentiation-operator` - `prefer-object-has-own` - `n/no-restricted-require` (using shared `GLOBAL_RESTRICTED_REQUIRES`) Removed these from the `dd-trace/src/all` block to avoid duplication. Fix tests and app code for the new global rules: - Safe typeof checks - Remove useless assignments - Use `**` and template literals - Use `filter.includes()` instead of `~indexOf`) - Fix typo in vertexai test (`} if` → `} else if`).
1 parent 179e273 commit 323fd0d

File tree

14 files changed

+79
-76
lines changed

14 files changed

+79
-76
lines changed

eslint.config.mjs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ const TEST_FILES = [
4141
'**/*.spec.js',
4242
]
4343

44+
const GLOBAL_RESTRICTED_REQUIRES = [
45+
{
46+
name: 'diagnostics_channel',
47+
message: 'Please use `dc-polyfill` instead.',
48+
},
49+
{
50+
name: 'get-port',
51+
message: 'Please listen on port 0 instead.',
52+
},
53+
{
54+
name: 'rimraf',
55+
message: 'Please use `fs.rm(path, { recursive: true, force: true })` instead.',
56+
},
57+
{
58+
name: 'koalas',
59+
message: 'Please use nullish coalescing operator (??) instead.',
60+
},
61+
{
62+
name: 'chai',
63+
message: 'Please use `node:assert/strict` instead.',
64+
},
65+
{
66+
name: 'tap',
67+
message: 'Please use `mocha` instead.',
68+
},
69+
]
70+
4471
export default [
4572
{
4673
name: 'dd-trace/global-ignore',
@@ -381,17 +408,18 @@ export default [
381408
importAttributes: 'always-multiline',
382409
dynamicImports: 'always-multiline',
383410
}],
411+
'eslint-rules/eslint-safe-typeof-object': 'error',
384412
'eslint-rules/eslint-require-export-exists': 'error',
385413
'import/no-extraneous-dependencies': 'error',
414+
'n/hashbang': 'off', // TODO: Enable this rule once we have a plan to address it
386415
'n/no-extraneous-require': ['error', {
387416
allowModules: Object.keys(dependencies),
388417
}],
418+
'n/no-process-exit': 'off', // TODO: Enable this rule once we have a plan to address it
419+
'n/no-restricted-require': ['error', GLOBAL_RESTRICTED_REQUIRES],
389420
'n/no-unpublished-require': ['error', {
390421
allowModules: Object.keys(dependencies),
391422
}],
392-
'n/no-restricted-require': ['error', ['diagnostics_channel']],
393-
'n/hashbang': 'off', // TODO: Enable this rule once we have a plan to address it
394-
'n/no-process-exit': 'off', // TODO: Enable this rule once we have a plan to address it
395423
'n/no-unsupported-features/node-builtins': ['error', {
396424
ignores: [
397425
'Request',
@@ -404,8 +432,14 @@ export default [
404432
],
405433
}],
406434
'no-console': 'error',
435+
'no-implicit-coercion': ['error', { boolean: true, number: true, string: true, allow: ['!!'] }],
407436
'no-prototype-builtins': 'off', // Override (turned on by @eslint/js/recommended)
437+
'no-useless-assignment': 'error',
408438
'no-var': 'error',
439+
'no-void': ['error', { allowAsStatement: true }],
440+
'operator-assignment': 'error',
441+
'prefer-exponentiation-operator': 'error',
442+
'prefer-object-has-own': 'error',
409443
'prefer-object-spread': 'error',
410444
'require-await': 'error',
411445
strict: 'error',
@@ -420,48 +454,19 @@ export default [
420454
rules: {
421455
'eslint-rules/eslint-process-env': 'error',
422456
'eslint-rules/eslint-env-aliases': 'error',
423-
'eslint-rules/eslint-safe-typeof-object': 'error',
424457
'eslint-rules/eslint-log-printf-style': 'error',
458+
425459
'n/no-restricted-require': ['error', [
426-
{
427-
name: 'diagnostics_channel',
428-
message: 'Please use `dc-polyfill` instead.',
429-
},
460+
...GLOBAL_RESTRICTED_REQUIRES,
430461
{
431462
name: 'semver',
432463
message: 'Please use `semifies` instead.',
433464
},
434-
{
435-
name: 'get-port',
436-
message: 'Please listen on port 0 instead.',
437-
},
438-
{
439-
name: 'rimraf',
440-
message: 'Please use `fs.rm(path, { recursive: true, force: true })` instead.',
441-
},
442-
{
443-
name: 'koalas',
444-
message: 'Please use nullish coalescing operator (??) instead.',
445-
},
446-
{
447-
name: 'chai',
448-
message: 'Please use `node:assert/strict` instead.',
449-
},
450-
{
451-
name: 'tap',
452-
message: 'Please use `mocha` instead.',
453-
},
454465
]],
455466

456467
'no-await-in-loop': 'error',
457468
'no-else-return': ['error', { allowElseIf: true }],
458-
'no-implicit-coercion': ['error', { boolean: true, number: true, string: true, allow: ['!!'] }],
459469
'no-unused-expressions': 'error',
460-
'no-useless-assignment': 'error',
461-
'no-void': ['error', { allowAsStatement: true }],
462-
'operator-assignment': 'error',
463-
'prefer-exponentiation-operator': 'error',
464-
'prefer-object-has-own': 'error',
465470

466471
// Too strict for now. Slowly migrate to this rule by using rest parameters.
467472
// 'prefer-rest-params': 'error',

packages/datadog-plugin-ai/test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ describe('Plugin', () => {
490490
})
491491

492492
let tools
493-
let maxStepsArg = {}
493+
let maxStepsArg
494494
const toolSchema = ai.jsonSchema({
495495
type: 'object',
496496
properties: {

packages/datadog-plugin-google-cloud-pubsub/test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ describe('Plugin', () => {
602602
prefixedResource = resource
603603
}
604604

605-
let defaultOpName = 'pubsub.receive'
605+
let defaultOpName
606606
if (spanKind === 'consumer') {
607607
defaultOpName = expectedSchema.receive.opName
608608
} else if (spanKind === 'producer') {

packages/datadog-plugin-google-cloud-vertexai/test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function useScenario ({ scenario, statusCode = 200, stream = false }) {
2929

3030
if (statusCode !== 200) {
3131
body = '{}'
32-
} if (stream) {
32+
} else if (stream) {
3333
body = fs.createReadStream(path.join(__dirname, 'resources', `${scenario}.txt`))
3434
} else {
3535
const contents = require(`./resources/${scenario}.json`)
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { NextResponse } from 'next/server'
22

33
export async function GET (req) {
4-
let status = 200
5-
try {
6-
throw new Error('error in app dir api route')
7-
} catch (error) {
8-
req.error = error
9-
status = 500
10-
}
4+
req.error = new Error('error in app dir api route')
115

12-
return NextResponse.json({}, { status })
6+
return NextResponse.json({}, { status: 500 })
137
}

packages/dd-trace/test/appsec/iast/analyzers/code-injection-analyzer.express.plugin.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('Code injection vulnerability', () => {
7373

7474
testThatRequestHasNoVulnerability({
7575
fn: (req, res) => {
76-
res.send('' + require(evalFunctionsPath).runFakeEval(req.query.script))
76+
res.send(`${require(evalFunctionsPath).runFakeEval(req.query.script)}`)
7777
},
7878
vulnerability: 'CODE_INJECTION',
7979
makeRequest: (done, config) => {
@@ -82,7 +82,7 @@ describe('Code injection vulnerability', () => {
8282
})
8383

8484
testThatRequestHasNoVulnerability((req, res) => {
85-
res.send('' + require(evalFunctionsPath).runEval('1 + 2'))
85+
res.send(`${require(evalFunctionsPath).runEval('1 + 2')}`)
8686
}, 'CODE_INJECTION')
8787
})
8888
})

packages/dd-trace/test/appsec/iast/taint-tracking/plugin.spec.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,29 @@ describe('IAST Taint tracking plugin', () => {
5454
})
5555

5656
it('Should subscribe to body parser, qs, cookie and process_params channel', () => {
57-
assert.strictEqual(taintTrackingPlugin._subscriptions.length, 17)
58-
let i = 0
59-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:body-parser:read:finish')
60-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:multer:read:finish')
61-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:fastify:body-parser:finish')
62-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'apm:express:middleware:next')
63-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:query:read:finish')
64-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:fastify:query-params:finish')
65-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:express:query:finish')
66-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:cookie:parse:finish')
67-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:fastify-cookie:read:finish')
68-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:sequelize:query:finish')
69-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'apm:pg:query:finish')
70-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:express:process_params:start')
71-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:router:param:start')
72-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:fastify:path-params:finish')
73-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'apm:graphql:resolve:start')
74-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:url:parse:finish')
75-
assert.strictEqual(taintTrackingPlugin._subscriptions[i++]._channel.name, 'datadog:url:getter:finish')
57+
const expectedChannels = [
58+
'datadog:body-parser:read:finish',
59+
'datadog:multer:read:finish',
60+
'datadog:fastify:body-parser:finish',
61+
'apm:express:middleware:next',
62+
'datadog:query:read:finish',
63+
'datadog:fastify:query-params:finish',
64+
'datadog:express:query:finish',
65+
'datadog:cookie:parse:finish',
66+
'datadog:fastify-cookie:read:finish',
67+
'datadog:sequelize:query:finish',
68+
'apm:pg:query:finish',
69+
'datadog:express:process_params:start',
70+
'datadog:router:param:start',
71+
'datadog:fastify:path-params:finish',
72+
'apm:graphql:resolve:start',
73+
'datadog:url:parse:finish',
74+
'datadog:url:getter:finish',
75+
]
76+
assert.strictEqual(taintTrackingPlugin._subscriptions.length, expectedChannels.length)
77+
for (let i = 0; i < expectedChannels.length; i++) {
78+
assert.strictEqual(taintTrackingPlugin._subscriptions[i]._channel.name, expectedChannels[i])
79+
}
7680
})
7781

7882
describe('taint sources', () => {

packages/dd-trace/test/appsec/iast/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ function checkVulnerabilityInRequest (
201201
assert.notStrictEqual(vulnerabilitiesTrace, null)
202202
const vulnerabilitiesCount = new Map()
203203
vulnerabilitiesTrace.vulnerabilities.forEach(v => {
204-
let count = vulnerabilitiesCount.get(v.type) || 0
205-
vulnerabilitiesCount.set(v.type, ++count)
204+
const count = (vulnerabilitiesCount.get(v.type) || 0) + 1
205+
vulnerabilitiesCount.set(v.type, count)
206206
})
207207

208208
assert.ok(((vulnerabilitiesCount.get(vulnerability)) > (0)))

packages/dd-trace/test/llmobs/plugins/ai/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ describe('Plugin', () => {
350350

351351
it('creates a span for a tool call', async () => {
352352
let tools
353-
let additionalOptions = {}
353+
let additionalOptions
354354
const toolSchema = ai.jsonSchema({
355355
type: 'object',
356356
properties: {
@@ -495,7 +495,7 @@ describe('Plugin', () => {
495495

496496
it('created a span for a tool call from a stream', async () => {
497497
let tools
498-
let additionalOptions = {}
498+
let additionalOptions
499499
const toolSchema = ai.jsonSchema({
500500
type: 'object',
501501
properties: {

packages/dd-trace/test/opentelemetry/logs.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ describe('OpenTelemetry Logs', () => {
380380

381381
// Integer body (protobuf returns Long objects for int64)
382382
const intValue = logRecords[1].body.intValue
383-
assert.strictEqual(typeof intValue === 'object' ? intValue.toNumber() : intValue, 42)
383+
assert.strictEqual(intValue !== null && typeof intValue === 'object' ? intValue.toNumber() : intValue, 42)
384384

385385
// Double/float body
386386
assert(logRecords[2].body.doubleValue !== undefined)
@@ -396,7 +396,7 @@ describe('OpenTelemetry Logs', () => {
396396
assert.strictEqual(logRecords[4].body.kvlistValue.values[0].value.stringValue, 'bar')
397397
assert.strictEqual(logRecords[4].body.kvlistValue.values[1].key, 'baz')
398398
const bazValue = logRecords[4].body.kvlistValue.values[1].value.intValue
399-
assert.strictEqual(typeof bazValue === 'object' ? bazValue.toNumber() : bazValue, 123)
399+
assert.strictEqual(bazValue !== null && typeof bazValue === 'object' ? bazValue.toNumber() : bazValue, 123)
400400

401401
// Default case (symbol) - should convert to string
402402
assert.strictEqual(logRecords[5].body.stringValue, 'Symbol(test)')

0 commit comments

Comments
 (0)