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

chore: Updated lint rule suppression comment #2895

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

jsumners-nr
Copy link
Contributor

This PR resolves #2859.

I worked on a couple of different algorithms in an attempt to get around the rule violation, but the regular expression is our best option. The simplest algorithm I was able to devise still used regular expressions, but ended up being slower (despite being easier to follow). I have attached my benchmark below.

Benchmark
'use strict'

const assert = require('node:assert')

const tests = [
  ['use test_db;', 'test_db'],
  ['USE INIT', 'INIT'],
  ['use test_db', 'test_db'],
  ['use test_db;;;;;;', 'test_db'],
  ['            use test_db;', 'test_db'],
  ['use            test_db;', 'test_db'],
  ['use test_db            ;', 'test_db'],
  ['use test_db;            ', 'test_db'],
  ['use `test_db`;', '`test_db`'],
  ['use `☃☃☃☃☃☃`;', '`☃☃☃☃☃☃`'],
  ['use cxvozicjvzocixjv`oasidfjaosdfij`;', null],
  ['use `oasidfjaosdfij`123;', null],
  ['use `oasidfjaosdfij` 123;', null],
  ['use \u0001;', null],
  ['use oasidfjaosdfij 123;', null]
]
const iterations = 10_000

const regexA = process.hrtime.bigint()
for (let i = 0; i < iterations; i += 1) {
  for (const [input, expected] of tests) {
    const found = extractWithRegex(input)
    assert.equal(found, expected)
  }
}
const regexB = process.hrtime.bigint()

const withoutA = process.hrtime.bigint()
for (let i = 0; i < iterations; i += 1) {
  for (const [input, expected] of tests) {
    const found = extractWithoutRegex(input)
    assert.equal(found, expected)
  }
}
const withoutB = process.hrtime.bigint()

console.log('with regex time:', regexB - regexA)
console.log('without regex time:', withoutB - withoutA)
console.log('with < without:', (regexB - regexA) < (withoutB - withoutA))

function extractWithRegex(input) {
  const match = /^\s*use[^\w`]+([\w$_\u0080-\uFFFF]+|`[^`]+`)[\s;]*$/i.exec(input)
  return (match && match[1]) || null
}

function extractWithoutRegex(input) {
  const dbName = input
    .trim()
    .replace(/use\s+/i, '')
    .replace(/;+$/, '')
    .trim()
  if (/^`?[\w$_\u0080-\uFFFF]+`?$/.test(dbName) === true) {
    return dbName
  }
  return null
}

@jsumners-nr jsumners-nr marked this pull request as ready for review January 21, 2025 20:37
Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.35%. Comparing base (cc65763) to head (3f6202d).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2895      +/-   ##
==========================================
- Coverage   97.39%   97.35%   -0.05%     
==========================================
  Files         310      310              
  Lines       47512    47512              
==========================================
- Hits        46276    46254      -22     
- Misses       1236     1258      +22     
Flag Coverage Δ
integration-tests-cjs-18.x 73.03% <0.00%> (ø)
integration-tests-cjs-20.x 73.03% <0.00%> (ø)
integration-tests-cjs-22.x 73.06% <0.00%> (ø)
integration-tests-esm-18.x 49.78% <0.00%> (ø)
integration-tests-esm-20.x 49.79% <0.00%> (ø)
integration-tests-esm-22.x 49.83% <0.00%> (ø)
unit-tests-18.x 88.99% <100.00%> (ø)
unit-tests-20.x 88.99% <100.00%> (ø)
unit-tests-22.x 89.00% <100.00%> (ø)
versioned-tests-18.x 79.27% <100.00%> (-0.19%) ⬇️
versioned-tests-20.x ?
versioned-tests-22.x ?

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.

// The lint rule being suppressed here has been evaluated, and it has been
// determined that the regular expression is sufficient for our use case.
// eslint-disable-next-line sonarjs/slow-regex
const match = /^\s*use[^\w`]+([\w$\u0080-\uFFFF]+|`[^`]+`)[\s;]*$/i.exec(sql)
Copy link
Member

Choose a reason for hiding this comment

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

Am I reading this correctly? It seems as though the regex has changed from the previous commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Notice that the second suppressed rule has been removed from suppression.

Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking about the underscore (\w$_\u0080 to \w$\u0080), which has been in that regex for a long time prior to this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsumners-nr jsumners-nr requested a review from mrickard January 24, 2025 14:19
@jsumners-nr jsumners-nr merged commit 559dc98 into newrelic:main Jan 27, 2025
28 of 29 checks passed
@jsumners-nr jsumners-nr deleted the issue-2859 branch January 27, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done: Issues recently completed
Development

Successfully merging this pull request may close these issues.

Improve db util regular expression
2 participants