Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new ESLint plugin: @datadog/eslint-plugin #4539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"extends": [
"eslint:recommended",
"standard",
"plugin:mocha/recommended"
"plugin:mocha/recommended",
"plugin:@datadog/recommended"
],
"plugins": [
"mocha",
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require,tlhunter-sorted-set,MIT,Copyright (c) 2023 Datadog Inc.
require,retry,MIT,Copyright 2011 Tim Koschützki Felix Geisendörfer
require,semver,ISC,Copyright Isaac Z. Schlueter and Contributors
require,shell-quote,mit,Copyright (c) 2013 James Halliday
dev,@datadog/eslint-plugin,MIT,Copyright 2024 Datadog Inc.
dev,@types/node,MIT,Copyright Authors
dev,autocannon,MIT,Copyright 2016 Matteo Collina
dev,aws-sdk,Apache 2.0,Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"tlhunter-sorted-set": "^0.1.0"
},
"devDependencies": {
"@datadog/eslint-plugin": "DataDog/eslint-plugin",
"@types/node": ">=18",
"autocannon": "^4.5.2",
"aws-sdk": "^2.1446.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const hooks = require('../datadog-instrumentations/src/helpers/hooks.js')
const extractPackageAndModulePath = require('../datadog-instrumentations/src/utils/src/extract-package-and-module-path')

for (const hook of Object.values(hooks)) {
if (typeof hook === 'object') {
if (hook !== null && typeof hook === 'object') {
hook.fn()
} else {
hook()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ for (const packageName of names) {

let hook = hooks[packageName]

if (typeof hook === 'object') {
if (hook !== null && typeof hook === 'object') {
hookOptions.internals = hook.esmFirst
hook = hook.fn
}
Expand Down
22 changes: 10 additions & 12 deletions packages/datadog-plugin-aws-sdk/src/services/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ class DynamoDb extends BaseAwsSdkPlugin {
// batch operations have different format, collect table name for batch
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchGetItem-property`
// dynamoDB batch TableName
if (params.RequestItems !== null) {
if (typeof params.RequestItems === 'object') {
if (Object.keys(params.RequestItems).length === 1) {
const tableName = Object.keys(params.RequestItems)[0]

// also add span type to match serverless convention
Object.assign(tags, {
'resource.name': `${operation} ${tableName}`,
'aws.dynamodb.table_name': tableName,
tablename: tableName
})
}
if (params.RequestItems !== null && typeof params.RequestItems === 'object') {
if (Object.keys(params.RequestItems).length === 1) {
const tableName = Object.keys(params.RequestItems)[0]

// also add span type to match serverless convention
Object.assign(tags, {
'resource.name': `${operation} ${tableName}`,
'aws.dynamodb.table_name': tableName,
tablename: tableName
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function scrubChildProcessCmd (expression) {

if (token === null) {
continue
} else if (typeof token === 'object') {
} else if (typeof token === 'object') { // eslint-disable-line @datadog/safe-typeof-object
Copy link
Member

Choose a reason for hiding this comment

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

Seems like a limitation of the plugin to me, as there is a check for null. If that check is removed for any reason, because of this comment the rule won't catch the now invalid code.

if (token.pattern) {
result.push(token.pattern)
} else if (token.op) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function taintObject (iastContext, object, type) {
} else {
parent[key] = tainted
}
// eslint-disable-next-line @datadog/safe-typeof-object
Copy link
Member

Choose a reason for hiding this comment

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

Why not just add the check for null instead of disabling the rule? Same question for pretty much all other occurrences.

} else if (typeof value === 'object' && !visited.has(value)) {
visited.add(value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class VulnerabilityFormatter {

if (evidence.value == null) return { valueParts }

// eslint-disable-next-line @datadog/safe-typeof-object
if (typeof evidence.value === 'object' && evidence.rangesToApply) {
const { value, ranges } = stringifyWithRanges(evidence.value, evidence.rangesToApply)
evidence.value = value
Expand Down
1 change: 1 addition & 0 deletions packages/dd-trace/src/exporters/common/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function request (data, options, callback) {
}

if (options.url) {
// eslint-disable-next-line @datadog/safe-typeof-object
const url = typeof options.url === 'object' ? urlToOptions(options.url) : fromUrlString(options.url)
if (url.protocol === 'unix:') {
options.socketPath = url.pathname
Expand Down
1 change: 1 addition & 0 deletions packages/dd-trace/src/opentracing/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class DatadogSpan {
addEvent (name, attributesOrStartTime, startTime) {
const event = { name }
if (attributesOrStartTime) {
// eslint-disable-next-line @datadog/safe-typeof-object
if (typeof attributesOrStartTime === 'object') {
event.attributes = this._sanitizeEventAttributes(attributesOrStartTime)
} else {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@
resolved "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz"
integrity "sha1-u1BFecHK6SPmV2pPXaQ9Jfl729k= sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ=="

"@datadog/eslint-plugin@DataDog/eslint-plugin":
version "1.0.0"
resolved "https://codeload.github.com/DataDog/eslint-plugin/tar.gz/97f4183315bbee879958d651d826f4d674b7e853"

"@datadog/[email protected]":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@datadog/native-appsec/-/native-appsec-8.0.1.tgz#be06be92d79d7462aa64ee3a33108133083134fc"
Expand Down
Loading